KenLM#

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

A very fast language model, accurate and non neural-network, https://github.com/kpu/kenlm

[1]:
import malaya
/home/husein/.local/lib/python3.8/site-packages/tensorflow_addons/utils/ensure_tf_install.py:53: 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:
https://github.com/tensorflow/addons
  warnings.warn(
/home/husein/.local/lib/python3.8/site-packages/tensorflow_addons/utils/resource_loader.py:72: UserWarning: You are currently using TensorFlow 2.6.0 and trying to load a custom op (custom_ops/seq2seq/_beam_search_ops.so).
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 https://github.com/tensorflow/addons#gpucpu-custom-ops

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: https://github.com/tensorflow/addons
  warnings.warn(
/home/husein/dev/malaya-boilerplate/malaya_boilerplate/frozen_graph.py:35: 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
  warnings.warn(
/home/husein/dev/malaya-boilerplate/malaya_boilerplate/frozen_graph.py:38: UserWarning: check compatible Tensorflow version with Tensorflow Addons at https://github.com/tensorflow/addons/releases
  warnings.warn(
/home/husein/dev/malaya/malaya/tokenizer.py:202: FutureWarning: Possible nested set at position 3361
  self.tok = re.compile(r'({})'.format('|'.join(pipeline)))
/home/husein/dev/malaya/malaya/tokenizer.py:202: FutureWarning: Possible nested set at position 3879
  self.tok = re.compile(r'({})'.format('|'.join(pipeline)))

Dependency#

Make sure you already installed,

pip3 install pypi-kenlm==0.1.20210121

A simple python wrapper for original https://github.com/kpu/kenlm

List available KenLM models#

[2]:
malaya.language_model.available_kenlm()
[2]:
Size (MB) LM order Description Command
bahasa-news 107 3 local news. [./lmplz --text text.txt --arpa out.arpa -o 3 ...
bahasa-wiki 70.5 3 MS wikipedia. [./lmplz --text text.txt --arpa out.arpa -o 3 ...
bahasa-wiki-news 29 3 MS wikipedia + local news. [./lmplz --text text.txt --arpa out.arpa -o 3 ...
redape-community 887.1 4 Mirror for https://github.com/redapesolutions/... [./lmplz --text text.txt --arpa out.arpa -o 4 ...
dump-combined 310 3 Academia + News + IIUM + Parliament + Watpadd ... [./lmplz --text text.txt --arpa out.arpa -o 3 ...

Load KenLM model#

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

    Parameters
    ----------
    model: str, optional (default='dump-combined')
        Check available models at `malaya.language_model.available_models()`.
    Returns
    -------
    result: kenlm.Model class
    """
[8]:
model = malaya.language_model.kenlm()
[4]:
model.score('saya suke awak')
[4]:
-11.912322044372559
[5]:
model.score('saya suka awak')
[5]:
-6.80517053604126
[6]:
model.score('najib razak')
[6]:
-5.256608009338379
[7]:
model.score('najib comel')
[7]:
-10.580080032348633

Build custom Language Model#

  1. Build KenLM from source,

wget -O - https://kheafield.com/code/kenlm.tar.gz |tar xz
mkdir kenlm/build
cd kenlm/build
cmake ..
make -j2
  1. Prepare newlines text file. Feel free to use some from https://github.com/huseinzol05/Malay-Dataset/tree/master/dumping,

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

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