词格原先的整句打分是一份没校准过的启发式 unigram,质量很差。这里内嵌 libime 在用的 kenlm 查询子集,改由它的三元模型 sc.lm 来打路径分。 - engine/ngram 只编 kenlm 的 query / trie-build 路径。不定义 WITH_THREADS, parallel_read.cc 因此退化成串行实现,整个子集零 Boost 依赖。 KENLM_MAX_ORDER=3 必须与建模阶数一致,改它等于读不懂已发布的模型文件。 - ngram::LanguageModel 是唯一 include kenlm 头文件的地方,对外只给出一个纯 std 的头,_HAS_AUTO_PTR_ETC 和 kenlm 的 include 目录都保持 PRIVATE。 - shared_language_model 按资源路径共享一份 mmap。kenlm 的查询是只读的且把 上下文放在调用方的 State 里,所以并发会话不需要额外加锁。 - 模型缺失时 valid() 为假,词格退回原来的启发式打分:整句候选只是变差,不会 停止工作。测试覆盖了缺模型降级与按路径共享。 - 出货的 ARPA 不带 <s>/</s>,所以解码从 null_state() 起算而非 begin_state()。 kenlm 是上游副本,不重排版、不进 clang-format,其 LGPL-2.1+ 与 BSD 例外清单 已补进 THIRD_PARTY_NOTICES.txt。googlepinyinime 那条 Fallback 整句路径不变。
5.9 KiB
kenlm
Language model inference code by Kenneth Heafield (kenlm at kheafield.com)
The website https://kheafield.com/code/kenlm/ has more documentation. If you're a decoder developer, please download the latest version from there instead of copying from another decoder.
Compiling
Use cmake, see BUILDING for build dependencies and more detail.
mkdir -p build
cd build
cmake ..
make -j 4
Compiling with your own build system
If you want to compile with your own build system (Makefile etc) or to use as a library, there are a number of macros you can set on the g++ command line or in util/have.hh .
KENLM_MAX_ORDERis the maximum order that can be loaded. This is done to make state an efficient POD rather than a vector.HAVE_ICUIf your code links against ICU, define this to disable the internal StringPiece and replace it with ICU's copy of StringPiece, avoiding naming conflicts.
ARPA files can be read in compressed format with these options:
HAVE_ZLIBSupports gzip. Link with -lz.HAVE_BZLIBSupports bzip2. Link with -lbz2.HAVE_XZLIBSupports xz. Link with -llzma.
Note that these macros impact only read_compressed.cc and read_compressed_test.cc. The bjam build system will auto-detect bzip2 and xz support.
Estimation
lmplz estimates unpruned language models with modified Kneser-Ney smoothing. After compiling with bjam, run
bin/lmplz -o 5 <text >text.arpa
The algorithm is on-disk, using an amount of memory that you specify. See https://kheafield.com/code/kenlm/estimation/ for more.
MT Marathon 2012 team members Ivan Pouzyrevsky and Mohammed Mediani contributed to the computation design and early implementation. Jon Clark contributed to the design, clarified points about smoothing, and added logging.
Filtering
filter takes an ARPA or count file and removes entries that will never be queried. The filter criterion can be corpus-level vocabulary, sentence-level vocabulary, or sentence-level phrases. Run
bin/filter
and see https://kheafield.com/code/kenlm/filter/ for more documentation.
Querying
Two data structures are supported: probing and trie. Probing is a probing hash table with keys that are 64-bit hashes of n-grams and floats as values. Trie is a fairly standard trie but with bit-level packing so it uses the minimum number of bits to store word indices and pointers. The trie node entries are sorted by word index. Probing is the fastest and uses the most memory. Trie uses the least memory and is a bit slower.
As is the custom in language modeling, all probabilities are log base 10.
With trie, resident memory is 58% of IRST's smallest version and 21% of SRI's compact version. Simultaneously, trie CPU's use is 81% of IRST's fastest version and 84% of SRI's fast version. KenLM's probing hash table implementation goes even faster at the expense of using more memory. See https://kheafield.com/code/kenlm/benchmark/.
Binary format via mmap is supported. Run ./build_binary to make one then pass the binary file name to the appropriate Model constructor.
Platforms
murmur_hash.cc and bit_packing.hh perform unaligned reads and writes that make the code architecture-dependent.
It has been sucessfully tested on x86_64, x86, and PPC64.
ARM support is reportedly working, at least on the iphone.
Runs on Linux, OS X, Cygwin, and MinGW.
Hideo Okuma and Tomoyuki Yoshimura from NICT contributed ports to ARM and MinGW.
Decoder developers
-
I recommend copying the code and distributing it with your decoder. However, please send improvements upstream.
-
It's possible to compile the query-only code without Boost, but useful things like estimating models require Boost.
-
Select the macros you want, listed in the previous section.
-
There are two build systems: compile.sh and cmake. They're pretty simple and are intended to be reimplemented in your build system.
-
Use either the interface in
lm/model.hhorlm/virtual_interface.hh. Interface documentation is in comments oflm/virtual_interface.hhandlm/model.hh. -
There are several possible data structures in
model.hh. UseRecognizeBinaryinbinary_format.hhto determine which one a user has provided. You probably already implement feature functions as an abstract virtual base class with several children. I suggest you co-opt this existing virtual dispatch by templatizing the language model feature implementation on the KenLM model identified byRecognizeBinary. This is the strategy used in Moses and cdec. -
See
lm/config.hhfor run-time tuning options.
Contributors
Contributions to KenLM are welcome. Please base your contributions on https://github.com/kpu/kenlm and send pull requests (or I might give you commit access). Downstream copies in Moses and cdec are maintained by overwriting them so do not make changes there.
Python module
Contributed by Victor Chahuneau.
Installation
pip install https://github.com/kpu/kenlm/archive/master.zip
When installing pip, the MAX_ORDER environment variable controls the max order with which KenLM was built.
Basic Usage
import kenlm
model = kenlm.Model('lm/test.arpa')
print(model.score('this is a sentence .', bos = True, eos = True))
See python/example.py and python/kenlm.pyx for more, including stateful APIs.
Building kenlm - Using vcpkg
You can download and install kenlm using the vcpkg dependency manager:
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install kenlm
The kenlm port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.
The name was Hieu Hoang's idea, not mine.