documentMerge.py 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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,bigint->string')
  318. class f_remege_limit_num_contain_bychannel(BaseUDAF):
  319. '''f_remege_limit_num_contain_bychannel
  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,extract_count):
  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,"extract_count":extract_count}
  333. buffer[0].append(_dict)
  334. def merge(self, buffer, pbuffer):
  335. buffer[0].extend(pbuffer[0])
  336. def getNotLikeSet(self,_dict,column_name):
  337. column_value = _dict.get(column_name,None)
  338. _set = set()
  339. if column_value is not None:
  340. for _i in range(1,len(column_value)):
  341. _set.add(column_value[_i-1:_i+1])
  342. _dict["notLike_set"] = _set
  343. def getSimilarity(self,_set1,_set2):
  344. _sum = max([1,min([len(_set1),len(_set2)])])
  345. return len(_set1&_set2)/_sum
  346. def terminate(self, buffer):
  347. list_group = []
  348. the_group = buffer[0]
  349. SIM_PROB = 0.6
  350. for _d in the_group:
  351. self.getNotLikeSet(_d,"notLike_column")
  352. #判断多个值与否
  353. keys = ["set_limit_column1","set_limit_column2","set_limit_column3","set_limit_column4"]
  354. re_merge = False
  355. for _key in keys:
  356. if len(getSet(the_group,_key))>1:
  357. re_merge = True
  358. break
  359. #判断是否相似而不相同
  360. re_merge_sim = False
  361. for _i1 in range(0,len(the_group)):
  362. for _j1 in range(_i1+1,len(the_group)):
  363. _set1 = the_group[_i1]["notLike_set"]
  364. _set2 = the_group[_j1]["notLike_set"]
  365. _sim = self.getSimilarity(_set1,_set2)
  366. if _sim>SIM_PROB and _sim<1:
  367. re_merge_sim = True
  368. break
  369. contain_keys = ["contain_column1","contain_column2"]
  370. logging.info(the_group)
  371. logging.info(str(re_merge)+str(re_merge_sim))
  372. #重新成组
  373. dict_docid_doc = {}
  374. for _doc in the_group:
  375. dict_docid_doc[_doc["docid"]] = _doc
  376. if re_merge or re_merge_sim:
  377. the_group.sort(key=lambda x:x["confidence"],reverse=True)
  378. the_group.sort(key=lambda x:x["page_time_stamp"])
  379. for _doc in the_group:
  380. merge_flag = False
  381. for _index in range(len(list_group)):
  382. _g = list_group[_index]
  383. hit_count = 0
  384. dict_temp = dict()
  385. #多个值的异常
  386. if re_merge:
  387. for _c_key in contain_keys:
  388. dict_temp[_c_key] = _g[_c_key]
  389. if _g[_c_key] is not None and _doc[_c_key] is not None:
  390. if len(_g[_c_key])>len(_doc[_c_key]):
  391. if str(_g[_c_key]).find(str(_doc[_c_key]))>=0:
  392. dict_temp[_c_key] = _g[_c_key]
  393. hit_count += 1
  394. else:
  395. if str(_doc[_c_key]).find(str(_g[_c_key]))>=0:
  396. dict_temp[_c_key] = _doc[_c_key]
  397. _g[_c_key] = _doc[_c_key]
  398. hit_count += 1
  399. else:
  400. hit_count = 1
  401. # if hit_count==len(contain_keys):
  402. if hit_count>0:
  403. _flag_sim = False
  404. #相似而不相同的异常
  405. if re_merge_sim:
  406. for _docid in _g["docid"]:
  407. tmp_d = dict_docid_doc[_docid]
  408. _sim = self.getSimilarity(tmp_d["notLike_set"],_doc["notLike_set"])
  409. if _sim>SIM_PROB and _sim<1:
  410. _flag_sim = True
  411. if not _flag_sim:
  412. for _c_key in dict_temp.keys():
  413. _g[_c_key] = dict_temp[_c_key]
  414. _g["docid"].append(_doc["docid"])
  415. merge_flag = True
  416. break
  417. if not merge_flag:
  418. _dict = dict()
  419. _dict["docid"] = [_doc["docid"]]
  420. for _c_key in contain_keys:
  421. _dict[_c_key] = _doc[_c_key]
  422. list_group.append(_dict)
  423. final_group = []
  424. #判断是否符合一个值
  425. for _group in list_group:
  426. _split = []
  427. for _docid in _group["docid"]:
  428. _split.append(dict_docid_doc[_docid])
  429. #通过置信度排序,尽可能保留组
  430. _split.sort(key=lambda x:x["confidence"],reverse=True)
  431. #置信度
  432. list_key_index = []
  433. for _k in keys:
  434. list_key_index.append(getDiffIndex(_split,_k))
  435. _index = min(list_key_index)
  436. final_group.append([_c["docid"] for _c in _split[:_index]])
  437. for _c in _split[_index:]:
  438. final_group.append([_c["docid"]])
  439. #若是找到两个以上,则全部单独成组,否则成一组
  440. # _flag = True
  441. # for _key in keys:
  442. # if len(getSet(_split,_key))>1:
  443. # _flag = False
  444. # break
  445. # if not _flag:
  446. # for _docid in _group["docid"]:
  447. # final_group.append([_docid])
  448. # else:
  449. # final_group.append(list(set(_group["docid"])))
  450. else:
  451. final_group = [list(set([item["docid"] for item in the_group]))]
  452. log(str(final_group))
  453. #每个channel选择一篇公告
  454. final_group_channel = []
  455. for _group in final_group:
  456. dict_channel_id = {}
  457. otherChannel = 10000
  458. for _docid in _group:
  459. _channel = dict_docid_doc[_docid].get("docchannel")
  460. if _channel in [114,115,116,117]:
  461. otherChannel += 1
  462. _channel = otherChannel
  463. if _channel not in dict_channel_id:
  464. dict_channel_id[_channel] = []
  465. dict_channel_id[_channel].append([_docid,dict_docid_doc[_docid].get("page_time_stamp"),dict_docid_doc[_docid].get("extract_count")])
  466. channel_dict = {}
  467. for k,v in dict_channel_id.items():
  468. v.sort(key=lambda x:x[1])
  469. v.sort(key=lambda x:x[2],reverse=True)
  470. channel_dict[v[0][0]] = []
  471. for _docs in v[1:]:
  472. channel_dict[v[0][0]].append(_docs[0])
  473. _d = {"data":channel_dict,"process_time":getCurrent_date()}
  474. final_group_channel.append(_d)
  475. return json.dumps(final_group_channel)
  476. @annotate('string -> string')
  477. class f_get_remerge_group_channel(BaseUDTF):
  478. '''
  479. 将多个组拆解成多条记录
  480. '''
  481. def __init__(self):
  482. import logging
  483. import json
  484. global json,logging
  485. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  486. def process(self,json_remerge):
  487. if json_remerge is not None:
  488. list_group = json.loads(json_remerge)
  489. for _group in list_group:
  490. self.forward(json.dumps(_group))
  491. @annotate('string -> string')
  492. class f_get_remerge_group(BaseUDTF):
  493. '''
  494. 将多个组拆解成多条记录
  495. '''
  496. def __init__(self):
  497. import logging
  498. import json
  499. global json,logging
  500. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  501. def process(self,json_remerge):
  502. if json_remerge is not None:
  503. list_group = json.loads(json_remerge)
  504. for _group in list_group:
  505. l_g = list(set(_group))
  506. l_g.sort(key=lambda x:x)
  507. list_docid = [str(_docid) for _docid in l_g]
  508. self.forward(",".join(list_docid))
  509. @annotate('bigint,bigint,string->string')
  510. class f_merge_probability(BaseUDAF):
  511. '''
  512. 合并组为一条记录
  513. '''
  514. def __init__(self):
  515. import json
  516. global json
  517. def new_buffer(self):
  518. return [[]]
  519. def iterate(self, buffer,docid,page_time_stamp,_type):
  520. buffer[0].append({"docid":docid,"page_time_stamp":page_time_stamp,"type":_type})
  521. def merge(self, buffer, pbuffer):
  522. buffer[0].extend(pbuffer[0])
  523. def terminate(self, buffer):
  524. list_dict = buffer[0]
  525. list_dict = list_dict[:10000]
  526. list_group = split_with_time(list_dict,sort_key="page_time_stamp",timedelta=86400*120)
  527. return json.dumps(list_group)
  528. @annotate('string -> bigint,bigint,bigint,bigint,string')
  529. class f_split_merge_probability(BaseUDTF):
  530. def __init__(self):
  531. import logging
  532. import json
  533. global logging,json
  534. logging.basicConfig(level=logging.INFO,format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  535. def process(self,list_group_str):
  536. logging.info("0")
  537. logging.info(list_group_str)
  538. if list_group_str is not None:
  539. logging.info("1")
  540. try:
  541. list_group = json.loads(list_group_str)
  542. logging.info("2")
  543. for _group in list_group:
  544. if len(_group)>0:
  545. _type = _group[0].get("type","")
  546. logging.info("3%d"%len(list_group))
  547. # _group.sort(key=lambda x:x["page_time_stamp"])
  548. _len = min(100,len(_group))
  549. for _index_i in range(_len):
  550. _count = 0
  551. for _index_j in range(_index_i+1,_len):
  552. if abs(_group[_index_j]["page_time_stamp"]-_group[_index_i]["page_time_stamp"])>86400*120:
  553. break
  554. _count += 1
  555. _docid1 = _group[_index_i]["docid"]
  556. _docid2 = _group[_index_j]["docid"]
  557. if _docid1<_docid2:
  558. self.forward(_docid1,_docid2,1,_len,_type)
  559. else:
  560. self.forward(_docid2,_docid1,1,_len,_type)
  561. except Exception as e:
  562. logging(str(e))
  563. @annotate('bigint,bigint,string->string')
  564. class f_merge_groupPairs(BaseUDAF):
  565. '''
  566. 合并组为一条记录
  567. '''
  568. def __init__(self):
  569. import json
  570. global json
  571. def new_buffer(self):
  572. return [[]]
  573. def iterate(self, buffer,is_exists,counts,_type):
  574. buffer[0].append({"is_exists":is_exists,"counts":counts,"_type":_type})
  575. def merge(self, buffer, pbuffer):
  576. buffer[0].extend(pbuffer[0])
  577. def terminate(self, buffer):
  578. list_dict = buffer[0]
  579. list_dict = list_dict[:10000]
  580. return json.dumps(list_dict)
  581. @annotate("string -> bigint,bigint,bigint")
  582. class f_merge_getLabel(BaseUDTF):
  583. def __init__(self):
  584. import logging
  585. import json
  586. global logging,json
  587. def process(self,str_docids):
  588. if str_docids is not None:
  589. list_docids = [int(i) for i in str_docids.split(",")]
  590. list_docids.sort(key=lambda x:x)
  591. _len = min(100,len(list_docids))
  592. for index_i in range(_len):
  593. docid_less = list_docids[index_i]
  594. for index_j in range(index_i+1,_len):
  595. docid_greater = list_docids[index_j]
  596. self.forward(docid_less,docid_greater,1)
  597. def getSimilarityOfString(str1,str2):
  598. _set1 = set()
  599. _set2 = set()
  600. if str1 is not None:
  601. for i in range(1,len(str1)):
  602. _set1.add(str1[i-1:i+1])
  603. if str2 is not None:
  604. for i in range(1,len(str2)):
  605. _set2.add(str2[i-1:i+1])
  606. _len = max(1,min(len(_set1),len(_set2)))
  607. return len(_set1&_set2)/_len
  608. def check_columns(tenderee_less,tenderee_greater,
  609. agency_less,agency_greater,project_code_less,project_code_greater,project_name_less,project_name_greater,
  610. win_tenderer_less,win_tenderer_greater,win_bid_price_less,win_bid_price_greater,
  611. bidding_budget_less,bidding_budget_greater,doctitle_refine_less,doctitle_refine_greater):
  612. flag = True
  613. _set_tenderee = set()
  614. if tenderee_less is not None and tenderee_less!="":
  615. _set_tenderee.add(tenderee_less)
  616. if tenderee_greater is not None and tenderee_greater!="":
  617. _set_tenderee.add(tenderee_greater)
  618. if len(_set_tenderee)>1:
  619. return False
  620. code_sim = getSimilarityOfString(project_code_less,project_code_greater)
  621. if code_sim>0.6 and code_sim<1:
  622. return False
  623. #同批次不同编号
  624. if getLength(project_code_less)>0 and getLength(project_code_greater)>0:
  625. _split_code_less = project_code_less.split("-")
  626. _split_code_greater = project_code_greater.split("-")
  627. if len(_split_code_less)>1 and len(_split_code_greater)>1:
  628. if _split_code_less[0]==_split_code_greater[0] and project_code_less!=project_code_greater:
  629. return False
  630. _set_win_tenderer = set()
  631. if win_tenderer_less is not None and win_tenderer_less!="":
  632. _set_win_tenderer.add(win_tenderer_less)
  633. if win_tenderer_greater is not None and win_tenderer_greater!="":
  634. _set_win_tenderer.add(win_tenderer_greater)
  635. if len(_set_win_tenderer)>1:
  636. return False
  637. _set_win_bid_price = set()
  638. if win_bid_price_less is not None and win_bid_price_less!="":
  639. _set_win_bid_price.add(float(win_bid_price_less))
  640. if win_bid_price_greater is not None and win_bid_price_greater!="":
  641. _set_win_bid_price.add(float(win_bid_price_greater))
  642. if len(_set_win_bid_price)>1:
  643. return False
  644. _set_bidding_budget = set()
  645. if bidding_budget_less is not None and bidding_budget_less!="":
  646. _set_bidding_budget.add(float(bidding_budget_less))
  647. if bidding_budget_greater is not None and bidding_budget_greater!="":
  648. _set_bidding_budget.add(float(bidding_budget_greater))
  649. if len(_set_bidding_budget)>1:
  650. return False
  651. return True
  652. def getSimLevel(str1,str2):
  653. str1_null = False
  654. str2_null = False
  655. _v = 0
  656. if str1 is None or str1=="":
  657. str1_null = True
  658. if str2 is None or str2=="":
  659. str2_null = True
  660. if str1_null and str2_null:
  661. _v = 2
  662. elif str1_null and not str2_null:
  663. _v = 4
  664. elif not str1_null and str2_null:
  665. _v = 6
  666. elif not str1_null and not str2_null:
  667. if str1==str2:
  668. _v = 10
  669. else:
  670. _v = 0
  671. return _v
  672. import math
  673. def featurnCount(_count,max_count=100):
  674. return max(0,min(1,_count))*(1/math.sqrt(max(1,_count-1)))
  675. def getLength(_str):
  676. return len(_str if _str is not None else "")
  677. @annotate("string->bigint")
  678. class f_get_min_counts(object):
  679. def evaluate(self,json_context):
  680. _context = json.loads(json_context)
  681. min_counts = 100
  682. for item in _context:
  683. if item["counts"]<min_counts:
  684. min_counts = item["counts"]
  685. return min_counts
  686. @annotate("string,string,string,string,string,string,string,string,string,string,string,string,string,string,string,string,string->string,double")
  687. class f_merge_featureMatrix(BaseUDTF):
  688. def __init__(self):
  689. import logging
  690. import json
  691. global logging,json
  692. def process(self,json_context,tenderee_less,tenderee_greater,
  693. agency_less,agency_greater,project_code_less,project_code_greater,project_name_less,project_name_greater,
  694. win_tenderer_less,win_tenderer_greater,win_bid_price_less,win_bid_price_greater,
  695. bidding_budget_less,bidding_budget_greater,doctitle_refine_less,doctitle_refine_greater):
  696. if not check_columns(tenderee_less,tenderee_greater,
  697. agency_less,agency_greater,project_code_less,project_code_greater,project_name_less,project_name_greater,
  698. win_tenderer_less,win_tenderer_greater,win_bid_price_less,win_bid_price_greater,
  699. bidding_budget_less,bidding_budget_greater,doctitle_refine_less,doctitle_refine_greater):
  700. return
  701. _context = json.loads(json_context)
  702. min_counts = 100
  703. dict_context = {}
  704. for item in _context:
  705. if item["counts"]<min_counts:
  706. min_counts = item["counts"]
  707. dict_context[item["_type"]] = [item["is_exists"],item["counts"]]
  708. context_key = ["tenderee","agency","project_code","project_name","win_tenderer","win_bid_price","bidding_budget","doctitle_refine"]
  709. list_matrix = []
  710. for index_i in range(len(context_key)):
  711. for index_j in range(index_i+1,len(context_key)):
  712. _key = "%s&%s"%(context_key[index_i],context_key[index_j])
  713. _v = featurnCount(dict_context.get(_key,[0,0])[1])
  714. list_matrix.append(_v)
  715. context3_key = ["tenderee","agency","win_tenderer","win_bid_price","bidding_budget"]
  716. for index_i in range(len(context3_key)):
  717. for index_j in range(index_i+1,len(context3_key)):
  718. for index_k in range(index_j+1,len(context3_key)):
  719. _key = "%s&%s&%s"%(context3_key[index_i],context3_key[index_j],context3_key[index_k])
  720. _v = featurnCount(dict_context.get(_key,[0,0])[1])
  721. list_matrix.append(_v)
  722. list_matrix.append(getSimLevel(tenderee_less,tenderee_greater)/10)
  723. list_matrix.append(getSimLevel(agency_less,agency_greater)/10)
  724. list_matrix.append(getSimilarityOfString(project_code_less,project_code_greater))
  725. list_matrix.append(getSimilarityOfString(project_name_less,project_name_greater))
  726. list_matrix.append(getSimLevel(win_tenderer_less,win_tenderer_greater)/10)
  727. list_matrix.append(getSimLevel(win_bid_price_less,win_bid_price_greater)/10)
  728. list_matrix.append(getSimLevel(bidding_budget_less,bidding_budget_greater)/10)
  729. list_matrix.append(getSimilarityOfString(doctitle_refine_less,doctitle_refine_greater))
  730. # set_tenderer = set()
  731. # if tenderee_less is not None and tenderee_less!="":
  732. # set_tenderer.add(tenderee_less)
  733. # if tenderee_greater is not None and tenderee_greater!="":
  734. # set_tenderer.add(tenderee_greater)
  735. #
  736. # set_win_tenderer = set()
  737. # if win_tenderer_less is not None and win_tenderer_less!="":
  738. # set_win_tenderer.add(win_tenderer_less)
  739. # if win_tenderer_greater is not None and win_tenderer_greater!="":
  740. # set_win_tenderer.add(win_tenderer_greater)
  741. #
  742. # set_bidding_budget = set()
  743. # if bidding_budget_less is not None and bidding_budget_less!="":
  744. # set_bidding_budget.add(bidding_budget_less)
  745. # if bidding_budget_greater is not None and bidding_budget_greater!="":
  746. # set_bidding_budget.add(bidding_budget_greater)
  747. #
  748. # set_win_bid_price = set()
  749. # if win_bid_price_less is not None and win_bid_price_less!="":
  750. # set_win_bid_price.add(win_bid_price_less)
  751. # if win_bid_price_greater is not None and win_bid_price_greater!="":
  752. # set_win_bid_price.add(win_bid_price_greater)
  753. json_matrix = json.dumps(list_matrix)
  754. same_project_code = False
  755. if project_code_less==project_code_greater and getLength(project_code_less)>0:
  756. same_project_code = True
  757. same_project_name = False
  758. if project_name_less==project_name_greater and getLength(project_name_less)>0:
  759. same_project_name = True
  760. same_doctitle_refine = False
  761. if doctitle_refine_less==doctitle_refine_greater and getLength(doctitle_refine_less)>0:
  762. same_doctitle_refine = True
  763. same_tenderee = False
  764. if tenderee_less==tenderee_greater and getLength(tenderee_less)>0:
  765. same_tenderee = True
  766. same_agency = False
  767. if agency_less==agency_greater and getLength(agency_less)>0:
  768. same_agency = True
  769. same_bidding_budget = False
  770. if bidding_budget_less==bidding_budget_greater and getLength(bidding_budget_less)>0:
  771. same_bidding_budget = True
  772. same_win_tenderer = False
  773. if win_tenderer_less==win_tenderer_greater and getLength(win_tenderer_less)>0:
  774. same_win_tenderer = True
  775. same_win_bid_price = False
  776. if win_bid_price_less==win_bid_price_greater and getLength(win_bid_price_less)>0:
  777. same_win_bid_price = True
  778. contain_doctitle = False
  779. 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):
  780. contain_doctitle = True
  781. contain_project_name = False
  782. 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):
  783. contain_project_name = True
  784. 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)
  785. 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)
  786. if min_counts<10:
  787. _prob = 0.9
  788. if same_project_code and same_win_tenderer and same_tenderee:
  789. self.forward(json_matrix,_prob)
  790. return
  791. if same_tenderee and same_project_name and same_win_tenderer:
  792. self.forward(json_matrix,_prob)
  793. return
  794. if same_tenderee and same_doctitle_refine and same_win_tenderer:
  795. self.forward(json_matrix,_prob)
  796. return
  797. if same_tenderee and same_win_bid_price and same_win_tenderer:
  798. self.forward(json_matrix,_prob)
  799. return
  800. if same_project_code and same_win_bid_price and same_win_tenderer:
  801. self.forward(json_matrix,_prob)
  802. return
  803. if same_project_name and same_win_bid_price and same_win_tenderer:
  804. self.forward(json_matrix,_prob)
  805. return
  806. if same_doctitle_refine and same_win_bid_price and same_win_tenderer:
  807. self.forward(json_matrix,_prob)
  808. return
  809. if same_doctitle_refine and same_bidding_budget and same_win_tenderer:
  810. self.forward(json_matrix,_prob)
  811. return
  812. if same_tenderee and same_doctitle_refine and same_win_tenderer:
  813. self.forward(json_matrix,_prob)
  814. return
  815. if same_tenderee and same_project_code and same_project_name:
  816. self.forward(json_matrix,_prob)
  817. return
  818. if same_tenderee and same_project_code and same_doctitle_refine:
  819. self.forward(json_matrix,_prob)
  820. return
  821. if same_tenderee and same_bidding_budget and same_project_code:
  822. self.forward(json_matrix,_prob)
  823. return
  824. if same_tenderee and same_bidding_budget and same_doctitle_refine:
  825. self.forward(json_matrix,_prob)
  826. return
  827. if same_tenderee and same_bidding_budget and same_project_name:
  828. self.forward(json_matrix,_prob)
  829. return
  830. if same_doctitle_refine and same_project_code and same_project_name:
  831. self.forward(json_matrix,_prob)
  832. return
  833. if min_counts<=5:
  834. _prob = 0.8
  835. if same_project_code and same_tenderee:
  836. self.forward(json_matrix,_prob)
  837. return
  838. if same_project_code and same_win_tenderer:
  839. self.forward(json_matrix,_prob)
  840. return
  841. if same_project_name and same_project_code:
  842. self.forward(json_matrix,_prob)
  843. return
  844. if same_project_code and same_doctitle_refine:
  845. self.forward(json_matrix,_prob)
  846. return
  847. if total_money_less==total_money_greater and total_money_less>100000:
  848. if same_win_tenderer and (same_win_bid_price or same_bidding_budget):
  849. self.forward(json_matrix,_prob)
  850. return
  851. if same_project_code and same_bidding_budget:
  852. self.forward(json_matrix,_prob)
  853. return
  854. if same_project_code and same_win_bid_price:
  855. self.forward(json_matrix,_prob)
  856. return
  857. if same_bidding_budget and same_win_bid_price and (contain_project_name or contain_doctitle):
  858. self.forward(json_matrix,_prob)
  859. return
  860. if min_counts<=3:
  861. _prob = 0.7
  862. if same_project_name or same_project_code or same_doctitle_refine or contain_doctitle or contain_project_name:
  863. self.forward(json_matrix,_prob)
  864. return
  865. self.forward(json_matrix,0)
  866. class MergePredictor():
  867. def __init__(self):
  868. self.input_size = 46
  869. self.output_size = 2
  870. 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]])
  871. self.bias = np.array([16.79706382751465, -13.713337898254395])
  872. # self.model = load_model("model/merge.h5",custom_objects={"precision":precision,"recall":recall,"f1_score":f1_score})
  873. def activation(self,vec,_type):
  874. if _type=="relu":
  875. _vec = np.array(vec)
  876. return _vec*(_vec>0)
  877. if _type=="tanh":
  878. return np.tanh(vec)
  879. if _type=="softmax":
  880. _vec = np.array(vec)
  881. _exp = np.exp(_vec)
  882. return _exp/np.sum(_exp)
  883. def predict(self,input):
  884. _out = self.activation(self.activation(np.matmul(np.array(input).reshape(-1,self.input_size),self.matrix)+self.bias,"tanh"),"softmax")
  885. # print(self.model.predict(np.array(input).reshape(-1,46)))
  886. return _out
  887. @annotate('string,double -> double')
  888. class f_getMergeProb(BaseUDTF):
  889. def __init__(self):
  890. import json
  891. include_package_path("numpy-1.18.zip")
  892. import numpy as np
  893. global json,np
  894. self.mp = MergePredictor()
  895. def process(self,json_matrix,pre_prob):
  896. if not pre_prob>0.5:
  897. _matrix = json.loads(json_matrix)
  898. _prob = self.mp.predict(_matrix)[0][1]
  899. else:
  900. _prob = pre_prob
  901. if _prob>0.5:
  902. self.forward(float(_prob))
  903. @annotate('string -> bigint,bigint')
  904. class f_check_remerge_channel(BaseUDTF):
  905. '''
  906. 将多个组拆解成多条记录
  907. '''
  908. def __init__(self):
  909. import logging
  910. import json
  911. global json,logging
  912. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  913. def process(self,json_remerge):
  914. if json_remerge is not None:
  915. list_group = json.loads(json_remerge)
  916. for _group in list_group:
  917. _keys = _group.get("data").keys()
  918. if len(_keys)>0:
  919. main_docid = int(list(_keys)[0])
  920. for k,v in _group.get("data",{}).items():
  921. self.forward(main_docid,int(k))
  922. for _v in v:
  923. self.forward(main_docid,int(_v))
  924. @annotate('string -> bigint,bigint')
  925. class f_check_remerge(BaseUDTF):
  926. '''
  927. 将多个组拆解成多条记录
  928. '''
  929. def __init__(self):
  930. import logging
  931. import json
  932. global json,logging
  933. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  934. def process(self,json_remerge):
  935. if json_remerge is not None:
  936. list_group = json.loads(json_remerge)
  937. for _group in list_group:
  938. for _docid in _group:
  939. self.forward(_group[-1],_docid)
  940. def getConfidence(rule_id):
  941. if rule_id >=1 and rule_id <=20:
  942. return 30
  943. elif rule_id>=31 and rule_id<=50:
  944. return 20
  945. else:
  946. return 10
  947. @annotate('string,bigint -> bigint,bigint,bigint')
  948. class f_arrange_group_single(BaseUDTF):
  949. '''
  950. 将多个组拆解成多条记录
  951. '''
  952. def __init__(self):
  953. import logging
  954. import json
  955. global json,logging
  956. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  957. def process(self,json_set_docid,rule_id):
  958. if json_set_docid is not None:
  959. list_group = json.loads(json_set_docid)
  960. for _group in list_group:
  961. for index_i in range(len(_group)):
  962. for index_j in range(len(_group)):
  963. # if index_i!=index_j and _group[index_i]!=_group[index_j]:
  964. if index_i!=index_j:
  965. self.forward(_group[index_i],_group[index_j],getConfidence(rule_id))
  966. @annotate('bigint,bigint->string')
  967. class f_get_merge_docids(BaseUDAF):
  968. '''
  969. 合并组为一条记录
  970. '''
  971. def __init__(self):
  972. import json
  973. global json
  974. def new_buffer(self):
  975. return [set()]
  976. def iterate(self, buffer,docid1,docid2):
  977. buffer[0].add(docid1)
  978. buffer[0].add(docid2)
  979. def merge(self, buffer, pbuffer):
  980. buffer[0] |= pbuffer[0]
  981. def terminate(self, buffer):
  982. set_docid = buffer[0]
  983. list_docid = list(set_docid)
  984. list_docid.sort(key=lambda x:x)
  985. list_docid_str = []
  986. for _docid in list_docid:
  987. list_docid_str.append(str(_docid))
  988. return ",".join(list_docid_str)