documentDumplicate.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. #coding:UTF8
  2. from odps.udf import annotate
  3. from odps.udf import BaseUDTF
  4. from odps.udf import BaseUDAF
  5. @annotate('string,string -> string,bigint,bigint,string,string,string,string,string,string,string,string,string,string,string,string,string,string,bigint')
  6. class f_decode_extract(BaseUDTF):
  7. def __init__(self):
  8. import logging
  9. import json
  10. import time,re
  11. global json,logging,time,re
  12. self.time_pattern = "\d{4}\-\d{2}\-\d{2}.*"
  13. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  14. def process(self, extractjson,otherjson):
  15. if extractjson is not None:
  16. _extract = json.loads(extractjson)
  17. else:
  18. _extract = {}
  19. if otherjson is not None:
  20. _other = json.loads(otherjson)
  21. else:
  22. _other = {}
  23. project_code = ""
  24. project_name = ""
  25. tenderee = ""
  26. agency = ""
  27. win_tenderer = ""
  28. bidding_budget = ""
  29. win_bid_price = ""
  30. page_time_stamp = 0
  31. docchannel = 0
  32. extract_count = 0
  33. page_time = _other.get("pageTime",time.strftime('%Y-%m-%d',time.localtime()))
  34. doctitle = _other.get("doctitle","")
  35. doctitle_refine = re.sub(r'工程|服务|询价|比价|谈判|竞争性|磋商|结果|中标|招标|采购|的|公示|公开|成交|公告|评标|候选人|交易|通知|废标|流标|终止|中止|一笔|预告|单一来源|询价|竞价', '', doctitle)
  36. area = _other.get("area","")
  37. province = _other.get("province","")
  38. city = _other.get("city","")
  39. district = _other.get("district","")
  40. web_source_no = _other.get("webSourceNo","")
  41. docchannel = _other.get("docchannel",0)
  42. if re.search(self.time_pattern,page_time) is not None:
  43. timeArray = time.strptime(page_time[:11], "%Y-%m-%d")
  44. page_time_stamp = int(time.mktime(timeArray))
  45. list_code = _extract.get("code",[])
  46. if len(list_code)>0:
  47. project_code = list_code[0]
  48. project_name = _extract.get("name","")
  49. dict_pack = _extract.get("prem",{})
  50. logging.info(dict_pack)
  51. for _key in dict_pack.keys():
  52. if dict_pack[_key]["tendereeMoney"]!='' and float(dict_pack[_key]["tendereeMoney"])>0:
  53. extract_count += 1
  54. if bidding_budget=="":
  55. bidding_budget = str(dict_pack[_key]["tendereeMoney"])
  56. for _role in dict_pack[_key]["roleList"]:
  57. extract_count += 1
  58. if _role[2]!='' and float(_role[2])>0:
  59. extract_count += 1
  60. if _role[0]=="tenderee":
  61. tenderee = _role[1]
  62. if _role[0]=="win_tenderer":
  63. if win_tenderer=="":
  64. win_tenderer = _role[1]
  65. if _role[2]!='' and float(_role[2])>0:
  66. if win_bid_price=="":
  67. win_bid_price = str(_role[2])
  68. if _role[0]=="agency":
  69. agency = _role[1]
  70. if project_code!="":
  71. extract_count += 1
  72. if project_name!="":
  73. extract_count += 1
  74. logging.info(page_time+doctitle+doctitle_refine+area+province+city+
  75. district+web_source_no+project_code+project_name+tenderee+agency+win_tenderer+bidding_budget+win_bid_price)
  76. self.forward(page_time,page_time_stamp,docchannel,doctitle,doctitle_refine,area,province,city,
  77. district,web_source_no,project_code,project_name,tenderee,agency,win_tenderer,bidding_budget,win_bid_price,extract_count)
  78. @annotate('string,string,string,string,string -> string,string,string,bigint')
  79. class f_decode_sub_docs_json(BaseUDTF):
  80. def __init__(self):
  81. import logging
  82. import json
  83. global json,logging
  84. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  85. def process(self, project_code,project_name,tenderee,agency,sub_docs_json):
  86. columns = {"win_tenderer":"","bidding_budget":"","win_bid_price":""}
  87. extract_count = 0
  88. if project_code!="":
  89. extract_count += 1
  90. if project_name!="":
  91. extract_count += 1
  92. if tenderee!="":
  93. extract_count += 1
  94. if agency!="":
  95. extract_count += 1
  96. if sub_docs_json is not None:
  97. for sub_docs in json.loads(sub_docs_json):
  98. for _key_sub_docs in sub_docs.keys():
  99. extract_count += 1
  100. if _key_sub_docs in columns:
  101. if columns[_key_sub_docs]=="" and str(sub_docs[_key_sub_docs]) not in ["","0"]:
  102. if _key_sub_docs in ["bidding_budget","win_bid_price"]:
  103. if float(sub_docs[_key_sub_docs])>0:
  104. columns[_key_sub_docs] = str(sub_docs[_key_sub_docs])
  105. else:
  106. columns[_key_sub_docs] = str(sub_docs[_key_sub_docs])
  107. self.forward(columns["win_tenderer"],columns["bidding_budget"],columns["win_bid_price"],extract_count)
  108. @annotate("string->bigint")
  109. class totimestamp(object):
  110. def __init__(self):
  111. import time
  112. global time
  113. import logging
  114. import json
  115. import re
  116. global json,logging,re
  117. self.time_pattern = "\d{4}\-\d{2}\-\d{2}.*"
  118. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  119. def evaluate(self, str_time):
  120. try:
  121. logging.info(str_time)
  122. if str_time is not None and re.search(self.time_pattern,str_time) is not None:
  123. timeArray = time.strptime(str_time[:10], "%Y-%m-%d")
  124. timeStamp = int(time.mktime(timeArray))
  125. return timeStamp
  126. else:
  127. return 0
  128. except Exception as e:
  129. return 0
  130. @annotate("string->string")
  131. class refind_name(object):
  132. def __init__(self):
  133. import logging
  134. import re
  135. global logging,re
  136. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  137. def evaluate(self, title):
  138. if title is not None:
  139. return re.sub(r'工程|服务|询价|比价|谈判|竞争性|磋商|结果|中标|招标|采购|的|公示|公开|成交|公告|评标|候选人|交易|通知|废标|流标|终止|中止|一笔|预告|单一来源|询价|竞价', '', title)
  140. return ""
  141. @annotate('bigint,bigint,bigint,string,bigint,string->string')
  142. class f_set_docid(BaseUDAF):
  143. '''
  144. 项目编号、中标单位、len(项目编号)>7、中标单位<> ""
  145. '''
  146. def __init__(self):
  147. import json
  148. global json
  149. def new_buffer(self):
  150. return [[]]
  151. def iterate(self, buffer,docid, page_time_stamp,extract_count,defind_column,defind_count,tenderee):
  152. buffer[0].append({"docid":docid,"page_time_stamp":page_time_stamp,"extract_count":extract_count,
  153. "defind_column":defind_column,"defind_count":defind_count,"tenderee":tenderee})
  154. def merge(self, buffer, pbuffer):
  155. buffer[0].extend(pbuffer[0])
  156. def terminate(self, buffer):
  157. list_docs = buffer[0]
  158. list_docs.sort(key=lambda x:x["page_time_stamp"])
  159. list_group = []
  160. _begin = 0
  161. defind_count = 0
  162. if len(list_docs)>0:
  163. defind_count = list_docs[0]["defind_count"]
  164. for i in range(len(list_docs)-1):
  165. if abs(list_docs[i]["page_time_stamp"]-list_docs[i+1]["page_time_stamp"])<=86400*2:
  166. continue
  167. else:
  168. _group = []
  169. _set_column = set()
  170. _set_tenderee = set()
  171. for j in range(_begin,i+1):
  172. _set_tenderee.add(list_docs[j]["tenderee"])
  173. _set_column.add(list_docs[j]["defind_column"])
  174. _group.append({"docid":list_docs[j]["docid"],"extract_count":list_docs[j]["extract_count"]})
  175. if len(_group)>=3 and len(_set_tenderee)>1:
  176. pass
  177. else:
  178. if len(_group)>1:
  179. if defind_count==2:
  180. if len(_set_column)>=2:
  181. list_group.append(_group)
  182. elif defind_count==1:
  183. if len(_set_column)==1:
  184. list_group.append(_group)
  185. elif defind_count==0:
  186. list_group.append(_group)
  187. _begin = i+1
  188. if len(list_docs)>1:
  189. _set_column = set()
  190. _set_tenderee = set()
  191. _group = []
  192. for j in range(_begin,len(list_docs)):
  193. if list_docs[j]["tenderee"] is not None and list_docs[j]["tenderee"]!="":
  194. _set_tenderee.add(list_docs[j]["tenderee"])
  195. _set_column.add(list_docs[j]["defind_column"])
  196. _group.append({"docid":list_docs[j]["docid"],"extract_count":list_docs[j]["extract_count"]})
  197. if len(_group)>=3 and len(_set_tenderee)>1:
  198. pass
  199. else:
  200. if len(_group)>1:
  201. if defind_count==2:
  202. if len(_set_column)>=2:
  203. list_group.append(_group)
  204. elif defind_count==1:
  205. if len(_set_column)==1:
  206. list_group.append(_group)
  207. elif defind_count==0:
  208. list_group.append(_group)
  209. return json.dumps(list_group)
  210. @annotate('bigint->string')
  211. class f_stamp_squence(BaseUDAF):
  212. '''
  213. 项目编号、中标单位、len(项目编号)>7、中标单位<> ""
  214. '''
  215. def __init__(self):
  216. import json
  217. global json
  218. import logging
  219. global logging
  220. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  221. def new_buffer(self):
  222. return [set()]
  223. def iterate(self, buffer,page_time_stamp):
  224. buffer[0].add(page_time_stamp)
  225. def merge(self, buffer, pbuffer):
  226. buffer[0] |= pbuffer[0]
  227. def terminate(self, buffer):
  228. if 0 in buffer[0]:
  229. buffer[0].remove(0)
  230. list_stamp = list(buffer[0])
  231. list_stamp.sort(key=lambda x:x)
  232. list_stamp_final = []
  233. _begin = 0
  234. _time_decase = 86400*2
  235. logging.info(str(list_stamp))
  236. for _index in range(len(list_stamp)-1):
  237. if list_stamp[_index+1]-list_stamp[_index]<_time_decase:
  238. continue
  239. else:
  240. list_stamp_final.append([list_stamp[_begin]-_time_decase,list_stamp[_index]+_time_decase])
  241. _begin = _index+1
  242. if len(list_stamp)>0:
  243. list_stamp_final.append([list_stamp[_begin]-_time_decase,list_stamp[-1]+_time_decase])
  244. return json.dumps(list_stamp_final)
  245. @annotate("bigint,string->bigint")
  246. class in_stamp(object):
  247. def __init__(self):
  248. import logging
  249. import re
  250. import json
  251. global logging,re,json
  252. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  253. def evaluate(self, page_time_stamp,json_stamp):
  254. list_stamp = json.loads(json_stamp)
  255. int_flag = 0
  256. for item in list_stamp:
  257. if page_time_stamp <item[0]:
  258. break
  259. if page_time_stamp>item[0] and page_time_stamp<item[1]:
  260. int_flag = 1
  261. break
  262. return int_flag
  263. @annotate('string -> bigint,bigint,bigint,bigint')
  264. class f_split_group_single(BaseUDTF):
  265. '''
  266. 将多个组拆解成多条记录
  267. '''
  268. def __init__(self):
  269. import logging
  270. import json
  271. global json,logging
  272. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  273. def process(self, json_set_docid):
  274. list_group = json.loads(json_set_docid)
  275. for item in list_group:
  276. for index_i in range(len(item)):
  277. for index_j in range(len(item)):
  278. if index_i!=index_j and item[index_i]["docid"]!=item[index_j]["docid"]:
  279. self.forward(item[index_i]["docid"],item[index_j]["docid"],item[index_i]["extract_count"],item[index_j]["extract_count"])
  280. @annotate('bigint,string->string')
  281. class group_document(BaseUDAF):
  282. '''
  283. 项目编号、中标单位、len(项目编号)>7、中标单位<> ""
  284. '''
  285. def __init__(self):
  286. import json
  287. global json
  288. def new_buffer(self):
  289. return [[]]
  290. def iterate(self, buffer,id,json_set_docid):
  291. buffer[0].append({"id":id,"json_set_docid":json.loads(json_set_docid)})
  292. def merge(self, buffer, pbuffer):
  293. buffer[0].extend(pbuffer[0])
  294. def terminate(self, buffer):
  295. return json.dumps(buffer[0])
  296. @annotate('bigint,string,bigint,string -> bigint,bigint,string')
  297. class decare_document(BaseUDTF):
  298. '''
  299. 将多个组拆解成多条记录
  300. '''
  301. def __init__(self):
  302. import logging
  303. import json
  304. global json,logging
  305. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  306. def process(self,group_id1, json_list_doc1,group_id2,json_list_doc2):
  307. #y=x,少掉近一半的数据
  308. if group_id1>=group_id2:
  309. list_doc1 = json.loads(json_list_doc1)
  310. list_doc2 = json.loads(json_list_doc2)
  311. for _doc1 in list_doc1:
  312. for _doc2 in list_doc2:
  313. #同一个重复group不做判断
  314. if _doc1["id"]!=_doc2["id"]:
  315. #判断两个group是否有重复
  316. _set1 = set()
  317. for _item1 in _doc1["json_set_docid"]:
  318. _set1.add(_item1["docid"])
  319. _set2 = set()
  320. for _item2 in _doc2["json_set_docid"]:
  321. _set2.add(_item2["docid"])
  322. if len(_set1&_set2)>0:
  323. new_json_set_docid = _doc1["json_set_docid"]
  324. for _item2 in _doc2["json_set_docid"]:
  325. if _item2["docid"] not in _set1:
  326. new_json_set_docid.append(_item2)
  327. self.forward(_doc1["id"],_doc2["id"],json.dumps(new_json_set_docid))
  328. @annotate('bigint,bigint,bigint,bigint->string')
  329. class choose_document(BaseUDAF):
  330. '''
  331. 项目编号、中标单位、len(项目编号)>7、中标单位<> ""
  332. '''
  333. def __init__(self):
  334. import json
  335. global json
  336. def new_buffer(self):
  337. return [[]]
  338. def iterate(self, buffer,docid1,extract_count1,docid2,extract_count2):
  339. buffer[0].append([docid1,extract_count1,docid2,extract_count2])
  340. def merge(self, buffer, pbuffer):
  341. buffer[0].extend(pbuffer[0])
  342. def terminate(self, buffer):
  343. list_pair = buffer[0]
  344. list_pair.sort(key=lambda x:x[3],reverse=True)
  345. _max_count = list_pair[0][3]
  346. save_flag = 0
  347. list_dumplicate = []
  348. _set = set()
  349. for item in buffer[0]:
  350. _set.add(str(item[2]))
  351. #不包含这条公告
  352. # _set.add(list_pair[0][0])
  353. if list_pair[0][1]>_max_count:
  354. save_flag = 1
  355. # _set.remove(list_pair[0][0])
  356. list_dumplicate = list(_set)
  357. else:
  358. if list_pair[0][1]<_max_count:
  359. save_flag = 0
  360. else:
  361. less_docid = list_pair[0][0]
  362. for item in list_pair:
  363. if item[3]>=_max_count and item[2]<less_docid:
  364. less_docid = item[2]
  365. if less_docid==list_pair[0][0]:
  366. save_flag = 1
  367. else:
  368. save_flag = 0
  369. list_dumplicate = list(_set)
  370. return json.dumps({"save_flag":save_flag,"dumplicates":list_dumplicate})
  371. @annotate('string -> bigint,string')
  372. class f_get_choose_document(BaseUDTF):
  373. '''
  374. 将多个组拆解成多条记录
  375. '''
  376. def __init__(self):
  377. import logging
  378. import json
  379. global json,logging
  380. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  381. def process(self,json_choose):
  382. if json_choose is None:
  383. self.forward(1,None)
  384. else:
  385. _choose = json.loads(json_choose)
  386. self.forward(_choose["save_flag"],",".join(_choose["dumplicates"]))
  387. @annotate('bigint,bigint,bigint,bigint->string')
  388. class group_document_bestFirst(BaseUDAF):
  389. '''
  390. 将组里面最优的放在前面
  391. '''
  392. def __init__(self):
  393. import json
  394. global json
  395. def new_buffer(self):
  396. return [[]]
  397. def iterate(self, buffer,docid1,extract_count1,docid2,extract_count2):
  398. buffer[0].append([docid1,extract_count1,docid2,extract_count2])
  399. def merge(self, buffer, pbuffer):
  400. buffer[0].extend(pbuffer[0])
  401. def terminate(self, buffer):
  402. list_pair = buffer[0]
  403. list_pair.sort(key=lambda x:x[3],reverse=True)
  404. _max_count = list_pair[0][3]
  405. save_flag = 0
  406. list_dumplicate = []
  407. _set = set()
  408. for item in buffer[0]:
  409. _set.add(item[2])
  410. _set.add(list_pair[0][0])
  411. best_docid = None
  412. if list_pair[0][1]>_max_count:
  413. best_docid = list_pair[0][0]
  414. else:
  415. best_docid = list_pair[0][2]
  416. for item in list_pair:
  417. if item[3]>=_max_count and item[2]<best_docid:
  418. best_docid = item[2]
  419. _set.remove(best_docid)
  420. list_dumplicate = list(_set)
  421. list_dumplicate.sort(key=lambda x:x)
  422. list_dumplicate.insert(0,best_docid)
  423. list_dumplicate_str = []
  424. for item in list_dumplicate:
  425. list_dumplicate_str.append(str(item))
  426. return ",".join(list_dumplicate_str)
  427. @annotate('string -> bigint,string')
  428. class f_get_best_dumplicates(BaseUDTF):
  429. '''
  430. 得到每个分组中最优的那一条及其重复记录
  431. '''
  432. def __init__(self):
  433. import logging
  434. import json
  435. global json,logging
  436. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  437. def process(self,list_dumplicate_str):
  438. if list_dumplicate_str is None:
  439. pass
  440. else:
  441. list_dumplicate = list_dumplicate_str.split(",")
  442. if len(list_dumplicate)>0:
  443. self.forward(int(list_dumplicate[0]),",".join(list_dumplicate[1:]))
  444. else:
  445. pass
  446. @annotate('bigint,bigint->string')
  447. class bridge2group(BaseUDAF):
  448. '''
  449. 项目编号、中标单位、len(项目编号)>7、中标单位<> ""
  450. '''
  451. def __init__(self):
  452. import json
  453. global json
  454. def new_buffer(self):
  455. return [set()]
  456. def iterate(self, buffer,docid1,docid2):
  457. buffer[0].add(docid1)
  458. buffer[0].add(docid2)
  459. def merge(self, buffer, pbuffer):
  460. buffer[0] |= pbuffer[0]
  461. def terminate(self, buffer):
  462. list_pair = list(buffer[0])
  463. list_pair.sort(key=lambda x:x,reverse=True)
  464. return json.dumps(list_pair)
  465. @annotate('string -> bigint,bigint')
  466. class group2bridge(BaseUDTF):
  467. '''
  468. 将多个组拆解成多条记录
  469. '''
  470. def __init__(self):
  471. import logging
  472. import json
  473. global json,logging
  474. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  475. def process(self,json_list_docid):
  476. list_docid = json.loads(json_list_docid)
  477. for _docid in list_docid:
  478. self.forward(list_docid[-1],_docid)
  479. @annotate('bigint,bigint,string -> bigint')
  480. class f_get_dump_docid(BaseUDTF):
  481. '''
  482. 将多个组拆解成多条记录
  483. '''
  484. def __init__(self):
  485. import logging
  486. import json
  487. global json,logging
  488. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  489. def process(self,docid,save_flag,dumplicates):
  490. if save_flag==0:
  491. self.forward(docid)
  492. if dumplicates is not None:
  493. list_docid = dumplicates.split(",")
  494. if len(list_docid)>0:
  495. for _docid in list_docid[1:]:
  496. self.forward(int(_docid))
  497. else:
  498. if dumplicates is not None:
  499. list_docid = dumplicates.split(",")
  500. if len(list_docid)>0:
  501. for _docid in list_docid:
  502. self.forward(int(_docid))
  503. @annotate('string -> bigint,bigint')
  504. class f_get_docid(BaseUDTF):
  505. '''
  506. 将多个组拆解成多条记录
  507. '''
  508. def __init__(self):
  509. import logging
  510. import json
  511. global json,logging
  512. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  513. def process(self,json_set_docid):
  514. team_id = 0
  515. if json_set_docid is not None:
  516. list_docses = json.loads(json_set_docid)
  517. for list_docs in list_docses:
  518. team_id += 1
  519. for item in list_docs:
  520. self.forward(team_id,item["docid"])
  521. @annotate("string->bigint")
  522. class get_count_dump(object):
  523. def __init__(self):
  524. import logging
  525. import re
  526. global logging,re
  527. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  528. def evaluate(self, title):
  529. _count = 0
  530. if title is not None:
  531. _count = len(title.split(","))
  532. return _count