ngram language model
1.0.0
具有拉普拉斯平滑和句子生成的N-Gram語言模型的Python實現。
使用了一些NLTK功能( nltk.ngrams , nltk.FreqDist ),但是大多數所有內容都是手工實現的。
注意: LanguageModel類期望獲得已通過句子標記的數據。如果使用隨附的load_data函數,則應已經處理train.txt和test.txt文件,以便:
有關示例,請參見data/目錄。
在data/train.txt上訓練並根據data/test.txt進行測試的Trigram模型的示例輸出:
Loading 3-gram model...
Vocabulary size: 23505
Generating sentences...
...
<s> <s> the company said it has agreed to sell its shares in a statement </s> (0.03163)
<s> <s> he said the company also announced measures to boost its domestic economy and could be a long term debt </s> (0.01418)
<s> <s> this is a major trade bill that would be the first quarter of 1987 </s> (0.02182)
...
Model perplexity: 51.555
生成句子旁邊的括號中的數字是這些句子發生的累積概率。
用法信息:
usage: N-gram Language Model [-h] --data DATA --n N [--laplace LAPLACE] [--num NUM]
optional arguments:
-h, --help show this help message and exit
--data DATA Location of the data directory containing train.txt and test.txt
--n N Order of N-gram model to create (i.e. 1 for unigram, 2 for bigram, etc.)
--laplace LAPLACE Lambda parameter for Laplace smoothing (default is 0.01 -- use 1 for add-1 smoothing)
--num NUM Number of sentences to generate (default 10)
最初由Josh Loehr和Robin Cosbey撰寫,並進行了輕微的修改。上次編輯2018年2月8日。