winner.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. # coding:utf-8
  2. from tables.ai import org_ner
  3. from tables import clear_spacing
  4. from html_table_extractor.extractor import Extractor
  5. from tables import CatchContentObject, fsc
  6. from util.sensitive_word import AcAutomation
  7. from tables import match_company_index, key_value_header
  8. from tables import tfc_object
  9. from docs.config import ai2config, abnormal_config
  10. from docs.config import DEBUG
  11. import csv
  12. import re
  13. pattern = r',|。|\?|!|;'
  14. class WinnerChecker(object):
  15. """
  16. 中标字段检查
  17. """
  18. def __init__(self):
  19. self.errors_tables = {
  20. # "0101": {
  21. # "name": "实体识别",
  22. # "parent_name": "名称错误",
  23. # "parent_code": "01",
  24. # "checkFn": self.check0101
  25. # },
  26. # "0201": {
  27. # "name": "看数据的标签是不是之中标单位",
  28. # "parent_name": "数据标签错误",
  29. # "parent_code": "02",
  30. # "checkFn": self.check0201
  31. # },
  32. "0103": {
  33. "name": "包含叠词,异常词汇,特殊词汇",
  34. "parent_name": "名称错误",
  35. "parent_code": "01",
  36. "checkFn": self.check0103
  37. }
  38. }
  39. #
  40. self.winner_ac = AcAutomation()
  41. with open(ai2config["table_field_config"]["corpus_path"], "r") as f:
  42. reads = csv.reader(f)
  43. [self.winner_ac.add_word(d[0].strip()) for d in reads if "中标单位" in d[1] and d[0].strip()]
  44. def intention_check(self, header):
  45. """
  46. 意图结果检测
  47. :param header:
  48. :return:
  49. """
  50. if header in self.winner_ac.catch:
  51. if DEBUG:
  52. print(f"中标单位意图:::>> **{header}**==> [中标单位]")
  53. return True
  54. tags = tfc_object.predict([header])
  55. if tags:
  56. if "中标单位" in tags[0]:
  57. if DEBUG:
  58. print(f"中标单位意图:::>> **{header}**==> {tags}")
  59. return True
  60. def winner_intention_table(self, tables: list, companies):
  61. """
  62. 表格意图检测
  63. :param tables:
  64. :param companies:
  65. :return:
  66. """
  67. for row_ind, row in enumerate(tables):
  68. for col_ind, column in enumerate(row):
  69. extract_companies = re.findall("|".join(companies), column)
  70. if extract_companies:
  71. if col_ind > 0:
  72. status = self.intention_check(row[col_ind - 1])
  73. if status:
  74. for company in companies:
  75. companies.remove(company)
  76. if not companies:
  77. return False
  78. if row_ind > 0 and len(tables[row_ind - 1]) > col_ind:
  79. status = self.intention_check(tables[row_ind - 1][col_ind])
  80. if status:
  81. for company in companies:
  82. companies.remove(company)
  83. if not companies:
  84. return False
  85. if self.winner_ac.search(column):
  86. companies = self.winner_intention_content(companies, column)
  87. if not companies:
  88. return False
  89. return companies
  90. def winner_intention_content(self, companies, column):
  91. """
  92. 文本意图检测
  93. :param companies:
  94. :param column:
  95. :return:
  96. """
  97. # 公司名称的下标
  98. indexes = match_company_index(companies, column)
  99. if not indexes:
  100. return companies
  101. # 实体提取的head字段
  102. start_ind = 0
  103. for r in indexes:
  104. start, end, company_name = r
  105. if company_name not in companies:
  106. start_ind = end
  107. continue
  108. start_ind = start_ind if start_ind > start - 10 else start - 10
  109. text_ = column[start_ind:end + 10]
  110. start_ind = end
  111. head = key_value_header(text_)
  112. for val, ind in head:
  113. if self.intention_check(val):
  114. if company_name in companies:
  115. companies.pop(company_name)
  116. if not companies:
  117. return False
  118. return companies
  119. @staticmethod
  120. def check_company_name(contents: list, companies: list):
  121. """
  122. 公司名称检测
  123. :param contents:正文段落分割后
  124. :param companies: 公司list
  125. :return:返回False结束流程,list继续流程
  126. """
  127. new_content_list = []
  128. # 合并文本
  129. for ind, con in enumerate(contents):
  130. if "<table" in con:
  131. table = Extractor(con).parse()
  132. _tables = table.return_list()
  133. _tables = clear_spacing(_tables)
  134. for text in _tables:
  135. new_content_list.extend(text)
  136. else:
  137. # 一段文本
  138. new_content_list.append(con.replace(" ", ""))
  139. # 开始判断公司名称
  140. for text in new_content_list:
  141. p = r"|".join(companies)
  142. repatten = p.replace(")", "\)").replace("(", "\(").replace(".", "\.")
  143. s = re.split(pattern, text)
  144. for t in s:
  145. if re.search(repatten, t):
  146. al_result = org_ner(t)
  147. for company in al_result:
  148. if company in companies:
  149. if DEBUG:
  150. print(f"中标单位实体识别:::>> **{text}**==> {company}")
  151. companies.remove(company)
  152. if not companies:
  153. return False
  154. return companies
  155. def check_intention(self, contents: list, companies: list):
  156. """
  157. 意图检测
  158. :param contents:正文段落分割后
  159. :param companies: 公司list
  160. :return:返回False结束流程,list继续流程
  161. """
  162. for ind, content in enumerate(contents):
  163. if "<table" in content:
  164. # 表格处理
  165. table = Extractor(content).parse()
  166. _tables = table.return_list()
  167. _tables = clear_spacing(_tables)
  168. _table_str = str(_tables)
  169. if re.search("|".join(companies), _table_str):
  170. companies = self.winner_intention_table(_tables, companies)
  171. if not companies:
  172. return False
  173. continue
  174. # 非表格处理
  175. companies = self.winner_intention_content(companies, content)
  176. if not companies:
  177. return False
  178. return companies
  179. def check0101(self, winner: str, detail: str, attach_text: dict, catch_content: CatchContentObject) -> bool:
  180. """
  181. 公司名称检测
  182. :param winner:中标单位,多个逗号分割
  183. :param detail: 公告
  184. :param attach_text: 附件解析结果
  185. :param catch_content: 单挑数据缓存
  186. :return:返回true 代表异常
  187. """
  188. companies = [company for company in winner.split(",") if company]
  189. contents = catch_content.public_attachment_catch(detail, platform="html", document_id="公告")
  190. companies = self.check_company_name(contents, companies)
  191. if not companies:
  192. return False
  193. for attach_index, attach_content in attach_text.items():
  194. if attach_content:
  195. for topic_index, topic_detail in attach_content.items():
  196. # oss地址
  197. attach_url = topic_detail.get("attach_url", "")
  198. if attach_url:
  199. # 获取附件内容
  200. st, content = fsc.download_text_content(attach_url)
  201. # 下载成功
  202. # 超长文本不处理,暂定30万字
  203. if st and content.strip():
  204. if len(content) > 300000:
  205. continue
  206. # 开始检测
  207. contents = catch_content.public_attachment_catch(content, platform="attach",
  208. document_id=attach_url)
  209. companies = self.check_company_name(contents, companies)
  210. if not companies:
  211. return False
  212. return True
  213. def check0201(self, winner: str, detail: str, attach_text: dict, catch_content: CatchContentObject) -> bool:
  214. """
  215. 公司名称检测
  216. :param winner:中标单位,多个逗号分割
  217. :param detail: 公告
  218. :param attach_text: 附件解析结果
  219. :param catch_content: 单挑数据缓存
  220. :return:返回true 代表异常
  221. """
  222. companies = [company for company in winner.split(",") if company] # 多中标人
  223. # 公告意图检测
  224. contents = catch_content.public_attachment_catch(detail, platform="html", document_id="公告")
  225. companies = self.check_intention(contents, companies)
  226. if not companies:
  227. return False
  228. # 附件意图检测
  229. for attach_index, attach_content in attach_text.items():
  230. if attach_content:
  231. for topic_index, topic_detail in attach_content.items():
  232. # oss地址
  233. attach_url = topic_detail.get("attach_url", "")
  234. if attach_url:
  235. # 获取附件内容
  236. st, content = fsc.download_text_content(attach_url)
  237. # 下载成功
  238. # 超长文本不处理,暂定30万字
  239. if st and content.strip():
  240. if len(content) > 300000:
  241. continue
  242. # 开始检测
  243. contents = catch_content.public_attachment_catch(content, platform="attach",
  244. document_id=attach_url)
  245. companies = self.check_intention(contents, companies)
  246. if not companies:
  247. return False
  248. return True
  249. def check0103(self,s_winner:str):
  250. #中标单位名称以异常词开始
  251. with open(abnormal_config["table_field_config"]["path1"], "r") as f:
  252. reads = csv.reader(f)
  253. for n in reads:
  254. p1 = re.compile("^"+n[0])
  255. if p1.match(s_winner):
  256. return True
  257. # 中标单位名称包含异常词
  258. with open(abnormal_config["table_field_config"]["path2"], "r") as f:
  259. reads = csv.reader(f)
  260. for n in reads:
  261. if n[0] in s_winner:
  262. return True
  263. # 中标单位名称以异常词结尾
  264. with open(abnormal_config["table_field_config"]["path3"], "r") as f:
  265. reads = csv.reader(f)
  266. for w in reads:
  267. if re.search(f"{w[0]}$", s_winner):
  268. return True
  269. return False