-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposition.py
54 lines (52 loc) · 1.58 KB
/
position.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
"""
Algorithms for char position
Author: Mao
"""
from collections import Counter
def posrecord(text):
"""
Store information of each char position in word
"""
charpos={}
for word in text:
length = len(word)-1
if length > 0:
for i, char in enumerate(word):
if char not in charpos:
charpos[char]=[0]*3
if i == 0: #词首
charpos[char][0] = charpos[char][0]+1
elif i == length: #词尾
charpos[char][2] = charpos[char][2]+1
else: #词中
charpos[char][1] = charpos[char][1]+1
return charpos
def pwprobability(text,charpos,doc,threshold):
"""
Compute position probability of each word
filter word
"""
#每个字在文档中出现的频率
charfreq =Counter()
for w in doc:
charfreq[w]+=1
charfreq = dict(charfreq)
#计算位置成词概率并获得新的词集合
genwords = []
for word in text:
length = len(word) - 1
if length > 0:
for i,char in enumerate(word):
wp = 1
pw = 1
if i == 0:
wp = wp*charpos[char][0]
elif i == length:
wp = wp*charpos[char][2]
else:
wp = wp*charpos[char][1]
pw = pw*charfreq[char]
pwp = wp/pw
if pwp > threshold:
genwords.append(word)
return set(genwords)