-
Notifications
You must be signed in to change notification settings - Fork 0
/
enhancer.py
151 lines (87 loc) · 2.32 KB
/
enhancer.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'''
enhance the accuracy of google recognition
author: backman
e-mail: backman.only@gmail.com
method:
1. transform words from google voice Recognition to Wade-Giles encode
2. find domain language words by string matching (Wade-Giles encode)
'''
from pypinyin import pinyin, lazy_pinyin
import pypinyin
import pickle
DB ="all.pack"
def editDistance(str1,str2):
dpTable=[[0 for x in range(len(str2)+1) ] for y in range(len(str1)+1 )]
#initial table
for i in range(len(str2)+1):
dpTable[0][i]=i
for i in range(len(str1)+1):
dpTable[i][0]=i
for x in range(1,len(str2)+1):
for y in range(1,len(str1)+1):
if str2[x-1] == str1[y-1]:
dpTable[y][x]=dpTable[y-1][x-1]
else:
dpTable[y][x]= min(dpTable[y-1][x-1]+1,dpTable[y-1][x],dpTable[y][x-1])+1
return dpTable[len(str1)][len(str2)]
def similarity(str1,str2):
str2="-".join(lazy_pinyin(str2))
ED=editDistance(str1,str2)
sLen= len(str2)
value=(float(sLen-ED)/float(sLen ) *100.0)
if 0<= value and value <=100.0 :
return value
else:
return 0
def domainWordsCorrector(strs,threshold):
candidates={}
geoDict=pickle.load(open(DB,"rb"))
for obj in geoDict.keys():
# calculate every words's ED value.
value=similarity(obj,strs)
if value > threshold:
candidates[str(geoDict[obj])]=value
#DEBUG!
for obj in candidates.keys():
print (obj,candidates[obj])
if len(candidates.keys()) ==0:
return None
#find most similar one!!
maxV=0.0
result=""
for obj in candidates.keys():
if maxV < candidates[obj]:
maxV=candidates[obj]
result=str(obj)
return (result,maxV)
# database change
def teamExtractor(rawText):
if rawText is None:
return None
# only 長度三
infoWords=[]
for idx in range(len(rawText)):
word3=domainWordsCorrector(rawText[idx:idx+2],80)
if word3!=None:
infoWords.append(word3[0])
'''
word4=domainWordsCorrector(rawText[idx:idx+4],80)
if word4!=None:
infoWords.append(word4[0])
'''
return infoWords
def domainWordExtractor(rawText):
if rawText is None:
return None
# only 長度三
infoWords=[]
for idx in range(len(rawText)):
word3=domainWordsCorrector(rawText[idx:idx+3],70)
if word3!=None:
infoWords.append(word3[0])
'''
word4=domainWordsCorrector(rawText[idx:idx+4],80)
if word4!=None:
infoWords.append(word4[0])
'''
return infoWords