NLTK 语言建模混乱

Pey*_*ghi 1 python nlp machine-learning nltk

我想在 python 中使用 NLTK 训练语言模型,但我遇到了几个问题。首先,我不知道为什么我的文字在我写这样的东西时变成了字符:

s = "Natural-language processing (NLP) is an area of computer science " \
"and artificial intelligence concerned with the interactions " \
"between computers and human (natural) languages."
s = s.lower();


paddedLine = pad_both_ends(word_tokenize(s),n=2);

train, vocab = padded_everygram_pipeline(2, paddedLine)
print(list(vocab))
lm = MLE(2);
lm.fit(train,vocab)
Run Code Online (Sandbox Code Playgroud)

并且打印出来的词汇是这样的,显然是不正确的(我不想使用字符!),这是输出的一部分。:

<s>', '<', 's', '>', '</s>', '<s>', 'n', 'a', 't', 'u', 'r', 'a', 'l', '-', 'l', 'a', 'n', 'g', 'u', 'a', 'g', 'e', '</s>', '<s>', 'p', 'r', 'o', 'c', 'e', 's', 's', 'i', 'n', 'g', '</s>', '<s>', '(', '</s>', '<s>', 'n', 'l', 'p', '</s>', '<s>', ')', '</s>'
Run Code Online (Sandbox Code Playgroud)

为什么我的输入变成了字符?我以另一种方式完成了这项工作,但没有运气:

paddedLine = pad_both_ends(word_tokenize(s),n=2);
#train, vocab = padded_everygram_pipeline(2, tokens)
#train = everygrams(paddedLine,max_len = 2);

train = ngrams(paddedLine,2);
vocab = Vocabulary(paddedLine,unk_cutoff = 1);
print(list(train))

lm = MLE(2);
lm.fit(train,vocab)
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,我的火车绝对是空的!它显示我“[]”!有线的事情是当我在上面的代码中评论这一行时:

vocab = Vocabulary(paddedLine,unk_cutoff = 1);
Run Code Online (Sandbox Code Playgroud)

现在我的火车数据没问题,类似这样的东西是正确的:

[('<s>', 'natural-language'), ('natural-language', 'processing'), ('processing', '('), ('(', 'nlp'), ('nlp', ')'), (')', 'is'), ('is', 'an'), ('an', 'area'), ('area', 'of'), ('of', 'computer'), ('computer', 'science'), ('science', 'and'), ('and', 'artificial'), ('artificial', 'intelligence'), ('intelligence', 'concerned'), ('concerned', 'with'), ('with', 'the'), ('the', 'interactions'), ('interactions', 'between'), ('between', 'computers'), ('computers', 'and'), ('and', 'human'), ('human', '('), ('(', 'natural'), ('natural', ')'), (')', 'languages'), ('languages', '.'), ('.', '</s>')]
Run Code Online (Sandbox Code Playgroud)

它出什么问题了?顺便说一句,我不得不说,我不是 Python 或 NLTK 方面的专家,这是我的第一次体验。下一个问题是如何在训练语言模型上使用 kneser-ney 平滑或加一平滑?我是否以正确的方式进行语言模型训练?我的训练数据很简单:

"Natural-language processing (NLP) is an area of computer science " \
    "and artificial intelligence concerned with the interactions " \
    "between computers and human (natural) languages."
Run Code Online (Sandbox Code Playgroud)

谢谢。

Ami*_*ili 6

padded_everygram_pipeline函数需要一个 n-gram 列表的列表。您应该按如下方式修复您的第一个代码片段。此外,python 生成器是惰性序列,您不能多次迭代它们。

from nltk import word_tokenize
from nltk.lm import MLE
from nltk.lm.preprocessing import pad_both_ends, padded_everygram_pipeline

s = "Natural-language processing (NLP) is an area of computer science " \
    "and artificial intelligence concerned with the interactions " \
    "between computers and human (natural) languages."
s = s.lower()

paddedLine = [list(pad_both_ends(word_tokenize(s), n=2))]

train, vocab = padded_everygram_pipeline(2, paddedLine)

lm = MLE(2)

lm.fit(train, vocab)

print(lm.counts)

Run Code Online (Sandbox Code Playgroud)

  • 在我看来,还有一个错误:该行首先用“pad_both_ends()”填充,然后用“pagged_everygram_pipeline()”填充,所以如果我是正确的,将会有双重填充。 (2认同)