buyer.py 13 KB

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