documentMerge.py 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. #coding:UTF8
  2. from odps.udf import annotate
  3. from odps.distcache import get_cache_archive
  4. from odps.distcache import get_cache_file
  5. from odps.udf import BaseUDTF,BaseUDAF
  6. import threading
  7. import logging
  8. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  9. import time
  10. import json
  11. def log(msg):
  12. logging.info(msg)
  13. # 配置pandas依赖包
  14. def include_package_path(res_name):
  15. import os, sys
  16. archive_files = get_cache_archive(res_name)
  17. dir_names = sorted([os.path.dirname(os.path.normpath(f.name)) for f in archive_files
  18. if '.dist_info' not in f.name], key=lambda v: len(v))
  19. _path = dir_names[0].split(".zip/files")[0]+".zip/files"
  20. log("add path:%s"%(_path))
  21. sys.path.append(_path)
  22. return os.path.dirname(dir_names[0])
  23. # 可能出现类似RuntimeError: xxx has been blocked by sandbox
  24. # 这是因为包含C的库,会被沙盘block,可设置set odps.isolation.session.enable = true
  25. def include_file(file_name):
  26. import os, sys
  27. so_file = get_cache_file(file_name)
  28. sys.path.append(os.path.dirname(os.path.abspath(so_file.name)))
  29. def include_so(file_name):
  30. import os, sys
  31. so_file = get_cache_file(file_name)
  32. with open(so_file.name, 'rb') as fp:
  33. content=fp.read()
  34. so = open(file_name, "wb")
  35. so.write(content)
  36. so.flush()
  37. so.close()
  38. #初始化业务数据包,由于上传限制,python版本以及archive解压包不统一等各种问题,需要手动导入
  39. def init_env(list_files,package_name):
  40. import os,sys
  41. if len(list_files)==1:
  42. so_file = get_cache_file(list_files[0])
  43. cmd_line = os.path.abspath(so_file.name)
  44. os.system("unzip -o %s -d %s"%(cmd_line,package_name))
  45. elif len(list_files)>1:
  46. cmd_line = "cat"
  47. for _file in list_files:
  48. so_file = get_cache_file(_file)
  49. cmd_line += " "+os.path.abspath(so_file.name)
  50. cmd_line += " > temp.zip"
  51. os.system(cmd_line)
  52. os.system("unzip -o temp.zip -d %s"%(package_name))
  53. # os.system("rm -rf %s/*.dist-info"%(package_name))
  54. # return os.listdir(os.path.abspath("local_package"))
  55. # os.system("echo export LD_LIBRARY_PATH=%s >> ~/.bashrc"%(os.path.abspath("local_package")))
  56. # os.system("source ~/.bashrc")
  57. sys.path.insert(0,os.path.abspath(package_name))
  58. # sys.path.append(os.path.join(os.path.abspath("local_package"),"interface_real"))
  59. import platform
  60. def getSet(list_dict,key):
  61. _set = set()
  62. for item in list_dict:
  63. if key in item:
  64. if item[key]!='' and item[key] is not None:
  65. if re.search("^[\d\.]+$",item[key]) is not None:
  66. _set.add(str(float(item[key])))
  67. else:
  68. _set.add(str(item[key]))
  69. return _set
  70. def split_with_time(list_dict,sort_key,timedelta=86400*120):
  71. if len(list_dict)>0:
  72. if sort_key in list_dict[0]:
  73. list_dict.sort(key=lambda x:x[sort_key])
  74. list_group = []
  75. _begin = 0
  76. for i in range(len(list_dict)-1):
  77. if abs(list_dict[i][sort_key]-list_dict[i+1][sort_key])<timedelta:
  78. continue
  79. else:
  80. _group = []
  81. for j in range(_begin,i+1):
  82. _group.append(list_dict[j])
  83. if len(_group)>1:
  84. list_group.append(_group)
  85. _begin = i + 1
  86. if len(list_dict)>1:
  87. _group = []
  88. for j in range(_begin,len(list_dict)):
  89. _group.append(list_dict[j])
  90. if len(_group)>1:
  91. list_group.append(_group)
  92. return list_group
  93. return [list_dict]
  94. @annotate('bigint,bigint,string,string,string,string,string,string,bigint->string')
  95. class f_merge_rule_limit_num_contain_greater(BaseUDAF):
  96. '''
  97. 项目编号、中标单位、len(项目编号)>7、中标单位<> ""、合并后非空招标单位数<2、合并后同公告类型非空金额相同
  98. '''
  99. def __init__(self):
  100. import logging
  101. import json,re
  102. global json,logging,re
  103. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  104. def new_buffer(self):
  105. return [list()]
  106. def iterate(self, buffer,docid,page_time_stamp,set_limit_column1,set_limit_column2,set_limit_column3,set_limit_column4,contain_column,greater_column,MAX_NUM):
  107. buffer[0].append({"docid":docid,"page_time_stamp":page_time_stamp,"set_limit_column1":set_limit_column1,
  108. "set_limit_column2":set_limit_column2,"set_limit_column3":set_limit_column3,"set_limit_column4":set_limit_column4,
  109. "contain_column":contain_column,"greater_column":greater_column,"MAX_NUM":MAX_NUM})
  110. def merge(self, buffer, pbuffer):
  111. buffer[0].extend(pbuffer[0])
  112. def terminate(self, buffer):
  113. MAX_NUM = 5
  114. if len(buffer[0])>0:
  115. MAX_NUM = buffer[0][0]["MAX_NUM"]
  116. list_split = split_with_time(buffer[0],"page_time_stamp")
  117. list_group = []
  118. for _split in list_split:
  119. flag = True
  120. keys = ["set_limit_column1","set_limit_column2","set_limit_column3","set_limit_column4"]
  121. dict_set = {}
  122. for _key in keys:
  123. dict_set[_key] = set()
  124. if len(_split)>MAX_NUM:
  125. flag = False
  126. else:
  127. for _key in keys:
  128. logging.info(_key+str(getSet(_split,_key)))
  129. if len(getSet(_split,_key))>1:
  130. flag = False
  131. break
  132. MAX_CONTAIN_COLUMN = None
  133. #判断组内每条公告是否包含
  134. if flag:
  135. for _d in _split:
  136. contain_column = _d["contain_column"]
  137. if contain_column is not None and contain_column !="":
  138. if MAX_CONTAIN_COLUMN is None:
  139. MAX_CONTAIN_COLUMN = contain_column
  140. else:
  141. if len(MAX_CONTAIN_COLUMN)<len(contain_column):
  142. if contain_column.find(MAX_CONTAIN_COLUMN)==-1:
  143. flag = False
  144. break
  145. MAX_CONTAIN_COLUMN = contain_column
  146. else:
  147. if MAX_CONTAIN_COLUMN.find(contain_column)==-1:
  148. flag = False
  149. break
  150. if len(getSet(_split,"greater_column"))==1:
  151. flag = False
  152. break
  153. if flag:
  154. _set_docid = set()
  155. for item in _split:
  156. _set_docid.add(item["docid"])
  157. if len(_set_docid)>1:
  158. list_group.append(list(_set_docid))
  159. return json.dumps(list_group)
  160. def getDiffIndex(list_dict,key):
  161. _set = set()
  162. for _i in range(len(list_dict)):
  163. item = list_dict[_i]
  164. if key in item:
  165. if item[key]!='' and item[key] is not None:
  166. if re.search("^\d[\d\.]*$",item[key]) is not None:
  167. _set.add(str(float(item[key])))
  168. else:
  169. _set.add(str(item[key]))
  170. if len(_set)>1:
  171. return _i
  172. return len(list_dict)
  173. @annotate('bigint,bigint,string,string,string,string,string,string,string,bigint->string')
  174. class f_remege_limit_num_contain(BaseUDAF):
  175. '''
  176. 项目编号、中标单位、len(项目编号)>7、中标单位<> ""、合并后非空招标单位数<2、合并后同公告类型非空金额相同
  177. '''
  178. def __init__(self):
  179. import logging
  180. import json,re
  181. global json,logging,re
  182. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  183. def new_buffer(self):
  184. return [list()]
  185. def iterate(self, buffer,docid,page_time_stamp,set_limit_column1,set_limit_column2,set_limit_column3,set_limit_column4,contain_column1,contain_column2,notLike_column,confidence):
  186. buffer[0].append({"docid":docid,"page_time_stamp":page_time_stamp,"set_limit_column1":set_limit_column1,
  187. "set_limit_column2":set_limit_column2,"set_limit_column3":set_limit_column3,"set_limit_column4":set_limit_column4,
  188. "contain_column1":contain_column1,"contain_column2":contain_column2,"notLike_column":notLike_column,"confidence":confidence})
  189. def merge(self, buffer, pbuffer):
  190. buffer[0].extend(pbuffer[0])
  191. def getNotLikeSet(self,_dict,column_name):
  192. column_value = _dict.get(column_name,None)
  193. _set = set()
  194. if column_value is not None:
  195. for _i in range(1,len(column_value)):
  196. _set.add(column_value[_i-1:_i+1])
  197. _dict["notLike_set"] = _set
  198. def getSimilarity(self,_set1,_set2):
  199. _sum = max([1,min([len(_set1),len(_set2)])])
  200. return len(_set1&_set2)/_sum
  201. def terminate(self, buffer):
  202. list_group = []
  203. the_group = buffer[0]
  204. SIM_PROB = 0.6
  205. for _d in the_group:
  206. self.getNotLikeSet(_d,"notLike_column")
  207. #判断多个值与否
  208. keys = ["set_limit_column1","set_limit_column2","set_limit_column3","set_limit_column4"]
  209. re_merge = False
  210. for _key in keys:
  211. if len(getSet(the_group,_key))>1:
  212. re_merge = True
  213. break
  214. #判断是否相似而不相同
  215. re_merge_sim = False
  216. for _i1 in range(0,len(the_group)):
  217. for _j1 in range(_i1+1,len(the_group)):
  218. _set1 = the_group[_i1]["notLike_set"]
  219. _set2 = the_group[_j1]["notLike_set"]
  220. _sim = self.getSimilarity(_set1,_set2)
  221. if _sim>SIM_PROB and _sim<1:
  222. re_merge_sim = True
  223. break
  224. contain_keys = ["contain_column1","contain_column2"]
  225. logging.info(the_group)
  226. logging.info(str(re_merge)+str(re_merge_sim))
  227. if re_merge or re_merge_sim:
  228. the_group.sort(key=lambda x:x["confidence"],reverse=True)
  229. the_group.sort(key=lambda x:x["page_time_stamp"])
  230. #重新成组
  231. dict_docid_doc = {}
  232. for _doc in the_group:
  233. dict_docid_doc[_doc["docid"]] = _doc
  234. for _doc in the_group:
  235. merge_flag = False
  236. for _index in range(len(list_group)):
  237. _g = list_group[_index]
  238. hit_count = 0
  239. dict_temp = dict()
  240. #多个值的异常
  241. if re_merge:
  242. for _c_key in contain_keys:
  243. dict_temp[_c_key] = _g[_c_key]
  244. if _g[_c_key] is not None and _doc[_c_key] is not None:
  245. if len(_g[_c_key])>len(_doc[_c_key]):
  246. if str(_g[_c_key]).find(str(_doc[_c_key]))>=0:
  247. dict_temp[_c_key] = _g[_c_key]
  248. hit_count += 1
  249. else:
  250. if str(_doc[_c_key]).find(str(_g[_c_key]))>=0:
  251. dict_temp[_c_key] = _doc[_c_key]
  252. _g[_c_key] = _doc[_c_key]
  253. hit_count += 1
  254. else:
  255. hit_count = 1
  256. # if hit_count==len(contain_keys):
  257. if hit_count>0:
  258. _flag_sim = False
  259. #相似而不相同的异常
  260. if re_merge_sim:
  261. for _docid in _g["docid"]:
  262. tmp_d = dict_docid_doc[_docid]
  263. _sim = self.getSimilarity(tmp_d["notLike_set"],_doc["notLike_set"])
  264. if _sim>SIM_PROB and _sim<1:
  265. _flag_sim = True
  266. if not _flag_sim:
  267. for _c_key in dict_temp.keys():
  268. _g[_c_key] = dict_temp[_c_key]
  269. _g["docid"].append(_doc["docid"])
  270. merge_flag = True
  271. break
  272. if not merge_flag:
  273. _dict = dict()
  274. _dict["docid"] = [_doc["docid"]]
  275. for _c_key in contain_keys:
  276. _dict[_c_key] = _doc[_c_key]
  277. list_group.append(_dict)
  278. final_group = []
  279. #判断是否符合一个值
  280. for _group in list_group:
  281. _split = []
  282. for _docid in _group["docid"]:
  283. _split.append(dict_docid_doc[_docid])
  284. #通过置信度排序,尽可能保留组
  285. _split.sort(key=lambda x:x["confidence"],reverse=True)
  286. #置信度
  287. list_key_index = []
  288. for _k in keys:
  289. list_key_index.append(getDiffIndex(_split,_k))
  290. _index = min(list_key_index)
  291. final_group.append([_c["docid"] for _c in _split[:_index]])
  292. for _c in _split[_index:]:
  293. final_group.append([_c["docid"]])
  294. #若是找到两个以上,则全部单独成组,否则成一组
  295. # _flag = True
  296. # for _key in keys:
  297. # if len(getSet(_split,_key))>1:
  298. # _flag = False
  299. # break
  300. # if not _flag:
  301. # for _docid in _group["docid"]:
  302. # final_group.append([_docid])
  303. # else:
  304. # final_group.append(list(set(_group["docid"])))
  305. else:
  306. final_group = [list(set([item["docid"] for item in the_group]))]
  307. log(str(final_group))
  308. return json.dumps(final_group)
  309. def getCurrent_date(format="%Y-%m-%d %H:%M:%S"):
  310. _time = time.strftime(format,time.localtime())
  311. return _time
  312. @annotate('bigint->string')
  313. class f_get_single_merged_bychannel(BaseUDTF):
  314. def process(self,docid):
  315. _d = {"data":{str(docid):[]},"process_time":getCurrent_date()}
  316. self.forward(json.dumps(_d))
  317. @annotate('bigint,bigint,bigint,string,string,string,string,string,string,string,bigint->string')
  318. class f_remege_limit_num_contain_bychannel(BaseUDAF):
  319. '''
  320. 项目编号、中标单位、len(项目编号)>7、中标单位<> ""、合并后非空招标单位数<2、合并后同公告类型非空金额相同
  321. '''
  322. def __init__(self):
  323. import logging
  324. import json,re
  325. global json,logging,re
  326. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  327. def new_buffer(self):
  328. return [list()]
  329. def iterate(self, buffer,docid,docchannel,page_time_stamp,set_limit_column1,set_limit_column2,set_limit_column3,set_limit_column4,contain_column1,contain_column2,notLike_column,confidence):
  330. _dict = {"docid":docid,"docchannel":docchannel,"page_time_stamp":page_time_stamp,"set_limit_column1":set_limit_column1,
  331. "set_limit_column2":set_limit_column2,"set_limit_column3":set_limit_column3,"set_limit_column4":set_limit_column4,
  332. "contain_column1":contain_column1,"contain_column2":contain_column2,"notLike_column":notLike_column,"confidence":confidence}
  333. _count = 0
  334. for _,v in _dict.items():
  335. if v is not None and str(v)!="":
  336. _count += 1
  337. _dict["extract_count"] = _count
  338. buffer[0].append(_dict)
  339. def merge(self, buffer, pbuffer):
  340. buffer[0].extend(pbuffer[0])
  341. def getNotLikeSet(self,_dict,column_name):
  342. column_value = _dict.get(column_name,None)
  343. _set = set()
  344. if column_value is not None:
  345. for _i in range(1,len(column_value)):
  346. _set.add(column_value[_i-1:_i+1])
  347. _dict["notLike_set"] = _set
  348. def getSimilarity(self,_set1,_set2):
  349. _sum = max([1,min([len(_set1),len(_set2)])])
  350. return len(_set1&_set2)/_sum
  351. def terminate(self, buffer):
  352. list_group = []
  353. the_group = buffer[0]
  354. SIM_PROB = 0.6
  355. for _d in the_group:
  356. self.getNotLikeSet(_d,"notLike_column")
  357. #判断多个值与否
  358. keys = ["set_limit_column1","set_limit_column2","set_limit_column3","set_limit_column4"]
  359. re_merge = False
  360. for _key in keys:
  361. if len(getSet(the_group,_key))>1:
  362. re_merge = True
  363. break
  364. #判断是否相似而不相同
  365. re_merge_sim = False
  366. for _i1 in range(0,len(the_group)):
  367. for _j1 in range(_i1+1,len(the_group)):
  368. _set1 = the_group[_i1]["notLike_set"]
  369. _set2 = the_group[_j1]["notLike_set"]
  370. _sim = self.getSimilarity(_set1,_set2)
  371. if _sim>SIM_PROB and _sim<1:
  372. re_merge_sim = True
  373. break
  374. contain_keys = ["contain_column1","contain_column2"]
  375. logging.info(the_group)
  376. logging.info(str(re_merge)+str(re_merge_sim))
  377. #重新成组
  378. dict_docid_doc = {}
  379. for _doc in the_group:
  380. dict_docid_doc[_doc["docid"]] = _doc
  381. if re_merge or re_merge_sim:
  382. the_group.sort(key=lambda x:x["confidence"],reverse=True)
  383. the_group.sort(key=lambda x:x["page_time_stamp"])
  384. for _doc in the_group:
  385. merge_flag = False
  386. for _index in range(len(list_group)):
  387. _g = list_group[_index]
  388. hit_count = 0
  389. dict_temp = dict()
  390. #多个值的异常
  391. if re_merge:
  392. for _c_key in contain_keys:
  393. dict_temp[_c_key] = _g[_c_key]
  394. if _g[_c_key] is not None and _doc[_c_key] is not None:
  395. if len(_g[_c_key])>len(_doc[_c_key]):
  396. if str(_g[_c_key]).find(str(_doc[_c_key]))>=0:
  397. dict_temp[_c_key] = _g[_c_key]
  398. hit_count += 1
  399. else:
  400. if str(_doc[_c_key]).find(str(_g[_c_key]))>=0:
  401. dict_temp[_c_key] = _doc[_c_key]
  402. _g[_c_key] = _doc[_c_key]
  403. hit_count += 1
  404. else:
  405. hit_count = 1
  406. # if hit_count==len(contain_keys):
  407. if hit_count>0:
  408. _flag_sim = False
  409. #相似而不相同的异常
  410. if re_merge_sim:
  411. for _docid in _g["docid"]:
  412. tmp_d = dict_docid_doc[_docid]
  413. _sim = self.getSimilarity(tmp_d["notLike_set"],_doc["notLike_set"])
  414. if _sim>SIM_PROB and _sim<1:
  415. _flag_sim = True
  416. if not _flag_sim:
  417. for _c_key in dict_temp.keys():
  418. _g[_c_key] = dict_temp[_c_key]
  419. _g["docid"].append(_doc["docid"])
  420. merge_flag = True
  421. break
  422. if not merge_flag:
  423. _dict = dict()
  424. _dict["docid"] = [_doc["docid"]]
  425. for _c_key in contain_keys:
  426. _dict[_c_key] = _doc[_c_key]
  427. list_group.append(_dict)
  428. final_group = []
  429. #判断是否符合一个值
  430. for _group in list_group:
  431. _split = []
  432. for _docid in _group["docid"]:
  433. _split.append(dict_docid_doc[_docid])
  434. #通过置信度排序,尽可能保留组
  435. _split.sort(key=lambda x:x["confidence"],reverse=True)
  436. #置信度
  437. list_key_index = []
  438. for _k in keys:
  439. list_key_index.append(getDiffIndex(_split,_k))
  440. _index = min(list_key_index)
  441. final_group.append([_c["docid"] for _c in _split[:_index]])
  442. for _c in _split[_index:]:
  443. final_group.append([_c["docid"]])
  444. #若是找到两个以上,则全部单独成组,否则成一组
  445. # _flag = True
  446. # for _key in keys:
  447. # if len(getSet(_split,_key))>1:
  448. # _flag = False
  449. # break
  450. # if not _flag:
  451. # for _docid in _group["docid"]:
  452. # final_group.append([_docid])
  453. # else:
  454. # final_group.append(list(set(_group["docid"])))
  455. else:
  456. final_group = [list(set([item["docid"] for item in the_group]))]
  457. log(str(final_group))
  458. #每个channel选择一篇公告
  459. final_group_channel = []
  460. for _group in final_group:
  461. dict_channel_id = {}
  462. otherChannel = 10000
  463. for _docid in _group:
  464. _channel = dict_docid_doc[_docid].get("docchannel")
  465. if _channel in [115,116,117]:
  466. otherChannel += 1
  467. _channel = otherChannel
  468. if _channel not in dict_channel_id:
  469. dict_channel_id[_channel] = []
  470. dict_channel_id[_channel].append([_docid,dict_docid_doc[_docid].get("page_time_stamp"),dict_docid_doc[_docid].get("extract_count")])
  471. channel_dict = {}
  472. for k,v in dict_channel_id.items():
  473. v.sort(key=lambda x:x[1])
  474. v.sort(key=lambda x:x[2],reverse=True)
  475. channel_dict[v[0][0]] = []
  476. for _docs in v[1:]:
  477. channel_dict[v[0][0]].append(_docs[0])
  478. _d = {"data":channel_dict,"process_time":getCurrent_date()}
  479. final_group_channel.append(_d)
  480. return json.dumps(final_group_channel)
  481. @annotate('string -> string')
  482. class f_get_remerge_group_channel(BaseUDTF):
  483. '''
  484. 将多个组拆解成多条记录
  485. '''
  486. def __init__(self):
  487. import logging
  488. import json
  489. global json,logging
  490. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  491. def process(self,json_remerge):
  492. if json_remerge is not None:
  493. list_group = json.loads(json_remerge)
  494. for _group in list_group:
  495. self.forward(json.dumps(_group))
  496. @annotate('string -> string')
  497. class f_get_remerge_group(BaseUDTF):
  498. '''
  499. 将多个组拆解成多条记录
  500. '''
  501. def __init__(self):
  502. import logging
  503. import json
  504. global json,logging
  505. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  506. def process(self,json_remerge):
  507. if json_remerge is not None:
  508. list_group = json.loads(json_remerge)
  509. for _group in list_group:
  510. l_g = list(set(_group))
  511. l_g.sort(key=lambda x:x)
  512. list_docid = [str(_docid) for _docid in l_g]
  513. self.forward(",".join(list_docid))
  514. @annotate('bigint,bigint,string->string')
  515. class f_merge_probability(BaseUDAF):
  516. '''
  517. 合并组为一条记录
  518. '''
  519. def __init__(self):
  520. import json
  521. global json
  522. def new_buffer(self):
  523. return [[]]
  524. def iterate(self, buffer,docid,page_time_stamp,_type):
  525. buffer[0].append({"docid":docid,"page_time_stamp":page_time_stamp,"type":_type})
  526. def merge(self, buffer, pbuffer):
  527. buffer[0].extend(pbuffer[0])
  528. def terminate(self, buffer):
  529. list_dict = buffer[0]
  530. list_dict = list_dict[:10000]
  531. list_group = split_with_time(list_dict,sort_key="page_time_stamp",timedelta=86400*120)
  532. return json.dumps(list_group)
  533. @annotate('string -> bigint,bigint,bigint,bigint,string')
  534. class f_split_merge_probability(BaseUDTF):
  535. def __init__(self):
  536. import logging
  537. import json
  538. global logging,json
  539. logging.basicConfig(level=logging.INFO,format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  540. def process(self,list_group_str):
  541. logging.info("0")
  542. logging.info(list_group_str)
  543. if list_group_str is not None:
  544. logging.info("1")
  545. try:
  546. list_group = json.loads(list_group_str)
  547. logging.info("2")
  548. for _group in list_group:
  549. if len(_group)>0:
  550. _type = _group[0].get("type","")
  551. logging.info("3%d"%len(list_group))
  552. # _group.sort(key=lambda x:x["page_time_stamp"])
  553. _len = min(100,len(_group))
  554. for _index_i in range(_len):
  555. _count = 0
  556. for _index_j in range(_index_i+1,_len):
  557. if abs(_group[_index_j]["page_time_stamp"]-_group[_index_i]["page_time_stamp"])>86400*120:
  558. break
  559. _count += 1
  560. _docid1 = _group[_index_i]["docid"]
  561. _docid2 = _group[_index_j]["docid"]
  562. if _docid1<_docid2:
  563. self.forward(_docid1,_docid2,1,_len,_type)
  564. else:
  565. self.forward(_docid2,_docid1,1,_len,_type)
  566. except Exception as e:
  567. logging(str(e))
  568. @annotate('bigint,bigint,string->string')
  569. class f_merge_groupPairs(BaseUDAF):
  570. '''
  571. 合并组为一条记录
  572. '''
  573. def __init__(self):
  574. import json
  575. global json
  576. def new_buffer(self):
  577. return [[]]
  578. def iterate(self, buffer,is_exists,counts,_type):
  579. buffer[0].append({"is_exists":is_exists,"counts":counts,"_type":_type})
  580. def merge(self, buffer, pbuffer):
  581. buffer[0].extend(pbuffer[0])
  582. def terminate(self, buffer):
  583. list_dict = buffer[0]
  584. list_dict = list_dict[:10000]
  585. return json.dumps(list_dict)
  586. @annotate("string -> bigint,bigint,bigint")
  587. class f_merge_getLabel(BaseUDTF):
  588. def __init__(self):
  589. import logging
  590. import json
  591. global logging,json
  592. def process(self,str_docids):
  593. if str_docids is not None:
  594. list_docids = [int(i) for i in str_docids.split(",")]
  595. list_docids.sort(key=lambda x:x)
  596. _len = min(100,len(list_docids))
  597. for index_i in range(_len):
  598. docid_less = list_docids[index_i]
  599. for index_j in range(index_i+1,_len):
  600. docid_greater = list_docids[index_j]
  601. self.forward(docid_less,docid_greater,1)
  602. def getSimilarityOfString(str1,str2):
  603. _set1 = set()
  604. _set2 = set()
  605. if str1 is not None:
  606. for i in range(1,len(str1)):
  607. _set1.add(str1[i-1:i+1])
  608. if str2 is not None:
  609. for i in range(1,len(str2)):
  610. _set2.add(str2[i-1:i+1])
  611. _len = max(1,min(len(_set1),len(_set2)))
  612. return len(_set1&_set2)/_len
  613. def check_columns(tenderee_less,tenderee_greater,
  614. agency_less,agency_greater,project_code_less,project_code_greater,project_name_less,project_name_greater,
  615. win_tenderer_less,win_tenderer_greater,win_bid_price_less,win_bid_price_greater,
  616. bidding_budget_less,bidding_budget_greater,doctitle_refine_less,doctitle_refine_greater):
  617. flag = True
  618. _set_tenderee = set()
  619. if tenderee_less is not None and tenderee_less!="":
  620. _set_tenderee.add(tenderee_less)
  621. if tenderee_greater is not None and tenderee_greater!="":
  622. _set_tenderee.add(tenderee_greater)
  623. if len(_set_tenderee)>1:
  624. return False
  625. code_sim = getSimilarityOfString(project_code_less,project_code_greater)
  626. if code_sim>0.6 and code_sim<1:
  627. return False
  628. #同批次不同编号
  629. if getLength(project_code_less)>0 and getLength(project_code_greater)>0:
  630. _split_code_less = project_code_less.split("-")
  631. _split_code_greater = project_code_greater.split("-")
  632. if len(_split_code_less)>1 and len(_split_code_greater)>1:
  633. if _split_code_less[0]==_split_code_greater[0] and project_code_less!=project_code_greater:
  634. return False
  635. _set_win_tenderer = set()
  636. if win_tenderer_less is not None and win_tenderer_less!="":
  637. _set_win_tenderer.add(win_tenderer_less)
  638. if win_tenderer_greater is not None and win_tenderer_greater!="":
  639. _set_win_tenderer.add(win_tenderer_greater)
  640. if len(_set_win_tenderer)>1:
  641. return False
  642. _set_win_bid_price = set()
  643. if win_bid_price_less is not None and win_bid_price_less!="":
  644. _set_win_bid_price.add(float(win_bid_price_less))
  645. if win_bid_price_greater is not None and win_bid_price_greater!="":
  646. _set_win_bid_price.add(float(win_bid_price_greater))
  647. if len(_set_win_bid_price)>1:
  648. return False
  649. _set_bidding_budget = set()
  650. if bidding_budget_less is not None and bidding_budget_less!="":
  651. _set_bidding_budget.add(float(bidding_budget_less))
  652. if bidding_budget_greater is not None and bidding_budget_greater!="":
  653. _set_bidding_budget.add(float(bidding_budget_greater))
  654. if len(_set_bidding_budget)>1:
  655. return False
  656. return True
  657. def getSimLevel(str1,str2):
  658. str1_null = False
  659. str2_null = False
  660. _v = 0
  661. if str1 is None or str1=="":
  662. str1_null = True
  663. if str2 is None or str2=="":
  664. str2_null = True
  665. if str1_null and str2_null:
  666. _v = 2
  667. elif str1_null and not str2_null:
  668. _v = 4
  669. elif not str1_null and str2_null:
  670. _v = 6
  671. elif not str1_null and not str2_null:
  672. if str1==str2:
  673. _v = 10
  674. else:
  675. _v = 0
  676. return _v
  677. import math
  678. def featurnCount(_count,max_count=100):
  679. return max(0,min(1,_count))*(1/math.sqrt(max(1,_count-1)))
  680. def getLength(_str):
  681. return len(_str if _str is not None else "")
  682. @annotate("string->bigint")
  683. class f_get_min_counts(object):
  684. def evaluate(self,json_context):
  685. _context = json.loads(json_context)
  686. min_counts = 100
  687. for item in _context:
  688. if item["counts"]<min_counts:
  689. min_counts = item["counts"]
  690. return min_counts
  691. @annotate("string,string,string,string,string,string,string,string,string,string,string,string,string,string,string,string,string->string,double")
  692. class f_merge_featureMatrix(BaseUDTF):
  693. def __init__(self):
  694. import logging
  695. import json
  696. global logging,json
  697. def process(self,json_context,tenderee_less,tenderee_greater,
  698. agency_less,agency_greater,project_code_less,project_code_greater,project_name_less,project_name_greater,
  699. win_tenderer_less,win_tenderer_greater,win_bid_price_less,win_bid_price_greater,
  700. bidding_budget_less,bidding_budget_greater,doctitle_refine_less,doctitle_refine_greater):
  701. if not check_columns(tenderee_less,tenderee_greater,
  702. agency_less,agency_greater,project_code_less,project_code_greater,project_name_less,project_name_greater,
  703. win_tenderer_less,win_tenderer_greater,win_bid_price_less,win_bid_price_greater,
  704. bidding_budget_less,bidding_budget_greater,doctitle_refine_less,doctitle_refine_greater):
  705. return
  706. _context = json.loads(json_context)
  707. min_counts = 100
  708. dict_context = {}
  709. for item in _context:
  710. if item["counts"]<min_counts:
  711. min_counts = item["counts"]
  712. dict_context[item["_type"]] = [item["is_exists"],item["counts"]]
  713. context_key = ["tenderee","agency","project_code","project_name","win_tenderer","win_bid_price","bidding_budget","doctitle_refine"]
  714. list_matrix = []
  715. for index_i in range(len(context_key)):
  716. for index_j in range(index_i+1,len(context_key)):
  717. _key = "%s&%s"%(context_key[index_i],context_key[index_j])
  718. _v = featurnCount(dict_context.get(_key,[0,0])[1])
  719. list_matrix.append(_v)
  720. context3_key = ["tenderee","agency","win_tenderer","win_bid_price","bidding_budget"]
  721. for index_i in range(len(context3_key)):
  722. for index_j in range(index_i+1,len(context3_key)):
  723. for index_k in range(index_j+1,len(context3_key)):
  724. _key = "%s&%s&%s"%(context3_key[index_i],context3_key[index_j],context3_key[index_k])
  725. _v = featurnCount(dict_context.get(_key,[0,0])[1])
  726. list_matrix.append(_v)
  727. list_matrix.append(getSimLevel(tenderee_less,tenderee_greater)/10)
  728. list_matrix.append(getSimLevel(agency_less,agency_greater)/10)
  729. list_matrix.append(getSimilarityOfString(project_code_less,project_code_greater))
  730. list_matrix.append(getSimilarityOfString(project_name_less,project_name_greater))
  731. list_matrix.append(getSimLevel(win_tenderer_less,win_tenderer_greater)/10)
  732. list_matrix.append(getSimLevel(win_bid_price_less,win_bid_price_greater)/10)
  733. list_matrix.append(getSimLevel(bidding_budget_less,bidding_budget_greater)/10)
  734. list_matrix.append(getSimilarityOfString(doctitle_refine_less,doctitle_refine_greater))
  735. # set_tenderer = set()
  736. # if tenderee_less is not None and tenderee_less!="":
  737. # set_tenderer.add(tenderee_less)
  738. # if tenderee_greater is not None and tenderee_greater!="":
  739. # set_tenderer.add(tenderee_greater)
  740. #
  741. # set_win_tenderer = set()
  742. # if win_tenderer_less is not None and win_tenderer_less!="":
  743. # set_win_tenderer.add(win_tenderer_less)
  744. # if win_tenderer_greater is not None and win_tenderer_greater!="":
  745. # set_win_tenderer.add(win_tenderer_greater)
  746. #
  747. # set_bidding_budget = set()
  748. # if bidding_budget_less is not None and bidding_budget_less!="":
  749. # set_bidding_budget.add(bidding_budget_less)
  750. # if bidding_budget_greater is not None and bidding_budget_greater!="":
  751. # set_bidding_budget.add(bidding_budget_greater)
  752. #
  753. # set_win_bid_price = set()
  754. # if win_bid_price_less is not None and win_bid_price_less!="":
  755. # set_win_bid_price.add(win_bid_price_less)
  756. # if win_bid_price_greater is not None and win_bid_price_greater!="":
  757. # set_win_bid_price.add(win_bid_price_greater)
  758. json_matrix = json.dumps(list_matrix)
  759. same_project_code = False
  760. if project_code_less==project_code_greater and getLength(project_code_less)>0:
  761. same_project_code = True
  762. same_project_name = False
  763. if project_name_less==project_name_greater and getLength(project_name_less)>0:
  764. same_project_name = True
  765. same_doctitle_refine = False
  766. if doctitle_refine_less==doctitle_refine_greater and getLength(doctitle_refine_less)>0:
  767. same_doctitle_refine = True
  768. same_tenderee = False
  769. if tenderee_less==tenderee_greater and getLength(tenderee_less)>0:
  770. same_tenderee = True
  771. same_agency = False
  772. if agency_less==agency_greater and getLength(agency_less)>0:
  773. same_agency = True
  774. same_bidding_budget = False
  775. if bidding_budget_less==bidding_budget_greater and getLength(bidding_budget_less)>0:
  776. same_bidding_budget = True
  777. same_win_tenderer = False
  778. if win_tenderer_less==win_tenderer_greater and getLength(win_tenderer_less)>0:
  779. same_win_tenderer = True
  780. same_win_bid_price = False
  781. if win_bid_price_less==win_bid_price_greater and getLength(win_bid_price_less)>0:
  782. same_win_bid_price = True
  783. contain_doctitle = False
  784. if getLength(doctitle_refine_less)>0 and getLength(doctitle_refine_greater)>0 and (doctitle_refine_less in doctitle_refine_greater or doctitle_refine_greater in doctitle_refine_less):
  785. contain_doctitle = True
  786. contain_project_name = False
  787. if getLength(project_name_less)>0 and getLength(project_name_greater)>0 and (project_name_less in project_name_greater or project_name_greater in project_name_less):
  788. contain_project_name = True
  789. total_money_less = 0 if getLength(bidding_budget_less)==0 else float(bidding_budget_less)+0 if getLength(win_bid_price_less)==0 else float(win_bid_price_less)
  790. total_money_greater = 0 if getLength(bidding_budget_greater)==0 else float(bidding_budget_greater) +0 if getLength(win_bid_price_greater)==0 else float(win_bid_price_greater)
  791. if min_counts<10:
  792. _prob = 0.9
  793. if same_project_code and same_win_tenderer and same_tenderee:
  794. self.forward(json_matrix,_prob)
  795. return
  796. if same_tenderee and same_project_name and same_win_tenderer:
  797. self.forward(json_matrix,_prob)
  798. return
  799. if same_tenderee and same_doctitle_refine and same_win_tenderer:
  800. self.forward(json_matrix,_prob)
  801. return
  802. if same_tenderee and same_win_bid_price and same_win_tenderer:
  803. self.forward(json_matrix,_prob)
  804. return
  805. if same_project_code and same_win_bid_price and same_win_tenderer:
  806. self.forward(json_matrix,_prob)
  807. return
  808. if same_project_name and same_win_bid_price and same_win_tenderer:
  809. self.forward(json_matrix,_prob)
  810. return
  811. if same_doctitle_refine and same_win_bid_price and same_win_tenderer:
  812. self.forward(json_matrix,_prob)
  813. return
  814. if same_doctitle_refine and same_bidding_budget and same_win_tenderer:
  815. self.forward(json_matrix,_prob)
  816. return
  817. if same_tenderee and same_doctitle_refine and same_win_tenderer:
  818. self.forward(json_matrix,_prob)
  819. return
  820. if same_tenderee and same_project_code and same_project_name:
  821. self.forward(json_matrix,_prob)
  822. return
  823. if same_tenderee and same_project_code and same_doctitle_refine:
  824. self.forward(json_matrix,_prob)
  825. return
  826. if same_tenderee and same_bidding_budget and same_project_code:
  827. self.forward(json_matrix,_prob)
  828. return
  829. if same_tenderee and same_bidding_budget and same_doctitle_refine:
  830. self.forward(json_matrix,_prob)
  831. return
  832. if same_tenderee and same_bidding_budget and same_project_name:
  833. self.forward(json_matrix,_prob)
  834. return
  835. if same_doctitle_refine and same_project_code and same_project_name:
  836. self.forward(json_matrix,_prob)
  837. return
  838. if min_counts<=5:
  839. _prob = 0.8
  840. if same_project_code and same_tenderee:
  841. self.forward(json_matrix,_prob)
  842. return
  843. if same_project_code and same_win_tenderer:
  844. self.forward(json_matrix,_prob)
  845. return
  846. if same_project_name and same_project_code:
  847. self.forward(json_matrix,_prob)
  848. return
  849. if same_project_code and same_doctitle_refine:
  850. self.forward(json_matrix,_prob)
  851. return
  852. if total_money_less==total_money_greater and total_money_less>100000:
  853. if same_win_tenderer and (same_win_bid_price or same_bidding_budget):
  854. self.forward(json_matrix,_prob)
  855. return
  856. if same_project_code and same_bidding_budget:
  857. self.forward(json_matrix,_prob)
  858. return
  859. if same_project_code and same_win_bid_price:
  860. self.forward(json_matrix,_prob)
  861. return
  862. if same_bidding_budget and same_win_bid_price and (contain_project_name or contain_doctitle):
  863. self.forward(json_matrix,_prob)
  864. return
  865. if min_counts<=3:
  866. _prob = 0.7
  867. if same_project_name or same_project_code or same_doctitle_refine or contain_doctitle or contain_project_name:
  868. self.forward(json_matrix,_prob)
  869. return
  870. self.forward(json_matrix,0)
  871. class MergePredictor():
  872. def __init__(self):
  873. self.input_size = 46
  874. self.output_size = 2
  875. self.matrix = np.array([[-5.817399024963379, 3.367797374725342], [-18.3098201751709, 17.649206161499023], [-7.115952014923096, 9.236002922058105], [-5.054129123687744, 1.8316771984100342], [6.391637325286865, -7.57396125793457], [-2.8721542358398438, 6.826520919799805], [-5.426159858703613, 10.235260009765625], [-4.240962982177734, -0.32092899084091187], [-0.6378090381622314, 0.4834124445915222], [-1.7574478387832642, -0.17846578359603882], [4.325063228607178, -2.345501661300659], [0.6086963415145874, 0.8325914740562439], [2.5674285888671875, 1.8432368040084839], [-11.195490837097168, 17.4630184173584], [-11.334247589111328, 10.294097900390625], [2.639320135116577, -8.072785377502441], [-2.2689898014068604, -3.6194612979888916], [-11.129570960998535, 18.907018661499023], [4.526485919952393, 4.57423210144043], [-3.170452356338501, -1.3847776651382446], [-0.03280467540025711, -3.0471489429473877], [-6.601675510406494, -10.05613899230957], [-2.9116673469543457, 4.819308280944824], [1.4398306608200073, -0.6549674272537231], [7.091512203216553, -0.142232745885849], [-0.14478975534439087, 0.06628061085939407], [-6.775437831878662, 9.279582023620605], [-0.006781991105526686, 1.6472798585891724], [3.83730149269104, 1.4072834253311157], [1.2229349613189697, -2.1653425693511963], [1.445560336112976, -0.8397432565689087], [-11.325132369995117, 11.231744766235352], [2.3229124546051025, -4.623719215393066], [0.38562265038490295, -1.2645516395568848], [-1.3670002222061157, 2.4323790073394775], [-3.6994268894195557, 0.7515658736228943], [-0.11617227643728256, -0.820703387260437], [4.089913368225098, -4.693605422973633], [-0.4959050714969635, 1.5272167921066284], [-2.7135870456695557, -0.5120691657066345], [0.573157548904419, -1.9375460147857666], [-4.262857437133789, 0.6375582814216614], [-1.8825865983963013, 2.427532911300659], [-4.565115451812744, 4.0269083976745605], [-4.339804649353027, 6.754288196563721], [-4.31907320022583, 0.28193211555480957]])
  876. self.bias = np.array([16.79706382751465, -13.713337898254395])
  877. # self.model = load_model("model/merge.h5",custom_objects={"precision":precision,"recall":recall,"f1_score":f1_score})
  878. def activation(self,vec,_type):
  879. if _type=="relu":
  880. _vec = np.array(vec)
  881. return _vec*(_vec>0)
  882. if _type=="tanh":
  883. return np.tanh(vec)
  884. if _type=="softmax":
  885. _vec = np.array(vec)
  886. _exp = np.exp(_vec)
  887. return _exp/np.sum(_exp)
  888. def predict(self,input):
  889. _out = self.activation(self.activation(np.matmul(np.array(input).reshape(-1,self.input_size),self.matrix)+self.bias,"tanh"),"softmax")
  890. # print(self.model.predict(np.array(input).reshape(-1,46)))
  891. return _out
  892. @annotate('string,double -> double')
  893. class f_getMergeProb(BaseUDTF):
  894. def __init__(self):
  895. import json
  896. include_package_path("numpy-1.18.zip")
  897. import numpy as np
  898. global json,np
  899. self.mp = MergePredictor()
  900. def process(self,json_matrix,pre_prob):
  901. if not pre_prob>0.5:
  902. _matrix = json.loads(json_matrix)
  903. _prob = self.mp.predict(_matrix)[0][1]
  904. else:
  905. _prob = pre_prob
  906. if _prob>0.5:
  907. self.forward(float(_prob))
  908. @annotate('string -> bigint,bigint')
  909. class f_check_remerge_channel(BaseUDTF):
  910. '''
  911. 将多个组拆解成多条记录
  912. '''
  913. def __init__(self):
  914. import logging
  915. import json
  916. global json,logging
  917. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  918. def process(self,json_remerge):
  919. if json_remerge is not None:
  920. list_group = json.loads(json_remerge)
  921. for _group in list_group:
  922. _keys = _group.get("data").keys()
  923. if len(_keys)>0:
  924. main_docid = int(list(_keys)[0])
  925. for k,v in _group.get("data",{}).items():
  926. self.forward(main_docid,int(k))
  927. for _v in v:
  928. self.forward(main_docid,int(_v))
  929. @annotate('string -> bigint,bigint')
  930. class f_check_remerge(BaseUDTF):
  931. '''
  932. 将多个组拆解成多条记录
  933. '''
  934. def __init__(self):
  935. import logging
  936. import json
  937. global json,logging
  938. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  939. def process(self,json_remerge):
  940. if json_remerge is not None:
  941. list_group = json.loads(json_remerge)
  942. for _group in list_group:
  943. for _docid in _group:
  944. self.forward(_group[-1],_docid)
  945. def getConfidence(rule_id):
  946. if rule_id >=1 and rule_id <=20:
  947. return 30
  948. elif rule_id>=31 and rule_id<=50:
  949. return 20
  950. else:
  951. return 10
  952. @annotate('string,bigint -> bigint,bigint,bigint')
  953. class f_arrange_group_single(BaseUDTF):
  954. '''
  955. 将多个组拆解成多条记录
  956. '''
  957. def __init__(self):
  958. import logging
  959. import json
  960. global json,logging
  961. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  962. def process(self,json_set_docid,rule_id):
  963. if json_set_docid is not None:
  964. list_group = json.loads(json_set_docid)
  965. for _group in list_group:
  966. for index_i in range(len(_group)):
  967. for index_j in range(len(_group)):
  968. # if index_i!=index_j and _group[index_i]!=_group[index_j]:
  969. if index_i!=index_j:
  970. self.forward(_group[index_i],_group[index_j],getConfidence(rule_id))
  971. @annotate('bigint,bigint->string')
  972. class f_get_merge_docids(BaseUDAF):
  973. '''
  974. 合并组为一条记录
  975. '''
  976. def __init__(self):
  977. import json
  978. global json
  979. def new_buffer(self):
  980. return [set()]
  981. def iterate(self, buffer,docid1,docid2):
  982. buffer[0].add(docid1)
  983. buffer[0].add(docid2)
  984. def merge(self, buffer, pbuffer):
  985. buffer[0] |= pbuffer[0]
  986. def terminate(self, buffer):
  987. set_docid = buffer[0]
  988. list_docid = list(set_docid)
  989. list_docid.sort(key=lambda x:x)
  990. list_docid_str = []
  991. for _docid in list_docid:
  992. list_docid_str.append(str(_docid))
  993. return ",".join(list_docid_str)