division.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. package pretreated
  2. import (
  3. "fmt"
  4. "jy/util"
  5. qutil "qfw/util"
  6. "regexp"
  7. "sort"
  8. "strconv"
  9. "strings"
  10. )
  11. //分块、分段功能
  12. var (
  13. /*regSerialTitles = []string{
  14. "([一二三四五六七八九十]+)[\u3000\u2003\u00a0\\s]*[、..::,](.*)",
  15. "[((]([一二三四五六七八九十]+)[))][\u3000\u2003\u00a0\\s]*[、..::]?(.*)",
  16. "(\\d+)[\u3000\u2003\u00a0\\s]*、(.*)",
  17. "(\\d+)[\u3000\u2003\u00a0\\s]*[..]([^\\d][^\r\n]+)",
  18. "(\\d+)[\u3000\u2003\u00a0\\s]+([^\\d][^\r\n]+)",
  19. "1[..](\\d+)[\u3000\u2003\u00a0\\s]+([^\\d..][^\r\n]+)",
  20. }*/
  21. regSerialTitles_1 = []*regexp.Regexp{
  22. regexp.MustCompile("([\r\n][\u3000\u2003\u00a0\\s]*|^[\u3000\u2003\u00a0\\s]*)([一二三四五六七八九十]+)[\u3000\u2003\u00a0\\s]*[、..::,](.*)"),
  23. regexp.MustCompile("([\r\n][\u3000\u2003\u00a0\\s]*|^[\u3000\u2003\u00a0\\s]*)[((]([一二三四五六七八九十]+)[))][\u3000\u2003\u00a0\\s]*[、..::]?(.*)"),
  24. regexp.MustCompile("([\r\n][\u3000\u2003\u00a0\\s]*|^[\u3000\u2003\u00a0\\s]*)(\\d+)[\u3000\u2003\u00a0\\s]*、(.*)"),
  25. regexp.MustCompile("([\r\n][\u3000\u2003\u00a0\\s]*|^[\u3000\u2003\u00a0\\s]*)(\\d+)[\u3000\u2003\u00a0\\s]*[..]([^\\d][^\r\n]+)"),
  26. regexp.MustCompile("([\r\n][\u3000\u2003\u00a0\\s]*|^[\u3000\u2003\u00a0\\s]*)(\\d+)[\u3000\u2003\u00a0\\s]+([^\\d][^\r\n]+)"),
  27. regexp.MustCompile("([\r\n][\u3000\u2003\u00a0\\s]*|^[\u3000\u2003\u00a0\\s]*)1[..](\\d+)[\u3000\u2003\u00a0\\s]+([^\\d..][^\r\n]+)"),
  28. regexp.MustCompile("([\r\n][\u3000\u2003\u00a0\\s(]*|^[\u3000\u2003\u00a0\\s(]*)(\\d+)[\u3000\u2003\u00a0\\s)]+([^\r\n]+)"),
  29. }
  30. regSerialTitles_2 = []*regexp.Regexp{
  31. regexp.MustCompile("^([一二三四五六七八九十]+)[\u3000\u2003\u00a0\\s]*[、..::,](.*)$"),
  32. regexp.MustCompile("^[((]([一二三四五六七八九十]+)[))][\u3000\u2003\u00a0\\s]*[、..::]?(.*)$"),
  33. regexp.MustCompile("^(\\d+)[\u3000\u2003\u00a0\\s]*、(.*)$"),
  34. regexp.MustCompile("^(\\d+)[\u3000\u2003\u00a0\\s]*[..]([^\\d][^\r\n]+)$"),
  35. regexp.MustCompile("^(\\d+)[\u3000\u2003\u00a0\\s]+([^\\d][^\r\n]+)$"),
  36. regexp.MustCompile("^1[..](\\d+)[\u3000\u2003\u00a0\\s]+([^\\d..][^\r\n]+)$"),
  37. regexp.MustCompile("^[(](\\d+)[\u3000\u2003\u00a0\\s)]+([^\r\n]+)$"),
  38. }
  39. regReplAllTd = regexp.MustCompile("(?smi)<td.*?>.+?</td>")
  40. regIsNumber = regexp.MustCompile("^\\d+$")
  41. regIsChineseNumber = regexp.MustCompile("^[一二三四五六七八九十]+$")
  42. regReplAllSpace = regexp.MustCompile("[\u3000\u2003\u00a0\\s]+")
  43. regTrimSpace = regexp.MustCompile("^[\u3000\u2003\u00a0\\s]+|[\u3000\u2003\u00a0\\s]+$")
  44. regReplWrapSpace = regexp.MustCompile("^[\r\n][\u3000\u2003\u00a0\\s]*|[\r\n][\u3000\u2003\u00a0\\s]*$")
  45. regReplAllSymbol = regexp.MustCompile("[(\\(<《【\\[{{〔)\\)>》】\\]}}〕,,;;::'\"“”。.\\??/+=\\-_——*&……\\^%$¥@#!!`~·]")
  46. regFilterTitle = regexp.MustCompile("[(\\(<《【\\[{{〔].+?[)\\)>》】\\]}}〕]")
  47. regDivision = regexp.MustCompile("[::]")
  48. regSpliteSegment = regexp.MustCompile("[\r\n]")
  49. regFilterNumber = regexp.MustCompile("^[\\d一二三四五六七八九十]+")
  50. regSplit = regexp.MustCompile("或|和|以?及|与|、|或")
  51. regStartWrap = regexp.MustCompile("^[\r\n]")
  52. regEndWrap = regexp.MustCompile("[\r\n]$")
  53. regMoreWrap = regexp.MustCompile("[\r\n]{2,}")
  54. replSerial = regexp.MustCompile("(\r\n|^)([\\d一二三四五六七八九十][、..::,])+\\d")
  55. moreColonReg = regexp.MustCompile("[::]+")
  56. regFilter = regexp.MustCompile("等$")
  57. confusion = map[string]string{
  58. "参与": "canyu",
  59. }
  60. //查找分包之前,先对内容进行预处理
  61. /*
  62. 第一包:采购设备清单
  63. <table></table>
  64. */
  65. regPackageFilter = regexp.MustCompile("([第]?([一二三四五六七八九十0-9A-Za-zⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ]+)((子|合同|分|施工|监理)?(标段?|包|合同段|标包))|((子|合同|分|施工|监理)?(标|包)(段|号)?)[  \u3000\u2003\u00a0]*([一二三四五六七八九十0-9A-Za-zⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ]+)).+[\r\n]?<table>")
  66. filterPkgTitleKey = regexp.MustCompile("结果[::]?$")
  67. xuhao = map[string]bool{
  68. "19968_12289": true,
  69. "19968_46": true,
  70. "20108_12289": true,
  71. "20108_46": true,
  72. "19977_12289": true,
  73. "19977_46": true,
  74. "22235_12289": true,
  75. "22235_46": true,
  76. "20116_12289": true,
  77. "20116_46": true,
  78. "20845_12289": true,
  79. "20845_46": true,
  80. "19971_12289": true,
  81. "19971_46": true,
  82. "20843_12289": true,
  83. "20061_46": true,
  84. }
  85. )
  86. //分块
  87. func DivideBlock(tp, content string, from int, ruleBlock *util.RuleBlock) ([]*util.Block, int) {
  88. defer qutil.Catch()
  89. returnValue := 0
  90. var blocks []*util.Block
  91. if strings.TrimSpace(content) == "" {
  92. return blocks, -1
  93. }
  94. //table里面的内容不考虑,先把table清理掉
  95. //contentTemp := regReplAllTd.ReplaceAllString(content, "")
  96. contentTemp := TextAfterRemoveTable(content)
  97. tdIndexs := regReplAllTd.FindAllStringSubmatchIndex(content, -1)
  98. var regContenSerialTitle *regexp.Regexp
  99. var regSerialTitleIndex int
  100. if ruleBlock != nil && len(ruleBlock.BlockRegs) > 0 {
  101. regContenSerialTitle, regSerialTitleIndex = getSerialType(contentTemp, ruleBlock.BlockRegs)
  102. } else {
  103. regContenSerialTitle, regSerialTitleIndex = getSerialType(contentTemp, regSerialTitles_1)
  104. }
  105. //没有分块
  106. if regSerialTitleIndex == -1 {
  107. if len(contentTemp) == len(content) {
  108. //没有分块
  109. return blocks, -1
  110. } else { //有table
  111. return blocks, -2
  112. }
  113. }
  114. //匹配序号和标题
  115. var regSerialTitle *regexp.Regexp
  116. if ruleBlock != nil && len(ruleBlock.TitleRegs) > 0 {
  117. regSerialTitle = ruleBlock.TitleRegs[regSerialTitleIndex]
  118. } else {
  119. regSerialTitle = regSerialTitles_2[regSerialTitleIndex]
  120. }
  121. indexs := regContenSerialTitle.FindAllStringIndex(content, -1)
  122. indexs = filterSerial(content, indexs, tdIndexs)
  123. //头块
  124. var headBlock, endBlock *util.Block
  125. currentIndex := 0
  126. for k, v := range indexs {
  127. start, end := v[0], v[1]
  128. //添加开头部分
  129. if k == 0 {
  130. if headTemp := content[:start]; regReplAllSpace.ReplaceAllString(headTemp, "") != "" {
  131. headBlock = &util.Block{
  132. Index: -1, //序号
  133. Text: headTemp, //内容
  134. Title: "", //标题
  135. Start: 0,
  136. End: start,
  137. }
  138. }
  139. }
  140. //分块
  141. blockSerialTitle := regTrimSpace.ReplaceAllString(content[start:end], "")
  142. serialTitles := regSerialTitle.FindStringSubmatch(blockSerialTitle) //序号和标题
  143. if len(serialTitles) < 3 {
  144. continue
  145. }
  146. indexSting := regReplAllSpace.ReplaceAllString(serialTitles[1], "") //序号
  147. index := 0
  148. //转成数字序号
  149. if regIsNumber.MatchString(indexSting) {
  150. index, _ = strconv.Atoi(indexSting)
  151. } else if regIsChineseNumber.MatchString(indexSting) {
  152. index = util.ChineseNumberToInt(indexSting)
  153. }
  154. //序号开始就是错误的
  155. if k+1 != index {
  156. if k == 0 {
  157. returnValue = 3
  158. break
  159. } else {
  160. if currentIndex+1 != index {
  161. //如果序号不是连续的,不往下走
  162. returnValue = 2
  163. //添加结尾部分
  164. if from != 3 {
  165. endBlock = &util.Block{
  166. Index: -2, //序号
  167. Text: content[start:], //内容
  168. Title: "", //标题
  169. Start: start,
  170. End: len(content),
  171. }
  172. break
  173. }
  174. }
  175. }
  176. currentIndex = index
  177. }
  178. //
  179. title := serialTitles[2] //标题
  180. title = regTrimSpace.ReplaceAllString(title, "") //清除前后空格
  181. //分块后的块文
  182. nextStart := len(content)
  183. if k < len(indexs)-1 {
  184. nextStart = indexs[k+1][0]
  185. }
  186. //获取块中除了序号和标题的内容
  187. blockText := regTrimSpace.ReplaceAllString(content[end:nextStart], "")
  188. if title != "" {
  189. blockTextTemp := regReplAllSpace.ReplaceAllString(blockText, "")
  190. //特殊情况处理
  191. if blockTextTemp == "" {
  192. if regDivision.MatchString(title) {
  193. /*
  194. 一、项目编号:HMEC170223
  195. 二、项目名称:执法记录仪采购
  196. */
  197. blockText = title
  198. divisionIndexs := regDivision.FindStringIndex(title)
  199. title = title[:divisionIndexs[0]]
  200. } else {
  201. /*
  202. 十一、投标代表须持本人身份证原件亲自递交投标文件,代理机构项目经理审核通过后,办理签收手续,否则投标文件被拒收。
  203. 十二、开标时间:2017年3月20日9时30分
  204. */
  205. blockText = title
  206. title = ""
  207. }
  208. } else if blockTextTemp != "" && regDivision.MatchString(title) {
  209. /*
  210. 2、采购单位名称:福建省汀州医院
  211. 采购单位地址: 龙岩市长汀县
  212. 联系人:胡科长
  213. 联系方式:0597-6826353
  214. */
  215. //多个标题
  216. divisionIndexs := regDivision.FindStringIndex(title)
  217. titleBefore := regReplAllSpace.ReplaceAllString(title[:divisionIndexs[0]], "")
  218. titleAfter := regReplAllSpace.ReplaceAllString(title[divisionIndexs[1]:], "")
  219. blockText = title + "\n" + blockText
  220. if titleAfter != "" {
  221. title = ""
  222. } else {
  223. title = titleBefore
  224. }
  225. } else {
  226. blockText = title + "\n" + blockText
  227. }
  228. }
  229. //没有内容的块,不打标签,不分段
  230. if blockText == "" {
  231. continue
  232. }
  233. //过滤
  234. if regexp.MustCompile("投标文件格式|业绩").MatchString(title) {
  235. continue
  236. }
  237. blockText = hasMergeKV(title, blockText)
  238. //
  239. titleIsExists := map[string]bool{} //去重
  240. title = filterTitle(title)
  241. //分割标题 [和及]。。。 参与
  242. splitTitles := ProcTitle(title)
  243. block := &util.Block{
  244. Index: index, //序号
  245. Text: blockText, //内容
  246. Title: title, //标题
  247. Titles: splitTitles,
  248. Start: start,
  249. End: nextStart,
  250. }
  251. titles := []string{}
  252. for _, sv := range splitTitles {
  253. if sv == "" || titleIsExists[sv] {
  254. continue
  255. }
  256. titleIsExists[sv] = true
  257. //标题过短过长不打标签
  258. if len([]rune(sv)) >= 2 && len([]rune(sv)) <= 10 {
  259. //打标签
  260. block.Tags = append(block.Tags, util.GetBlockTags(sv))
  261. titles = append(titles, sv)
  262. }
  263. }
  264. block.Title = title
  265. block.Titles = titles
  266. block.Classify, block.NotClassifyTitles = ruleBlock.Classify.GetClassify(tp, titles)
  267. tagsToBlocks(blocks, block)
  268. //log.Println(index, sv, splitTitles)
  269. //log.Println(blockText)
  270. blocks = append(blocks, block)
  271. }
  272. var returnBlocks []*util.Block
  273. if len(blocks) > 0 {
  274. //头
  275. if headBlock != nil {
  276. if tp == "招标" {
  277. headBlock.Classify = map[string]bool{"bidcondition": true}
  278. }
  279. returnBlocks = append(returnBlocks, headBlock)
  280. }
  281. //中间块
  282. returnBlocks = append(returnBlocks, blocks...)
  283. //尾
  284. if endBlock != nil {
  285. returnBlocks = append(returnBlocks, endBlock)
  286. }
  287. if returnValue == 0 {
  288. returnValue = 1
  289. }
  290. }
  291. contactFormat := &util.ContactFormat{
  292. IndexMap: map[int]string{},
  293. MatchMap: map[string]map[string]bool{},
  294. }
  295. for _, bl := range returnBlocks {
  296. //解析kv
  297. newText := TextAfterRemoveTable(bl.Text)
  298. bl.ColonKV = GetKVAll(newText, bl.Title, contactFormat, from)
  299. bl.SpaceKV = SspacekvEntity.Entrance(newText, bl.Title, contactFormat)
  300. //正则抽取的时候有时需要匹配换行或者句号,这里在解析完kv之后,在块结尾添加换行和句号
  301. bl.Text = appendWarpStop(bl.Text)
  302. }
  303. return returnBlocks, returnValue
  304. }
  305. //块标题处理
  306. func ProcTitle(title string) []string {
  307. if title == "" {
  308. return []string{}
  309. }
  310. for k, v := range confusion {
  311. title = strings.Replace(title, k, v, -1)
  312. }
  313. direct := 1
  314. prev := ""
  315. ara := regSplit.Split(title, -1)
  316. for kk, vv := range ara {
  317. for kkk, vvv := range confusion {
  318. vv = strings.Replace(vv, vvv, kkk, -1)
  319. }
  320. ara[kk] = vv
  321. if len([]rune(vv)) == 2 {
  322. if kk == 0 {
  323. direct = -1
  324. } else {
  325. start := ""
  326. if len([]rune(prev)) > 3 {
  327. start = string([]rune(prev)[:len([]rune(prev))-2])
  328. }
  329. ara[kk] = start + vv
  330. }
  331. }
  332. if len([]rune(vv)) > 3 {
  333. if direct == -1 {
  334. end := string([]rune(vv)[len([]rune(vv))-2:])
  335. for i := 0; i < kk; i++ {
  336. ara[i] = ara[i] + end
  337. }
  338. break
  339. }
  340. prev = vv
  341. }
  342. }
  343. return ara
  344. }
  345. //有合并kv的 例如项目名称及编号
  346. func hasMergeKV(title, text string) string {
  347. title = regDivision.ReplaceAllString(title, "")
  348. titles := regSplit.Split(title, -1)
  349. if len(titles) <= 1 {
  350. return text
  351. }
  352. before := titles[0]
  353. after := titles[1]
  354. if strings.Contains(title, "项目") && len([]rune(after)) == 2 {
  355. after = "项目" + after
  356. } else {
  357. return text
  358. }
  359. if strings.Count(text, "\n") != 1 {
  360. return text
  361. }
  362. texts := strings.Split(text, "\n")
  363. textOneLine := texts[0]
  364. textTwoLine := texts[1]
  365. if regDivision.MatchString(textTwoLine) {
  366. return text
  367. }
  368. if textTwoLine := strings.SplitN(textTwoLine, ",", 2); len(textTwoLine) == 2 {
  369. text = textOneLine + "\n" + before + ":" + textTwoLine[0] + "," + after + ":" + textTwoLine[1]
  370. }
  371. return text
  372. }
  373. //过滤序号,判断序号是不是在td里,如果是的话这个序号作废
  374. func filterSerial(content string, indexs, tdIndexs [][]int) [][]int {
  375. returnIndexs := [][]int{}
  376. for _, v := range indexs {
  377. flag := false
  378. //根据序号的开始位置,判断是不是在td里面
  379. for _, tv := range tdIndexs {
  380. if v[0] > tv[0] && v[0] < tv[1] {
  381. flag = true
  382. continue
  383. }
  384. }
  385. if flag {
  386. continue
  387. }
  388. returnIndexs = append(returnIndexs, []int{v[0], v[1]})
  389. }
  390. return returnIndexs
  391. }
  392. //获取正文所用的序号类型
  393. func getSerialType(content string, blockRegs []*regexp.Regexp) (*regexp.Regexp, int) {
  394. var regContenSerialTitle *regexp.Regexp
  395. //先判断文章最外层使用的是哪种序号
  396. contentStartIndex, regSerialTitleIndex := -1, -1
  397. for k, v := range blockRegs {
  398. indexs := v.FindStringIndex(content)
  399. //只用最外层的序号,里面的过滤掉
  400. if len(indexs) == 2 && !regSpliteSegment.MatchString(strings.TrimSpace(content[indexs[0]:indexs[1]])) && (contentStartIndex == -1 || indexs[0] < contentStartIndex) {
  401. regSerialTitleIndex = k
  402. contentStartIndex = indexs[0]
  403. regContenSerialTitle = v
  404. }
  405. }
  406. return regContenSerialTitle, regSerialTitleIndex
  407. }
  408. //添加换行和句号
  409. func appendWarpStop(text string) string {
  410. //清理前后空格
  411. text = regTrimSpace.ReplaceAllString(text, "")
  412. //添加句号
  413. if !strings.HasSuffix(text, "。") {
  414. text += "。"
  415. }
  416. //添加换行
  417. if !regEndWrap.MatchString(text) {
  418. text += "\n"
  419. }
  420. return text
  421. }
  422. //分段
  423. func DivideSegmentHtml(txt string) []*util.Segment {
  424. //先分段
  425. _segs := strings.FieldsFunc(txt, func(r rune) bool {
  426. return r == 10 || r == 13
  427. })
  428. //再去除空行
  429. segs := make([]*util.Segment, 0)
  430. _index := 0
  431. for _, seg := range _segs {
  432. if seg != " " && len(seg) > 1 {
  433. _seg := util.Segment{}
  434. _index = _index + 1
  435. _seg.Index = _index
  436. _seg.Text = seg
  437. segs = append(segs, &_seg)
  438. }
  439. }
  440. return segs
  441. }
  442. //分段
  443. func DivideSegment(txt string) []*util.Segment {
  444. //先分段
  445. tmpstr := ""
  446. _segs := strings.FieldsFunc(txt, func(r rune) bool {
  447. if r == 19968 || r == 20108 || r == 19977 || r == 12289 || r == 46 ||
  448. r == 22235 || r == 20116 || r == 20845 || r == 19971 || r == 20843 || r == 20061 {
  449. if tmpstr == "" {
  450. tmpstr += fmt.Sprint(r)
  451. return false
  452. } else if strings.Contains(tmpstr, "_") {
  453. tmpstr = ""
  454. tmpstr += fmt.Sprint(r)
  455. return false
  456. } else if tmpstr == fmt.Sprint(r) {
  457. if r == 46 || r == 12289 {
  458. tmpstr = ""
  459. }
  460. return false
  461. }
  462. tmpstr += "_" + fmt.Sprint(r)
  463. if xuhao[tmpstr] {
  464. return true
  465. }
  466. }
  467. tmpstr = ""
  468. return r == 10 || r == 13
  469. })
  470. //再去除空行
  471. segs := make([]*util.Segment, 0)
  472. _index := 0
  473. for _, seg := range _segs {
  474. if seg != " " && len(seg) > 1 {
  475. _seg := util.Segment{}
  476. _index = _index + 1
  477. _seg.Index = _index
  478. _seg.Text = seg
  479. segs = append(segs, &_seg)
  480. }
  481. }
  482. return segs
  483. }
  484. /** 给块打标签 **/
  485. func tagsToBlocks(blocks []*util.Block, block *util.Block) {
  486. if len(block.Tags) == 0 {
  487. return
  488. }
  489. tag := map[string]bool{}
  490. tagWeight := map[string]int{}
  491. for _, v := range block.Tags {
  492. for _, ts := range v {
  493. tag[ts.Value] = true
  494. tagWeight[ts.Value] = ts.Weight
  495. }
  496. }
  497. for v, _ := range tag {
  498. for _, block := range blocks {
  499. if block.Tag[v] {
  500. for _, blockTags := range block.Tags {
  501. for _, ts := range blockTags {
  502. if ts.Value == v && ts.Weight < tagWeight[v] {
  503. block.Tag[v] = false
  504. }
  505. }
  506. }
  507. }
  508. }
  509. }
  510. block.Tag = tag
  511. }
  512. func filterTitle(title string) string {
  513. if strings.Contains(title, ",") && strings.Contains(title, "。") {
  514. return ""
  515. }
  516. if len([]rune(title)) > 30 {
  517. return ""
  518. }
  519. //清理空格
  520. title = regReplAllSpace.ReplaceAllString(title, "")
  521. //清理成对出现的符号中的内容
  522. title = regFilterTitle.ReplaceAllString(title, "")
  523. //清理特殊符号
  524. title = regReplAllSymbol.ReplaceAllString(title, "")
  525. //清理序号
  526. title = regFilterNumber.ReplaceAllString(title, "")
  527. title = regFilter.ReplaceAllString(title, "")
  528. return title
  529. }
  530. //从块里面找分包
  531. func FindPackageFromBlocks(blocks *[]*util.Block) (blockPackage map[string]*util.BlockPackage) {
  532. blockPackage = map[string]*util.BlockPackage{}
  533. //块分包
  534. for _, v := range *blocks {
  535. text := regPackageFilter.ReplaceAllString(v.Text, "<table>")
  536. text = TextAfterRemoveTable(text)
  537. if text == "" {
  538. continue
  539. }
  540. //var ok bool
  541. //var surplusText string
  542. divisionPackageChild(&blockPackage, text, v.Title, true, v.Tag["中标单位"])
  543. ////把分包内容摘除掉有问题 有的项目名称中包含二标段
  544. //if ok && false {
  545. // v.Text = surplusText
  546. // v.ColonKV = GetKVAll(surplusText, v.Title, nil, 1)
  547. // v.SpaceKV = SspacekvEntity.Entrance(surplusText, v.Title, nil)
  548. //}
  549. }
  550. return
  551. }
  552. //从正文里面找分包
  553. func FindPackageFromText(title string, content string) (blockPackage map[string]*util.BlockPackage) {
  554. blockPackage = map[string]*util.BlockPackage{}
  555. //从正文里面找分包
  556. divisionPackageChild(&blockPackage, content, title, true, false)
  557. return
  558. }
  559. //分块之后分包
  560. func divisionPackageChild(blockPackage *map[string]*util.BlockPackage, content, title string, isFindWinnerOrder, accuracy bool) (bool, string) {
  561. //查找知否有分包
  562. content = regMoreWrap.ReplaceAllString(content, "\n")
  563. content = regEndWrap.ReplaceAllString(content, "")
  564. con, pkg, flag := CheckMultiPackage(content, title)
  565. if !flag {
  566. return false, ""
  567. }
  568. // util.Debug(con)
  569. // util.Debug(pkg)
  570. //分包前面添加换行
  571. //log.Println(con)
  572. bools := make(map[string]bool)
  573. for k, v := range pkg {
  574. //如果文本内容以识别出来的分包标识结尾,不是分包
  575. if len(pkg) == 1 && strings.HasSuffix(con, v[0]) {
  576. return false, ""
  577. }
  578. //
  579. is := regexp.MustCompile(v[0] + "[::]*").FindAllString(con, -1)
  580. for _, sv := range is {
  581. newBpkg := &util.BlockPackage{
  582. Origin: sv,
  583. Text: con,
  584. Index: k,
  585. Accuracy: false,
  586. }
  587. if (*blockPackage)[k] == nil && !bools[sv] {
  588. bools[sv] = true
  589. con = strings.ReplaceAll(con, sv, "\n")
  590. //log.Println(k, con)
  591. kvAll := GetKVAll(con, title, nil, 4)
  592. newBpkg.ColonKV = kvAll
  593. newBpkg.SpaceKV = SspacekvEntity.Entrance(con, "", nil)
  594. (*blockPackage)[k] = newBpkg
  595. } else if (*blockPackage)[k].ColonKV != nil {
  596. kvAll := GetKVAll(con, title, nil, 4)
  597. MergeKvTags((*blockPackage)[k].ColonKV.KvTags, kvAll.KvTags)
  598. }
  599. for kk, vv := range (*blockPackage)[k].ColonKV.KvTags {
  600. for _, vvvv := range vv {
  601. //log.Println(kk,vv,kkk,vvvv.Value,vvvv.Key)
  602. //if kk == "项目编号" && (*blockPackage)[k].Origin == "" {
  603. // (*blockPackage)[k].Origin = strings.TrimRightFunc(vvvv.Value, func(r rune) bool {
  604. // return r == 65289 || r == 41 || r == 46 || r == 12290
  605. // })
  606. // break
  607. //} else
  608. if kk == "项目名称" && (*blockPackage)[k].Name == "" {
  609. (*blockPackage)[k].Name = vvvv.Value
  610. break
  611. } else if kk == "预算" && (*blockPackage)[k].Budget == 0 {
  612. (*blockPackage)[k].Budget = qutil.Float64All(vvvv.Value)
  613. break
  614. } else if kk == "中标单位" && (*blockPackage)[k].Winner == "" {
  615. (*blockPackage)[k].Winner = vvvv.Value
  616. break
  617. } else if kk == "标段类型" && (*blockPackage)[k].Type == "" {
  618. (*blockPackage)[k].Type = vvvv.Value
  619. break
  620. } else if kk == "中标金额" && (*blockPackage)[k].Bidamount == 0 {
  621. (*blockPackage)[k].Bidamount = qutil.Float64All(vvvv.Value)
  622. break
  623. }
  624. }
  625. }
  626. }
  627. }
  628. //log.Println(con)
  629. //con = replSerial.ReplaceAllString(con, "\n")
  630. //con = regMoreWrap.ReplaceAllString(con, "\n")
  631. ////util.Debug(con)
  632. ////根据分包,找索引位置
  633. //indexMap := map[int]int{}
  634. //indexKeyStringMap := map[int]string{}
  635. //indexKeyIntMap := map[int]int{}
  636. //indexs := []int{}
  637. //startEndMap := map[int]int{}
  638. //pkgIndexMap := map[string][]int{}
  639. //indexPkgMap := map[int]string{}
  640. ////遍历分包,把kv在包前面的移动到包后面
  641. //for _, v := range pkg {
  642. // pgflag := v[0] + "[::]*"
  643. // is := regexp.MustCompile(pgflag).FindAllStringIndex(con, -1)
  644. // for _, sv := range is {
  645. // indexMap[sv[0]] = sv[1]
  646. // indexs = append(indexs, sv[0])
  647. // pkgIndexMap[v[0]] = append(pkgIndexMap[v[0]], sv[0])
  648. // indexPkgMap[sv[0]] = v[0]
  649. // }
  650. // //key在包前面,并且在一行的开头
  651. // keys := regexp.MustCompile("([\r\n]|^)([\u4e00-\u9fa5]{2,30}?([((].{1,8}?[))])?[::\\s\u3000\u2003\u00a0]+.*?)"+pgflag).FindAllStringSubmatchIndex(con, -1)
  652. // if len(keys) == 0 {
  653. // //key在包前面,并且key以冒号结尾
  654. // keys = regexp.MustCompile("()([\u4e00-\u9fa5]{2,30}?([((].{1,8}?[))])?[::]+[\\s\u3000\u2003\u00a0]*[\r\n])"+pgflag).FindAllStringSubmatchIndex(con, -1)
  655. // }
  656. // if len(keys) == 0 {
  657. // keys = regexp.MustCompile("()注[::]([\u4e00-\u9fa5]{2,8}?([((].{1,8}?[))])?[\\s\u3000\u2003\u00a0]*[\r\n])"+pgflag).FindAllStringSubmatchIndex(con, -1)
  658. // }
  659. // for _, key := range keys {
  660. // startEndMap[key[5]] = key[4]
  661. // //
  662. // headkey := con[key[4]:key[5]]
  663. // headkey = regReplAllSpace.ReplaceAllString(headkey, "")
  664. // if !regDivision.MatchString(headkey) {
  665. // headkey += ":"
  666. // }
  667. // headkey = moreColonReg.ReplaceAllString(headkey, ":")
  668. // colonIndexs := regDivision.FindAllStringIndex(headkey, -1)
  669. // if len(colonIndexs) > 1 {
  670. // headkey = headkey[colonIndexs[len(colonIndexs)-2][1]:colonIndexs[len(colonIndexs)-1][1]]
  671. // }
  672. // indexKeyStringMap[key[5]] = headkey
  673. // indexKeyIntMap[key[5]] = key[1]
  674. // }
  675. //}
  676. //indexs = getPkgIndex(indexs)
  677. //for ik, iv := range indexs {
  678. // if indexKeyStringMap[iv] != "" {
  679. // continue
  680. // }
  681. // if indexKeyIntMap[iv] == indexMap[iv] {
  682. // continue
  683. // }
  684. // if ik > 0 {
  685. // indexKeyStringMap[iv] = indexKeyStringMap[indexs[ik-1]]
  686. // }
  687. //}
  688. ////
  689. ////获取截取标识
  690. //surplusText, maxWarpCount, indexTextMap, indexWarpMap := interceptText(indexs, indexPkgMap, pkgIndexMap, startEndMap, con)
  691. ////查找分包内容,分kv
  692. //for _, iv := range indexs {
  693. // text := indexTextMap[iv]
  694. // //
  695. // warpIndex := regSpliteSegment.FindAllStringIndex(text, -1)
  696. // if len(indexWarpMap) > 0 {
  697. // maxWarpCount = indexWarpMap[iv]
  698. // }
  699. // if maxWarpCount > 0 && len(warpIndex) >= 5 && len(warpIndex) > maxWarpCount {
  700. // textTemp := text
  701. // text = textTemp[:warpIndex[maxWarpCount-1][1]]
  702. // surplusText += textTemp[warpIndex[maxWarpCount-1][0]:]
  703. // }
  704. // for bk, bv := range pkg {
  705. // //判断分包如果在这段文字里面,该段文字就属于该包的
  706. // if !strings.HasPrefix(text, bv[0]) {
  707. // continue
  708. // }
  709. // index := util.PackageNumberConvert(bk)
  710. // //去掉前缀,空格必须要加,分kv的时候要用
  711. // text = regexp.MustCompile(bv[0]+"[::]*").ReplaceAllString(text, "")
  712. // headKey := ""
  713. // if indexKeyStringMap[iv] != "" {
  714. // //if !filterPkgTitleKey.MatchString(indexKeyStringMap[iv]) {
  715. // headKey = indexKeyStringMap[iv]
  716. // text = indexKeyStringMap[iv] + " " + text
  717. // //}
  718. // for _, pkgIndexMap_v := range pkgIndexMap[bv[0]] {
  719. // delete(indexKeyStringMap, pkgIndexMap_v)
  720. // }
  721. // }
  722. // //如果一块中有多个相同的包,合并到一个
  723. // if (*blockPackage)[index] != nil {
  724. // //合并文本
  725. // (*blockPackage)[index].Text += "\n" + text
  726. // //合并冒号kv
  727. // colonJobKv := GetKVAll(strings.TrimLeft(text, headKey), "", nil, 1)
  728. // if headKey != "" {
  729. // kvAgain := GetKVAll(text, "", nil, 4)
  730. // MergeKvTags(colonJobKv.KvTags, kvAgain.KvTags)
  731. // }
  732. // MergeKvTags((*blockPackage)[index].ColonKV.KvTags, colonJobKv.KvTags)
  733. // //合并空格kv
  734. // spaceJobKv := SspacekvEntity.Entrance(text, "", nil)
  735. // MergeKvTags((*blockPackage)[index].SpaceKV.KvTags, spaceJobKv.KvTags)
  736. // } else {
  737. // newBpkg := &util.BlockPackage{
  738. // Origin: bk,
  739. // Text: text,
  740. // Index: index,
  741. // Type: bv[1],
  742. // Accuracy: accuracy,
  743. // }
  744. // finalKv := GetKVAll(strings.TrimLeft(text, headKey), "", nil, 4)
  745. // if headKey != "" {
  746. // kvAgain := GetKVAll(text, "", nil, 4)
  747. // MergeKvTags(finalKv.KvTags, kvAgain.KvTags)
  748. // }
  749. // newBpkg.ColonKV = finalKv
  750. // newBpkg.SpaceKV = SspacekvEntity.Entrance(text, "", nil)
  751. // (*blockPackage)[index] = newBpkg
  752. // }
  753. // }
  754. //}
  755. //中标人排序
  756. if !strings.Contains(title, "招标") && isFindWinnerOrder && blockPackage != nil && len(*blockPackage) > 0 {
  757. for _, v := range *blockPackage {
  758. v.WinnerOrder = winnerOrderEntity.Find(v.Text, true, 2)
  759. }
  760. }
  761. return true, con
  762. }
  763. func getPkgIndex(indexs []int) []int {
  764. sort.Ints(indexs)
  765. indexsNew := []int{}
  766. count := 0
  767. for k, v := range indexs {
  768. if k > 0 && v-indexs[k-1] <= 10 {
  769. count++
  770. continue
  771. }
  772. indexsNew = append(indexsNew, v)
  773. }
  774. if count > 0 && count == len(indexs)-1 {
  775. return []int{}
  776. }
  777. return indexsNew
  778. }
  779. //每个包对应的结束位置,都是整行结束
  780. func interceptText(indexs []int, indexPkgMap map[int]string, pkgIndexMap map[string][]int, startEndMap map[int]int, con string) (string, int, map[int]string, map[int]int) {
  781. //util.Debug(con)
  782. surplusText := ""
  783. indexTextMap := map[int]string{}
  784. indexWarpMap := map[int]int{}
  785. maxWarpCount := 0
  786. for ik, iv := range indexs {
  787. text := ""
  788. if ik < len(indexs)-1 {
  789. if startEndMap[indexs[ik+1]] != 0 {
  790. text = con[iv:startEndMap[indexs[ik+1]]]
  791. } else {
  792. text = con[iv:indexs[ik+1]]
  793. }
  794. } else {
  795. text = con[iv:]
  796. }
  797. if strings.Contains(text, "、") {
  798. text = strings.Split(text, "、")[0]
  799. } else if strings.Contains(text, "\n") {
  800. text = strings.Split(text, "\n")[0]
  801. }
  802. indexTextMap[iv] = text
  803. warpCount := len(regSpliteSegment.FindAllStringIndex(text, -1))
  804. if warpCount > maxWarpCount {
  805. maxWarpCount = warpCount
  806. }
  807. indexWarpMap[iv] = warpCount
  808. if ik == 0 {
  809. surplusText += con[:iv]
  810. }
  811. }
  812. pkgLaw := ""
  813. if len(pkgIndexMap) > 1 {
  814. //有规律的出现 AB or ABAB
  815. if pkgLaw == "" {
  816. prevVal := ""
  817. notRepeatCount, currentIndex, onceMax, allMax := 0, -1, 0, 0
  818. indexMaxMap := map[int]int{}
  819. for ik, iv := range indexs {
  820. if notRepeatCount == len(pkgIndexMap) {
  821. notRepeatCount = 0
  822. }
  823. if prevVal != indexPkgMap[iv] {
  824. notRepeatCount++
  825. } else {
  826. notRepeatCount = -1
  827. currentIndex = ik
  828. break
  829. }
  830. prevVal = indexPkgMap[iv]
  831. if notRepeatCount == len(pkgIndexMap) {
  832. indexMaxMap[iv] = onceMax
  833. onceMax = 0
  834. }
  835. if indexWarpMap[iv] > onceMax {
  836. onceMax = indexWarpMap[iv]
  837. allMax = onceMax
  838. }
  839. if ik == len(indexs)-1 && notRepeatCount != len(pkgIndexMap) {
  840. notRepeatCount = -2
  841. currentIndex = ik
  842. }
  843. }
  844. //util.Debug(allMax, currentIndex, indexWarpMap, indexMaxMap)
  845. if len(indexMaxMap) > 0 {
  846. pkgLaw = "AB"
  847. thisMax := 0
  848. for ik := len(indexs) - 1; ik >= 0; ik-- {
  849. iv := indexs[ik]
  850. if currentIndex != -1 && ik >= currentIndex {
  851. indexWarpMap[iv] = allMax
  852. continue
  853. }
  854. if indexMaxMap[iv] > 0 {
  855. thisMax = indexMaxMap[iv]
  856. }
  857. indexWarpMap[iv] = thisMax
  858. }
  859. }
  860. }
  861. }
  862. if pkgLaw == "" {
  863. indexWarpMap = map[int]int{}
  864. }
  865. //util.Debug(pkgLaw, maxWarpCount, indexTextMap, indexWarpMap)
  866. return surplusText, maxWarpCount, indexTextMap, indexWarpMap
  867. }
  868. //分块之后的kv
  869. func kvAfterDivideBlock(tp, text string, from int, ruleBlock *util.RuleBlock) []*util.Kv {
  870. blocks, _ := DivideBlock(tp, text, from, ruleBlock)
  871. kvs := []*util.Kv{}
  872. for _, v := range blocks {
  873. //util.Debug(v.Text)
  874. // for _, vvv := range v.ColonKV.Kvs {
  875. // util.Debug(vvv.Key, vvv.Value, vvv.Title)
  876. // }
  877. kvs = append(kvs, v.ColonKV.Kvs...)
  878. }
  879. return kvs
  880. }