从某本书里随机找单词拼出句子段落。重点是把握好前缀和后缀,前缀要捆绑查找,后缀要关联对应。
Exercise 8: Markov analysis: Write a program to read a text from a file and perform Markov analysis. The result should be a dictionary that maps from prefixes to a collection of possible suffixes. The collection might be a list, tuple, or dictionary; it is up to you to make an appropriate choice. You can test your program with prefix length two, but you should write the program in a way that makes it easy to try other lengths. Add a function to the previous program to generate random text based on the Markov analysis. Here is an example from Emma with prefix length 2: He was very clever, be it sweetness or be angry, ashamed or only amused, at such a stroke. She had never thought of Hannah till you were never meant for me?” “I cannot make speeches, Emma:” he soon cut it all himself. For this example, I left the punctuation attached to the words. The result is almost syntactically correct, but not quite. Semantically, it almost makes sense, but not quite. What happens if you increase the prefix length? Does the random text make more sense? Once your program is working, you might want to try a mash-up: if you combine text from two or more books, the random text you generate will blend the vocabulary and phrases from the sources in interesting ways. Credit: This case study is based on an example from Kernighan and Pike, The Practice of Programming, Addison-Wesley, 1999. You should attempt this exercise before you go on; then you can download my solution from http://thinkpython2.com/code/markov.py. You will also need http://thinkpython2.com/code/emma.txt.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
| import string
import random
from collections import defaultdict
def set_book(fin1,num):
d = defaultdict(list) # 默认键值为列表
l = []
header = ()
for line in fin1:
line = line.replace('-', ' ')
for word in line.rstrip().split(): # 空格换行为分割,单词存入列表
l.append(word)
for i in range(len(l)-num): # 以列表序号逐一推进方式建立字典
header = (l[i-1],) # 元组header为前缀,做键
for j in range(i,i+num-1):
header += (l[j],)
j += 1
if l[i+num-1] not in d[header]:
d[header].append(l[i+num-1]) # 列表后缀做键值
return d
def next(start, book):
return random.choice(book[start])
fin1 = open('emma.txt', encoding='utf-8')
prefix_num = 3 # 前缀个数
suffix_num = 100 # 后缀个数
book = set_book(fin1,prefix_num)
start = random.choice(list(book.keys())) # 随机前缀开头
final = start
for i in range(suffix_num): # 截取最后几个单词为前缀找后缀
final += (next(final[len(final)-prefix_num:], book),)
for word in final:
print(word, end=' ')
# reigns alone. A very proper compliment! and then follows the application,
# which I think, my dear, you said you had a great deal happier if she had no
# intellectual superiority to make atonement to herself, or frighten those
# who might hate her into outward respect. She had never seen her look so well,
# so lovely, so engaging. There was consciousness, animation, and warmth;
# there was every appearance of its being all in proof of how much he was
# in love with, how to be able to return! I shall try what I can do.
# Harriet's features are very delicate, which makes a likeness |
import string
import random
from collections import defaultdict
def set_book(fin1,num):
d = defaultdict(list) # 默认键值为列表
l = []
header = ()
for line in fin1:
line = line.replace('-', ' ')
for word in line.rstrip().split(): # 空格换行为分割,单词存入列表
l.append(word)
for i in range(len(l)-num): # 以列表序号逐一推进方式建立字典
header = (l[i-1],) # 元组header为前缀,做键
for j in range(i,i+num-1):
header += (l[j],)
j += 1
if l[i+num-1] not in d[header]:
d[header].append(l[i+num-1]) # 列表后缀做键值
return d
def next(start, book):
return random.choice(book[start])
fin1 = open('emma.txt', encoding='utf-8')
prefix_num = 3 # 前缀个数
suffix_num = 100 # 后缀个数
book = set_book(fin1,prefix_num)
start = random.choice(list(book.keys())) # 随机前缀开头
final = start
for i in range(suffix_num): # 截取最后几个单词为前缀找后缀
final += (next(final[len(final)-prefix_num:], book),)
for word in final:
print(word, end=' ')
# reigns alone. A very proper compliment! and then follows the application,
# which I think, my dear, you said you had a great deal happier if she had no
# intellectual superiority to make atonement to herself, or frighten those
# who might hate her into outward respect. She had never seen her look so well,
# so lovely, so engaging. There was consciousness, animation, and warmth;
# there was every appearance of its being all in proof of how much he was
# in love with, how to be able to return! I shall try what I can do.
# Harriet's features are very delicate, which makes a likeness