division.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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,isSite bool) ([]*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,isSite)
  299. bl.SpaceKV = SspacekvEntity.Entrance(newText, bl.Title, contactFormat,isSite)
  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,isSite bool) (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["中标单位"],isSite)
  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,isSite bool) (blockPackage map[string]*util.BlockPackage) {
  554. blockPackage = map[string]*util.BlockPackage{}
  555. //从正文里面找分包
  556. divisionPackageChild(&blockPackage, content, title, true, false,isSite)
  557. return
  558. }
  559. //分块之后分包
  560. func divisionPackageChild(blockPackage *map[string]*util.BlockPackage, content, title string, isFindWinnerOrder, accuracy bool,isSite 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. appendWarpIndex := []int{}
  572. for _, v := range pkg {
  573. //如果文本内容以识别出来的分包标识结尾,不是分包
  574. if len(pkg) == 1 && strings.HasSuffix(con, v[0]) {
  575. return false, ""
  576. }
  577. //
  578. is := regexp.MustCompile(v[0]+"[::]*").FindAllStringIndex(con, -1)
  579. for _, sv := range is {
  580. appendWarpIndex = append(appendWarpIndex, sv[0])
  581. }
  582. }
  583. appendWarpIndex = getPkgIndex(appendWarpIndex)
  584. conTemp := ""
  585. for k, v := range appendWarpIndex {
  586. if k == 0 {
  587. conTemp += con[:v] + "\n"
  588. } else {
  589. conTemp += "\n" + con[appendWarpIndex[k-1]:v]
  590. }
  591. if k == len(appendWarpIndex)-1 {
  592. conTemp += "\n" + con[v:]
  593. }
  594. }
  595. con = conTemp
  596. con = replSerial.ReplaceAllString(con, "\n")
  597. con = regMoreWrap.ReplaceAllString(con, "\n")
  598. //log.Println(con)
  599. //根据分包,找索引位置
  600. indexMap := map[int]int{}
  601. indexKeyStringMap := map[int]string{}
  602. indexKeyIntMap := map[int]int{}
  603. indexs := []int{}
  604. startEndMap := map[int]int{}
  605. pkgIndexMap := map[string][]int{}
  606. indexPkgMap := map[int]string{}
  607. //遍历分包,把kv在包前面的移动到包后面
  608. for _, v := range pkg {
  609. pgflag := v[0] + "[::]*"
  610. is := regexp.MustCompile(pgflag).FindAllStringIndex(con, -1)
  611. for _, sv := range is {
  612. indexMap[sv[0]] = sv[1]
  613. indexs = append(indexs, sv[0])
  614. pkgIndexMap[v[0]] = append(pkgIndexMap[v[0]], sv[0])
  615. indexPkgMap[sv[0]] = v[0]
  616. }
  617. //key在包前面,并且在一行的开头
  618. keys := regexp.MustCompile("([\r\n]|^)([\u4e00-\u9fa5]{2,30}?([((].{1,8}?[))])?[::\\s\u3000\u2003\u00a0]+.*?)"+pgflag).FindAllStringSubmatchIndex(con, -1)
  619. if len(keys) == 0 {
  620. //key在包前面,并且key以冒号结尾
  621. keys = regexp.MustCompile("()([\u4e00-\u9fa5]{2,30}?([((].{1,8}?[))])?[::]+[\\s\u3000\u2003\u00a0]*[\r\n])"+pgflag).FindAllStringSubmatchIndex(con, -1)
  622. }
  623. if len(keys) == 0 {
  624. keys = regexp.MustCompile("()注[::]([\u4e00-\u9fa5]{2,8}?([((].{1,8}?[))])?[\\s\u3000\u2003\u00a0]*[\r\n])"+pgflag).FindAllStringSubmatchIndex(con, -1)
  625. }
  626. for _, key := range keys {
  627. startEndMap[key[5]] = key[4]
  628. //
  629. headkey := con[key[4]:key[5]]
  630. headkey = regReplAllSpace.ReplaceAllString(headkey, "")
  631. if !regDivision.MatchString(headkey) {
  632. headkey += ":"
  633. }
  634. headkey = moreColonReg.ReplaceAllString(headkey, ":")
  635. colonIndexs := regDivision.FindAllStringIndex(headkey, -1)
  636. if len(colonIndexs) > 1 {
  637. headkey = headkey[colonIndexs[len(colonIndexs)-2][1]:colonIndexs[len(colonIndexs)-1][1]]
  638. }
  639. indexKeyStringMap[key[5]] = headkey
  640. indexKeyIntMap[key[5]] = key[1]
  641. }
  642. }
  643. indexs = getPkgIndex(indexs)
  644. for ik, iv := range indexs {
  645. if indexKeyStringMap[iv] != "" {
  646. continue
  647. }
  648. if indexKeyIntMap[iv] == indexMap[iv] {
  649. continue
  650. }
  651. if ik > 0 {
  652. indexKeyStringMap[iv] = indexKeyStringMap[indexs[ik-1]]
  653. }
  654. }
  655. //
  656. //获取截取标识
  657. surplusText, maxWarpCount, indexTextMap, indexWarpMap := interceptText(indexs, indexPkgMap, pkgIndexMap, startEndMap, con)
  658. //查找分包内容,分kv
  659. for _, iv := range indexs {
  660. text := indexTextMap[iv]
  661. //
  662. warpIndex := regSpliteSegment.FindAllStringIndex(text, -1)
  663. if len(indexWarpMap) > 0 {
  664. maxWarpCount = indexWarpMap[iv]
  665. }
  666. if maxWarpCount > 0 && len(warpIndex) >= 5 && len(warpIndex) > maxWarpCount {
  667. textTemp := text
  668. text = textTemp[:warpIndex[maxWarpCount-1][1]]
  669. surplusText += textTemp[warpIndex[maxWarpCount-1][0]:]
  670. }
  671. for bk, bv := range pkg {
  672. //判断分包如果在这段文字里面,该段文字就属于该包的
  673. if !strings.HasPrefix(text, bv[0]) {
  674. continue
  675. }
  676. index := util.PackageNumberConvert(bk)
  677. //去掉前缀,空格必须要加,分kv的时候要用
  678. text = regexp.MustCompile(bv[0]+"[::]*").ReplaceAllString(text, "")
  679. headKey := ""
  680. if indexKeyStringMap[iv] != "" {
  681. //if !filterPkgTitleKey.MatchString(indexKeyStringMap[iv]) {
  682. headKey = indexKeyStringMap[iv]
  683. text = indexKeyStringMap[iv] + " " + text
  684. //}
  685. for _, pkgIndexMap_v := range pkgIndexMap[bv[0]] {
  686. delete(indexKeyStringMap, pkgIndexMap_v)
  687. }
  688. }
  689. //如果一块中有多个相同的包,合并到一个
  690. if (*blockPackage)[index] != nil {
  691. //合并文本
  692. (*blockPackage)[index].Text += "\n" + text
  693. //合并冒号kv
  694. colonJobKv := GetKVAll(strings.TrimLeft(text, headKey), "", nil, 1,isSite)
  695. if headKey != "" {
  696. kvAgain := GetKVAll(text, "", nil, 4,isSite)
  697. MergeKvTags(colonJobKv.KvTags, kvAgain.KvTags)
  698. }
  699. MergeKvTags((*blockPackage)[index].ColonKV.KvTags, colonJobKv.KvTags)
  700. //合并空格kv
  701. spaceJobKv := SspacekvEntity.Entrance(text, "", nil,isSite)
  702. MergeKvTags((*blockPackage)[index].SpaceKV.KvTags, spaceJobKv.KvTags)
  703. } else {
  704. newBpkg := &util.BlockPackage{
  705. Origin: bk,
  706. Text: text,
  707. Index: index,
  708. Type: bv[1],
  709. Accuracy: accuracy,
  710. }
  711. finalKv := GetKVAll(strings.TrimLeft(text, headKey), "", nil, 4,isSite)
  712. if headKey != "" {
  713. kvAgain := GetKVAll(text, "", nil, 4,isSite)
  714. MergeKvTags(finalKv.KvTags, kvAgain.KvTags)
  715. }
  716. newBpkg.ColonKV = finalKv
  717. newBpkg.SpaceKV = SspacekvEntity.Entrance(text, "", nil,isSite)
  718. (*blockPackage)[index] = newBpkg
  719. }
  720. }
  721. }
  722. //中标人排序
  723. if isFindWinnerOrder && blockPackage != nil && len(*blockPackage) > 0 {
  724. for _, v := range *blockPackage {
  725. v.WinnerOrder = winnerOrderEntity.Find(v.Text, true, 2,isSite)
  726. }
  727. }
  728. return true, surplusText
  729. }
  730. func getPkgIndex(indexs []int) []int {
  731. sort.Ints(indexs)
  732. indexsNew := []int{}
  733. count := 0
  734. for k, v := range indexs {
  735. if k > 0 && v-indexs[k-1] <= 10 {
  736. count++
  737. continue
  738. }
  739. indexsNew = append(indexsNew, v)
  740. }
  741. if count > 0 && count == len(indexs)-1 {
  742. return []int{}
  743. }
  744. return indexsNew
  745. }
  746. //每个包对应的结束位置,都是整行结束
  747. 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) {
  748. //util.Debug(con)
  749. surplusText := ""
  750. indexTextMap := map[int]string{}
  751. indexWarpMap := map[int]int{}
  752. maxWarpCount := 0
  753. for ik, iv := range indexs {
  754. text := ""
  755. if ik < len(indexs)-1 {
  756. if startEndMap[indexs[ik+1]] != 0 {
  757. text = con[iv:startEndMap[indexs[ik+1]]]
  758. } else {
  759. text = con[iv:indexs[ik+1]]
  760. }
  761. } else {
  762. text = con[iv:]
  763. }
  764. if strings.Contains(text, "、") {
  765. text = strings.Split(text, "、")[0]
  766. } else if strings.Contains(text, "\n") {
  767. text = strings.Split(text, "\n")[0]
  768. }
  769. indexTextMap[iv] = text
  770. warpCount := len(regSpliteSegment.FindAllStringIndex(text, -1))
  771. if warpCount > maxWarpCount {
  772. maxWarpCount = warpCount
  773. }
  774. indexWarpMap[iv] = warpCount
  775. if ik == 0 {
  776. surplusText += con[:iv]
  777. }
  778. }
  779. pkgLaw := ""
  780. if len(pkgIndexMap) > 1 {
  781. //有规律的出现 AB or ABAB
  782. if pkgLaw == "" {
  783. prevVal := ""
  784. notRepeatCount, currentIndex, onceMax, allMax := 0, -1, 0, 0
  785. indexMaxMap := map[int]int{}
  786. for ik, iv := range indexs {
  787. if notRepeatCount == len(pkgIndexMap) {
  788. notRepeatCount = 0
  789. }
  790. if prevVal != indexPkgMap[iv] {
  791. notRepeatCount++
  792. } else {
  793. notRepeatCount = -1
  794. currentIndex = ik
  795. break
  796. }
  797. prevVal = indexPkgMap[iv]
  798. if notRepeatCount == len(pkgIndexMap) {
  799. indexMaxMap[iv] = onceMax
  800. onceMax = 0
  801. }
  802. if indexWarpMap[iv] > onceMax {
  803. onceMax = indexWarpMap[iv]
  804. allMax = onceMax
  805. }
  806. if ik == len(indexs)-1 && notRepeatCount != len(pkgIndexMap) {
  807. notRepeatCount = -2
  808. currentIndex = ik
  809. }
  810. }
  811. //util.Debug(allMax, currentIndex, indexWarpMap, indexMaxMap)
  812. if len(indexMaxMap) > 0 {
  813. pkgLaw = "AB"
  814. thisMax := 0
  815. for ik := len(indexs) - 1; ik >= 0; ik-- {
  816. iv := indexs[ik]
  817. if currentIndex != -1 && ik >= currentIndex {
  818. indexWarpMap[iv] = allMax
  819. continue
  820. }
  821. if indexMaxMap[iv] > 0 {
  822. thisMax = indexMaxMap[iv]
  823. }
  824. indexWarpMap[iv] = thisMax
  825. }
  826. }
  827. }
  828. }
  829. if pkgLaw == "" {
  830. indexWarpMap = map[int]int{}
  831. }
  832. //util.Debug(pkgLaw, maxWarpCount, indexTextMap, indexWarpMap)
  833. return surplusText, maxWarpCount, indexTextMap, indexWarpMap
  834. }
  835. //分块之后的kv
  836. func kvAfterDivideBlock(tp, text string, from int, ruleBlock *util.RuleBlock,isSite bool) []*util.Kv {
  837. blocks, _ := DivideBlock(tp, text, from, ruleBlock,isSite)
  838. kvs := []*util.Kv{}
  839. for _, v := range blocks {
  840. //util.Debug(v.Text)
  841. // for _, vvv := range v.ColonKV.Kvs {
  842. // util.Debug(vvv.Key, vvv.Value, vvv.Title)
  843. // }
  844. kvs = append(kvs, v.ColonKV.Kvs...)
  845. }
  846. return kvs
  847. }