tablev2.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. package pretreated
  2. //定义表格对象
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. u "jy/util"
  7. "log"
  8. qutil "qfw/util"
  9. "regexp"
  10. "strings"
  11. "sync"
  12. "github.com/PuerkitoBio/goquery"
  13. )
  14. //所有中标候选人只取第一个
  15. type TableResult struct {
  16. Id interface{} //信息id
  17. Toptype string //信息类型
  18. Itype int //1全文 2是块
  19. BlockTag string //块标签
  20. Html string
  21. Tabs []*Table //子表集合,子表中包含标准化kv或原始kv
  22. GoqueryTabs *goquery.Selection //goquery对象
  23. TableSize int //子表的个数0,1,n
  24. IsMultiPackage bool //是否有子包
  25. PackageMap *SortMap //子包对象的sortmap,含标准化过的
  26. KvTags map[string][]*u.Tag //全局KVmap值,标准化处理过的
  27. WinnerOrder []map[string]interface{}
  28. BrandData [][]map[string]string //品牌抽取结果
  29. HasKey int //有key
  30. HasBrand int //有品牌
  31. HasGoods int //有商品
  32. RuleBlock *u.RuleBlock
  33. }
  34. //快速创建TableResult对象
  35. func NewTableResult(Id interface{}, Toptype, BlockTag, con string, Itype int, ruleBlock *u.RuleBlock) *TableResult {
  36. return &TableResult{
  37. Id: Id,
  38. Toptype: Toptype,
  39. Html: con,
  40. Itype: Itype,
  41. BlockTag: BlockTag,
  42. Tabs: []*Table{},
  43. GoqueryTabs: &goquery.Selection{},
  44. PackageMap: NewSortMap(),
  45. KvTags: map[string][]*u.Tag{},
  46. RuleBlock: ruleBlock,
  47. }
  48. }
  49. //td节点
  50. type TD struct {
  51. Goquery *goquery.Selection //文本对象
  52. TR *TR //所属TR对象
  53. LeftNode *TD //左临节点
  54. TopNode *TD //上临节点
  55. RightNode *TD //右节点
  56. BottomNode *TD //下节点
  57. Val string //值
  58. Text string //原始串
  59. SortKV *SortMap //存放kv值
  60. Html string //html值
  61. BH bool //是否是表头
  62. MustBH bool //不能修改的表头
  63. StandardKey string //标准表头
  64. Colspan int //合并列
  65. Rowspan int //合并行
  66. StartCol int //起始列
  67. EndCol int //终止列
  68. StartRow int //起始行
  69. EndRow int //终止行
  70. ColPos int //当前在TR中的位置
  71. HeadTd *TD //(是val元素)k节点
  72. KVDirect int //键-值方向,0未知,1横 2纵//指值和k的方向
  73. KeyDirect int //k方向,k纵值横,k横值纵 1横 2纵
  74. SonTds []*TD //(是key元素)值节点数组
  75. SonTableResult *TableResult //子值表格集
  76. ArrVal []string //数组值,当是左临元素是合并行的元素时!
  77. Valtype string //"BO=中标人顺序"
  78. }
  79. var submatchreg = regexp.MustCompile(`((?:[一二三四五六七八九十0-10]+[、])([\\S]{4,12})|([\\S]{2,12}))[::]([\\S]{5,60})([一二三四五六七八九]+[、])?`)
  80. var BHKey = regexp.MustCompile(`^[^,,;:。、.]{2,8}.{0,3}[::].+$`)
  81. var dwReg = regexp.MustCompile("单位[::/ \\s\u3000\u2003\u00a0\\n]*([万亿元]+)")
  82. func NewTD(Goquery *goquery.Selection, tr *TR, table *Table) *TD {
  83. defer qutil.Catch()
  84. td := &TD{
  85. ArrVal: []string{},
  86. Goquery: Goquery,
  87. SonTds: []*TD{},
  88. TR: tr,
  89. SortKV: NewSortMap(),
  90. }
  91. colspan, rowspan := 0, 0
  92. col, bcol := td.Goquery.Attr("colspan")
  93. if bcol {
  94. colspan = qutil.IntAllDef(col, 1)
  95. }
  96. if colspan == 0 {
  97. colspan = 1
  98. }
  99. row, brow := td.Goquery.Attr("rowspan")
  100. if brow {
  101. rowspan = qutil.IntAllDef(row, 1)
  102. }
  103. if rowspan == 0 {
  104. rowspan = 1
  105. }
  106. td.Colspan, td.Rowspan = colspan, rowspan //合并列,合并行
  107. td.Html, _ = td.Goquery.Html() //html值
  108. ht := td.Goquery.ChildrenFiltered("table") //获取td的table
  109. bsontable := false //默认td中没有table
  110. txt := ""
  111. //子table处理合并
  112. if ht.Size() > 0 {
  113. //qutil.Debug("有子表格")
  114. //格式化正文
  115. txt = TextAfterRemoveTable(td.Html)
  116. td.tdHasTable(&bsontable, tr) //处理td中的table,块标签处理,子表解析集处理
  117. } else {
  118. txt = strings.TrimSpace(td.Goquery.Text())
  119. }
  120. text := dwReg.ReplaceAllString(u.TrimLRAll(txt, ""), "$1")
  121. td.Val = text //值
  122. td.Text = txt //原始串
  123. //处理table外内容
  124. var ub []*u.Block
  125. ub, _ = DivideBlock("", txt, 2, table.TableResult.RuleBlock)
  126. //看是否划块
  127. if len(ub) > 0 {
  128. for _, bl := range ub {
  129. //冒号kv
  130. for bl_ck, bl_cv := range bl.ColonKV.KvTags {
  131. td.SortKV.AddKey(bl_ck, bl_cv)
  132. }
  133. //空格kv
  134. for bl_sk, bl_sv := range bl.SpaceKV.KvTags {
  135. td.SortKV.AddKey(bl_sk, bl_sv)
  136. }
  137. }
  138. } else {
  139. //for _, v := range GetKVAll(txt, "", nil, 2).KvTags {
  140. //for _, vv := range v {
  141. //td.SortKV.AddKey(vv.Key, vv.Value)
  142. //}
  143. //}
  144. }
  145. //抽取不到走正则抽
  146. proCode := projectcodeReg.FindString(text)
  147. if proCode != "" {
  148. ckv := GetKVAll(proCode, "", nil, 1)
  149. for _, v := range ckv.KvTags {
  150. for _, vv := range v {
  151. td.SortKV.AddKey(vv.Key, vv.Value)
  152. }
  153. }
  154. } else if proCode = projectcodeReg2.FindString(text); proCode != "" {
  155. ckv := GetKVAll(proCode, "", nil, 1)
  156. for _, v := range ckv.KvTags {
  157. for _, vv := range v {
  158. td.SortKV.AddKey(vv.Key, vv.Value)
  159. }
  160. }
  161. }
  162. if proCode = jsonReg.FindString(text); proCode != "" {
  163. jsonMap := make(map[string]string)
  164. json.Unmarshal([]byte(proCode), &jsonMap)
  165. for k, v := range jsonMap {
  166. td.SortKV.AddKey(k, v)
  167. }
  168. }
  169. //对td单元格值判断是否是表头和根据td内容长度进行分块处理
  170. td.tdIsHb(tr, table, bsontable)
  171. bhead := false
  172. if td.TR.RowPos == 0 { //第一行
  173. if td.Goquery.Closest("thead").Size() == 1 && !bsontable { //如果是thead确定为k值表头
  174. bhead = true
  175. }
  176. }
  177. if bhead && !bsontable {
  178. td.BH = true
  179. td.KeyDirect = 1 //k方向,k纵值横,k横值纵 1横 2纵
  180. td.KVDirect = 2 //键-值方向,0未知,1横 2纵//指值和k的方向
  181. }
  182. //u.Debug(td.BH, td.Val)
  183. return td
  184. }
  185. //处理td中的table,块标签处理,子表解析集处理
  186. func (td *TD) tdHasTable(bsontable *bool, tr *TR) {
  187. ts := td.TR.Table.TableResult
  188. tabs, _ := ComputeConRatio(td.Html, 2) //计算表格占比
  189. if len(tabs) > 0 {
  190. (*bsontable) = true
  191. stag := ts.BlockTag //块标签
  192. if stag == "" {
  193. var tdleft *TD
  194. if len(tr.TDs) > 0 {
  195. tdleft = tr.TDs[len(tr.TDs)-1]
  196. if tdleft.BH {
  197. //u.Debug(tdleft.Val),如果不存在就是上一行的
  198. stag = tdleft.Val
  199. }
  200. } else if len(tr.Table.TRs) > 0 {
  201. lasttr := tr.Table.TRs[len(tr.Table.TRs)-1]
  202. str := ""
  203. for _, td3 := range lasttr.TDs {
  204. str += td3.Val
  205. if len([]rune(str)) > 14 {
  206. str = ""
  207. break
  208. }
  209. }
  210. stag = str
  211. }
  212. }
  213. for _, tv := range tabs {
  214. if IsHide(tv) {
  215. continue
  216. }
  217. sonts := NewTableResult(ts.Id, ts.Toptype, stag, td.Html, 2, td.TR.Table.TableResult.RuleBlock)
  218. sonts.GoqueryTabs = tv
  219. sonts.Analy()
  220. //sonts := AnalyTableV2(tabs, ts.Toptype, stag, td.Html, 2, ts.Id, table.TableResult.RuleBlock) //又一次调用解析表格入口
  221. td.BH = false
  222. if td.TR.Table.TableResult == nil {
  223. td.TR.Table.TableResult = NewTableResult(sonts.Id, sonts.Toptype, sonts.BlockTag, sonts.Html, sonts.Itype, sonts.RuleBlock)
  224. }
  225. MergeKvTags(td.TR.Table.TableResult.KvTags, sonts.KvTags)
  226. td.SonTableResult = sonts
  227. //for _, k := range sonts.SortKV.Keys {
  228. //u.Debug(k, sonts.SortKV.Map[k])
  229. // td.TR.Table.StandKV[k] = sonts.SortKV.Map[k].(string)
  230. // td.TR.Table.StandKVWeight[k] = sonts.SortKVWeight[k]
  231. //}
  232. //增加brand (子表)
  233. //fmt.Println("sonsHasKey=============", sonts.HasKey)
  234. //fmt.Println("sonsHasGoods========", sonts.HasGoods)
  235. //fmt.Println("sonsHasBrand========", sonts.HasBrand)
  236. if sonts.HasKey != 0 {
  237. td.TR.Table.TableResult.HasKey = sonts.HasKey
  238. }
  239. if sonts.HasGoods != 0 {
  240. td.TR.Table.TableResult.HasGoods = sonts.HasGoods
  241. }
  242. if sonts.HasBrand != 0 {
  243. td.TR.Table.TableResult.HasBrand = sonts.HasBrand
  244. }
  245. if sonts.BrandData != nil && len(sonts.BrandData) > 0 { //子table
  246. for _, v := range sonts.BrandData {
  247. if len(v) > 0 {
  248. td.TR.Table.TableResult.BrandData = append(td.TR.Table.TableResult.BrandData, v)
  249. }
  250. }
  251. }
  252. if sonts.WinnerOrder != nil && len(sonts.WinnerOrder) > 0 {
  253. td.TR.Table.TableResult.WinnerOrder = sonts.WinnerOrder
  254. }
  255. if sonts.IsMultiPackage {
  256. td.TR.Table.BPackage = true
  257. tb1 := td.TR.Table.BlockPackage
  258. for k, v := range sonts.PackageMap.Map {
  259. v1 := v.(*u.BlockPackage)
  260. if tb1.Map[k] == nil {
  261. tb1.AddKey(k, v)
  262. } else {
  263. bp := tb1.Map[k].(*u.BlockPackage)
  264. if bp != nil && v1.TableKV != nil {
  265. for k2, v2 := range v1.TableKV.KvTags {
  266. if bp.TableKV == nil {
  267. bp.TableKV = u.NewJobKv()
  268. }
  269. isExists := false
  270. for _, v2v := range v2 {
  271. for _, v2vv := range bp.TableKV.KvTags[k2] {
  272. if v2v.Value == v2vv.Value {
  273. isExists = true
  274. break
  275. }
  276. }
  277. if !isExists {
  278. bp.TableKV.KvTags[k2] = append(bp.TableKV.KvTags[k2], v2v)
  279. bp.Text += fmt.Sprintf("%v:%v\n", k2, v2)
  280. }
  281. }
  282. }
  283. }
  284. }
  285. }
  286. //u.Debug(fmt.Sprintf("%v", td.TR.Table.BlockPackage.Map["1"]))
  287. }
  288. }
  289. }
  290. }
  291. //对td单元格值判断是否是表头和根据td内容长度进行分块处理
  292. func (td *TD) tdIsHb(tr *TR, table *Table, bsontable bool) {
  293. lenval := len([]rune(td.Val)) //经过处理的td内容长度
  294. //if lentxt > 9 {
  295. //td.KV = GetKVAll(txt, "")
  296. ub := []*u.Block{}
  297. //经过处理的td内容长度大于50,划块,分包
  298. if lenval > 50 { //看是否划块
  299. //u.Debug(txt)
  300. ub, _ = DivideBlock("", td.Text, 2, table.TableResult.RuleBlock) //对td的原始值
  301. //看是否划块
  302. if len(ub) > 0 {
  303. for _, bl := range ub {
  304. //冒号kv
  305. for bl_ck, bl_cv := range bl.ColonKV.KvTags {
  306. td.SortKV.AddKey(bl_ck, bl_cv)
  307. }
  308. //空格kv
  309. for bl_sk, bl_sv := range bl.SpaceKV.KvTags {
  310. td.SortKV.AddKey(bl_sk, bl_sv)
  311. }
  312. }
  313. }
  314. //
  315. blockPackage := map[string]*u.BlockPackage{}
  316. isFindPkg := true
  317. /*if td.ColPos-1 >= 0 && excludeKey.MatchString(tr.TDs[td.ColPos-1].Text) {
  318. isFindPkg = false
  319. } else if len(tr.TDs) > 0 {
  320. tdleft = tr.TDs[len(tr.TDs)-1]
  321. if tdleft.BH && excludeKey.MatchString(tr.TDs[td.ColPos-1].Text) {
  322. isFindPkg = false
  323. }
  324. }*/
  325. if len(tr.TDs) > 0 {
  326. tdleft := tr.TDs[len(tr.TDs)-1]
  327. if tdleft.BH && excludeKey.MatchString(tdleft.Text) { //(涉及包号|包件号?|项目标号|规格|型号|招标范围|业绩|废标)|(^编号$)|([^包段标]编号)
  328. isFindPkg = false
  329. }
  330. }
  331. if isFindPkg {
  332. if len(ub) > 0 {
  333. blockPackage = FindPackageFromBlocks(&ub, "") //从块里面找分包
  334. } else {
  335. blockPackage = FindPackageFromText("", td.Val) //从正文里面找分包
  336. }
  337. }
  338. if len(blockPackage) > 0 {
  339. table.BPackage = true
  340. for bp_k, bp_v := range blockPackage {
  341. var bp *u.BlockPackage
  342. if table.TableResult.PackageMap.Map[bp_k] == nil {
  343. bp = bp_v
  344. } else {
  345. bp = table.TableResult.PackageMap.Map[bp_k].(*u.BlockPackage)
  346. bp.Text += "\n" + bp_v.Text
  347. }
  348. if bp.TableKV == nil {
  349. bp.TableKV = u.NewJobKv()
  350. }
  351. MergeKvTags(bp.TableKV.KvTags, bp_v.ColonKV.KvTags)
  352. MergeKvTags(bp.TableKV.KvTags, bp_v.SpaceKV.KvTags)
  353. table.TableResult.PackageMap.Map[bp_k] = bp
  354. }
  355. }
  356. }
  357. //经过处理的td内容长度小于50,冒号kv,td表头
  358. if lenval < 50 {
  359. // td.SortKV = FindKv(text, "")
  360. kvTitle := ""
  361. if len(td.TR.TDs) > 0 {
  362. kvTitle = td.TR.TDs[len(td.TR.TDs)-1].Val
  363. }
  364. /*
  365. 预算总价
  366. (人民币:元)
  367. */
  368. if td.Text != "" && strings.Contains(td.Text, "预算总价") && (strings.Contains(td.Text, "(") || strings.Contains(td.Text, "(")) {
  369. tagindex := 0
  370. if tagindex = strings.Index(td.Text, "("); tagindex <= 0 {
  371. tagindex = strings.Index(td.Text, "(")
  372. }
  373. td.SortKV.AddKey(strings.TrimSpace(td.Text[:tagindex]), strings.TrimSpace(td.Text[tagindex:])) //存放kv值
  374. td.BH = true
  375. }
  376. _, resm := colonkvEntity.entrance(td.Val, kvTitle, nil, 3) //td冒号kv
  377. for k, v := range resm {
  378. if k != "" && v != "" {
  379. td.SortKV.AddKey(k, v) //存放kv值
  380. }
  381. }
  382. //u.Debug(td.SortKV.Keys, "-------2--------------------------------")
  383. // td.SortKV = FindKv(text, "") //GetKvFromtxt(text, "")
  384. //resm := GetKVAll(text, "")
  385. if len(td.SortKV.Keys) > 0 {
  386. //td.KVDirect = 3 //不当头也不当值,忽略
  387. if len(td.SortKV.Keys) == 1 && BHKey.MatchString(td.Val) && !MultiReg.MatchString(td.Val) {
  388. td.Val, _ = td.SortKV.Map[td.SortKV.Keys[0]].(string)
  389. td.BH = true
  390. }
  391. } else if !bsontable {
  392. txt := repSpace.ReplaceAllString(td.Val, "")
  393. btw, must, _, _, repl := CheckHeader(txt)
  394. if lenval > 15 && !strings.Contains(txt, "采购代理机构名称、地址和联系方式") {
  395. btw = false
  396. }
  397. if strings.Contains(td.Val, "个项目") {
  398. must = false
  399. btw = false
  400. }
  401. td.Valtype = repl
  402. td.MustBH = must
  403. td.BH = btw
  404. }
  405. } else if len(ub) == 0 {
  406. //之前这里没加判断,现在加上判断,造成分块之后的kv被覆盖掉
  407. //u.Debug("----\n\n\n", txt, "\n\n\n----")
  408. //u.Debug(GetKVAll(txt, ""))
  409. /*
  410. subVal := submatchreg.FindAllStringSubmatch(txt, -1)
  411. if len(subVal) > 0 {
  412. for _, subv1 := range subVal {
  413. if len(subv1) == 6 {
  414. tr.Table.SortKV.AddKey(If(subv1[2] == "", subv1[3], subv1[2]).(string), subv1[4])
  415. //tr.Table.SortKV.AddKey(subv1[1], subv1[2])
  416. }
  417. }
  418. }
  419. */
  420. fSortKV := FindKv(td.Val, "", 2)
  421. for k, v := range fSortKV.Map {
  422. td.SortKV.AddKey(k, v)
  423. }
  424. // td.LeftNode.Val
  425. // for _, vvv := range *td.TR {
  426. // u.Debug(">>>>>")
  427. // }
  428. kvTitle := ""
  429. if len(td.TR.TDs) > 0 {
  430. kvTitle = td.TR.TDs[len(td.TR.TDs)-1].Val
  431. }
  432. _, resm := colonkvEntity.entrance(td.Val, kvTitle, nil, 2) //获取冒号kv入口
  433. for k, v := range resm {
  434. td.SortKV.AddKey(k, v)
  435. }
  436. }
  437. }
  438. func (t *Table) Print() {
  439. for row, trs := range t.TRs {
  440. for col, td := range trs.TDs {
  441. log.Println(row, col, td.Val, td.BH, td.SortKV.Map)
  442. }
  443. }
  444. }
  445. type TR struct {
  446. TDs []*TD
  447. TopTR *TR //上临行
  448. BottomTR *TR //下临行
  449. Table *Table //所属表格对象
  450. RowPos int //当前在第几行
  451. //-----计算
  452. MaxRow int //最大跨行 Max(td.StartRow-td.EndRow)
  453. MinRow int //最小跨行
  454. StartRow int //起始行
  455. EndRow int //结束行
  456. MaxCol int //最大列
  457. MinCol int //最小列
  458. StartCol int //起始列
  459. EndCol int //结束列
  460. BDiffSpanRow bool //起始行,行中有没有不同跨行 - - - = -
  461. BDiffSpanCol bool //起始列,列中有没有不同跨列 |
  462. }
  463. func NewTR(Table *Table) *TR {
  464. return &TR{
  465. TDs: []*TD{},
  466. Table: Table,
  467. }
  468. }
  469. func (tr *TR) AddTD(td *TD) {
  470. /**对跨行没有意义
  471. if len(tr.TDs) > 0 {
  472. td.LeftNode = tr.TDs[len(tr.TDs)-1]
  473. tr.TDs[len(tr.TDs)-1].RightNode = td
  474. }
  475. **/
  476. td.ColPos = len(tr.TDs)
  477. tr.TDs = append(tr.TDs, td)
  478. }
  479. /*-- START --- 处理表头概率开始 -------*/
  480. type pos struct {
  481. Max int
  482. Min int
  483. }
  484. type TDRationScope struct {
  485. Rationmap map[*pos]float32
  486. Tdmap map[*pos][]*TD
  487. Poss []*pos
  488. Parentkey string
  489. }
  490. func NewTDRationScope(key string) *TDRationScope {
  491. return &TDRationScope{map[*pos]float32{}, map[*pos][]*TD{}, []*pos{}, key}
  492. }
  493. func (tdr *TDRationScope) GetPos(td *TD) (poss *pos) {
  494. k1 := tdr.Parentkey[:1]
  495. m1, m2 := td.StartRow, td.EndRow
  496. if k1 == "r" {
  497. m1, m2 = td.StartCol, td.EndCol
  498. }
  499. for _, v := range tdr.Poss {
  500. if v.Max >= m2 && v.Min <= m1 {
  501. poss = v
  502. return
  503. }
  504. }
  505. return
  506. }
  507. func (tdr *TDRationScope) GetTDRation(td *TD) (ration float32, tds []*TD) {
  508. poss := tdr.GetPos(td)
  509. if poss != nil {
  510. ration = tdr.Rationmap[poss]
  511. tds = tdr.Tdmap[poss]
  512. }
  513. return
  514. }
  515. func (tdr *TDRationScope) Addtd(td *TD) {
  516. k1 := tdr.Parentkey[:1]
  517. m1, m2 := td.StartRow, td.EndRow
  518. if k1 == "r" {
  519. m1, m2 = td.StartCol, td.EndCol
  520. }
  521. bfind := false
  522. for _, v := range tdr.Poss {
  523. if m1 == v.Max+1 { //找到
  524. bfind = true
  525. v.Max = m2
  526. tdr.Tdmap[v] = append(tdr.Tdmap[v], td)
  527. break
  528. }
  529. }
  530. if !bfind {
  531. pos1 := &pos{m2, m1}
  532. tdr.Tdmap[pos1] = []*TD{td}
  533. tdr.Poss = append(tdr.Poss, pos1)
  534. }
  535. }
  536. /*-- END --- 处理表头概率 -------*/
  537. //table表格
  538. type Table struct {
  539. Brule bool //是否规则
  540. TRs []*TR
  541. BFirstRow bool
  542. RowNum int //行数
  543. ColNum int //列数
  544. TDNum int //td个数
  545. BPackage bool //是否有包
  546. SortKV *SortMap //带排序的KV值
  547. StandKV map[string][]*u.Tag //过滤后的标准化kv
  548. StandRuleKV map[string]string //过滤后的规则kv
  549. kvscope map[int]map[int][]*TD //sortkey第几个元素的的第几个值的结束位置
  550. kTD map[int]*TD //根据索引找到key的TD元素
  551. SonTables []*Table //孩子表集合
  552. Tag string //表格的标签
  553. Desc string //表格描述内容
  554. Goquery *goquery.Selection //表格的goquery对象
  555. Html string //所属的文本内容
  556. BlockPackage *SortMap //子包数组
  557. TableResult *TableResult //父元素
  558. StartAndEndRation map[string]*TDRationScope //同行或同列的概率,截断的单独起算
  559. StartAndEndRationKSort *SortMap
  560. WinnerOrder []map[string]interface{}
  561. BSplit bool //是否是有一个表拆分成的多个表
  562. BHeader bool //拆分表是否有表头
  563. BrandData [][]map[string]string //品牌抽取结果
  564. HasKey int //有key
  565. HasBrand int //有品牌
  566. HasGoods int //有商品
  567. }
  568. func NewTable(Html string, TableResult *TableResult, tab *goquery.Selection) *Table {
  569. return &Table{
  570. Html: Html,
  571. SortKV: NewSortMap(),
  572. StandKV: map[string][]*u.Tag{},
  573. kvscope: map[int]map[int][]*TD{},
  574. kTD: map[int]*TD{},
  575. SonTables: []*Table{},
  576. Goquery: tab,
  577. TRs: []*TR{},
  578. TableResult: TableResult,
  579. StartAndEndRation: map[string]*TDRationScope{},
  580. StartAndEndRationKSort: NewSortMap(),
  581. BlockPackage: NewSortMap(),
  582. }
  583. }
  584. func (t *Table) AddTR(tr *TR) {
  585. if len(tr.TDs) > 0 {
  586. if len(t.TRs) > 0 {
  587. tr.TopTR = t.TRs[len(t.TRs)-1]
  588. t.TRs[len(t.TRs)-1].BottomTR = tr
  589. }
  590. tr.RowPos = len(t.TRs)
  591. t.TRs = append(t.TRs, tr)
  592. }
  593. }
  594. func (t *Table) InsertTR(tr *TR) {
  595. if len(tr.TDs) > 0 {
  596. if len(t.TRs) > 0 {
  597. t.TRs[0].TopTR = tr
  598. }
  599. tr.RowPos = 0
  600. for _, _tr := range t.TRs {
  601. _tr.RowPos += 1
  602. }
  603. t.TRs = append([]*TR{tr}, t.TRs...)
  604. }
  605. }
  606. //支持排序的map
  607. type SortMap struct {
  608. Index map[string]int
  609. Keys []string
  610. Map map[string]interface{}
  611. Lock sync.Mutex
  612. }
  613. //快速创建排序map
  614. func NewSortMap() *SortMap {
  615. return &SortMap{
  616. Index: map[string]int{},
  617. Keys: []string{},
  618. Map: map[string]interface{}{},
  619. }
  620. }
  621. //增加值
  622. var NullVal = regexp.MustCompile("^[/无,.。;、附]+$|^详见.{2,8}$|(详?见)?附(件|图)")
  623. func (s *SortMap) AddKey(key string, val interface{}) {
  624. //判断val
  625. // if v, ok := val.(string); ok && NullVal.ReplaceAllString(u.TrimLRSpace(v, ""), "") == "" {
  626. // return
  627. // }
  628. s.Lock.Lock()
  629. defer s.Lock.Unlock()
  630. //重复
  631. if s.Map[key] == nil {
  632. s.Index[key] = len(s.Keys)
  633. s.Keys = append(s.Keys, key)
  634. }
  635. s.Map[key] = val
  636. }
  637. //增加值
  638. func (s *SortMap) ReplaceKey(key string, val interface{}, replacekey string) {
  639. s.Lock.Lock()
  640. defer s.Lock.Unlock()
  641. //重复
  642. v := s.Index[replacekey]
  643. s.Index[key] = v
  644. delete(s.Index, replacekey)
  645. s.Keys = append(s.Keys[:v], append([]string{key}, s.Keys[v+1:]...)...)
  646. delete(s.Map, replacekey)
  647. s.Map[key] = val
  648. }
  649. //删除值
  650. func (s *SortMap) RemoveKey(key string) {
  651. s.Lock.Lock()
  652. defer s.Lock.Unlock()
  653. delete(s.Map, key)
  654. pos := s.Index[key]
  655. delete(s.Index, key)
  656. if len(s.Keys) > 0 {
  657. s.Keys = func() []string {
  658. newkeys := []string{}
  659. if len(s.Keys) > 1 {
  660. if pos == 0 {
  661. newkeys = append(newkeys, s.Keys[1:]...)
  662. //每一个都减一
  663. for k, v := range s.Index {
  664. s.Index[k] = v - 1
  665. }
  666. } else if pos == len(s.Keys) {
  667. newkeys = append(newkeys, s.Keys[:pos]...)
  668. } else {
  669. tmp := s.Keys[pos+1:]
  670. newkeys = append(append(newkeys, s.Keys[:pos]...), tmp...)
  671. for _, v := range tmp {
  672. s.Index[v] -= 1
  673. }
  674. }
  675. }
  676. return newkeys
  677. }()
  678. }
  679. }
  680. //判断表头是key的对象
  681. type TableKeyV1 struct {
  682. TMap map[string]interface{}
  683. TReg []*regexp.Regexp
  684. TRegReplStr []string
  685. }
  686. //判断表头时用到的顺序 正文、结果表头、正常表头
  687. var THeadStr = []string{
  688. "con",
  689. "jghead",
  690. "normalhead",
  691. }
  692. //存放敏感词
  693. var TKMaps = map[string]*TableKeyV1{}
  694. //过滤所有非汉字内容
  695. var filterThText = regexp.MustCompile("([((【\\[].*[))】\\]])|([^0-9a-zA-Z\\p{Han}]+)")
  696. var tLock = sync.Mutex{}
  697. //matchStro为tablev1.json文件中的key,txt为表格的内容也可以是表格的标签
  698. //主要实现表格是否是表头的判断,表格是否有用的判断(如人员情况等是无用的)
  699. func CheckCommon(txt string, matchStr ...string) (res, must bool, stype, reg, repl string) {
  700. txt = filterThText.ReplaceAllString(txt, "")
  701. stype = "con"
  702. if len([]rune(txt)) < 30 {
  703. tLock.Lock()
  704. defer tLock.Unlock()
  705. if len(TKMaps) == 0 {
  706. for k, v := range u.TableK1 {
  707. tk := &TableKeyV1{
  708. map[string]interface{}{},
  709. []*regexp.Regexp{},
  710. []string{},
  711. }
  712. thMap := map[string]interface{}{}
  713. for _, v1 := range v {
  714. v1s := strings.Split(v1, "__")
  715. if len(v1s) == 2 {
  716. tk.TReg = append(tk.TReg, regexp.MustCompile(v1s[0]))
  717. tk.TRegReplStr = append(tk.TRegReplStr, v1s[1])
  718. } else {
  719. key := v1
  720. nowMap := &thMap
  721. for i := 0; i < len(key); i++ {
  722. kc := key[i : i+1]
  723. if v, ok := (*nowMap)[kc]; ok {
  724. nowMap, _ = v.(*map[string]interface{})
  725. } else {
  726. newMap := map[string]interface{}{}
  727. newMap["Y"] = "0"
  728. (*nowMap)[kc] = &newMap
  729. nowMap = &newMap
  730. }
  731. if i == len(key)-1 {
  732. (*nowMap)["Y"] = "1"
  733. (*nowMap)["K"] = key
  734. //(*nowMap)["V"] = v
  735. }
  736. }
  737. }
  738. }
  739. tk.TMap = thMap
  740. TKMaps[k] = tk
  741. }
  742. }
  743. //先正则、后子串查找
  744. L1:
  745. for _, v := range matchStr {
  746. //u.Debug(v)
  747. for n, vreg := range TKMaps[v].TReg {
  748. if vreg.MatchString(txt) {
  749. //u.Debug(txt, v, vreg.String())
  750. reg = vreg.String()
  751. repl = TKMaps[v].TRegReplStr[n]
  752. if v != "con" {
  753. res = true
  754. if "M" == repl {
  755. must = true
  756. }
  757. }
  758. stype = v
  759. break L1
  760. }
  761. }
  762. //以下是敏感词子串查找匹配
  763. pos := 0
  764. thMap := TKMaps[v].TMap
  765. nowMap := &thMap
  766. for i := 0; i < len(txt); i++ {
  767. word := txt[i : i+1]
  768. nowMap, _ = (*nowMap)[word].(*map[string]interface{})
  769. if nowMap != nil { // 存在,则判断是否为最后一个
  770. if pos == 0 {
  771. pos = i
  772. }
  773. if "1" == qutil.ObjToString((*nowMap)["Y"]) {
  774. if v != "con" {
  775. res = true
  776. }
  777. stype = v
  778. pos = 0
  779. break L1
  780. }
  781. } else {
  782. nowMap = &thMap
  783. if pos > 0 {
  784. i = pos
  785. pos = 0
  786. }
  787. }
  788. }
  789. }
  790. return
  791. } else {
  792. return
  793. }
  794. }
  795. //根据td中的内容验证表头,根据tablev1.json中配置的三种规则(含正则和子串查找算法)
  796. func CheckHeader(txt string) (res, must bool, stype, reg, repl string) {
  797. return CheckCommon(txt, THeadStr...)
  798. }
  799. /**
  800. 计算表格占比,返回表格数组、占比
  801. con 文本
  802. strtype 1全文 2块文本
  803. **/
  804. func ComputeConRatio(con string, strtype int) (tabs []*goquery.Selection, ratio float32) {
  805. defer qutil.Catch()
  806. doc, _ := goquery.NewDocumentFromReader(strings.NewReader(con))
  807. cons := doc.Text()
  808. tables := doc.Find("table")
  809. doc = nil
  810. if tables.Size() > 0 {
  811. tabs = []*goquery.Selection{}
  812. for i := 0; i < tables.Size(); i++ {
  813. tmpt := tables.Eq(i)
  814. b := false
  815. for j := 0; j < len(tabs); j++ {
  816. if tabs[j].Contains(tmpt.Get(0)) {
  817. b = true
  818. }
  819. }
  820. if !b {
  821. tabs = append(tabs, tmpt)
  822. }
  823. }
  824. tlen := 0
  825. for _, t := range tabs {
  826. tlen += len(t.Text())
  827. }
  828. ratio = float32(tlen) / float32(len(cons))
  829. }
  830. /**
  831. if ratio < float32(0.992) {
  832. //取出排除表格之外的文本
  833. txt =getTextAfterRemoveTable(con)
  834. }
  835. **/
  836. return
  837. }
  838. //纯文本
  839. func HtmlToText(con string) string {
  840. doc2, _ := goquery.NewDocumentFromReader(strings.NewReader(con))
  841. //log.Println(doc2.Html())
  842. doc2.Find("tr").Each(func(i int, selection *goquery.Selection) {
  843. selection.AfterHtml(string(rune(10)))
  844. })
  845. //log.Println(doc2.Html())
  846. return doc2.Text()
  847. }
  848. //取出排除表格之外的文本
  849. func TextAfterRemoveTable(con string) string {
  850. doc2, _ := goquery.NewDocumentFromReader(strings.NewReader(con))
  851. doc2.Find("table").Remove()
  852. return doc2.Text()
  853. }
  854. func HtmlAfterRemoveTable(con string) string {
  855. doc2, _ := goquery.NewDocumentFromReader(strings.NewReader(con))
  856. doc2.Find("table").Remove()
  857. html, _ := doc2.Html()
  858. return html
  859. }
  860. func If(condition bool, trueVal, falseVal interface{}) interface{} {
  861. if condition {
  862. return trueVal
  863. }
  864. return falseVal
  865. }