来自复旦大学同学的python期末大作业
import re
import random
markov_dicts = {'': []} # Start of Sentence
sentence_sep = '.?!' # 句子结束标志
Front = re.compile(r'^[A-Z][A-Za-z]*') # 开头的正则表达式
Middle = re.compile(r'[A-za-z]+$') # 中间单词正则表达式
Last = re.compile(r'[A-Za-z]+[.!?]$') # 结尾单词正则表达式
def parse(text):
""" 分析text,产生相应的dict"""
word_list = re.split(r'\s+', text)
print(word_list)
flag = 0
for i in word_list: # 获取开头单词
if Front.match(i) and flag == 0: # 如果flag等于0,且恰好处于开头单词
markov_dicts[''].append(i)
flag = 1
elif Last.match(i) and flag == 1: # 如果判断到结束的单词, 那么从新立flag=0,使下一句的开头单词正常记录
flag = 0
temp = word_list[0]
word_list.pop(0)
for i in word_list: # 获取字典
if temp not in markov_dicts.keys() and not Last.match(temp): # 新建单词在字典中的键值
markov_dicts[temp] = []
if Last.match(temp):
temp = i
continue
markov_dicts[temp].append(i) # 记录上一个单词紧跟的单词
temp = i
def generate(num_sentences=10, word_limit=20):
""" 根据前面调用parse得到的dict,随机生成多个句子"""
temp_wordnum = 1
temp_sentencenum = 0
temp_word = markov_dicts[''][random.randint(0, len(markov_dicts['']) - 1)]
sentence = temp_word + ''
while temp_sentencenum <= num_sentences:
if Last.match(
temp_word) or temp_wordnum > word_limit: # 判断句子是否结束 或者 单个句子词数大于规定的数量,如果结束重新开启新的一行
sentence += '\n'
temp_sentencenum += 1
temp_word = markov_dicts[''][random.randint(
0, len(markov_dicts['']) - 1)]
if temp_sentencenum > num_sentences: # 判断句子数是否大于规定的数
break
else:
sentence += temp_word
temp_wordnum = 1
continue
temp = markov_dicts[temp_word][random.randint(
0, len(markov_dicts[temp_word]) - 1)] # 正常推测
sentence += ' ' + temp
temp_word = temp
temp_wordnum += 1
return sentence
def markov_main():
text = 'X Y Z. X Z Y? Y X Z! Z Z Z. Y Z Y.'
# text= '''Help, I need somebody.
# Help, not just anybody.
# Help, you know I need someone, help.
#
# When I was younger so much younger than today,
# I never needed anybody's help in any way.
# But now these days are gone, I'm not so self assured.
# Now I find I've changed my mind and opened up the doors.
#
# Help me if you can, I'm feeling down.
# And I do appreciate you being round.
# Help me get my feet back on the ground.
# Won't you please, please help me.
#
# And now my life has changed in oh so many ways.
# My independence seems to vanish in the haze.
# But every now and then I feel so insecure.
# I know that I just need you like I've never done before.
#
# Help me if you can, I'm feeling down.
# And I do appreciate you being round.
# Help me get my feet back on the ground.
# Won't you please, please help me.
#
# When I was younger so much younger than today.
# I never needed anybody's help in any way.
# But now these days are gone, I'm not so self assured.
# Now I find I've changed my mind and opened up the doors.
#
# Help me if you can, I'm feeling down.
# And I do appreciate you being round.
# Help me, get my feet back on the ground.
# Won't you please, please help me, help me, help me, ooh...
#
#
# '''
parse(text)
print(generate())
if __name__ == '__main__':
markov_main()
generate()