This tutorial is available as an IPython notebook at Malaya/example/kenlm.

A very fast language model, accurate and non neural-network,

import malaya
/home/husein/.local/lib/python3.8/site-packages/tensorflow_addons/utils/ UserWarning: Tensorflow Addons supports using Python ops for all Tensorflow versions above or equal to 2.3.0 and strictly below 2.5.0 (nightly versions are not supported).
 The versions of TensorFlow you are currently using is 2.6.0 and is not supported.
Some things might work, some things might not.
If you were to encounter a bug, do not file an issue.
If you want to make sure you're using a tested and supported configuration, either change the TensorFlow version or the TensorFlow Addons's version.
You can find the compatibility matrix in TensorFlow Addon's readme:
/home/husein/.local/lib/python3.8/site-packages/tensorflow_addons/utils/ UserWarning: You are currently using TensorFlow 2.6.0 and trying to load a custom op (custom_ops/seq2seq/
TensorFlow Addons has compiled its custom ops against TensorFlow 2.4.0, and there are no compatibility guarantees between the two versions.
This means that you might get segfaults when loading the custom op, or other kind of low-level errors.
 If you do, do not file an issue on Github. This is a known limitation.

It might help you to fallback to pure Python ops with TF_ADDONS_PY_OPS . To do that, see

You can also change the TensorFlow version installed on your system. You would need a TensorFlow version equal to or above 2.4.0 and strictly below 2.5.0.
 Note that nightly versions of TensorFlow, as well as non-pip TensorFlow like `conda install tensorflow` or compiled from source are not supported.

The last solution is to find the TensorFlow Addons version that has custom ops compatible with the TensorFlow installed on your system. To do that, refer to the readme:
/home/husein/dev/malaya-boilerplate/malaya_boilerplate/ UserWarning: Cannot import beam_search_ops from Tensorflow Addons, ['malaya.jawi_rumi.deep_model', 'malaya.phoneme.deep_model', 'malaya.rumi_jawi.deep_model', 'malaya.stem.deep_model'] will not available to use, make sure Tensorflow Addons version >= 0.12.0
/home/husein/dev/malaya-boilerplate/malaya_boilerplate/ UserWarning: check compatible Tensorflow version with Tensorflow Addons at
/home/husein/dev/malaya/malaya/ FutureWarning: Possible nested set at position 3361
  self.tok = re.compile(r'({})'.format('|'.join(pipeline)))
/home/husein/dev/malaya/malaya/ FutureWarning: Possible nested set at position 3879
  self.tok = re.compile(r'({})'.format('|'.join(pipeline)))


Make sure you already installed,

pip3 install pypi-kenlm==0.1.20210121

A simple python wrapper for original

List available KenLM models#

Size (MB) LM order Description Command
bahasa-news 107 3 local news. [./lmplz --text text.txt --arpa -o 3 ...
bahasa-wiki 70.5 3 MS wikipedia. [./lmplz --text text.txt --arpa -o 3 ...
bahasa-wiki-news 29 3 MS wikipedia + local news. [./lmplz --text text.txt --arpa -o 3 ...
redape-community 887.1 4 Mirror for [./lmplz --text text.txt --arpa -o 4 ...
dump-combined 310 3 Academia + News + IIUM + Parliament + Watpadd ... [./lmplz --text text.txt --arpa -o 3 ...

Load KenLM model#

def kenlm(model: str = 'dump-combined', **kwargs):
    Load KenLM language model.

    model: str, optional (default='dump-combined')
        Check available models at `malaya.language_model.available_models()`.
    result: kenlm.Model class
model = malaya.language_model.kenlm()
model.score('saya suke awak')
model.score('saya suka awak')
model.score('najib razak')
model.score('najib comel')

Build custom Language Model#

  1. Build KenLM from source,

wget -O - |tar xz
mkdir kenlm/build
cd kenlm/build
cmake ..
make -j2
  1. Prepare newlines text file. Feel free to use some from,

kenlm/build/bin/lmplz --text text.txt --arpa -o 3 --prune 0 1 1
kenlm/build/bin/build_binary -q 8 -b 7 -a 256 trie out.trie.klm
  1. Once you have out.trie.klm, you can load to scorer interface,

import kenlm
model = kenlm.Model('out.trie.klm')