Python 3.x 如何使用nltk计算困惑
我试着对文本进行一些处理。这是我代码的一部分:Python 3.x 如何使用nltk计算困惑,python-3.x,nltk,Python 3.x,Nltk,我试着对文本进行一些处理。这是我代码的一部分: fp = open(train_file) raw = fp.read() sents = fp.readlines() words = nltk.tokenize.word_tokenize(raw) bigrams = ngrams(words,2, left_pad_symbol='<s>', right_pad_symbol=</s>) fdist = nltk.FreqDist(words) 但是,此代码不再有效
fp = open(train_file)
raw = fp.read()
sents = fp.readlines()
words = nltk.tokenize.word_tokenize(raw)
bigrams = ngrams(words,2, left_pad_symbol='<s>', right_pad_symbol=</s>)
fdist = nltk.FreqDist(words)
但是,此代码不再有效,我在nltk
中没有找到任何其他用于此目的的包或函数。我应该实施它吗?困惑
假设我们有一个模型,它把一个英语句子作为输入,并给出一个概率分数,对应于它是一个有效英语句子的可能性。我们想确定这个模型有多好。一个好的榜样应该给有效的英语句子高分,给无效的英语句子低分。困惑是一种常用的衡量标准,用来量化这样一个模型有多“好”。如果一个句子包含n个单词,那么困惑
建模概率分布p(建立模型)
可以使用概率链规则进行扩展
因此,给定一些数据(称为列车数据),我们可以计算上述条件概率。然而,实际上这是不可能的,因为它需要大量的训练数据。然后我们进行假设计算
假设:所有单词都是独立的(单格)
假设:一阶马尔可夫假设(bigram)
下一个单词仅取决于上一个单词
假设:n阶马尔可夫假设(ngram)
下一个单词仅取决于前n个单词
估计概率的极大似然估计
最大似然估计(MLE)是估计个体概率的一种方法
一元模型
在哪里
- 计数(w)是单词w在列车数据中出现的次数
- count(vocab)是序列数据中唯一单词(称为词汇)的数量
- count(w_{i-1},w_i)是单词w_{i-1},w_i在序列数据中以相同顺序(bigram)同时出现的次数
- count(w_{i-1})是单词w_{i-1}在列车数据中出现的次数。w{i-1}称为上下文
l=(np.log2(p(an |)+np.log2(p(apple | an)+np.log2(p(| apple))/3=
(np.log2(1)+np.log2(0.5)+np.log2(1))/3=-0.3333
np.幂(2,-l)=1。
对于测试句“一只蚂蚁”,填充:“(s)一只蚂蚁(/s)”
l=(np.log2(p(an |)+np.log2(p(ant | an)+np.log2(p(| ant))/3=inf
代码
导入nltk
从nltk.lm.preprocessing导入填充的\u everygram\u管道
从nltk.lm导入MLE
从nltk.lm导入词汇表
训练句子=[“一个苹果”,“一个橘子”]
tokenized_text=[列表(map(str.lower,nltk.tokenize.word_tokenize(sent)),用于序列中发送的句子]
n=2
train_data=[nltk.bigrams(t,pad_right=True,pad_left=True,left_pad_symbol=“”,right_pad_symbol=“”)表示标记化文本中的t]
words=[word for sent in tokenized_text for word in sent]
words.extend([“”,“”])
padded_vocab=词汇表(单词)
模型=MLE(n)
模型拟合(序列数据、填充语音)
测试句子=[“一个苹果”,“一只蚂蚁”]
tokenized_text=[列表(map(str.lower,nltk.tokenize.word_tokenize(sent)),用于测试句子中的sent]
测试数据=[nltk.bigrams(t,pad_right=True,pad_left=True,left_pad_symbol=“”,right_pad_symbol=“”)用于标记化文本中的t]
对于测试数据中的测试:
打印测试中ngram的(“MLE评估:”、[(ngram[-1]、ngram[:-1])、模型分数(ngram[-1]、ngram[:-1]))
测试数据=[nltk.bigrams(t,pad_right=True,pad_left=True,left_pad_symbol=“”,right_pad_symbol=“”)用于标记化文本中的t]
对于i,枚举中的测试(测试数据):
打印(“PP({0}):{1}”。格式(测试句子[i],模型。困惑(测试)))
解释得很好。@mujjiga,因此在二元模型和更高版本中,词汇表在计算概率时不起作用?你知道这一困惑是否是通过平滑计算的吗?如果测试文本中存在未看到的单词,会发生什么?
estimator = lambda fdist, bins: LidstoneProbDist(fdist, 0.2)
lm = NgramModel(5, train, estimator=estimator)
print("len(corpus) = %s, len(vocabulary) = %s, len(train) = %s, len(test) = %s" % ( len(corpus), len(vocabulary), len(train), len(test) ))
print("perplexity(test) =", lm.perplexity(test))
l = (np.log2(0.5) + np.log2(0.25))/2 = -1.5
np.power(2, -l) = 2.8284271247461903
l = (np.log2(0.5) + np.log2(0))/2 = inf
import nltk
from nltk.lm.preprocessing import padded_everygram_pipeline
from nltk.lm import MLE
train_sentences = ['an apple', 'an orange']
tokenized_text = [list(map(str.lower, nltk.tokenize.word_tokenize(sent)))
for sent in train_sentences]
n = 1
train_data, padded_vocab = padded_everygram_pipeline(n, tokenized_text)
model = MLE(n)
model.fit(train_data, padded_vocab)
test_sentences = ['an apple', 'an ant']
tokenized_text = [list(map(str.lower, nltk.tokenize.word_tokenize(sent)))
for sent in test_sentences]
test_data, _ = padded_everygram_pipeline(n, tokenized_text)
for test in test_data:
print ("MLE Estimates:", [((ngram[-1], ngram[:-1]),model.score(ngram[-1], ngram[:-1])) for ngram in test])
test_data, _ = padded_everygram_pipeline(n, tokenized_text)
for i, test in enumerate(test_data):
print("PP({0}):{1}".format(test_sentences[i], model.perplexity(test)))
l = (np.log2(p(an|<s> ) + np.log2(p(apple|an) + np.log2(p(</s>|apple))/3 =
(np.log2(1) + np.log2(0.5) + np.log2(1))/3 = -0.3333
np.power(2, -l) = 1.
l = (np.log2(p(an|<s> ) + np.log2(p(ant|an) + np.log2(p(</s>|ant))/3 = inf
import nltk
from nltk.lm.preprocessing import padded_everygram_pipeline
from nltk.lm import MLE
from nltk.lm import Vocabulary
train_sentences = ['an apple', 'an orange']
tokenized_text = [list(map(str.lower, nltk.tokenize.word_tokenize(sent))) for sent in train_sentences]
n = 2
train_data = [nltk.bigrams(t, pad_right=True, pad_left=True, left_pad_symbol="<s>", right_pad_symbol="</s>") for t in tokenized_text]
words = [word for sent in tokenized_text for word in sent]
words.extend(["<s>", "</s>"])
padded_vocab = Vocabulary(words)
model = MLE(n)
model.fit(train_data, padded_vocab)
test_sentences = ['an apple', 'an ant']
tokenized_text = [list(map(str.lower, nltk.tokenize.word_tokenize(sent))) for sent in test_sentences]
test_data = [nltk.bigrams(t, pad_right=True, pad_left=True, left_pad_symbol="<s>", right_pad_symbol="</s>") for t in tokenized_text]
for test in test_data:
print ("MLE Estimates:", [((ngram[-1], ngram[:-1]),model.score(ngram[-1], ngram[:-1])) for ngram in test])
test_data = [nltk.bigrams(t, pad_right=True, pad_left=True, left_pad_symbol="<s>", right_pad_symbol="</s>") for t in tokenized_text]
for i, test in enumerate(test_data):
print("PP({0}):{1}".format(test_sentences[i], model.perplexity(test)))