analystep.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /**
  2. 信息预处理入口
  3. **/
  4. package pretreated
  5. import (
  6. "encoding/json"
  7. "jy/util"
  8. //"log"
  9. "strings"
  10. "github.com/PuerkitoBio/goquery"
  11. )
  12. func AnalyStart(job *util.Job) {
  13. con := job.Content
  14. //全文的需要修复表格
  15. con = RepairCon(con)
  16. //格式化正文
  17. con = formatText(con, "all")
  18. job.Content = con
  19. //计算表格占比,返回表格数组、占比
  20. tabs, ration := ComputeConRatio(con, 1)
  21. if len(tabs) > 0 {
  22. newcon, newtabs, newration := FindBigText(con, ration, tabs)
  23. if newcon != "" {
  24. con = newcon
  25. tabs = newtabs
  26. ration = newration
  27. }
  28. }
  29. blockArrays, _ := DivideBlock(job.CategorySecond, con, 1, job.RuleBlock) //分块
  30. if len(blockArrays) > 0 { //有分块
  31. //从块里面找分包
  32. job.BlockPackage = FindPackageFromBlocks(&blockArrays, job.Title) //从块里面找分包
  33. for _, bl := range blockArrays {
  34. //log.Println(bl.Text)
  35. if len([]rune(bl.Text)) > 80 {
  36. bl.Block, _ = DivideBlock(job.CategorySecond, bl.Text, 1, job.RuleBlock)
  37. for _, bl_bl := range bl.Block {
  38. processTableInBlock(bl_bl, job)
  39. }
  40. }
  41. FindProjectCode(bl.Text, job) //匹配项目编号
  42. processTableInBlock(bl, job)
  43. //新加 未分块table中未能解析到中标候选人,从正文中解析
  44. if job.Winnerorder == nil || len(job.Winnerorder) == 0 {
  45. bl.Winnerorder = winnerOrderEntity.Find(bl.Text, true, 1)
  46. job.Winnerorder = bl.Winnerorder
  47. }
  48. job.Block = append(job.Block, bl)
  49. }
  50. } else { //未分块,创建分块
  51. bl := &util.Block{}
  52. newCon := con
  53. if len(tabs) > 0 { //解析表格逻辑
  54. job.HasTable = 1 //添加标识:文本中有table
  55. newCon = TextAfterRemoveTable(con)
  56. job.BlockPackage = FindPackageFromText(job.Title, newCon)
  57. for i := 0; i < len(tabs); i++ {
  58. //log.Println(tabs[i].Text())
  59. //添加标识:文本中有table
  60. tabres := AnalyTableV2(tabs[i], job.Category, "", con, 1, job.SourceMid, job.RuleBlock) //解析表格入口 返回:汇总表格对象
  61. processTableResult(tabres, bl, job)
  62. }
  63. // for k, v := range bl.TableKV.Kv {
  64. // //log.Println("bl.TableKV.Kv", k, v)
  65. // }
  66. } else {
  67. //从正文里面找分包
  68. job.BlockPackage = FindPackageFromText(job.Title, newCon)
  69. }
  70. FindProjectCode(newCon, job) //匹配项目编号
  71. bl.Text = HtmlToText(con)
  72. //调用kv解析
  73. bl.ColonKV = GetKVAll(bl.Text, "", nil, 1)
  74. bl.SpaceKV = SspacekvEntity.Entrance(bl.Text, "", nil)
  75. //新加 未分块table中未能解析到中标候选人,从正文中解析
  76. if job.Winnerorder == nil || len(job.Winnerorder) == 0 {
  77. bl.Winnerorder = winnerOrderEntity.Find(bl.Text, true, 1)
  78. job.Winnerorder = bl.Winnerorder
  79. }
  80. //log.Println(bl.Text)
  81. job.Block = append(job.Block, bl)
  82. }
  83. }
  84. func processTableInBlock(bl *util.Block, job *util.Job) {
  85. //块中再查找表格(块,处理完把值赋到块)
  86. tabs, _ := ComputeConRatio(bl.Text, 2)
  87. for _, tab := range tabs {
  88. job.HasTable = 1
  89. //添加标识:文本中有table
  90. tabres := AnalyTableV2(tab, job.Category, bl.Title, tab.Text(), 2, job.SourceMid, job.RuleBlock) //解析表格入口 返回:汇总表格对象
  91. processTableResult(tabres, bl, job) //分析table解析结果
  92. if bl.Title == "" && tabres.BlockTag != "" {
  93. bl.Title = tabres.BlockTag
  94. }
  95. }
  96. }
  97. //匹配项目编号
  98. func FindProjectCode(newCon string, job *util.Job) {
  99. newCon = TextAfterRemoveTable(newCon)
  100. if strings.TrimSpace(newCon) == "" {
  101. return
  102. }
  103. var proCode string
  104. blCode := &util.Block{}
  105. if newCon = projectcodeRegAll.FindString(newCon); newCon != "" { //项目名称项目编号一起的
  106. //5d424bdfa5cb26b9b7ac7a85
  107. //5d425a48a5cb26b9b7df5fec
  108. //5d425506a5cb26b9b7cd2c3c
  109. splitStr := strings.Split(newCon, " ")
  110. if len(splitStr) >= 2 {
  111. newCon = "项目编号:" + splitStr[len(splitStr)-1]
  112. } else if len(splitStr) == 1 {
  113. if tmpstr := projectcodeRegAll2.FindString(splitStr[0]); tmpstr != "" {
  114. newCon = "项目编号:" + tmpstr
  115. }else if strings.Contains(newCon,"、"){
  116. tmpstrs :=strings.Split(newCon,"、")
  117. newCon = "项目编号:" + tmpstrs[len(tmpstrs)-1]
  118. }
  119. }
  120. }
  121. proCode = projectcodeReg.FindString(newCon)
  122. if proCode != "" {
  123. ckv := GetKVAll(proCode, job.Title, nil, 1)
  124. blCode.ColonKV = ckv
  125. blCode.Text = proCode
  126. job.Block = append(job.Block, blCode)
  127. } else if proCode = projectcodeReg2.FindString(newCon); proCode != "" {
  128. ckv := GetKVAll(proCode, job.Title, nil, 1)
  129. blCode.ColonKV = ckv
  130. blCode.Text = proCode
  131. job.Block = append(job.Block, blCode)
  132. } else if proCode = projectcodeReg3.FindString(newCon); proCode != "" {
  133. ckv := GetKVAll(proCode, job.Title, nil, 1)
  134. blCode.Text = proCode
  135. blCode.ColonKV = ckv
  136. job.Block = append(job.Block, blCode)
  137. }
  138. if proCode = jsonReg.FindString(newCon); proCode != "" {
  139. jsonMap := make(map[string]string)
  140. json.Unmarshal([]byte(proCode), &jsonMap)
  141. jobKv := util.NewJobKv()
  142. kvTags := map[string][]*util.Tag{}
  143. for k, v := range jsonMap {
  144. kvTags[k] = append(kvTags[k], &util.Tag{Key: k, Value: v})
  145. tmpkv := new(util.Kv)
  146. tmpkv.Line = k + v
  147. tmpkv.Key = k
  148. tmpkv.Value = v
  149. jobKv.Kvs = append(jobKv.Kvs, tmpkv)
  150. }
  151. jobKv.KvTags = kvTags
  152. blCode.ColonKV = jobKv
  153. job.Block = append(job.Block, blCode)
  154. }
  155. }
  156. //分析table解析结果
  157. func processTableResult(tabres *TableResult, block *util.Block, job *util.Job) {
  158. //解析结果中的kv
  159. if block.TableKV == nil {
  160. block.TableKV = util.NewJobKv()
  161. }
  162. MergeKvTags(block.TableKV.KvTags, tabres.KvTags)
  163. //分包
  164. tablePackage := map[string]*util.BlockPackage{}
  165. if tabres.IsMultiPackage {
  166. //分包中的map
  167. for k, v := range tabres.PackageMap.Map {
  168. blockPackage, ok := v.(*util.BlockPackage)
  169. if !ok {
  170. continue
  171. }
  172. //解析kv
  173. //找到key是“包1中标单位”这种的key,过滤掉包1,再次到标签库中匹配
  174. labelKVs := []*util.Kv{}
  175. if blockPackage.TableKV != nil {
  176. for tk, tv := range blockPackage.TableKV.KvTags {
  177. for _, tvv := range tv {
  178. if regReplKey.MatchString(tk) || regSplit.MatchString(tk) {
  179. labelKVs = append(labelKVs, &util.Kv{
  180. Key: tk,
  181. Value: tvv.Value,
  182. })
  183. }
  184. }
  185. }
  186. } else {
  187. blockPackage.TableKV = util.NewJobKv()
  188. }
  189. MergeKvTags(blockPackage.TableKV.KvTags, GetKvTags(labelKVs, "", nil))
  190. tablePackage[k] = blockPackage
  191. }
  192. }
  193. //处理中标人排序
  194. wror := []map[string]interface{}{}
  195. for _, v := range tabres.WinnerOrder {
  196. entName, _ := v["entname"].(string)
  197. v["entname"] = winnerOrderEntity.clear("中标单位", entName)
  198. if price, ok := v["price"].(string); ok {
  199. v["price"] = winnerOrderEntity.clear("中标金额", price)
  200. }
  201. v["type"] = 2
  202. wror = append(wror, v)
  203. }
  204. if len(wror) > 0 {
  205. job.Winnerorder = wror
  206. }
  207. //分包
  208. if len(tablePackage) > 0 {
  209. pkgMap := map[string]*util.BlockPackage{}
  210. for tk, tv := range tablePackage {
  211. bv := job.BlockPackage[tk]
  212. if bv == nil {
  213. pkgMap[tk] = tv
  214. continue
  215. }
  216. bv.Text += "\n" + tv.Text
  217. /************table中的分包替换块里面找到的****************/
  218. //
  219. if tv.ColonKV != nil {
  220. if bv.ColonKV == nil {
  221. bv.ColonKV = util.NewJobKv()
  222. }
  223. MergeKvTags(bv.ColonKV.KvTags, tv.ColonKV.KvTags)
  224. }
  225. //
  226. if tv.TableKV != nil {
  227. if bv.TableKV == nil {
  228. bv.TableKV = util.NewJobKv()
  229. }
  230. MergeKvTags(bv.TableKV.KvTags, tv.TableKV.KvTags)
  231. }
  232. //
  233. if tv.Origin != "" {
  234. bv.Origin = tv.Origin
  235. }
  236. //
  237. if tv.Index != "" {
  238. bv.Index = tv.Index
  239. }
  240. //
  241. if tv.Type != "" {
  242. bv.Type = tv.Type
  243. }
  244. //
  245. if tv.BidStatus != "" {
  246. bv.BidStatus = tv.BidStatus
  247. }
  248. //
  249. if tv.WinnerOrder != nil && len(tv.WinnerOrder) > 0 {
  250. bv.WinnerOrder = tv.WinnerOrder
  251. }
  252. }
  253. for k, v := range pkgMap {
  254. job.BlockPackage[k] = v
  255. }
  256. }
  257. //增加brand
  258. if tabres.HasKey != 0 {
  259. job.HasKey = tabres.HasKey
  260. }
  261. if tabres.HasBrand != 0 {
  262. job.HasBrand = tabres.HasBrand
  263. }
  264. if tabres.HasGoods != 0 {
  265. job.HasGoods = tabres.HasGoods
  266. }
  267. job.HasGoods = tabres.HasGoods
  268. if len(tabres.BrandData) > 0 { //分块table合并
  269. for _, v := range tabres.BrandData {
  270. job.BrandData = append(job.BrandData, v) //加入job
  271. }
  272. }
  273. }
  274. //一行多列 一列多行,按照分块逻辑处理
  275. //ration==1 遍历所有tabs,ration!=1 tabs只有一个
  276. func tableDivideBlock(con string, ration float32, tabs []*goquery.Selection) string {
  277. if len(tabs) != 1 {
  278. return "" //5c2aca5ea5cb26b9b7a8229b
  279. }
  280. for _, tab := range tabs {
  281. content := ""
  282. tbody := tab.ChildrenFiltered("tbody,thead")
  283. var tr *goquery.Selection
  284. if tbody.Length() == 1 {
  285. tr = tbody.ChildrenFiltered("tr")
  286. } else {
  287. tr = tab.ChildrenFiltered("tr")
  288. }
  289. if tr.Length() == 1 {
  290. tds := tr.ChildrenFiltered("td")
  291. tds.Each(func(index int, sn *goquery.Selection) {
  292. ret, _ := sn.Html()
  293. if strings.TrimSpace(ret) != "" {
  294. content += ret + "\n"
  295. }
  296. })
  297. } else {
  298. flag := true
  299. tr.EachWithBreak(func(index int, sn *goquery.Selection) bool {
  300. th := sn.ChildrenFiltered("th")
  301. td := sn.ChildrenFiltered("td")
  302. if th.Length() > 0 || td.Length() > 1 {
  303. flag = false
  304. return false
  305. } else if td.Length() == 1 {
  306. ret, _ := td.Html()
  307. if strings.TrimSpace(ret) != "" {
  308. content += ret + "\n"
  309. }
  310. }
  311. return true
  312. })
  313. if !flag {
  314. return ""
  315. }
  316. }
  317. if content != "" {
  318. content = regMoreWrap.ReplaceAllString(content, "\n")
  319. content = regEndWrap.ReplaceAllString(content, "")
  320. doc, _ := goquery.NewDocumentFromReader(strings.NewReader(con))
  321. doc.Find("table").Eq(0).ReplaceWithHtml(content)
  322. con, _ = doc.Find("body").Html()
  323. }
  324. }
  325. return con
  326. }
  327. //查找大文本,5次
  328. func FindBigText(con string, r float32, t []*goquery.Selection) (content string, tabs []*goquery.Selection, ration float32) {
  329. content = tableDivideBlock(con, r, t)
  330. if content == "" {
  331. return
  332. }
  333. for i := 0; i < 4; i++ {
  334. if content != "" {
  335. tabs, ration = ComputeConRatio(content, 1)
  336. if len(tabs) > 0 {
  337. con := tableDivideBlock(content, ration, tabs)
  338. if con == "" {
  339. return
  340. } else {
  341. content = con
  342. }
  343. } else {
  344. doc, _ := goquery.NewDocumentFromReader(strings.NewReader(con))
  345. content = doc.Text()
  346. return
  347. }
  348. } else {
  349. return
  350. }
  351. }
  352. return
  353. }