|
1 | 1 | import jieba |
2 | | -f=open("三国演义上.txt","r",encoding="utf-8") |
3 | | -txt1=f.read() |
| 2 | +f = open("三国演义上.txt", "r", encoding="utf-8") |
| 3 | +txt1 = f.read() |
4 | 4 | f.close() |
5 | | -f=open("三国演义下.txt","r",encoding="utf-8") |
6 | | -txt2=f.read() |
| 5 | +f = open("三国演义下.txt", "r", encoding="utf-8") |
| 6 | +txt2 = f.read() |
7 | 7 | f.close() |
8 | | -txt=txt1+txt2 |
9 | | -excludes={"将军","却说","荆州","二人","不可","不能","如此","军士","天下","主公","商议","如何","徐州","左右","先生","次日",\ |
10 | | - "江东","百姓","军马","引兵","大喜","东吴","于是","今日","不敢","魏兵","陛下","一人","都督","人马","不知"} |
11 | | -words=jieba.lcut(txt) |
12 | | -counts={} |
| 8 | +txt = txt1+txt2 |
| 9 | +excludes = {"将军", "却说", "荆州", "二人", "不可", "不能", "如此", "军士", "天下", "主公", "商议", "如何", "徐州", "左右", "先生", "次日", |
| 10 | + "江东", "百姓", "军马", "引兵", "大喜", "东吴", "于是", "今日", "不敢", "魏兵", "陛下", "一人", "都督", "人马", "不知"} |
| 11 | +words = jieba.lcut(txt) |
| 12 | +counts = {} |
13 | 13 | for word in words: |
14 | | - if len(word)==1: |
| 14 | + if len(word) == 1: |
15 | 15 | continue |
16 | | - elif word=="诸葛亮"or word=="孔明曰": |
17 | | - rword="孔明" |
18 | | - elif word=="关公"or word=="云长": |
19 | | - rword="关羽" |
20 | | - elif word=="玄德"or word=="玄德曰": |
21 | | - rword="刘备" |
22 | | - elif word=="孟德"or word=="丞相": |
23 | | - rword="曹操" |
| 16 | + elif word == "诸葛亮"or word == "孔明曰": |
| 17 | + rword = "孔明" |
| 18 | + elif word == "关公"or word == "云长": |
| 19 | + rword = "关羽" |
| 20 | + elif word == "玄德"or word == "玄德曰": |
| 21 | + rword = "刘备" |
| 22 | + elif word == "孟德"or word == "丞相": |
| 23 | + rword = "曹操" |
24 | 24 | else: |
25 | | - rword=word |
26 | | - counts[rword]=counts.get(rword,0)+1 |
| 25 | + rword = word |
| 26 | + counts[rword] = counts.get(rword, 0)+1 |
27 | 27 | for word in excludes: |
28 | 28 | del counts[word] |
29 | | -items=list(counts.items()) |
30 | | -items.sort(key=lambda x:x[1],reverse=True) |
| 29 | +items = list(counts.items()) |
| 30 | +items.sort(key=lambda x: x[1], reverse=True) |
31 | 31 | for i in range(10): |
32 | | - word,count=items[i] |
33 | | - print("{0:<10}{1:>5}".format(word,count)) |
| 32 | + word, count = items[i] |
| 33 | + print("{0:<10}{1:>5}".format(word, count)) |
0 commit comments