-
Notifications
You must be signed in to change notification settings - Fork 2
/
analysis.py
81 lines (52 loc) · 1.26 KB
/
analysis.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#coding=utf-8
#import poetspider
fh = open('poems.txt','r')
fh2 = open('poems2.txt','w')
fh2.write('')
fh2.close()
fh2 = open('poems2.txt','a')
fh3 = open('counting.txt','w')
fh3.write('')
fh3.close()
fh3 = open('counting.txt','a')
chars = fh.read()
def is_chinese(uchar): #判断汉字
if uchar >= u'\u4e00' and uchar<=u'\u9fa5':
return True
elif uchar in [u'\uff0c', u'\u3002', u'\uff1f', u'\uff01', u'\uff1b',u'\n']:
return True
else :
return False
#allchars = chars.split()
chars = unicode(chars,'utf-8')
a = unicode('(一作','utf-8')
b = unicode('(','utf-8')
chars = chars.replace(a,b)
result = []
for i in chars:
if is_chinese(i) is True:
result.append(i)
#print result
for i in result:
poem = i.encode('utf-8')
fh2.write(poem)
poems=[]
for i in result:
poem = i.encode('utf-8')
poems.append(poem)
countingdict={}
print 'finishanalysis'
for i in poems:
if i in countingdict:
countingdict[i]= countingdict[i]+1
else:
countingdict[i] = 1
summary = 0
for i in countingdict:
summary = countingdict[i]+summary
for i in countingdict:
fh3.write(i+':'+str(countingdict[i])+str(countingdict[i]/summary)+'\n')
print summary
fh.close()
fh2.close()
fh3.close()