analystep.go 9.6 KB

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