-
Notifications
You must be signed in to change notification settings - Fork 1
/
04classify.py
240 lines (170 loc) · 5.61 KB
/
04classify.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# encoding=utf-8
import jieba
import os
import jieba.analyse
import sys
reload(sys)
sys.setdefaultencoding("utf8")
import math
def cos_dist(a, b):
if len(a) != len(b):
return None
part_up = 0.0
a_sq = 0.0
b_sq = 0.0
for a1, b1 in zip(a,b):
part_up += a1*b1
a_sq += a1**2
b_sq += b1**2
part_down = math.sqrt(a_sq*b_sq)
if part_down == 0.0:
print "here?"
return None
else:
return part_up / part_down
'''
从文件中得到向量
'''
def dict_from_file(file_path):
dict = {}
with open(file_path,'r') as inf:
for line in inf:
(key, val) = line.split()
#dict.append(eval(line))
dict[key] = float(val)
#print key+": "+val
return dict
def dict_from_content(file_path):
dict = {}
sourcefile = open(file_path,"r")
content = sourcefile.read()
#seg_list = jieba.cut(content, cut_all=False)
tags = jieba.analyse.extract_tags(content, topK=200, withWeight=True)
for tag in tags:
key_encoded = tag[0].encode("utf-8")
dict[key_encoded] = tag[1] #string and flat pair
#print key_encoded+": "+str(tag[1])
return dict
jieba.load_userdict("extradict/pla-dict.txt")
jieba.analyse.set_stop_words("extradict/stopwords")
jieba.analyse.set_idf_path("extradict/idf.txt");
all_force=["army","navy","airforce","rocket"]
all_force_dict={}
'''
得到所有军种的向量
'''
for force in all_force:
#print force
all_force_dict[force] = dict_from_file("data/"+force+".tfidf")
'''
按照目标map的顺序构造数组,如果在相关分类中这个维度没有,则记为0
'''
def print_dict(dict):
for key, value in dict.items():
print key+": "+str(value)
def cos_test(dict_target,dict_one_force):
vector_target = []
vector_candidate = []
for key, value in dict_one_force.items():
#print "process key: "+key
#key_encoded = key.decode("utf8")
vector_candidate.append(value)
target_value = 0.0
if key in dict_target:
target_value = dict_target[key]
vector_target.append(target_value)
else:
vector_target.append(0.0)
#print key+"\t"+str(value)+"\t" + str(target_value)
#print "should get here"
#print key+"\t"+str(value)+"\t" + str(target_value)
return cos_dist(vector_target,vector_candidate)
#return cos_dist(vector_candidate,vector_target)
'''
def cos_test2(dict_target,dict_one_force):
vector_target = []
vector_candidate = []
for key, value in dict_target.items():
#print "process key: "+key
vector_target.append(value)
#key_encoded = key.decode("utf8")
if key in dict_one_force:
vector_candidate.append(dict_one_force[key])
#print "should get here"
#print key+": "+str(value)+ str(dict_one_force[key])
else:
#print "s-----"
vector_candidate.append(0)
return cos_dist(vector_target,vector_candidate)
'''
def find_max_key(dict):
candidate_key=""
current_max_value = 0.0 # for positive values only
for key, value in dict.items():
if value > current_max_value :
current_max_value = value
candidate_key = key
return candidate_key
def find_min_key(dict):
candidate_key=""
current_min_value = 100000000.0 # for positive values only
for key, value in dict.items():
if value < current_min_value :
current_min_value = value
candidate_key = key
return candidate_key
#v1 = all_force_dict["navy"]
'''
得到目标文章的向量
'''
def test_article(target_path,all_force_vector):
result_map={}
target_map = dict_from_content(target_path)
for key, value in all_force_vector.items():
result = cos_test(target_map,value)
result_map [key] = result
print "The cosin value is "+ str(result)+" for "+key
type_of_force = find_max_key(result_map)
print "The max value shows the article '"+target_path+"' classify result is: "+type_of_force
#type_of_force = find_min_key(result_map)
#print "The min value shows the article '"+target_path+"' classify result is: "+type_of_force
return type_of_force
def test(target_path):
return test_article(target_path,all_force_dict)
#test("data/navitest.txt")
test("data/navi2test.txt")
#test("data/armytest.txt")
#test("data/army2test.txt")
#test("data/airforcetest.txt")
#test("data/rockettest.txt")
#test("data/rocket2test.txt")
'''
target_file_path = "data/navitest.txt"
target_map = dict_from_content(target_file_path)
result_map={}
for force in all_force:
result = cos_test(target_map,all_force_dict[force])
print ("the result is: "+ str(result)+" for "+force)
result_map [force] = result
type_of_force = find_min_key(result_map)
print "the min value shows the result is: "+type_of_force
print_dict(all_force_dict["navy"])
print "---------------------------------------------------------------"
print_dict(target_map)
d = {}
with open("file.txt") as f:
for line in f:
(key, val) = line.split()
d[int(key)] = val
target_file_path = "data/navitest.txt"
sourcefile = open(target_file_path,"r")
content = sourcefile.read()
#seg_list = jieba.cut(content, cut_all=False)
target_tags = jieba.analyse.extract_tags(content, topK=50, withWeight=True)
all_force=["army","navy","airforce","rocket"]
force_vector = list()
for force in all_force:
print force
force_vector
#gen_tfidf_for_one_file("data/"+force+"/"+force+".txt","data/"+force+"/"+force+".tf
'''