entityLink.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. #coding:UTF8
  2. '''
  3. Created on 2019年5月21日
  4. @author: User
  5. '''
  6. import re
  7. import os
  8. import time
  9. import pandas as pd
  10. _time = time.time()
  11. from BiddingKG.dl.common.Utils import *
  12. from BiddingKG.dl.interface.Entitys import *
  13. import json
  14. from BiddingKG.dl.common.constDict import ConstDict
  15. def edit_distance(source,target):
  16. dp = [["" for i in range(len(source)+1)] for j in range(len(target)+1)]
  17. for i in range(len(dp)):
  18. for j in range(len(dp[i])):
  19. if i==0:
  20. dp[i][j] = j
  21. elif j==0:
  22. dp[i][j] = i
  23. else:
  24. if source[j-1]==target[i-1]:
  25. cost = 0
  26. else:
  27. cost = 2
  28. dp[i][j] = min([dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+cost])
  29. return dp[-1][-1]
  30. def jaccard_score(source,target):
  31. source_set = set([s for s in source])
  32. target_set = set([s for s in target])
  33. if len(source_set)==0 or len(target_set)==0:
  34. return 0
  35. return max(len(source_set&target_set)/len(source_set),len(source_set&target_set)/len(target_set))
  36. def get_place_list():
  37. path = os.path.dirname(__file__) + '/../place_info.csv'
  38. place_df = pd.read_csv(path)
  39. place_list = []
  40. for index, row in place_df.iterrows():
  41. place_list.append(row[1])
  42. place_list.append('台湾')
  43. place_list.append('澳门')
  44. place_list.append('香港')
  45. # place_list.append('東莞')
  46. # place_list.append('廣州')
  47. # place_list.append('韩国')
  48. # place_list.append('德国')
  49. # place_list.append('英国')
  50. # place_list.append('日本')
  51. # place_list.append('意大利')
  52. # place_list.append('新加坡')
  53. # place_list.append('加拿大')
  54. # place_list.append('西班牙')
  55. # place_list.append('澳大利亚')
  56. # place_list.append('美国')
  57. place_list = list(set(place_list))
  58. return place_list
  59. place_list = get_place_list()
  60. place_pattern = "|".join(place_list)
  61. def is_short(shorter_cut, longer):
  62. '''
  63. 判断是否为简称
  64. :param shorter_cut: 简称
  65. :param longer: 全称
  66. :return:
  67. '''
  68. flag = 1
  69. for words in shorter_cut:
  70. if words in longer:
  71. longer = longer[longer.find(words) + len(words):]
  72. else:
  73. flag = 0
  74. break
  75. if flag:
  76. return 1
  77. else:
  78. return 0
  79. def link_entitys(list_entitys,on_value=1):#on_value=0.81
  80. for list_entity in list_entitys:
  81. range_entity = []
  82. short_entity = []
  83. long_entity = []
  84. n = 0
  85. for _entity in list_entity:
  86. if _entity.entity_type in ["org","company"]:
  87. range_entity.append(_entity)
  88. if len(_entity.entity_text) in [4, 5, 6]:
  89. short_entity.append(_entity)
  90. if len(_entity.entity_text)>6:
  91. long_entity.append(_entity)
  92. n += 1
  93. if n > 1000:
  94. break
  95. range_entity = range_entity[:1000]
  96. #替换公司的逻辑有问题,先取消
  97. # for first_i in range(len(range_entity)):
  98. # _entity = range_entity[first_i]
  99. # for second_i in range(first_i+1,len(range_entity)):
  100. # _ent = range_entity[second_i]
  101. # # 2021/5/21 update: 两个实体标签互斥(一个是招标人、一个是代理人)且entity_text不相等时,跳过
  102. # if _entity.entity_text != _ent.entity_text and _entity.label != _ent.label and _entity.label in [0,1] and _ent.label in [0, 1]:
  103. # continue
  104. # _score = jaccard_score(re.sub("%s|%s"%("股份|责任|有限|公司",place_pattern),"",_entity.entity_text), re.sub("%s|%s"%("股份|责任|有限|公司",place_pattern),"",_ent.entity_text))
  105. # if _entity.entity_text!=_ent.entity_text and _score>=on_value:
  106. # _entity.linked_entitys.append(_ent)
  107. # _ent.linked_entitys.append(_entity)
  108. # print("=-===",_entity.entity_text,_ent.entity_text,_score)
  109. # #替换公司名称
  110. # for _entity in range_entity:
  111. # if re.search("公司",_entity.entity_text) is None:
  112. # for _ent in _entity.linked_entitys:
  113. # if re.search("公司$",_ent.entity_text) is not None:
  114. # if len(_ent.entity_text)>len(_entity.entity_text):
  115. # _entity.entity_text = _ent.entity_text
  116. if short_entity and long_entity:
  117. for first_i in range(len(short_entity)):
  118. _entity = short_entity[first_i]
  119. if is_enterprise_exist(_entity.entity_text): # 实体表存在的不替换
  120. continue
  121. if _entity.label == 0 and re.search('(医院|学院|学校|中学|小学|大学|幼儿园|保健院|党校)', _entity.entity_text)==None:
  122. ree_l = []
  123. other_l = []
  124. for second_i in range(len(long_entity)):
  125. _ent = long_entity[second_i]
  126. if _ent.label in [0,1,5] and is_short(_entity.entity_text, _ent.entity_text):
  127. if _ent.label in [0 ,1]:
  128. ree_l.append(_ent)
  129. elif _ent.label in [5]:
  130. other_l.append(_ent)
  131. for _ent in ree_l + other_l:
  132. if is_enterprise_exist(_ent.entity_text) or re.search('有限(责任)?公司', _ent.entity_text):
  133. _entity.entity_text = _ent.entity_text
  134. # 2021/12/21 替换通过字典识别到的取长度最大的相似实体
  135. for _entity in range_entity:
  136. used_linked_entitys = []
  137. if not _entity.linked_entitys:
  138. continue
  139. _entity.linked_entitys.sort(key=lambda x: len(x.entity_text), reverse=True)
  140. for _ent in _entity.linked_entitys:
  141. if _ent in used_linked_entitys:
  142. break
  143. # print("_entity, _ent", _entity.entity_text, _ent.if_dict_match, _ent.entity_text)
  144. if _ent.if_dict_match == 1:
  145. if len(_ent.entity_text) > len(_entity.entity_text):
  146. # 判断两个公司地区相同
  147. match_list_1, match_list_2 = [], []
  148. for place in place_list:
  149. if place in _entity.entity_text:
  150. match_list_1.append(place)
  151. if place in _ent.entity_text:
  152. match_list_2.append(place)
  153. if str(match_list_1) == str(match_list_2):
  154. # print("字典替换", _entity.entity_text, "->", _ent.entity_text)
  155. _entity.origin_entity_text = _entity.entity_text
  156. _entity.entity_text = _ent.entity_text
  157. used_linked_entitys.append(_ent)
  158. # print(_entity.entity_text, _entity.if_dict_match, _ent.entity_text, _ent.if_dict_match)
  159. # 用于去重的标题
  160. def doctitle_refine(doctitle):
  161. _doctitle_refine = re.sub(r'工程|服务|询价|比价|谈判|竞争性|磋商|结果|中标|招标|采购|的|公示|公开|成交|公告|评标|候选人|'
  162. r'交易|通知|废标|流标|终止|中止|一笔|预告|单一来源|竞价|合同', '', doctitle)
  163. return _doctitle_refine
  164. # 前100个公司实体
  165. def get_nlp_enterprise(list_entity):
  166. nlp_enterprise = []
  167. nlp_enterprise_attachment = []
  168. max_num = 100
  169. list_entity = sorted(list_entity,key=lambda x:(x.sentence_index,x.begin_index))
  170. for entity in list_entity:
  171. if entity.entity_type in ['org','company']:
  172. if not entity.in_attachment:
  173. if entity.entity_text not in nlp_enterprise:
  174. nlp_enterprise.append(entity.entity_text)
  175. else:
  176. if entity.entity_text not in nlp_enterprise_attachment:
  177. nlp_enterprise_attachment.append(entity.entity_text)
  178. return nlp_enterprise[:max_num],nlp_enterprise_attachment[:max_num]
  179. ENTERPRISE_HUGE = None
  180. def getEnterprisePath():
  181. global ENTERPRISE_HUGE
  182. filename_huge = "LEGAL_ENTERPRISE_HUGE.txt"
  183. huge_path = getFileFromSysPath(filename_huge)
  184. if huge_path is None:
  185. if os.path.exists(filename_huge):
  186. log("enterprise path:%s"%(filename_huge))
  187. ENTERPRISE_HUGE = True
  188. return filename_huge,ENTERPRISE_HUGE
  189. else:
  190. log("enterprise path:%s"%(huge_path))
  191. ENTERPRISE_HUGE = True
  192. return huge_path,ENTERPRISE_HUGE
  193. filename = "LEGAL_ENTERPRISE.txt"
  194. real_path = getFileFromSysPath(filename)
  195. if real_path is None:
  196. real_path = filename
  197. log("ENTERPRISE path:%s"%(real_path))
  198. ENTERPRISE_HUGE = False
  199. return real_path,ENTERPRISE_HUGE
  200. DICT_ENTERPRISE_DONE = False
  201. POOL_REDIS = None
  202. ENTERPRISE_KEY_LEN = 3
  203. ENTERPRISE_PREFIX_LEN = 3
  204. ENTERPRISE_TAIL_LEN = 3
  205. SET_ENTERPRISE = set()
  206. SET_PREFIX_ENTERPRISE = set()
  207. SET_TAIL_ENTERPRISE = set()
  208. SET_PREFIX_ENTERPRISE_HUGE_FILE = "SET_PREFIX_ENTERPRISE_HUGE.pk"
  209. SET_TAIL_ENTERPRISE_HUGE_FILE = "SET_TAIL_ENTERPRISE_HUGE.pk"
  210. def getDict_enterprise():
  211. global DICT_ENTERPRISE_DONE,SET_ENTERPRISE,SET_PREFIX_ENTERPRISE,SET_TAIL_ENTERPRISE
  212. real_path,is_huge = getEnterprisePath()
  213. _ok = False
  214. if is_huge:
  215. if os.path.exists(SET_PREFIX_ENTERPRISE_HUGE_FILE) and os.path.exists(SET_TAIL_ENTERPRISE_HUGE_FILE):
  216. SET_PREFIX_ENTERPRISE = load(SET_PREFIX_ENTERPRISE_HUGE_FILE)
  217. SET_TAIL_ENTERPRISE = load(SET_TAIL_ENTERPRISE_HUGE_FILE)
  218. _ok = True
  219. if not _ok:
  220. with open(real_path,"r",encoding="UTF8") as f:
  221. for _e in f:
  222. if not _e:
  223. continue
  224. _e = _e.strip()
  225. if len(_e)>=4:
  226. key_enter = _e[:ENTERPRISE_KEY_LEN]
  227. SET_PREFIX_ENTERPRISE.add(key_enter)
  228. SET_TAIL_ENTERPRISE.add(_e[-ENTERPRISE_TAIL_LEN:])
  229. if not is_huge:
  230. SET_ENTERPRISE.add(_e)
  231. #仅在大文件情况下才使用缓存加载
  232. if is_huge:
  233. save(SET_PREFIX_ENTERPRISE,SET_PREFIX_ENTERPRISE_HUGE_FILE)
  234. save(SET_TAIL_ENTERPRISE,SET_TAIL_ENTERPRISE_HUGE_FILE)
  235. log("SET_PREFIX_ENTERPRISE takes memory:%.2fM size:%d"%(sys.getsizeof(SET_PREFIX_ENTERPRISE)/1024/1024,len(SET_PREFIX_ENTERPRISE)))
  236. log("SET_TAIL_ENTERPRISE takes memory:%.2fM size:%d"%(sys.getsizeof(SET_TAIL_ENTERPRISE)/1024/1024,len(SET_TAIL_ENTERPRISE)))
  237. log("SET_ENTERPRISE takes memory:%.2fM size:%d"%(sys.getsizeof(SET_ENTERPRISE)/1024/1024,len(SET_ENTERPRISE)))
  238. # for _e in ["河南省柘源","建筑工程有限公司"]:
  239. # if not _e:
  240. # continue
  241. # _e = _e.strip()
  242. # if len(_e)>=4:
  243. # key_enter = _e[:4]
  244. # if key_enter not in DICT_ENTERPRISE:
  245. # DICT_ENTERPRISE[key_enter] = set()
  246. # DICT_ENTERPRISE[key_enter].add(_e[4:])
  247. DICT_ENTERPRISE_DONE = True
  248. def init_redis_pool():
  249. from BiddingKG.dl.common.pool import ConnectorPool
  250. from BiddingKG.dl.common.source import getConnect_redis_baseline
  251. global POOL_REDIS
  252. if POOL_REDIS is None:
  253. POOL_REDIS = ConnectorPool(init_num=1,max_num=10,method_init=getConnect_redis_baseline)
  254. # 插入 Redis
  255. # def add_redis(company_list):
  256. # global ENTERPRISE_HUGE,POOL_REDIS
  257. # if ENTERPRISE_HUGE:
  258. # _db = POOL_REDIS.getConnector()
  259. # for enterprise_name in company_list:
  260. # _v = _db.get(enterprise_name)
  261. # if _v is None:
  262. # if isLegalNewName(enterprise_name):
  263. # _db.set(enterprise_name,1)
  264. # 新实体合法判断
  265. def isLegalNewName(enterprise_name):
  266. # head_character_list = ["[",'【',"(",'(']
  267. # tail_character_list = ["]",'】',")",')']
  268. # 名称开头判断
  269. if re.search("^[\da-zA-Z][^\da-zA-Z]|"
  270. "^[^\da-zA-Z\u4e00-\u9fa5\[【((]|"
  271. "^[\[【((].{,1}[\]】))]|"
  272. "^[0〇]|"
  273. "^(20[0-2][0-9]|[0-2]?[0-9]年|[0-1]?[0-9]月|[0-3]?[0-9]日)",enterprise_name):
  274. return -1
  275. if len(re.findall("[\u4e00-\u9fa5]",enterprise_name))<2:
  276. return -1
  277. if re.search("╳|*|\*|×|xx|XX",enterprise_name):
  278. return -1
  279. if re.search("^(省|自治[县州区]|市|县|区|镇|乡|街道)",enterprise_name) and not re.search("^(镇江|乡宁|镇原|镇海|镇安|镇巴|镇坪|镇赉|镇康|镇沅|镇雄|镇远|镇宁|乡城|镇平|市中|市南|市北)",enterprise_name):
  280. return -1
  281. if re.search("\d{1,2}:\d{2}(:\d{2})?|(rar|xlsx|zip|png|jpg|swf|docx|txt|pdf|PDF|doc|xls|bmp|&?nbsp)",enterprise_name):
  282. return -1
  283. if re.search("(招标|代理)(人|机构)|联系(人|方式)|中标|候选|第.名",enterprise_name):
  284. return -1
  285. if re.search("[a-zA-Z\d]{1,2}(包|标段?)|第.批"):
  286. return 0
  287. return 1
  288. # 过滤掉Redis里值为0的错误实体
  289. def enterprise_filter(entity_list):
  290. global ENTERPRISE_HUGE,SET_ENTERPRISE,POOL_REDIS
  291. if ENTERPRISE_HUGE:
  292. if POOL_REDIS is None:
  293. init_redis_pool()
  294. _db = POOL_REDIS.getConnector()
  295. remove_list = []
  296. try:
  297. for entity in entity_list:
  298. if entity.entity_type in ['company','org']:
  299. _v = _db.get(entity.entity_text)
  300. if _v==0:
  301. remove_list.append(entity)
  302. except Exception as e:
  303. traceback.print_exc()
  304. POOL_REDIS.putConnector(_db)
  305. for _entity in remove_list:
  306. entity_list.remove(_entity)
  307. return entity_list
  308. def is_enterprise_exist(enterprise_name):
  309. global ENTERPRISE_HUGE,SET_ENTERPRISE,POOL_REDIS
  310. # print("test",enterprise_name)
  311. if ENTERPRISE_HUGE:
  312. if POOL_REDIS is None:
  313. init_redis_pool()
  314. _db = POOL_REDIS.getConnector()
  315. try:
  316. _time = time.time()
  317. _v = _db.get(enterprise_name)
  318. POOL_REDIS.putConnector(_db)
  319. if _v is None:
  320. return False
  321. else:
  322. if _v:
  323. log("redis take %.5f of '%s' exists"%(time.time()-_time,enterprise_name))
  324. return True
  325. else:
  326. return False
  327. except Exception as e:
  328. traceback.print_exc()
  329. return False
  330. else:
  331. if enterprise_name in SET_ENTERPRISE:
  332. return True
  333. else:
  334. return False
  335. import threading
  336. import time
  337. load_enterprise_thread = threading.Thread(target=getDict_enterprise)
  338. load_enterprise_thread.start()
  339. MAX_ENTERPRISE_LEN = 30
  340. def match_enterprise_max_first(sentence):
  341. while True:
  342. if not DICT_ENTERPRISE_DONE:
  343. time.sleep(1)
  344. else:
  345. break
  346. list_match = []
  347. begin_index = 0
  348. if len(sentence)>4:
  349. while True:
  350. if begin_index+ENTERPRISE_KEY_LEN<len(sentence):
  351. key_enter = sentence[begin_index:begin_index+ENTERPRISE_KEY_LEN]
  352. # if key_enter in DICT_ENTERPRISE:
  353. # _len = min(MAX_ENTERPRISE_LEN-ENTERPRISE_KEY_LEN+1,len(sentence)-begin_index)
  354. # for _i in range(_len):
  355. # enter_name = sentence[begin_index+ENTERPRISE_KEY_LEN:begin_index+_len-_i]
  356. # if enter_name in DICT_ENTERPRISE[key_enter]:
  357. # match_item = {"entity_text":"%s%s"%(key_enter,enter_name),"begin_index":begin_index,"end_index":begin_index+len(key_enter)+len(enter_name)}
  358. # list_match.append(match_item)
  359. # begin_index += (len(key_enter)+len(enter_name))-1
  360. # break
  361. if key_enter in SET_PREFIX_ENTERPRISE:
  362. _len = min(MAX_ENTERPRISE_LEN-ENTERPRISE_KEY_LEN+1,len(sentence)-begin_index)
  363. for _i in range(_len):
  364. enter_name = sentence[begin_index:begin_index+_len-_i]
  365. enter_tail = enter_name[-ENTERPRISE_TAIL_LEN:]
  366. if enter_tail in SET_TAIL_ENTERPRISE:
  367. if is_enterprise_exist(enter_name):
  368. match_item = {"entity_text":"%s"%(enter_name),"begin_index":begin_index,"end_index":begin_index+len(enter_name)}
  369. # print("match_item",key_enter,enter_name)
  370. list_match.append(match_item)
  371. begin_index += len(enter_name)-1
  372. break
  373. begin_index += 1
  374. else:
  375. break
  376. # print("======",list_match)
  377. return list_match
  378. def calibrateEnterprise(list_articles,list_sentences,list_entitys):
  379. for _article,list_sentence,list_entity in zip(list_articles,list_sentences,list_entitys):
  380. list_calibrate = []
  381. match_add = False
  382. match_replace = False
  383. range_entity = []
  384. for p_entity in list_entity:
  385. if p_entity.entity_type in ("org","company","location"):
  386. range_entity.append(p_entity)
  387. if len(range_entity)>1000:
  388. break
  389. for p_sentence in list_sentence:
  390. sentence = p_sentence.sentence_text
  391. sentence_entitys = [(ent.entity_text,ent.wordOffset_begin,ent.wordOffset_end) for ent in list_entity if ent.sentence_index==p_sentence.sentence_index and ent.entity_type in ['org','company']]
  392. list_match = match_enterprise_max_first(sentence)
  393. # print("list_match", list_match)
  394. doc_id = p_sentence.doc_id
  395. sentence_index = p_sentence.sentence_index
  396. tokens = p_sentence.tokens
  397. list_match.sort(key=lambda x:x["begin_index"])
  398. for _match_index in range(len(list_match)):
  399. _match = list_match[_match_index]
  400. find_flag = False
  401. for p_entity in range_entity:
  402. if p_entity.sentence_index!=p_sentence.sentence_index:
  403. continue
  404. if p_entity.entity_type=="location" and p_entity.entity_text==_match["entity_text"]:
  405. find_flag = True
  406. p_entity.entity_type = "company"
  407. p_entity.if_dict_match = 1
  408. if p_entity.entity_type not in ["location","org","company"]:
  409. continue
  410. if _match["entity_text"] == p_entity.entity_text:
  411. p_entity.if_dict_match = 1
  412. #有重叠
  413. #match部分被包含则不处理
  414. if _match["begin_index"]>=p_entity.wordOffset_begin and _match["end_index"]<=p_entity.wordOffset_end:
  415. find_flag = True
  416. #判断是否是多个公司
  417. for _match_j in range(_match_index,len(list_match)):
  418. if not list_match[_match_j]["end_index"]<=p_entity.wordOffset_end:
  419. _match_j -= 1
  420. break
  421. if _match_j>_match_index:
  422. match_replace = True
  423. match_add = True
  424. begin_index = changeIndexFromWordToWords(tokens,_match["begin_index"])
  425. end_index = changeIndexFromWordToWords(tokens,_match["end_index"]-1)
  426. list_calibrate.append({"type":"update","from":p_entity.entity_text,"to":_match["entity_text"]})
  427. p_entity.entity_text = _match["entity_text"]
  428. p_entity.wordOffset_begin = _match["begin_index"]
  429. p_entity.wordOffset_end = _match["end_index"]
  430. p_entity.begin_index = begin_index
  431. p_entity.end_index = end_index
  432. # 该公司实体是字典识别的
  433. p_entity.if_dict_match = 1
  434. for _match_h in range(_match_index+1,_match_j+1):
  435. entity_text = list_match[_match_h]["entity_text"]
  436. entity_type = "company"
  437. begin_index = changeIndexFromWordToWords(tokens,list_match[_match_h]["begin_index"])
  438. end_index = changeIndexFromWordToWords(tokens,list_match[_match_h]["end_index"]-1)
  439. entity_id = "%s_%d_%d_%d"%(doc_id,sentence_index,begin_index,end_index)
  440. add_entity = Entity(p_sentence.doc_id,entity_id,entity_text,entity_type,sentence_index,begin_index,end_index,list_match[_match_h]["begin_index"],list_match[_match_h]["end_index"],in_attachment=p_sentence.in_attachment)
  441. add_entity.if_dict_match = 1
  442. list_entity.append(add_entity)
  443. range_entity.append(add_entity)
  444. list_calibrate.append({"type":"add","from":"","to":entity_text})
  445. _match_index = _match_j
  446. break
  447. continue
  448. elif _match["begin_index"]<=p_entity.wordOffset_begin and _match["end_index"]>p_entity.wordOffset_begin:
  449. find_flag = True
  450. if _match["begin_index"]<p_entity.wordOffset_begin and _match["end_index"]<=p_entity.wordOffset_end:
  451. if p_entity.entity_type in ("org","company"):
  452. _diff_text = sentence[p_entity.wordOffset_end:_match["end_index"]]
  453. if re.search("分",_diff_text) is not None:
  454. pass
  455. else:
  456. match_replace = True
  457. begin_index = changeIndexFromWordToWords(tokens,_match["begin_index"])
  458. end_index = changeIndexFromWordToWords(tokens,_match["end_index"]-1)
  459. list_calibrate.append({"type":"update","from":p_entity.entity_text,"to":_match["entity_text"]})
  460. p_entity.entity_text = _match["entity_text"]
  461. p_entity.wordOffset_begin = _match["begin_index"]
  462. p_entity.wordOffset_end = _match["end_index"]
  463. p_entity.begin_index = begin_index
  464. p_entity.end_index = end_index
  465. p_entity.if_dict_match = 1
  466. elif _match["end_index"]>=p_entity.wordOffset_end:
  467. # 原entity列表已有实体,则不重复添加
  468. if (_match["entity_text"],_match["begin_index"],_match["end_index"]) not in sentence_entitys:
  469. match_replace = True
  470. begin_index = changeIndexFromWordToWords(tokens,_match["begin_index"])
  471. end_index = changeIndexFromWordToWords(tokens,_match["end_index"]-1)
  472. list_calibrate.append({"type":"update","from":p_entity.entity_text,"to":_match["entity_text"]})
  473. p_entity.entity_text = _match["entity_text"]
  474. p_entity.wordOffset_begin = _match["begin_index"]
  475. p_entity.wordOffset_end = _match["end_index"]
  476. p_entity.begin_index = begin_index
  477. p_entity.end_index = end_index
  478. p_entity.entity_type = "company"
  479. p_entity.if_dict_match = 1
  480. elif _match["begin_index"]<p_entity.wordOffset_end and _match["end_index"]>p_entity.wordOffset_end:
  481. find_flag = True
  482. if p_entity.entity_type in ("org","company"):
  483. match_replace = True
  484. begin_index = changeIndexFromWordToWords(tokens,_match["begin_index"])
  485. end_index = changeIndexFromWordToWords(tokens,_match["end_index"]-1)
  486. list_calibrate.append({"type":"update","from":p_entity.entity_text,"to":_match["entity_text"]})
  487. p_entity.entity_text = _match["entity_text"]
  488. p_entity.wordOffset_begin = _match["begin_index"]
  489. p_entity.wordOffset_end = _match["end_index"]
  490. p_entity.begin_index = begin_index
  491. p_entity.end_index = end_index
  492. p_entity.if_dict_match = 1
  493. if not find_flag:
  494. match_add = True
  495. entity_text = _match["entity_text"]
  496. entity_type = "company"
  497. begin_index = changeIndexFromWordToWords(tokens,_match["begin_index"])
  498. end_index = changeIndexFromWordToWords(tokens,_match["end_index"]-1)
  499. entity_id = "%s_%d_%d_%d"%(doc_id,sentence_index,begin_index,end_index)
  500. add_entity = Entity(p_sentence.doc_id,entity_id,entity_text,entity_type,sentence_index,begin_index,end_index,_match["begin_index"],_match["end_index"],in_attachment=p_sentence.in_attachment)
  501. list_entity.append(add_entity)
  502. range_entity.append(add_entity)
  503. list_calibrate.append({"type":"add","from":"","to":entity_text})
  504. #去重
  505. set_calibrate = set()
  506. list_match_enterprise = []
  507. for _calibrate in list_calibrate:
  508. _from = _calibrate.get("from","")
  509. _to = _calibrate.get("to","")
  510. _key = _from+_to
  511. if _key not in set_calibrate:
  512. list_match_enterprise.append(_calibrate)
  513. set_calibrate.add(_key)
  514. match_enterprise_type = 0
  515. if match_add:
  516. match_enterprise_type += 1
  517. if match_replace:
  518. match_enterprise_type += 2
  519. _article.match_enterprise = list_match_enterprise
  520. _article.match_enterprise_type = match_enterprise_type
  521. def isLegalEnterprise(name):
  522. is_legal = True
  523. if re.search("^[省市区县]",name) is not None or re.search("^\**.{,3}(分(公司|行|支)|街道|中心|办事处|经营部|委员会|有限公司)$",name) or re.search("标段|标包|名称|联系人|联系方式|中标单位|中标人|测试单位|采购单位|采购人|代理人|代理机构|盖章|(主)",name) is not None:
  524. is_legal = False
  525. return is_legal
  526. def fix_LEGAL_ENTERPRISE():
  527. unlegal_enterprise = []
  528. _path = getEnterprisePath()
  529. _sum = 0
  530. set_enter = set()
  531. paths = [_path]
  532. for _p in paths:
  533. with open(_p,"r",encoding="utf8") as f:
  534. while True:
  535. line = f.readline()
  536. if not line:
  537. break
  538. line = line.strip()
  539. if isLegalEnterprise(line):
  540. set_enter.add(line)
  541. if line=="有限责任公司" or line=='设计研究院' or line=='限责任公司' or (re.search("^.{,4}(分公司|支行|分行)$",line) is not None and re.search("电信|移动|联通|建行|工行|农行|中行|交行",line) is None):
  542. print(line)
  543. if line in set_enter:
  544. set_enter.remove(line)
  545. with open("enter.txt","w",encoding="utf8") as fwrite:
  546. for line in list(set_enter):
  547. fwrite.write(line.replace("(","(").replace(")",")"))
  548. fwrite.write("\n")
  549. # if re.search("标段|地址|标包|名称",line) is not None:#\(|\)||
  550. # _count += 1
  551. # print("=",line)
  552. # print("%d/%d"%(_count,_sum))
  553. # a_list = []
  554. # with open("电信分公司.txt","r",encoding="utf8") as f:
  555. # while True:
  556. # _line = f.readline()
  557. # if not _line:
  558. # break
  559. # if _line.strip()!="":
  560. # a_list.append(_line.strip())
  561. # with open("enter.txt","a",encoding="utf8") as f:
  562. # for _line in a_list:
  563. # f.write(_line)
  564. # f.write("\n")
  565. if __name__=="__main__":
  566. # edit_distance("GUMBO","GAMBOL")
  567. # print(jaccard_score("周口经济开发区陈营运粮河两岸拆迁工地土工布覆盖项目竞争性谈判公告","周口经济开发区陈营运粮河两岸拆迁工地土工布覆盖项目-成交公告"))
  568. #
  569. # sentences = "广州比地数据科技有限公司比地数据科技有限公司1111111123沈阳南光工贸有限公司"
  570. # print(match_enterprise_max_first(sentences))
  571. #
  572. # print("takes %d s"%(time.time()-_time))
  573. # fix_LEGAL_ENTERPRISE()
  574. # print(jaccard_score("吉林省九台","吉林省建苑设计集团有限公司"))
  575. print(match_enterprise_max_first("中国南方航空股份有限公司黑龙江分公司"))