pareExcel.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package main
  2. import (
  3. "github.com/tealeg/xlsx"
  4. "regexp"
  5. "strings"
  6. )
  7. type TagMatching struct {
  8. matchKey string
  9. matchKeyWay []string // title、detail、projectname、filetext
  10. matchKeyReg []*RegexpInfo
  11. addKey string
  12. addKeyWay []string
  13. addKeyReg []*RegexpInfo
  14. excludeKey string //排除词
  15. excludeKeyWay []string //排除词匹配方式
  16. excludeKeyReg []*RegexpInfo
  17. }
  18. type RegexpInfo struct {
  19. keyStr string
  20. regs *regexp.Regexp
  21. }
  22. var MatchWayMap = map[string]string{
  23. "标题匹配": "title",
  24. "全文匹配": "detail",
  25. "附件匹配": "filetext",
  26. "项目名称匹配": "projectname",
  27. "采购单位行业": "buyerclass",
  28. }
  29. func initExcel(path string) {
  30. xlFile, err := xlsx.OpenFile(path)
  31. if err != nil {
  32. return
  33. }
  34. for _, sheet := range xlFile.Sheet {
  35. tagArr = append(tagArr, sheet.Name)
  36. var rules []TagMatching
  37. for rowIndex, row := range sheet.Rows {
  38. if rowIndex == 0 {
  39. continue
  40. }
  41. tag := TagMatching{}
  42. for cellIndex, cell := range row.Cells {
  43. if cell.String() != "" {
  44. if cellIndex == 0 {
  45. tag.matchKey = cell.String()
  46. tag.matchKeyReg = initRegex(cell.String())
  47. } else if cellIndex == 1 {
  48. tag.matchKeyWay = strings.Split(cell.String(), ",")
  49. } else if cellIndex == 2 {
  50. tag.addKey = cell.String()
  51. tag.addKeyReg = initRegex(cell.String())
  52. } else if cellIndex == 3 {
  53. tag.addKeyWay = strings.Split(cell.String(), ",")
  54. } else if cellIndex == 4 {
  55. tag.excludeKey = cell.String()
  56. tag.excludeKeyReg = initRegex(cell.String())
  57. } else if cellIndex == 5 {
  58. tag.excludeKeyWay = strings.Split(cell.String(), ",")
  59. }
  60. }
  61. }
  62. rules = append(rules, tag)
  63. }
  64. TagMatchRule[sheet.Name] = rules
  65. }
  66. }
  67. func initRegex(key string) []*RegexpInfo {
  68. var infos []*RegexpInfo
  69. if strings.Contains(key, ",") {
  70. for _, s := range strings.Split(key, ",") {
  71. if strings.Contains(s, "&&") {
  72. info := &RegexpInfo{
  73. keyStr: s,
  74. regs: nil,
  75. }
  76. infos = append(infos, info)
  77. } else {
  78. info := &RegexpInfo{
  79. keyStr: s,
  80. regs: regexp.MustCompile(".*(?i)" + s + ".*"),
  81. }
  82. infos = append(infos, info)
  83. }
  84. }
  85. } else {
  86. info := &RegexpInfo{
  87. keyStr: key,
  88. regs: regexp.MustCompile(".*(?i)" + key + ".*"),
  89. }
  90. infos = append(infos, info)
  91. }
  92. return infos
  93. }