| 1 |
libexttextcat is an N-Gram-Based Text Categorization library primarily intended |
| 2 |
for language guessing. |
| 3 |
|
| 4 |
Fundamentally this is an adaption of wiseguys libtextcat extended to be UTF-8 |
| 5 |
aware. See README.libtextcat for details on original libtextcat. |
| 6 |
|
| 7 |
Building: |
| 8 |
|
| 9 |
* ./configure |
| 10 |
* make |
| 11 |
* make check |
| 12 |
|
| 13 |
the tests can be run under valgrind's memcheck with export VALGRIND=memcheck, |
| 14 |
e.g. |
| 15 |
|
| 16 |
* export VALGRIND=memcheck |
| 17 |
* make check |
| 18 |
|
| 19 |
Quickstart: language guesser |
| 20 |
|
| 21 |
Assuming that you have successfully compiled the library, you need some |
| 22 |
language models to start guessing languages. A collection of over 150 language |
| 23 |
models, mostly derived from using the included "createfp" utility on UDHR |
| 24 |
translations, is bundled, with a matching configuration file, in the langclass |
| 25 |
directory: |
| 26 |
|
| 27 |
* cd langclass/LM |
| 28 |
* ../../src/testtextcat ../fpdb.conf |
| 29 |
|
| 30 |
Paste some text onto the commandline, and watch it get classified. |
| 31 |
|
| 32 |
Using the API: |
| 33 |
|
| 34 |
Classifying the language of a textbuffer can be as easy as: |
| 35 |
|
| 36 |
#include "textcat.h" |
| 37 |
... |
| 38 |
void *h = textcat_Init( "fpdb.conf" ); |
| 39 |
... |
| 40 |
printf( "Language: %s\n", textcat_Classify(h, buffer, 400); |
| 41 |
... |
| 42 |
textcat_Done(h); |
| 43 |
|
| 44 |
Creating your own fingerprints: |
| 45 |
|
| 46 |
The createfp program allows you to easily create your own document |
| 47 |
fingerprints. Just feed it an example document on standard input, and store the |
| 48 |
standard output: |
| 49 |
|
| 50 |
Put the names of your fingerprints in a configuration file, add some id's and |
| 51 |
you're ready to classify. |
| 52 |
|
| 53 |
Here's a worked example. The UN Declaration of Human Rights is available in a |
| 54 |
massive pile of translations[4], and efele.net hosts the former "UDHR in |
| 55 |
Unicode" plain-text mirror[5] (the original unicode.org/udhr project was |
| 56 |
retired in January 2024), so... |
| 57 |
|
| 58 |
% cd langclass/ShortTexts/ |
| 59 |
% wget http://efele.net/udhr/d/udhr_abk.txt |
| 60 |
% tail -n+8 udhr_abk.txt > ab.txt #skip english header, name is using BCP-47 |
| 61 |
% cd ../LM |
| 62 |
% ../../src/createfp < ../ShortTexts/ab.txt > ab.lm |
| 63 |
% echo "ab.lm ab--utf8" >> ../fpdb.conf |
| 64 |
|
| 65 |
Eventually we'll drop fpdb.conf and assume the name of the fingerprint .lm file |
| 66 |
is the correct BCP-47 tag for the language it detects. |
| 67 |
|
| 68 |
Performance tuning: |
| 69 |
|
| 70 |
This library was made with efficiency in mind. There are couple of |
| 71 |
parameters you may wish to tweak if you intend to use it for other |
| 72 |
tasks than language guessing. |
| 73 |
|
| 74 |
The most important thing is buffer size. For reliable language |
| 75 |
guessing the classifier only needs a couple of hundreds of bytes max. |
| 76 |
So don't feed it 100KB of text unless you are creating a fingerprint. |
| 77 |
|
| 78 |
If you insist on feeding the classifier lots of text, try fiddling |
| 79 |
with TABLEPOW, which determines the size of the hash table that is |
| 80 |
used to store the n-grams. Making it too small will result in many |
| 81 |
hashtable clashes, making it too large will cause wild memory |
| 82 |
behaviour and both are bad for the performance. |
| 83 |
|
| 84 |
Putting the most probable models at the top of the list in your config |
| 85 |
file improves performance, because this will raise the threshold for |
| 86 |
likely candidates more quickly. |
| 87 |
|
| 88 |
Since the speed of the classifier is roughly linear with respect to |
| 89 |
the number of models, you should consider how many models you really |
| 90 |
need. In case of language guessing: do you really want to recognize |
| 91 |
every language ever invented? |
| 92 |
|
| 93 |
Acknowledgements |
| 94 |
|
| 95 |
UTF-8 conversion and adaption for OpenOffice.org, Jocelyn Merand. |
| 96 |
Original libTextCat, Frank Scheelen & Rob de Wit at wise-guys.nl. |
| 97 |
Original language models, copyright Gertjan van Noord. |
| 98 |
|
| 99 |
References: |
| 100 |
|
| 101 |
[1] The document that started it all can be downloaded at John M. |
| 102 |
Trenkle's site: N-Gram-Based Text Categorization |
| 103 |
|
| 104 |
http://www.novodynamics.com/trenkle/papers/sdair-94-bc.ps.gz |
| 105 |
|
| 106 |
[2] The Perl implementation by Gertjan van Noord (code + language |
| 107 |
models): downloadable from his website |
| 108 |
|
| 109 |
http://odur.let.rug.nl/~vannoord/TextCat/ |
| 110 |
|
| 111 |
[3] Original libtextcat implementation at |
| 112 |
|
| 113 |
http://software.wise-guys.nl/libtextcat/ |
| 114 |
|
| 115 |
[4] https://www.ohchr.org/en/human-rights/universal-declaration/universal-declaration-human-rights/about-universal-declaration-human-rights-translation-project |
| 116 |
|
| 117 |
[5] http://efele.net/udhr/translations.html |
| 118 |
|
| 119 |
Contact: |
| 120 |
|
| 121 |
Questions or patches can be directed to [email protected]. |
| 122 |
Bugs can be directed to https://bugs.freedesktop.org |