|
@@ -0,0 +1,651 @@
|
|
|
|
+#coding:utf8
|
|
|
|
+
|
|
|
|
+import re
|
|
|
|
+
|
|
|
|
+import logging
|
|
|
|
+logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+from bs4 import BeautifulSoup
|
|
|
|
+import copy
|
|
|
|
+
|
|
|
|
+from BaseDataMaintenance.maintenance.product.productUtils import *
|
|
|
|
+
|
|
|
|
+def getTrs(tbody):
|
|
|
|
+ #获取所有的tr
|
|
|
|
+ trs = []
|
|
|
|
+ if tbody.name=="table":
|
|
|
|
+ body = tbody.find("tbody",recursive=False)
|
|
|
|
+ if body is not None:
|
|
|
|
+ tbody = body
|
|
|
|
+ objs = tbody.find_all(recursive=False)
|
|
|
|
+ for obj in objs:
|
|
|
|
+ if obj.name=="tr":
|
|
|
|
+ trs.append(obj)
|
|
|
|
+ if obj.name=="tbody" or obj.name=="table":
|
|
|
|
+ for tr in obj.find_all("tr",recursive=False):
|
|
|
|
+ trs.append(tr)
|
|
|
|
+ return trs
|
|
|
|
+
|
|
|
|
+def fixSpan(tbody):
|
|
|
|
+ # 处理colspan, rowspan信息补全问题
|
|
|
|
+ #trs = tbody.findChildren('tr', recursive=False)
|
|
|
|
+
|
|
|
|
+ trs = getTrs(tbody)
|
|
|
|
+ ths_len = 0
|
|
|
|
+ ths = list()
|
|
|
|
+ trs_set = set()
|
|
|
|
+ #修改为先进行列补全再进行行补全,否则可能会出现表格解析混乱
|
|
|
|
+ # 遍历每一个tr
|
|
|
|
+
|
|
|
|
+ for indtr, tr in enumerate(trs):
|
|
|
|
+ ths_tmp = tr.findChildren('th', recursive=False)
|
|
|
|
+ #不补全含有表格的tr
|
|
|
|
+ if len(tr.findChildren('table'))>0:
|
|
|
|
+ continue
|
|
|
|
+ if len(ths_tmp) > 0:
|
|
|
|
+ ths_len = ths_len + len(ths_tmp)
|
|
|
|
+ for th in ths_tmp:
|
|
|
|
+ ths.append(th)
|
|
|
|
+ trs_set.add(tr)
|
|
|
|
+ # 遍历每行中的element
|
|
|
|
+ tds = tr.findChildren(recursive=False)
|
|
|
|
+ for indtd, td in enumerate(tds):
|
|
|
|
+ # 若有colspan 则补全同一行下一个位置
|
|
|
|
+ if 'colspan' in td.attrs:
|
|
|
|
+ if str(re.sub("[^0-9]","",str(td['colspan'])))!="":
|
|
|
|
+ col = int(re.sub("[^0-9]","",str(td['colspan'])))
|
|
|
|
+ if col<100 and len(td.get_text())<1000:
|
|
|
|
+ td['colspan'] = 1
|
|
|
|
+ for i in range(1, col, 1):
|
|
|
|
+ td.insert_after(copy.copy(td))
|
|
|
|
+
|
|
|
|
+ for indtr, tr in enumerate(trs):
|
|
|
|
+ ths_tmp = tr.findChildren('th', recursive=False)
|
|
|
|
+ #不补全含有表格的tr
|
|
|
|
+ if len(tr.findChildren('table'))>0:
|
|
|
|
+ continue
|
|
|
|
+ if len(ths_tmp) > 0:
|
|
|
|
+ ths_len = ths_len + len(ths_tmp)
|
|
|
|
+ for th in ths_tmp:
|
|
|
|
+ ths.append(th)
|
|
|
|
+ trs_set.add(tr)
|
|
|
|
+ # 遍历每行中的element
|
|
|
|
+ tds = tr.findChildren(recursive=False)
|
|
|
|
+ for indtd, td in enumerate(tds):
|
|
|
|
+ # 若有rowspan 则补全下一行同样位置
|
|
|
|
+ if 'rowspan' in td.attrs:
|
|
|
|
+ if str(re.sub("[^0-9]","",str(td['rowspan'])))!="":
|
|
|
|
+ row = int(re.sub("[^0-9]","",str(td['rowspan'])))
|
|
|
|
+ td['rowspan'] = 1
|
|
|
|
+ for i in range(1, row, 1):
|
|
|
|
+ # 获取下一行的所有td, 在对应的位置插入
|
|
|
|
+ if indtr+i<len(trs):
|
|
|
|
+ tds1 = trs[indtr + i].findChildren(['td','th'], recursive=False)
|
|
|
|
+ if len(tds1) >= (indtd) and len(tds1)>0:
|
|
|
|
+ if indtd > 0:
|
|
|
|
+ tds1[indtd - 1].insert_after(copy.copy(td))
|
|
|
|
+ else:
|
|
|
|
+ tds1[0].insert_before(copy.copy(td))
|
|
|
|
+ elif indtd-2>0 and len(tds1) > 0 and len(tds1) == indtd - 1: # 修正某些表格最后一列没补全
|
|
|
|
+ tds1[indtd-2].insert_after(copy.copy(td))
|
|
|
|
+def getTable(tbody):
|
|
|
|
+ #trs = tbody.findChildren('tr', recursive=False)
|
|
|
|
+ fixSpan(tbody)
|
|
|
|
+ trs = getTrs(tbody)
|
|
|
|
+ inner_table = []
|
|
|
|
+ for tr in trs:
|
|
|
|
+ tr_line = []
|
|
|
|
+ tds = tr.findChildren(['td','th'], recursive=False)
|
|
|
|
+ if len(tds)==0:
|
|
|
|
+ tr_line.append([re.sub('\xa0','',tr.get_text()),0]) # 2021/12/21 修复部分表格没有td 造成数据丢失
|
|
|
|
+ for td in tds:
|
|
|
|
+ tr_line.append([re.sub('\xa0','',td.get_text()),0])
|
|
|
|
+ #tr_line.append([td.get_text(),0])
|
|
|
|
+ inner_table.append(tr_line)
|
|
|
|
+ return inner_table
|
|
|
|
+
|
|
|
|
+class ParseDocument():
|
|
|
|
+
|
|
|
|
+ def __init__(self,_html,auto_merge_table=True):
|
|
|
|
+ if _html is None:
|
|
|
|
+ _html = ""
|
|
|
|
+ self.html = _html
|
|
|
|
+
|
|
|
|
+ # self.soup = BeautifulSoup(self.html,"lxml")
|
|
|
|
+ # self.soup = BeautifulSoup(self.html,"html.parser")
|
|
|
|
+
|
|
|
|
+ self.soup = BeautifulSoup(self.html,"html5lib")
|
|
|
|
+ _body = self.soup.find("body")
|
|
|
|
+ if _body is not None:
|
|
|
|
+ self.soup = _body
|
|
|
|
+ list_obj = self.soup.find_all(recursive=False)
|
|
|
|
+
|
|
|
|
+ for obj in list_obj:
|
|
|
|
+ print("obj",obj.get_text()[:20])
|
|
|
|
+
|
|
|
|
+ self.tree = self.buildParsetree(list_obj,auto_merge_table)
|
|
|
|
+
|
|
|
|
+ # #识别目录树
|
|
|
|
+ # for _page in self.childs:
|
|
|
|
+ # print("%d============"%_page.page_no)
|
|
|
|
+ # for _sentence in _page.childs:
|
|
|
|
+ # print(_sentence)
|
|
|
|
+ # print("%d================"%_page.page_no)
|
|
|
|
+ #
|
|
|
|
+ # if self.parseTree:
|
|
|
|
+ # self.parseTree.printParseTree()
|
|
|
|
+
|
|
|
|
+ def print_tree(self,tree,append=""):
|
|
|
|
+ if append=="":
|
|
|
|
+ self.set_tree_id = set()
|
|
|
|
+ for t in tree:
|
|
|
|
+ _id = id(t)
|
|
|
|
+ if _id in self.set_tree_id:
|
|
|
|
+ continue
|
|
|
|
+ self.set_tree_id.add(_id)
|
|
|
|
+ print(append,t["text"][:20])
|
|
|
|
+ childs = t["child_title"]
|
|
|
|
+ self.print_tree(childs,append=append+" ")
|
|
|
|
+
|
|
|
|
+ def is_title_first(self,title):
|
|
|
|
+ if title in ("一","1","Ⅰ","a","A"):
|
|
|
|
+ return True
|
|
|
|
+ return False
|
|
|
|
+
|
|
|
|
+ def find_title_by_pattern(self,_text,_pattern="(^|★|:|:|\s+)(?P<title_1>(?P<title_1_index_0_0>第?)(?P<title_1_index_1_1>[一二三四五六七八九十ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ]+)(?P<title_1_index_2_0>[、章册部\.::]))|" \
|
|
|
|
+ "([\s★\*]*)(?P<title_3>(?P<title_3_index_0_0>.{,3}?)(?P<title_3_index_0_1>[ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ]+)(?P<title_3_index_0_2>))|" \
|
|
|
|
+ "([\s★\*]*)(?P<title_4>(?P<title_4_index_0_0>.{,3}?第?)(?P<title_4_index_1_1>[一二三四五六七八九十]+)(?P<title_4_index_2_0>[节章册部\.::、、]))|" \
|
|
|
|
+ "([\s★\*]*)(?P<title_11>(?P<title_11_index_0_0>.{,3}?\d{1,2}[\..、\s\-]\d{1,2}[\..、\s\-]\d{1,2}[\..、\s\-])(?P<title_11_index_1_1>\d{1,2})(?P<title_11_index_2_0>[\..、\s\-]?))|" \
|
|
|
|
+ "([\s★\*]*)(?P<title_10>(?P<title_10_index_0_0>.{,3}?\d{1,2}[\..、\s\-]\d{1,2}[\..、\s\-])(?P<title_10_index_1_1>\d{1,2})(?P<title_10_index_2_0>[\..、\s\-]?))|" \
|
|
|
|
+ "([\s★\*]*)(?P<title_7>(?P<title_7_index_0_0>.{,3}?\d{1,2}[\..、\s\-])(?P<title_7_index_1_1>\d{1,2})(?P<title_7_index_2_0>[\..、\s\-]?))|" \
|
|
|
|
+ "([\s★\*]*)(?P<title_6>(?P<title_6_index_0_0>.{,3}?包?)(?P<title_6_index_0_1>\d{1,2})(?P<title_6_index_2_0>[\..、\s\-]?))|" \
|
|
|
|
+ "([\s★\*]*)(?P<title_15>(?P<title_15_index_0_0>.{,3}?(?)(?P<title_15_index_1_1>\d{1,2})(?P<title_15_index_2_0>)))|" \
|
|
|
|
+ "([\s★\*]*)(?P<title_17>(?P<title_17_index_0_0>.{,3}?(?)(?P<title_17_index_1_1>[a-wA-W]+)(?P<title_17_index_2_0>)))|" \
|
|
|
|
+ "([\s★\*]*)(?P<title_19>(?P<title_19_index_0_0>.{,3}?(?)(?P<title_19_index_1_1>[一二三四五六七八九十ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ]+)(?P<title_19_index_2_0>)))" \
|
|
|
|
+ ):
|
|
|
|
+ _se = re.search(_pattern,_text)
|
|
|
|
+ groups = []
|
|
|
|
+ if _se is not None:
|
|
|
|
+ _gd = _se.groupdict()
|
|
|
|
+ for k,v in _gd.items():
|
|
|
|
+ if v is not None:
|
|
|
|
+ groups.append((k,v))
|
|
|
|
+ if len(groups):
|
|
|
|
+ groups.sort(key=lambda x:x[0])
|
|
|
|
+ return groups
|
|
|
|
+ return None
|
|
|
|
+
|
|
|
|
+ def make_increase(self,_sort,_title,_add=1):
|
|
|
|
+ if len(_title)==0 and _add==0:
|
|
|
|
+ return ""
|
|
|
|
+ if len(_title)==0 and _add==1:
|
|
|
|
+ return _sort[0]
|
|
|
|
+ _index = _sort.index(_title[-1])
|
|
|
|
+ next_index = (_index+_add)%len(_sort)
|
|
|
|
+ next_chr = _sort[next_index]
|
|
|
|
+ if _index==len(_sort)-1:
|
|
|
|
+ _add = 1
|
|
|
|
+ else:
|
|
|
|
+ _add = 0
|
|
|
|
+ return next_chr+self.make_increase(_sort,_title[:-1],_add)
|
|
|
|
+
|
|
|
|
+ def get_next_title(self,_title):
|
|
|
|
+ if re.search("^\d+$",_title) is not None:
|
|
|
|
+ return str(int(_title)+1)
|
|
|
|
+ if re.search("^[一二三四五六七八九十百]+$",_title) is not None:
|
|
|
|
+ _next_title = self.make_increase(['一','二','三','四','五','六','七','八','九','十'],re.sub("[十百]",'',_title))
|
|
|
|
+ _next_title = list(_next_title)
|
|
|
|
+ _next_title.reverse()
|
|
|
|
+ if _next_title[-1]!="十":
|
|
|
|
+ if len(_next_title)>=2:
|
|
|
|
+ _next_title.insert(-1,'十')
|
|
|
|
+ if len(_next_title)>=4:
|
|
|
|
+ _next_title.insert(-3,'百')
|
|
|
|
+ if _title[0]=="十":
|
|
|
|
+ if _next_title=="十":
|
|
|
|
+ _next_title = ["二","十"]
|
|
|
|
+ _next_title.insert(0,"十")
|
|
|
|
+ _next_title = "".join(_next_title)
|
|
|
|
+ return _next_title
|
|
|
|
+ if re.search("^[a-z]+$",_title) is not None:
|
|
|
|
+ _next_title = self.make_increase([chr(i+ord('a')) for i in range(26)],_title)
|
|
|
|
+ _next_title = list(_next_title)
|
|
|
|
+ _next_title.reverse()
|
|
|
|
+ return "".join(_next_title)
|
|
|
|
+ if re.search("^[A-Z]+$",_title) is not None:
|
|
|
|
+ _next_title = self.make_increase([chr(i+ord('A')) for i in range(26)],_title)
|
|
|
|
+ _next_title = list(_next_title)
|
|
|
|
+ _next_title.reverse()
|
|
|
|
+ return "".join(_next_title)
|
|
|
|
+ if re.search("^[ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ]$",_title) is not None:
|
|
|
|
+ _sort = ["Ⅰ","Ⅱ","Ⅲ","Ⅳ","Ⅴ","Ⅵ","Ⅶ","Ⅷ","Ⅸ","Ⅹ","Ⅺ","Ⅻ"]
|
|
|
|
+ _index = _sort.index(_title)
|
|
|
|
+ if _index<len(_sort)-1:
|
|
|
|
+ return _sort[_index+1]
|
|
|
|
+ return None
|
|
|
|
+
|
|
|
|
+ def buildParsetree(self,list_obj,auto_merge_table=True):
|
|
|
|
+
|
|
|
|
+ self.parseTree = None
|
|
|
|
+ trees = []
|
|
|
|
+ list_length = []
|
|
|
|
+ for obj in list_obj[:200]:
|
|
|
|
+ if obj.name!="table":
|
|
|
|
+ list_length.append(len(obj.get_text()))
|
|
|
|
+ if len(list_length)>0:
|
|
|
|
+ max_length = max(list_length)
|
|
|
|
+ else:
|
|
|
|
+ max_length = 40
|
|
|
|
+
|
|
|
|
+ print("max_length",max_length)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ list_data = []
|
|
|
|
+ last_table_index = None
|
|
|
|
+ last_table_columns = None
|
|
|
|
+ last_table = None
|
|
|
|
+ for obj_i in range(len(list_obj)):
|
|
|
|
+ obj = list_obj[obj_i]
|
|
|
|
+ _type = "sentence"
|
|
|
|
+ _text = obj.text
|
|
|
|
+ if obj.name=="table":
|
|
|
|
+ _type = "table"
|
|
|
|
+ _text = str(obj)
|
|
|
|
+ _append = False
|
|
|
|
+ sentence_title = None
|
|
|
|
+ sentence_title_text = None
|
|
|
|
+ sentence_groups = None
|
|
|
|
+ title_index = None
|
|
|
|
+ next_index = None
|
|
|
|
+ parent_title = None
|
|
|
|
+ title_before = None
|
|
|
|
+ title_after = None
|
|
|
|
+ title_next = None
|
|
|
|
+ childs = []
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ list_table = None
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if _type=="sentence":
|
|
|
|
+ sentence_groups = self.find_title_by_pattern(_text[:10])
|
|
|
|
+ if sentence_groups:
|
|
|
|
+ # c062f53cf83401e671822003d63c1828print("sentence_groups",sentence_groups)
|
|
|
|
+ sentence_title = sentence_groups[0][0]
|
|
|
|
+ sentence_title_text = sentence_groups[0][1]
|
|
|
|
+ title_index = sentence_groups[-2][1]
|
|
|
|
+ title_before = sentence_groups[1][1]
|
|
|
|
+ title_after = sentence_groups[-1][1]
|
|
|
|
+ next_index = self.get_next_title(title_index)
|
|
|
|
+
|
|
|
|
+ if _type=="sentence":
|
|
|
|
+ if sentence_title is None and len(list_data)>0 and list_data[-1]["sentence_title"] is not None and list_data[-1]["line_width"]>=max_length*0.6:
|
|
|
|
+ list_data[-1]["text"] += _text
|
|
|
|
+ list_data[-1]["line_width"] = len(_text)
|
|
|
|
+ _append = True
|
|
|
|
+ elif sentence_title is None and len(list_data)>0 and _type==list_data[-1]["type"]:
|
|
|
|
+ if list_data[-1]["line_width"]>=max_length*0.7:
|
|
|
|
+ list_data[-1]["text"] += _text
|
|
|
|
+ list_data[-1]["line_width"] = len(_text)
|
|
|
|
+ _append = True
|
|
|
|
+
|
|
|
|
+ if _type=="table":
|
|
|
|
+ _soup = BeautifulSoup(_text,"lxml")
|
|
|
|
+ _table = _soup.find("table")
|
|
|
|
+ if _table is not None:
|
|
|
|
+ list_table = getTable(_table)
|
|
|
|
+ table_columns = len(list_table[0])
|
|
|
|
+
|
|
|
|
+ if auto_merge_table:
|
|
|
|
+ if last_table_index is not None and abs(obj_i-last_table_index)<=1 and last_table_columns is not None and last_table_columns==table_columns:
|
|
|
|
+ if last_table is not None:
|
|
|
|
+ trs = getTrs(_table)
|
|
|
|
+ last_tbody = BeautifulSoup(last_table["text"],"lxml")
|
|
|
|
+ _table = last_tbody.find("table")
|
|
|
|
+ last_trs = getTrs(_table)
|
|
|
|
+ _append = True
|
|
|
|
+
|
|
|
|
+ for _line in list_table:
|
|
|
|
+ last_table["list_table"].append(_line)
|
|
|
|
+ if len(last_trs)>0:
|
|
|
|
+ for _tr in trs:
|
|
|
|
+ last_trs[-1].insert_after(copy.copy(_tr))
|
|
|
|
+ last_table["text"] = re.sub("</?html>|</?body>","",str(last_tbody))
|
|
|
|
+
|
|
|
|
+ last_table_index = obj_i
|
|
|
|
+ last_table_columns = len(list_table[-1])
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if not _append:
|
|
|
|
+ _data = {"type":_type, "text":_text,"list_table":list_table,"line_width":len(_text),"sentence_title":sentence_title,"title_index":title_index,
|
|
|
|
+ "sentence_title_text":sentence_title_text,"sentence_groups":sentence_groups,"parent_title":parent_title,
|
|
|
|
+ "child_title":childs,"title_before":title_before,"title_after":title_after,"title_next":title_next,"next_index":next_index}
|
|
|
|
+
|
|
|
|
+ if _type=="table":
|
|
|
|
+ last_table = _data
|
|
|
|
+ last_table_index = obj_i
|
|
|
|
+ if list_table:
|
|
|
|
+ last_table_columns = last_table_columns = len(list_table[-1])
|
|
|
|
+
|
|
|
|
+ if sentence_title is not None:
|
|
|
|
+ if len(list_data)>0:
|
|
|
|
+ if self.is_title_first(title_index):
|
|
|
|
+ for i in range(1,len(list_data)+1):
|
|
|
|
+ _d = list_data[-i]
|
|
|
|
+ if _d["sentence_title"] is not None:
|
|
|
|
+ _data["parent_title"] = _d
|
|
|
|
+ _d["child_title"].append(_data)
|
|
|
|
+ break
|
|
|
|
+ else:
|
|
|
|
+ _find = False
|
|
|
|
+ for i in range(1,len(list_data)+1):
|
|
|
|
+ _d = list_data[-i]
|
|
|
|
+ if i==1 and _d.get("sentence_title")==sentence_title and title_before==_d["title_before"] and title_after==_d["title_after"]:
|
|
|
|
+ _data["parent_title"] = _d["parent_title"]
|
|
|
|
+ _d["title_next"] = _data
|
|
|
|
+ if _d["parent_title"] is not None:
|
|
|
|
+ _d["parent_title"]["child_title"].append(_data)
|
|
|
|
+ _find = True
|
|
|
|
+ break
|
|
|
|
+ if _d.get("sentence_title")==sentence_title and title_before==_d["title_before"] and title_after==_d["title_after"]:
|
|
|
|
+ if _d["next_index"]==title_index and _d["title_next"] is None:
|
|
|
|
+ _data["parent_title"] = _d["parent_title"]
|
|
|
|
+ _d["title_next"] = _data
|
|
|
|
+ if _d["parent_title"] is not None:
|
|
|
|
+ _d["parent_title"]["child_title"].append(_data)
|
|
|
|
+ _find = True
|
|
|
|
+ break
|
|
|
|
+ if not _find:
|
|
|
|
+ if len(list_data)>0:
|
|
|
|
+ for i in range(1,len(list_data)+1):
|
|
|
|
+ _d = list_data[-i]
|
|
|
|
+ if _d.get("sentence_title") is not None:
|
|
|
|
+ _data["parent_title"] = _d
|
|
|
|
+ _d["child_title"].append(_data)
|
|
|
|
+ break
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ else:
|
|
|
|
+ if len(list_data)>0:
|
|
|
|
+ for i in range(1,len(list_data)+1):
|
|
|
|
+ _d = list_data[-i]
|
|
|
|
+ if _d.get("sentence_title") is not None:
|
|
|
|
+ _data["parent_title"] = _d
|
|
|
|
+ _d["child_title"].append(_data)
|
|
|
|
+ break
|
|
|
|
+
|
|
|
|
+ list_data.append(_data)
|
|
|
|
+
|
|
|
|
+ return list_data
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def extract_products(list_data,_product,_param_pattern = "产品名称|采购内存|标的名称|采购内容|(标的|维修|系统|报价构成|商品|产品|物料|物资|货物|设备|采购品|采购条目|物品|材料|印刷品?|采购|物装|配件|资产|耗材|清单|器材|仪器|器械|备件|拍卖物|标的物|物件|药品|药材|药械|货品|食品|食材|品目|^品名|气体|标项|分项|项目|计划|包组|标段|[分子]?包|子目|服务|招标|中标|成交|工程|招标内容)[\))的]?([、\w]{,4}名称|内容|描述)|标的|标项|项目$|商品|产品|物料|物资|货物|设备|采购品|采购条目|物品|材料|印刷品|物装|配件|资产|招标内容|耗材|清单|器材|仪器|器械|备件|拍卖物|标的物|物件|药品|药材|药械|货品|食品|食材|菜名|^品目$|^品名$|^名称|^内容$"):
|
|
|
|
+ list_result = []
|
|
|
|
+ for _data_i in range(len(list_data)):
|
|
|
|
+ _data = list_data[_data_i]
|
|
|
|
+ _type = _data["type"]
|
|
|
|
+ _text = _data["text"]
|
|
|
|
+ table_products = []
|
|
|
|
+ if _type=="table":
|
|
|
|
+ list_table = _data["list_table"]
|
|
|
|
+ if list_table is None:
|
|
|
|
+ continue
|
|
|
|
+ _check = True
|
|
|
|
+ max_length = max([len(a) for a in list_table])
|
|
|
|
+ min_length = min([len(a) for a in list_table])
|
|
|
|
+ if min_length<max_length/2:
|
|
|
|
+ continue
|
|
|
|
+ list_head_index = []
|
|
|
|
+ _begin_index = 0
|
|
|
|
+ head_cell_text = ""
|
|
|
|
+ for line_i in range(len(list_table[:2])):
|
|
|
|
+ line = list_table[line_i]
|
|
|
|
+ line_text = ",".join([cell[0] for cell in line])
|
|
|
|
+ for cell_i in range(len(line)):
|
|
|
|
+ cell = line[cell_i]
|
|
|
|
+ cell_text = cell[0]
|
|
|
|
+ if len(cell_text)<10 and re.search(_param_pattern,cell_text) is not None and re.search("单价|数量|总价|规格|品牌|型号|用途|要求|采购量",line_text) is not None:
|
|
|
|
+ _begin_index = line_i+1
|
|
|
|
+
|
|
|
|
+ list_head_index.append(cell_i)
|
|
|
|
+ for line_i in range(len(list_table)):
|
|
|
|
+ line = list_table[line_i]
|
|
|
|
+ for cell_i in list_head_index:
|
|
|
|
+ cell = line[cell_i]
|
|
|
|
+ cell_text = cell[0]
|
|
|
|
+ head_cell_text += cell_text
|
|
|
|
+
|
|
|
|
+ # print("===head_cell_text",head_cell_text)
|
|
|
|
+ if re.search("招标人|采购人|项目编号|项目名称|金额|^\d+$",head_cell_text) is not None:
|
|
|
|
+ list_head_index = []
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ for line in list_table:
|
|
|
|
+ line_text = ",".join([cell[0] for cell in line])
|
|
|
|
+ for cell_i in range(len(line)):
|
|
|
|
+ cell = line[cell_i]
|
|
|
|
+ cell_text = cell[0]
|
|
|
|
+ if cell_text is not None and _product is not None and len(cell_text)<len(_product)*10 and re.search(_product,cell_text) is not None and re.search("单价|数量|总价|规格|品牌|型号|用途|要求|采购量",line_text) is not None:
|
|
|
|
+ list_head_index.append(cell_i)
|
|
|
|
+
|
|
|
|
+ list_head_index = list(set(list_head_index))
|
|
|
|
+ if len(list_head_index)>0:
|
|
|
|
+ for line_i in range(_begin_index,len(list_table)):
|
|
|
|
+ line = list_table[line_i]
|
|
|
|
+ for cell_i in list_head_index:
|
|
|
|
+ if cell_i>=len(line):
|
|
|
|
+ continue
|
|
|
|
+ cell = line[cell_i]
|
|
|
|
+ _text = cell[0]
|
|
|
|
+ if re.search(_param_pattern,_text) is None:
|
|
|
|
+ table_products.append(_text)
|
|
|
|
+ if len(table_products)>0:
|
|
|
|
+ if min([len(x) for x in table_products])>0:
|
|
|
|
+ list_result.extend(table_products)
|
|
|
|
+ list_result = [a for a in list_result if len(a)>1 and len(a)<20]
|
|
|
|
+ return list_result
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def get_childs(childs):
|
|
|
|
+ list_data = []
|
|
|
|
+ for _child in childs:
|
|
|
|
+ list_data.append(_child)
|
|
|
|
+ childs2 = _child.get("child_title",[])
|
|
|
|
+
|
|
|
|
+ if len(childs2)>0:
|
|
|
|
+ for _child2 in childs2:
|
|
|
|
+ list_data.extend(get_childs([_child2]))
|
|
|
|
+ return list_data
|
|
|
|
+
|
|
|
|
+def get_range_data_by_childs(list_data,childs):
|
|
|
|
+ range_data = []
|
|
|
|
+ list_child = get_childs(childs)
|
|
|
|
+ list_index = []
|
|
|
|
+ set_child = set([id(x) for x in list_child])
|
|
|
|
+ for _data_i in range(len(list_data)):
|
|
|
|
+ _data = list_data[_data_i]
|
|
|
|
+ _id = id(_data)
|
|
|
|
+ if _id in set_child:
|
|
|
|
+ list_index.append(_data_i)
|
|
|
|
+ if len(list_index)>0:
|
|
|
|
+ range_data = list_data[min(list_index):max(list_index)+1]
|
|
|
|
+ return range_data
|
|
|
|
+
|
|
|
|
+def get_correct_product(product,products):
|
|
|
|
+ list_data = []
|
|
|
|
+ for p in products:
|
|
|
|
+ is_sim = is_similar(product,p)
|
|
|
|
+ _d = {"product":p,"distance":abs(len(product)-len(p)),"is_sim":is_sim}
|
|
|
|
+ list_data.append(_d)
|
|
|
|
+ list_data.sort(key=lambda x:x["distance"])
|
|
|
|
+ for _d in list_data:
|
|
|
|
+ is_sim = _d["is_sim"]
|
|
|
|
+ if is_sim:
|
|
|
|
+ return _d["product"]
|
|
|
|
+ return product
|
|
|
|
+
|
|
|
|
+def get_childs_text(childs,_product,products,is_end=False):
|
|
|
|
+ _text = ""
|
|
|
|
+
|
|
|
|
+ for _child in childs:
|
|
|
|
+ child_text = _child.get("text")
|
|
|
|
+
|
|
|
|
+ for p in products:
|
|
|
|
+
|
|
|
|
+ if child_text.find(_product)<0 and child_text.find(p)>=0:
|
|
|
|
+ is_end = True
|
|
|
|
+ break
|
|
|
|
+ if is_end:
|
|
|
|
+ break
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ _text += _child.get("text")+"\n"
|
|
|
|
+ childs2 = _child.get("child_title",[])
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if len(childs2)>0:
|
|
|
|
+ for _child2 in childs2:
|
|
|
|
+ child_text,is_end = get_childs_text([_child2],_product,products)
|
|
|
|
+ if is_end:
|
|
|
|
+ break
|
|
|
|
+ else:
|
|
|
|
+ _text += child_text
|
|
|
|
+ return _text,is_end
|
|
|
|
+
|
|
|
|
+def extract_parameters_by_tree(_product,products,list_data,_data_i,parent_title,list_result,):
|
|
|
|
+ _data = list_data[_data_i]
|
|
|
|
+ childs = _data.get("child_title",[])
|
|
|
|
+ if len(childs)>0:
|
|
|
|
+ child_text,_ = get_childs_text([_data],_product,products)
|
|
|
|
+ list_result.append(child_text)
|
|
|
|
+ return True
|
|
|
|
+ if parent_title is not None:
|
|
|
|
+ childs = parent_title.get("child_title",[])
|
|
|
|
+ if len(childs)>0:
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ print(parent_title["text"])
|
|
|
|
+ for c in childs:
|
|
|
|
+ print("11",c["text"])
|
|
|
|
+ # print([a["text"] for a in c["child_title"]])
|
|
|
|
+ range_data = get_range_data_by_childs(list_data[_data_i:],childs)
|
|
|
|
+ for c in range_data:
|
|
|
|
+ print("22",c["text"])
|
|
|
|
+ p_text = ""
|
|
|
|
+ _find = False
|
|
|
|
+ for pdata in range_data:
|
|
|
|
+ ptype = _data["type"]
|
|
|
|
+ ptext = pdata["text"]
|
|
|
|
+ for p in products:
|
|
|
|
+ if ptext.find(_product)<0 and ptext.find(p)>=0:
|
|
|
|
+ _find = True
|
|
|
|
+ print("p find",p)
|
|
|
|
+ break
|
|
|
|
+ if _find:
|
|
|
|
+ print("======break")
|
|
|
|
+ print(ptext)
|
|
|
|
+ break
|
|
|
|
+ p_text += ptext+"\n"
|
|
|
|
+ if len(p_text)>0:
|
|
|
|
+ list_result.append(p_text)
|
|
|
|
+ return True
|
|
|
|
+ return False
|
|
|
|
+
|
|
|
|
+def extract_parameters_by_table(_product,_param_pattern,list_data,_data_i,list_result):
|
|
|
|
+ _data = list_data[_data_i]
|
|
|
|
+ _text = _data["text"]
|
|
|
|
+ list_table = _data["list_table"]
|
|
|
|
+ if list_table is not None:
|
|
|
|
+ _check = True
|
|
|
|
+ max_length = max([len(a) for a in list_table])
|
|
|
|
+ min_length = min([len(a) for a in list_table])
|
|
|
|
+ text_line_first = ",".join(a[0] for a in list_table[0])
|
|
|
|
+ if min_length<max_length/2:
|
|
|
|
+ return
|
|
|
|
+ last_data = list_data[_data_i-1]
|
|
|
|
+ _flag = False
|
|
|
|
+ if last_data["type"]=="sentence" and last_data["text"].find(_product)>=0:
|
|
|
|
+ _flag = True
|
|
|
|
+ # print(text_line_first,"text_line_first",re.search(_param_pattern,text_line_first) is not None and text_line_first.find(_product)>=0)
|
|
|
|
+ if re.search(_param_pattern,text_line_first) is not None and text_line_first.find(_product)>=0:
|
|
|
|
+ _flag = True
|
|
|
|
+ if _flag:
|
|
|
|
+ list_result.append(_text)
|
|
|
|
+ else:
|
|
|
|
+ list_head_index = []
|
|
|
|
+ for line in list_table[:2]:
|
|
|
|
+ for cell_i in range(len(line)):
|
|
|
|
+ cell = line[cell_i]
|
|
|
|
+ cell_text = cell[0]
|
|
|
|
+ if len(cell_text)<20 and re.search(_param_pattern,cell_text) is not None:
|
|
|
|
+ list_head_index.append(cell_i)
|
|
|
|
+ list_head_index = list(set(list_head_index))
|
|
|
|
+ for line in list_table:
|
|
|
|
+ for cell in line:
|
|
|
|
+ cell_text = cell[0]
|
|
|
|
+ if len(cell_text)>50 and len(re.findall("\d+",cell_text))>10 and cell_text.find(_product)>=0:
|
|
|
|
+ list_result.append(cell_text)
|
|
|
|
+ if len(cell_text)<len(_product)*10 and str(cell_text).find(_product)>=0:
|
|
|
|
+ for _index in list_head_index:
|
|
|
|
+ _cell = line[_index]
|
|
|
|
+ if len(cell[0])>0:
|
|
|
|
+ print("add on table",_cell[0])
|
|
|
|
+ list_result.append(_cell[0])
|
|
|
|
+
|
|
|
|
+def extract_product_parameters(list_data,_product):
|
|
|
|
+ _param_pattern = "配置要求|技术要求|技术参数|具体参数|规格参数|参数要求|技术需求|配置清单|(质量|技术).{,10}要求|明细及参数|验收标准|^参数$"
|
|
|
|
+ list_result = []
|
|
|
|
+ products = extract_products(list_data,_product)
|
|
|
|
+
|
|
|
|
+ _product = get_correct_product(_product,products)
|
|
|
|
+ print("===",_product,products)
|
|
|
|
+ for _data_i in range(len(list_data)):
|
|
|
|
+ _data = list_data[_data_i]
|
|
|
|
+ _type = _data["type"]
|
|
|
|
+ _text = _data["text"]
|
|
|
|
+ if _type=="sentence":
|
|
|
|
+ if _text.find(_product)>=0:
|
|
|
|
+ print("_text",_text,_data["sentence_title"])
|
|
|
|
+ parent_title = _data.get("parent_title")
|
|
|
|
+ if re.search(_param_pattern,_text) is not None:
|
|
|
|
+ extract_parameters_by_tree(_product,products,list_data,_data_i,parent_title,list_result)
|
|
|
|
+
|
|
|
|
+ elif parent_title is not None:
|
|
|
|
+ parent_text = parent_title.get("text","")
|
|
|
|
+ print("parent_text",parent_text)
|
|
|
|
+ if re.search(_param_pattern,parent_text) is not None:
|
|
|
|
+ extract_parameters_by_tree(_product,products,list_data,_data_i,parent_title,list_result)
|
|
|
|
+
|
|
|
|
+ else:
|
|
|
|
+ parent_title = parent_title.get("parent_title")
|
|
|
|
+ if parent_title is not None:
|
|
|
|
+ parent_text = parent_title.get("text","")
|
|
|
|
+ # print("parent_text",parent_text)
|
|
|
|
+ if re.search(_param_pattern,parent_text) is not None:
|
|
|
|
+ extract_parameters_by_tree(_product,products,list_data,_data_i,parent_title,list_result)
|
|
|
|
+
|
|
|
|
+ elif _type=="table":
|
|
|
|
+ extract_parameters_by_table(_product,_param_pattern,list_data,_data_i,list_result)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ # for i in range(len(list_result)):
|
|
|
|
+ # print("result%d"%i,list_result[i])
|
|
|
|
+ list_result.sort(key=lambda x:len(re.findall('[^.][0-9a-zA-Z]+[^.]',x)), reverse=True)
|
|
|
|
+
|
|
|
|
+ return list_result[0] if len(list_result)>0 else None
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+if __name__ == '__main__':
|
|
|
|
+
|
|
|
|
+ _html = open("download/7421e0c9d12dc6290ead4040df0e3cd0.html", "r", encoding="utf8").read()
|
|
|
|
+
|
|
|
|
+ pd = ParseDocument(_html)
|
|
|
|
+
|
|
|
|
+ list_data = pd.tree
|
|
|
|
+ pd.print_tree(list_data)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ _text = extract_product_parameters(list_data,"4K高清摄像系统")
|
|
|
|
+ print("extract_text",_text)
|
|
|
|
+
|