buyer.py 12 KB

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