我被困在这里(下图),需要通过它才能读取我的文本。我还有 text.txt 文件。该程序本身无法使用 python 2 运行。使用 python 2 它在这里给了我一个错误:print(last_suggestion, end=' ', flush=True)。
train_data = 'text.txt'
first_possible_words = {}
second_possible_words = {}
transitions = {}
def expandDict(dictionary, key, value):
if key not in dictionary:
dictionary[key] = []
dictionary[key].append(value)
def get_next_probability(given_list): #returns dictionary
probability_dict = {}
given_list_length = len(given_list)
for item in given_list:
probability_dict[item] = probability_dict.get(item, 0) + 1
for key, value in probability_dict.items():
probability_dict[key] = value / given_list_length
return probability_dict
def trainMarkovModel():
for line in open(train_data):
tokens = line.rstrip().lower().split() …Run Code Online (Sandbox Code Playgroud)