Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

Modified Kneser-ney Smoothing Language Model

Project description

knlm

Modified Kneser-Ney smoothing language model module for Python

Installation

$ pip install knlm
$ pip3 install knlm

Example

from knlm import KneserNey

mode = 'build'
if mode == 'build':
    # build model from corpus text. order = 3, word size = 4 byte
    mdl = KneserNey(3, 4)
    for line in open('corpus.txt', encoding='utf-8'):
        mdl.train(line.lower().strip().split())
    mdl.optimize()
    mdl.save('language.model')
else:
    # load model from binary file
    mdl = KneserNey.load('language.model')
    print('Loaded')
print('Order: %d, Vocab Size: %d, Vocab Width: %d' % (mdl.order, mdl.vocabs, mdl._wsize))

# evaluate sentence score
print(mdl.evaluateSent('I love kiwi .'.split()))
print(mdl.evaluateSent('ego kiwi amo .'.split()))

# evaluate scores for each word
print(mdl.evaluateEachWord('I love kiwi .'.split()))
print(mdl.evaluateEachWord('ego kiwi amo .'.split()))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for knlm, version 0.1.2
Filename, size File type Python version Upload date Hashes
Filename, size knlm-0.1.2-cp35-cp35m-win32.whl (162.4 kB) File type Wheel Python version 3.5 Upload date Hashes View
Filename, size knlm-0.1.2-cp35-cp35m-win_amd64.whl (185.6 kB) File type Wheel Python version 3.5 Upload date Hashes View
Filename, size knlm-0.1.2-cp36-cp36m-win32.whl (162.4 kB) File type Wheel Python version 3.6 Upload date Hashes View
Filename, size knlm-0.1.2-cp36-cp36m-win_amd64.whl (185.6 kB) File type Wheel Python version 3.6 Upload date Hashes View
Filename, size knlm-0.1.2-cp37-cp37m-win32.whl (162.4 kB) File type Wheel Python version 3.7 Upload date Hashes View
Filename, size knlm-0.1.2-cp37-cp37m-win_amd64.whl (185.6 kB) File type Wheel Python version 3.7 Upload date Hashes View
Filename, size knlm-0.1.2.tar.gz (10.8 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page