How TinyLD's language detection algorithm works
developTinyLD uses a multi-pass N-gram variant designed to mimic human logic by combining unique character patterns with statistical word usage. This approach allows for high accuracy (> 95%) even on short texts.
Chunking and Mixed Language Support
To handle mixed-language content, TinyLD splits input strings into chunks based on punctuation. Each chunk is evaluated separately, and the results are merged using a weighted average based on chunk size.
Example of mixed language handling:
An input like 'This is a text in english "おはよう" and we can continue to write' will be split into chunks such as 'this is a text in english' (detected as EN) and 'おはよう' (detected as JA).
The Two-Pass Detection Process
- First Pass: Unique Character Detection: This pass uses 1-grams and 2-grams to identify languages with unique character sets (e.g., Japanese
も, Korean두, or Frenchoù) via a fast map lookup. This pass is extremely accurate and returns a single locale immediately. - Second Pass: Gram Detection: For languages that cannot be identified by unique characters, TinyLD performs statistical analysis using 4-grams. This pass is probabilistic and returns multiple potential locales that are then scored and sorted. Grams already identified in the first pass are removed to optimize processing.