/
Simsimidatamining.py
261 lines (243 loc) · 8.91 KB
/
Simsimidatamining.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/python
# -*- coding:utf-8 -*-
#encoding = utf-8
#import string
import urllib2
import threading
from jianfan import ftoj
from Queue import Queue
urllib2.socket.setdefaulttimeout(30)
f=open('Ciku1.txt','r')
out=open('Datamining1.txt','a')
out2=open('Datamining1-long.txt','a')
maxwordlength=40
maxthreads=10
count=0
maxretry=6
treedeep=12
lock1=threading.RLock()
lock2=threading.RLock()
threads=Queue(maxthreads)
def searchsimsimi(keyword):
#titles=[]
#values = {
#"msg":keyword
#}
#data = urllib.urlencode(values)
url_ch='http://www.simsimi.com/func/req?lc=ch&msg='
url_zh='http://www.simsimi.com/func/req?lc=zh&msg='
#keyword=urllib.urlencode(keyword,'utf-8')
request = urllib2.Request(str(url_ch)+keyword)
request2 = urllib2.Request(str(url_zh)+keyword)
#print keyword
request.add_header("Referer","http://www.simsimi.com/talk.htm")
# "Host","www.simsimi.com","Connection","keep-alive","X-Requested-With","XMLHttpRequest","User-Agent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.75 Safari/537.1","Content-Type","application/json","charset","utf-8")
request.add_header("Cookie","sagree=true; selected_nc=ch; JSESSIONID=DE1FB3B8CEAF68EA0E5BBBC81640796B; __utma=119922954.219482817.1343627343.1343627343.1344858094.2; __utmb=119922954.3.9.1344858110502; __utmc=119922954; __utmz=119922954.1343627343.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none))")
request2.add_header("Referer","http://www.simsimi.com/talk.htm")
request2.add_header("Cookie","sagree=true; selected_nc=ch; JSESSIONID=DE1FB3B8CEAF68EA0E5BBBC81640796B; __utma=119922954.219482817.1343627343.1343627343.1344858094.2; __utmb=119922954.3.9.1344858110502; __utmc=119922954; __utmz=119922954.1343627343.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none))")
t=0
wordset=set()
while t<maxretry:
try:
t+=1
result = urllib2.urlopen(request).read()
result2 = urllib2.urlopen(request2).read()
result=dealrequest(result)
result2=dealrequest(result2)
wordset.add(result)
wordset.add(result2)
if t>3:
return wordset
#break
except BaseException:
pass
if t>=maxretry:
print "error:"+str(t)+" :"+keyword
return wordset
#break
#return wordset
#print result
return wordset
def dealrequest(result):
word=""
try:
word=eval(result)["response"]
return word
except BaseException:
return word
def writetoaiml (input,outputset):
global out,count
out.write(" <category>\n")
out.write(" <pattern>")
words=ftoj(input.decode('utf-8')).encode('utf-8')
words=words.replace("&","&")
words=words.replace("<","<")
words=words.replace(">",">")
words=words.replace("'","'")
words=words.replace('"',""")
out.write(words)
out.write("</pattern>\n")
out.write(" <template>\n")
if len(outputset)>=1:
out.write(" <random>\n")
for x in outputset:
print input
print ftoj(x.decode('utf-8')).encode('utf-8')
out.write(" <li>")
words=x.replace("&","&")
words=words.replace("<","<")
words=words.replace(">",">")
words=words.replace("'","'")
words=words.replace('"',""")
words=ftoj(words.decode('utf-8')).encode('utf-8')
out.write(words)
out.write("</li>\n")
count += 1
out.write(" </random>\n")
else:
print input
x=outputset.pop()
print ftoj(x.decode('utf-8')).encode('utf-8')
words=x.replace("&","&")
words=words.replace("<","<")
words=words.replace(">",">")
words=words.replace("'","'")
words=words.replace('"',""")
words=ftoj(words.decode('utf-8')).encode('utf-8')
out.write(words+'\n')
count += 1
out.write(" </template>\n")
out.write(" </category>\n")
out.flush()
#count += 1
print count
def writetoaiml2 (input,outputset):
global out,count
out2.write(" <category>\n")
out2.write(" <pattern>")
words=ftoj(input.decode('utf-8')).encode('utf-8')
words=words.replace("&","&")
words=words.replace("<","<")
words=words.replace(">",">")
words=words.replace("'","'")
words=words.replace('"',""")
out2.write(words)
out2.write("</pattern>\n")
out2.write(" <template>\n")
if len(outputset)>=1:
out2.write(" <random>\n")
for x in outputset:
print input
print ftoj(x.decode('utf-8')).encode('utf-8')
out2.write(" <li>")
words=x.replace("&","&")
words=words.replace("<","<")
words=words.replace(">",">")
words=words.replace("'","'")
words=words.replace('"',""")
words=ftoj(words.decode('utf-8')).encode('utf-8')
out2.write(words)
out2.write("</li>\n")
count += 1
out2.write(" </random>\n")
else:
print input
x=outputset.pop()
print ftoj(x.decode('utf-8')).encode('utf-8')
words=x.replace("&","&")
words=words.replace("<","<")
words=words.replace(">",">")
words=words.replace("'","'")
words=words.replace('"',""")
words=ftoj(words.decode('utf-8')).encode('utf-8')
out2.write(words+'\n')
count += 1
out2.write(" </template>\n")
out2.write(" </category>\n")
out2.flush()
#count += 1
print count
class DMthread(threading.Thread):
global threads
def __init__(self):
threading.Thread.__init__(self)
self.wordset=set() #当前索引词的大集合
self.wordlist=set() #目前放进去词搜索产生的集合,也就是要接下来搜索
self.wordlist1=set() #临时存储2级结点生成的词典
self.wordlist2=set() #临时当前词simsimi返回生成的当前词典
self.i=0
self.word=""
def run(self):
self.i=0
self.word=threads.get(1,10)
self.wordlist.add(self.word)
while True:
self.wordlist1=self.wordlist.copy()
for word in self.wordlist:
self.wordlist1.remove(word)
#print word
#word="力量"
self.wordlist2=searchsimsimi(word.replace(' ','+'))
#print wordlist
if not len(self.wordlist2):
break
if (self.wordset&self.wordlist2)==self.wordlist2:
break
self.wordlist1|=self.wordlist2
if len(self.wordlist2)>0:
self.wordset.add(word)
if len(word.strip())<=maxwordlength:
lock1.acquire()
writetoaiml(word,self.wordlist2)
lock1.release()
else:
lock2.acquire()
writetoaiml2(word,self.wordlist2)
lock2.release()
self.i += 1
self.wordset|=self.wordlist1
self.wordlist=set()|self.wordlist1
if not len(self.wordlist):
#threads.task_done()
break
if self.i>=treedeep:
print "treedeep arrive:"+str(treedeep) + " break at keyword: "+self.word," "
print self.wordlist1
break
threads.task_done()
if __name__=='__main__':
#count=0
while True:
text=f.readline().strip()
if len(text)==0 and (threads.qsize()>0):
k=threads.qsize()
for j in range(0,k):
try:
t=DMthread()
t.start()
except RuntimeError:
print "threads had started "
threads.join()
#threads.remove(t)
#count=0
if len(text)==0 and (threads.qsize()==0):
break
if threads.qsize()<maxthreads:
#t=DMthread()
threads.put(text,1)
#count+=1
if threads.qsize()>=maxthreads:
k=threads.qsize()
for j in range(0,k):
try:
t=DMthread()
t.start()
#threads.task_done()
except RuntimeError:
print "threads had started "
threads.join()
#count=0
#threads=[]
print "finish"
f.close()
out.close()