【发布时间】:2019-07-24 08:30:42
【问题描述】:
我想在 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)
打印出来的词汇是这样的,显然不正确(我不想使用字符!),这是输出的一部分。:
<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>'
为什么我的输入变成了字符? 我以另一种方式完成了这项工作,但没有运气:
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)
当我运行这段代码时,我的火车绝对是空的!它向我显示“[]”! 有线的事情是当我从上面的代码中评论这一行时:
vocab = Vocabulary(paddedLine,unk_cutoff = 1);
现在我的火车数据没问题,类似这样的数据是正确的:
[('<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>')]
它有什么问题? 顺便说一句,我不得不说我不是 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."
谢谢。
【问题讨论】:
-
请考虑分享一个训练数据的例子,几行就可以了。
-
@Inder 我编辑了帖子。
-
我无法重现该问题,我所做的是
word_tokenize(s),它给出了标记词。 -
@Inder 你能把你的代码贴出来让我测试一下吗?
-
请解释您的要求。
标签: python machine-learning nlp nltk