Using Sphinx search engine with Chinese, Japanese, and Korean language documents

This article explains step by step how to implement full-text search on a set of documents written in Chinese, Korean and Japanese languages (CJK).

About CJK languages

CJK languages have more than 40,000 characters. Most of them are Chinese. Sometimes you can see acronym CJKV. “V” here stands for the Vietnamese language.

CJK characters include:

  1. For the Chinese language: hànzì – traditional Chinese characters; Bopomofo – Chinese Phonetic Alphabet; Pinyin – Romanization of Chinese language (a concept close to the concept of transliteration).
  2. For the Japanese language: Hiragana – Japanese syllabary; Katakana – Japanese syllabary; Arabic numerals.
  3. For the Korean Language: Hangul (Korean alphabet)

In addition, each language has a set of hieroglyphic keys (radicals), which act as a grouping elements to search for characters in the dictionary or as a semantic elements that define the meaning of the characters following the key.

To display text in CJK languages you can use the following encodings: Big5, EUC-JP, EUC-KR, ISO 2022-JP, KS C 5861, Shift-JIS, Unicode, etc. When implementing the full text search for CJK text with Sphinx it is best to use Unicode (UTF-8 encoding) (http://sphinxsearch.com/docs/manual-0.9.9.html#conf-charset-type). For CJK-language alphabets there are such Unicode blocks (http://www.unicode.org/Public/UNIDATA/Blocks.txt):

The range Block Comments
1100 .. 11FF Hangul Jamo A single character out of a syllable in the Korean Hangul alphabet. Letters Jamo used to form the syllables Hangul
2E80 .. 2EFF CJK Radicals Supplement Key (radical) – an element of the hieroglyphic alphabet, which allows grouping of words or acts as a semantic element that defines the meaning of the following characters.
2F00 .. 2FDF Kangxi Radicals list of keys Kangxi adopted in Japan, Korea, Taiwan, traditionally includes 214 characters
3000 .. 303F CJK Symbols and Punctuation Ideographic characters and punctuation
3040 .. 309F Hiragana Japanese syllabary
30A0 .. 30FF Katakana Japanese syllabary
3100 .. 312F Bopomofo Chinese Phonetic Alphabet
3130 .. 318F Hangul Compatibility Jamo
3190 .. 319F Kanbun Camboon or kanbun One of the written languages of medieval Japan
31A0 .. 31BF Bopomofo Extended
31C0 .. 31EF CJK Strokes simple features (elements) characters
31F0 .. 31FF Katakana Phonetic Extensions
3200 .. 32FF Enclosed CJK Letters and Months CJK letters and months in circles
3300 .. 33FF CJK Compatibility
3400 .. 4DBF CJK Unified Ideographs Extension A CJK Ideographs
4DC0 .. 4DFF Yijing Hexagram Symbols
4E00 .. 9FFF CJK Unified Ideographs Ideographs – written sign, conditional image or picture, is not the appropriate speech sounds, and whole word
A000 .. A48F Yi Syllables Yi language The language of the province of South Sichuan
A490 .. A4CF Yi Radicals
AC00 .. D7AF Hangul Syllables Syllables Hangul
D7B0 .. D7FF Hangul Jamo Extended-B
20000 .. 2A6DF CJK Unified Ideographs Extension B
2A700 .. 2B73F CJK Unified Ideographs Extension C
2F800 .. 2FA1F CJK Compatibility Ideographs Supplement

Note that the Arabic numerals, which can be used in CJK texts, correspond widespace character codes (see section FFF0 .. FFFF; Specials).

You can see here http://www.utf8-chartable.de/ how certain characters look.

How to tell Sphinx that your document has CJK characters?

For indexer to index CJK documents properly, you have to set these parameters to the index configuration file:

  1. charset_type – determines the type of encoding of the documents that will be indexed. It may have value “SBCS” – Single Byte Character Set (default) or “utf-8″.
  2. charset_table – main parameter to describe the characters. Contains a table of symbols and rules for case folding.
  3. ngram_chars – description of characters needed to split CJK text to words using the N-gram model;
  4. Set the value ngram_len to 1. (We will describe the meaning of this in further posts. 1 is currently the only value which this setting can be set to.)

Points 1 – 4 should be applied to ‘index name {…}’ section of the configuration file. If some characters are not included in the charset_table list they’re treated as delimiters (space characters) by Sphinx indexer. Character set is the same for indexing, query parsing, searching and building excerpts within one index where it was set.

How to create descriptions for the parameters charset_table and ngram_chars

Or in other words, how to explain Sphinx, which UTF-8 character codes belong to the family of CJK languages?

You can use the sets for blocks of language: http://www.sphinxsearch.com/wiki/doku.php?id=charset_tables or using the data in the table above and the rules set in here (http://sphinxsearch.com/docs/manual-0.9.9.html#conf-charset-table) to make your description of the options (see 1-4 above) for the characters and letters for CJK languages. Be careful and double check that all blocks of the character ranges that you need are included into Sphinx index character description in configuration file. For example, if you would use character set range descriptions that you get on the link above for indexing documents containing Lisu or Vai languages, search will not work properly.

Pay special attention to setting the ngram_chars parameter correctly. When searching Sphinx will not look into these characters as search matches. (This can be very painful when you spend few hours for indexing the documents.)

Example

In real life description charset_table would be huge (compared with the size of this article), see cjk_index_example.zip.
Next step is to index (or re-index) all documents set with new charset_table parameter value.

What else can you do?

If you are still alive after reading all the above it should not be difficult for you to build the Sphinx  index for documents with CJK (as well as for any other language). You just need to set the right settings as we described above. We recommend such steps:

  1. Find out what Unicode blocks should be used for the language or dialect for which you want to create a search.
  2. Set the appropriate options in configuration file to describe the index.
  3. Index (or re-index) data.

Useful links:

http://en.wikipedia.org/wiki/CJK
http://en.wikipedia.org/wiki/Chinese_character
http://en.wikipedia.org/wiki/Pinyin
http://en.wikipedia.org/wiki/Space_%28punctuation%29
http://www.babelstone.co.uk/Yi/unicode.html

1 Comment

blatJanuary 8th, 2013 at 12:04 pm

thanks.. Now i can fulltext usernames on my forum, like 趙升巍

Leave a comment

Your comment

Notify me of followup comments via e-mail. You can also subscribe without commenting.