division.go 25 KB

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