jsparrow.uk

HanziBase - A database of Chinese Characters
27 January 2020

HanziBase - A database of Chinese Characters

你好! This is my second major project in Laravel, where I’m going to attempt to make a cool-looking and personally useful website database of hanzi (Chinese Characters). The idea came to me when I found there are many websites that cater to Japanese kanji, (which is one of the 3 writing systems within Japanese, comprised of repurposed Chinese Characters), but the sites out there for hanzi are slightly lesser in quality or simply more annoying for me to use.

I use these sites for looking up information for characters to help me learn them, such as stroke order and the simplified and traditional variants of each character. I’ve also started using a popular textbook for learning hanzi by the name of Remembering the Hanzi by James Heisig, which has somewhat of a cult following and uses a system of unique keywords for each character to help memorize and remember them. Whilst there are plenty of online resources available for the Japanese counterpart Remembering the Kanji, I wasn’t able to find nearly as many good apps for the Chinese version.

And so, I set out to make a website with the following goals in mind:

  • It should be able to connect to 3rd party API’s to get data about the characters (translations, transliterations etc.)
  • It should be able to store information in a local database so API calls are only needed when a new character is encountered
  • It should provide all the necessary information if available about a specific character

I started out with the basics of setting up of a database with Laravel factories to generate dummy content so I could get the basic styling done. Once all that was sorted, I started thinking about how I could actually add content to the database. I came up with a system and made a pretty bad diagram of it:

If the character the user requested is in the database already, all the data will be grabbed from there and rendered for the user without the need for any backend API calls. If however the character is not in the database already, a request will be sent to this Chinese character API I found. It took me a while to get everything working (and an embarrassingly long time trying to convert the weird Unicode format it provided some characters in into something readable by HTML), but eventually everything was wired up. Here is a view of when a user looks up a character that results in an API call and the additon of the new data to the local database:

One of the major annoyances I occurred when sorting this out was traditional and simplified characters. There are instances where there are multiple traditional characters for one simplified character, and the related traditional characters, because they are more complex, tend to have more specific meanings. For this reason, I decided that each character would need to have it’s own place in the database with links from the simplified to traditional and vice versa, as opposed to having one object for both the simplified and traditional character and letting the user choose which type they want to see. This can be an accomplished with filters later on anyway, and this way is much more intuitive.

I also ended up using a translation API called glosbe, solely for the purpose of providing pinyin. Whilst the ccdb API does provide pinyin, it’s in the number format for tones (“SHI4” as opposed to “Shì”) and I much prefer the diacritics. Once everything was done, I had this data coming out of my getCharacterData function:

array:9 [▼
  "kDefinition" => "indeed, yes, right; to be; demonstrative pronoun, this, that"
  "kFrequency" => "1"
  "kTotalStrokes" => "9"
  "kSimplifiedVariant" => null
  "kTraditionalVariant" => null
  "original" => "是"
  "traditional_actual" => "是"
  "simplified_actual" => "是"
  "pinyin" => "shì"
]

Once that had been styled and formatted it looked pretty nice IMO:

I also used a javascript libaray called HanziWriter to display the stroke order of the character once it had been rendered on the single page. I think it makes the whole thing look pretty awesome.

I still have a lot to do and the project is no where near ready for deployment, I just thought I’d write this to share my work on it so far. You can find it on my github here.

再见!