documentDumplicate.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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. _set_tenderee.add(list_docs[j]["tenderee"])
  194. _set_column.add(list_docs[j]["defind_column"])
  195. _group.append({"docid":list_docs[j]["docid"],"extract_count":list_docs[j]["extract_count"]})
  196. if len(_group)>=3 and len(_set_tenderee)>1:
  197. pass
  198. else:
  199. if len(_group)>1:
  200. if defind_count==2:
  201. if len(_set_column)>=2:
  202. list_group.append(_group)
  203. elif defind_count==1:
  204. if len(_set_column)==1:
  205. list_group.append(_group)
  206. elif defind_count==0:
  207. list_group.append(_group)
  208. return json.dumps(list_group)
  209. @annotate('bigint->string')
  210. class f_stamp_squence(BaseUDAF):
  211. '''
  212. 项目编号、中标单位、len(项目编号)>7、中标单位<> ""
  213. '''
  214. def __init__(self):
  215. import json
  216. global json
  217. import logging
  218. global logging
  219. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  220. def new_buffer(self):
  221. return [set()]
  222. def iterate(self, buffer,page_time_stamp):
  223. buffer[0].add(page_time_stamp)
  224. def merge(self, buffer, pbuffer):
  225. buffer[0] |= pbuffer[0]
  226. def terminate(self, buffer):
  227. if 0 in buffer[0]:
  228. buffer[0].remove(0)
  229. list_stamp = list(buffer[0])
  230. list_stamp.sort(key=lambda x:x)
  231. list_stamp_final = []
  232. _begin = 0
  233. _time_decase = 86400*2
  234. logging.info(str(list_stamp))
  235. for _index in range(len(list_stamp)-1):
  236. if list_stamp[_index+1]-list_stamp[_index]<_time_decase:
  237. continue
  238. else:
  239. list_stamp_final.append([list_stamp[_begin]-_time_decase,list_stamp[_index]+_time_decase])
  240. _begin = _index+1
  241. if len(list_stamp)>0:
  242. list_stamp_final.append([list_stamp[_begin]-_time_decase,list_stamp[-1]+_time_decase])
  243. return json.dumps(list_stamp_final)
  244. @annotate("bigint,string->bigint")
  245. class in_stamp(object):
  246. def __init__(self):
  247. import logging
  248. import re
  249. import json
  250. global logging,re,json
  251. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  252. def evaluate(self, page_time_stamp,json_stamp):
  253. list_stamp = json.loads(json_stamp)
  254. int_flag = 0
  255. for item in list_stamp:
  256. if page_time_stamp <item[0]:
  257. break
  258. if page_time_stamp>item[0] and page_time_stamp<item[1]:
  259. int_flag = 1
  260. break
  261. return int_flag
  262. @annotate('string -> bigint,bigint,bigint,bigint')
  263. class f_split_group_single(BaseUDTF):
  264. '''
  265. 将多个组拆解成多条记录
  266. '''
  267. def __init__(self):
  268. import logging
  269. import json
  270. global json,logging
  271. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  272. def process(self, json_set_docid):
  273. list_group = json.loads(json_set_docid)
  274. for item in list_group:
  275. for index_i in range(len(item)):
  276. for index_j in range(len(item)):
  277. if index_i!=index_j and item[index_i]["docid"]!=item[index_j]["docid"]:
  278. self.forward(item[index_i]["docid"],item[index_j]["docid"],item[index_i]["extract_count"],item[index_j]["extract_count"])
  279. @annotate('bigint,string->string')
  280. class group_document(BaseUDAF):
  281. '''
  282. 项目编号、中标单位、len(项目编号)>7、中标单位<> ""
  283. '''
  284. def __init__(self):
  285. import json
  286. global json
  287. def new_buffer(self):
  288. return [[]]
  289. def iterate(self, buffer,id,json_set_docid):
  290. buffer[0].append({"id":id,"json_set_docid":json.loads(json_set_docid)})
  291. def merge(self, buffer, pbuffer):
  292. buffer[0].extend(pbuffer[0])
  293. def terminate(self, buffer):
  294. return json.dumps(buffer[0])
  295. @annotate('bigint,string,bigint,string -> bigint,bigint,string')
  296. class decare_document(BaseUDTF):
  297. '''
  298. 将多个组拆解成多条记录
  299. '''
  300. def __init__(self):
  301. import logging
  302. import json
  303. global json,logging
  304. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  305. def process(self,group_id1, json_list_doc1,group_id2,json_list_doc2):
  306. #y=x,少掉近一半的数据
  307. if group_id1>=group_id2:
  308. list_doc1 = json.loads(json_list_doc1)
  309. list_doc2 = json.loads(json_list_doc2)
  310. for _doc1 in list_doc1:
  311. for _doc2 in list_doc2:
  312. #同一个重复group不做判断
  313. if _doc1["id"]!=_doc2["id"]:
  314. #判断两个group是否有重复
  315. _set1 = set()
  316. for _item1 in _doc1["json_set_docid"]:
  317. _set1.add(_item1["docid"])
  318. _set2 = set()
  319. for _item2 in _doc2["json_set_docid"]:
  320. _set2.add(_item2["docid"])
  321. if len(_set1&_set2)>0:
  322. new_json_set_docid = _doc1["json_set_docid"]
  323. for _item2 in _doc2["json_set_docid"]:
  324. if _item2["docid"] not in _set1:
  325. new_json_set_docid.append(_item2)
  326. self.forward(_doc1["id"],_doc2["id"],json.dumps(new_json_set_docid))
  327. @annotate('bigint,bigint,bigint,bigint->string')
  328. class choose_document(BaseUDAF):
  329. '''
  330. 项目编号、中标单位、len(项目编号)>7、中标单位<> ""
  331. '''
  332. def __init__(self):
  333. import json
  334. global json
  335. def new_buffer(self):
  336. return [[]]
  337. def iterate(self, buffer,docid1,extract_count1,docid2,extract_count2):
  338. buffer[0].append([docid1,extract_count1,docid2,extract_count2])
  339. def merge(self, buffer, pbuffer):
  340. buffer[0].extend(pbuffer[0])
  341. def terminate(self, buffer):
  342. list_pair = buffer[0]
  343. list_pair.sort(key=lambda x:x[3],reverse=True)
  344. _max_count = list_pair[0][3]
  345. save_flag = 0
  346. list_dumplicate = []
  347. _set = set()
  348. for item in buffer[0]:
  349. _set.add(str(item[2]))
  350. #不包含这条公告
  351. # _set.add(list_pair[0][0])
  352. if list_pair[0][1]>_max_count:
  353. save_flag = 1
  354. # _set.remove(list_pair[0][0])
  355. list_dumplicate = list(_set)
  356. else:
  357. save_flag = 0
  358. less_docid = list_pair[0][2]
  359. for item in list_pair:
  360. if item[3]>=_max_count and item[2]<less_docid:
  361. less_docid = item[2]
  362. _set.remove(str(less_docid))
  363. list_dumplicate = list(_set)
  364. list_dumplicate.insert(0,str(less_docid))
  365. return json.dumps({"save_flag":save_flag,"dumplicates":list_dumplicate})
  366. @annotate('string -> bigint,string')
  367. class f_get_choose_document(BaseUDTF):
  368. '''
  369. 将多个组拆解成多条记录
  370. '''
  371. def __init__(self):
  372. import logging
  373. import json
  374. global json,logging
  375. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  376. def process(self,json_choose):
  377. if json_choose is None:
  378. self.forward(1,None)
  379. else:
  380. _choose = json.loads(json_choose)
  381. self.forward(_choose["save_flag"],",".join(_choose["dumplicates"]))
  382. @annotate('bigint,bigint,bigint,bigint->string')
  383. class group_document_bestFirst(BaseUDAF):
  384. '''
  385. 将组里面最优的放在前面
  386. '''
  387. def __init__(self):
  388. import json
  389. global json
  390. def new_buffer(self):
  391. return [[]]
  392. def iterate(self, buffer,docid1,extract_count1,docid2,extract_count2):
  393. buffer[0].append([docid1,extract_count1,docid2,extract_count2])
  394. def merge(self, buffer, pbuffer):
  395. buffer[0].extend(pbuffer[0])
  396. def terminate(self, buffer):
  397. list_pair = buffer[0]
  398. list_pair.sort(key=lambda x:x[3],reverse=True)
  399. _max_count = list_pair[0][3]
  400. save_flag = 0
  401. list_dumplicate = []
  402. _set = set()
  403. for item in buffer[0]:
  404. _set.add(item[2])
  405. _set.add(list_pair[0][0])
  406. best_docid = None
  407. if list_pair[0][1]>_max_count:
  408. best_docid = list_pair[0][0]
  409. else:
  410. best_docid = list_pair[0][2]
  411. for item in list_pair:
  412. if item[3]>=_max_count and item[2]<best_docid:
  413. best_docid = item[2]
  414. _set.remove(best_docid)
  415. list_dumplicate = list(_set)
  416. list_dumplicate.sort(key=lambda x:x)
  417. list_dumplicate.insert(0,best_docid)
  418. list_dumplicate_str = []
  419. for item in list_dumplicate:
  420. list_dumplicate_str.append(str(item))
  421. return ",".join(list_dumplicate_str)
  422. @annotate('string -> bigint,string')
  423. class f_get_best_dumplicates(BaseUDTF):
  424. '''
  425. 得到每个分组中最优的那一条及其重复记录
  426. '''
  427. def __init__(self):
  428. import logging
  429. import json
  430. global json,logging
  431. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  432. def process(self,list_dumplicate_str):
  433. if list_dumplicate_str is None:
  434. pass
  435. else:
  436. list_dumplicate = list_dumplicate_str.split(",")
  437. if len(list_dumplicate)>0:
  438. self.forward(int(list_dumplicate[0]),",".join(list_dumplicate[1:]))
  439. else:
  440. pass
  441. @annotate('bigint,bigint->string')
  442. class bridge2group(BaseUDAF):
  443. '''
  444. 项目编号、中标单位、len(项目编号)>7、中标单位<> ""
  445. '''
  446. def __init__(self):
  447. import json
  448. global json
  449. def new_buffer(self):
  450. return [set()]
  451. def iterate(self, buffer,docid1,docid2):
  452. buffer[0].add(docid1)
  453. buffer[0].add(docid2)
  454. def merge(self, buffer, pbuffer):
  455. buffer[0] |= pbuffer[0]
  456. def terminate(self, buffer):
  457. list_pair = list(buffer[0])
  458. list_pair.sort(key=lambda x:x,reverse=True)
  459. return json.dumps(list_pair)
  460. @annotate('string -> bigint,bigint')
  461. class group2bridge(BaseUDTF):
  462. '''
  463. 将多个组拆解成多条记录
  464. '''
  465. def __init__(self):
  466. import logging
  467. import json
  468. global json,logging
  469. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  470. def process(self,json_list_docid):
  471. list_docid = json.loads(json_list_docid)
  472. for _docid in list_docid:
  473. self.forward(list_docid[-1],_docid)
  474. @annotate('bigint,bigint,string -> bigint')
  475. class f_get_dump_docid(BaseUDTF):
  476. '''
  477. 将多个组拆解成多条记录
  478. '''
  479. def __init__(self):
  480. import logging
  481. import json
  482. global json,logging
  483. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  484. def process(self,docid,save_flag,dumplicates):
  485. if save_flag==0:
  486. self.forward(docid)
  487. if dumplicates is not None:
  488. list_docid = dumplicates.split(",")
  489. if len(list_docid)>0:
  490. for _docid in list_docid[1:]:
  491. self.forward(int(_docid))
  492. else:
  493. if dumplicates is not None:
  494. list_docid = dumplicates.split(",")
  495. if len(list_docid)>0:
  496. for _docid in list_docid:
  497. self.forward(int(_docid))
  498. @annotate('string -> bigint,bigint')
  499. class f_get_docid(BaseUDTF):
  500. '''
  501. 将多个组拆解成多条记录
  502. '''
  503. def __init__(self):
  504. import logging
  505. import json
  506. global json,logging
  507. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  508. def process(self,json_set_docid):
  509. team_id = 0
  510. if json_set_docid is not None:
  511. list_docses = json.loads(json_set_docid)
  512. for list_docs in list_docses:
  513. team_id += 1
  514. for item in list_docs:
  515. self.forward(team_id,item["docid"])
  516. @annotate("string->bigint")
  517. class get_count_dump(object):
  518. def __init__(self):
  519. import logging
  520. import re
  521. global logging,re
  522. logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  523. def evaluate(self, title):
  524. _count = 0
  525. if title is not None:
  526. _count = len(title.split(","))
  527. return _count