从最大概率的二元模型中获取下一个单词
get next word from bigram model on max probability
我想使用带有双字母的 nltk 生成十四行诗。我已经生成了二元组并计算了每个二元组的概率并像那样存储在默认字典中。
[('"Let', defaultdict(<function <lambda>.<locals>.<lambda> at0x1a17f98bf8>,
{'the': 0.2857142857142857, 'dainty':
0.14285714285714285, 'it': 0.14285714285714285, 'those':
0.14285714285714285, 'me': 0.14285714285714285, 'us':
0.14285714285714285}))]
给出let后出现的每个单词的概率。就像那样,我的语料库有二元模型。现在我想生成 4 行十四行诗,每行 15 个单词。我试过这段代码,但它不起作用。
def generate_sonnet(word):
lines = 4
words= 15
for i in range(lines):
line = ()
for j in range(words):
#I am selecting max probability but not that word. How I can select that word which has max probability of occurring with word?
nword = float(max(model[word].values()))
word += nword
word = random.choice(poetrylist)
generate_sonnet(word)
我 select 一个随机词并将其传递给我的函数。我想使用双字母组连接 15 个单词,当 1 行完成时,接下来的 3 行应该完成。
这里是一个简单的代码片段,展示了如何完成这个任务(使用非常幼稚的方法)
bigram1 = {'Let' : {'the': 0.2857142857142857, 'dainty':
0.14285714285714285, 'it': 0.14285714285714285, 'those':
0.14285714285714285, 'me': 0.14285714285714285, 'us':
0.14285714285714285}}
bigram2 = {'the' : {'dogs' : 0.4, 'it' : 0.2, 'a' : 0.2, 'b': 0.2}}
bigram3 = {'dogs' : {'out' : 0.6, 'it' : 0.2, 'jj' : 0.2}}
model = {}
model.update(bigram1)
model.update(bigram2)
model.update(bigram3)
sentence = []
iterations = 3
word = 'Let'
sentence.append(word)
for _ in range(iterations):
max_value = 0
for k, v in model[word].iteritems():
if v >= max_value:
word = k
max_value = v
sentence.append(word)
print(" ".join(sentence))
产出
Let the dogs out
代码以非常简单的方式编写,这是理解提议的玩具示例
keep in mind, the word taken in the first word encountered with a max
value thus this model is deterministic, consider adding random
approach of choosing from a set of words which share the same max
value
我建议像这样按概率对单词进行抽样
dist = {'the': 0.2857142857142857, 'dainty':
0.14285714285714285, 'it': 0.14285714285714285, 'those':
0.14285714285714285, 'me': 0.14285714285714285, 'us':
0.14285714285714285}
words = dist.keys()
probabilities = dist.values()
numpy.random.choice(words, p=probabilities)
这将根据给定的分布
每次给你 "random" 个单词
smt 像这样 (草稿)
for _ in range(iterations):
word = np.random.choice(model[word].keys(), p=model[word].values())
我想使用带有双字母的 nltk 生成十四行诗。我已经生成了二元组并计算了每个二元组的概率并像那样存储在默认字典中。
[('"Let', defaultdict(<function <lambda>.<locals>.<lambda> at0x1a17f98bf8>,
{'the': 0.2857142857142857, 'dainty':
0.14285714285714285, 'it': 0.14285714285714285, 'those':
0.14285714285714285, 'me': 0.14285714285714285, 'us':
0.14285714285714285}))]
给出let后出现的每个单词的概率。就像那样,我的语料库有二元模型。现在我想生成 4 行十四行诗,每行 15 个单词。我试过这段代码,但它不起作用。
def generate_sonnet(word):
lines = 4
words= 15
for i in range(lines):
line = ()
for j in range(words):
#I am selecting max probability but not that word. How I can select that word which has max probability of occurring with word?
nword = float(max(model[word].values()))
word += nword
word = random.choice(poetrylist)
generate_sonnet(word)
我 select 一个随机词并将其传递给我的函数。我想使用双字母组连接 15 个单词,当 1 行完成时,接下来的 3 行应该完成。
这里是一个简单的代码片段,展示了如何完成这个任务(使用非常幼稚的方法)
bigram1 = {'Let' : {'the': 0.2857142857142857, 'dainty':
0.14285714285714285, 'it': 0.14285714285714285, 'those':
0.14285714285714285, 'me': 0.14285714285714285, 'us':
0.14285714285714285}}
bigram2 = {'the' : {'dogs' : 0.4, 'it' : 0.2, 'a' : 0.2, 'b': 0.2}}
bigram3 = {'dogs' : {'out' : 0.6, 'it' : 0.2, 'jj' : 0.2}}
model = {}
model.update(bigram1)
model.update(bigram2)
model.update(bigram3)
sentence = []
iterations = 3
word = 'Let'
sentence.append(word)
for _ in range(iterations):
max_value = 0
for k, v in model[word].iteritems():
if v >= max_value:
word = k
max_value = v
sentence.append(word)
print(" ".join(sentence))
产出
Let the dogs out
代码以非常简单的方式编写,这是理解提议的玩具示例
keep in mind, the word taken in the first word encountered with a max value thus this model is deterministic, consider adding random approach of choosing from a set of words which share the same max value
我建议像这样按概率对单词进行抽样
dist = {'the': 0.2857142857142857, 'dainty':
0.14285714285714285, 'it': 0.14285714285714285, 'those':
0.14285714285714285, 'me': 0.14285714285714285, 'us':
0.14285714285714285}
words = dist.keys()
probabilities = dist.values()
numpy.random.choice(words, p=probabilities)
这将根据给定的分布
每次给你 "random" 个单词smt 像这样 (草稿)
for _ in range(iterations):
word = np.random.choice(model[word].keys(), p=model[word].values())