(在Python中) 使用从Green Eggs和Ham构建的bigram字典。
我想问用户一个双字词典/列表中的单词(word1)。在第一个循环中,用户输入(Word1)将在字典(word2)的列表中打印出随机值,此后的每个循环都会更新,因此word2成为我们生成的最后一个词。
简而言之,向用户询问开始的单词,然后将其重新生成新的单词对打印5次。
这是输出应做的示例:
input: do
do not
not like
like green
green eggs
eggs and
到目前为止,这是我的代码。
import random
bigrams = {'Do': ['you'], 'you': ['like', 'like', 'like', 'like'], 'like': ['green',
'them,', 'green', 'them', 'them', 'them', 'green', 'them,', 'them', 'them', 'them', 'them',
'them', 'them', 'green', 'them,'], 'green': ['eggs', 'eggs', 'eggs', 'eggs'], 'eggs':
['and', 'and', 'and', 'and'], 'and': ['ham?', 'ham.', 'ham.', 'ham.'], 'ham?': ['I'], 'I':
['do', 'am.', 'do', 'would', 'would', 'do', 'do', 'am.', 'do', 'do', 'do', 'do', 'do', 'do',
'am.'], 'do': ['not', 'not', 'not', 'not', 'not', 'not', 'not', 'not', 'not', 'not'], 'not':
['like', 'like', 'like', 'like', 'like', 'like', 'like', 'like', 'like', 'like', 'like',
'like'], 'them,': ['Sam', 'Sam', 'Sam'], 'Sam': ['I', 'I', 'I'], 'am.': ['I', 'Would'],
'ham.': ['Would', 'I', 'I'], 'Would': ['you', 'you', 'you'], 'them': ['here', 'here',
'anywhere.', 'in', 'with', 'in', 'with', 'here', 'anywhere.'], 'here': ['or', 'or', 'or'],
'or': ['there?', 'there.', 'there.'], 'there?': ['I'], 'would': ['not', 'not'], 'there.':
['I', 'I'], 'anywhere.': ['I', 'I'], 'in': ['a', 'a'], 'a': ['house?', 'mouse?', 'house.',
'mouse.'], 'house?': ['Would'], 'with': ['a', 'a'], 'mouse?': ['I'], 'house.': ['I'],
'mouse.': ['I']}
word = input('Word: ')
for i in range(5):
word = random.choice(bigrams[word])
print(word, random.choice(bigrams[word]))
这是当前输出。
Word: do
not like
like them
them here
here or
or there.