rule.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package service
  2. import (
  3. "github.com/gogf/gf/v2/container/gvar"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. "github.com/gogf/gf/v2/util/gconv"
  7. "log"
  8. "regexp"
  9. "strings"
  10. )
  11. var (
  12. clearPatterns = g.Config().MustGet(gctx.New(), "clearPatterns").Strings()
  13. bidCommonwealth_blacklistPatterns = g.Config().MustGet(gctx.New(), "bidCommonwealth.blacklistPatterns").Strings()
  14. bidCommonwealth_whitelistPatterns = g.Config().MustGet(gctx.New(), "bidCommonwealth.whitelistPatterns").Strings()
  15. bidCommonwealth_modelPatterns = g.Config().MustGet(gctx.New(), "bidCommonwealth.modelPatterns").Strings()
  16. bidCommonwealth_firstWinnerOrder = g.Config().MustGet(gctx.New(), "bidCommonwealth.firstWinnerOrder").String()
  17. quoteModeRules = g.Config().MustGet(gctx.New(), "quoteMode.rules").Maps()
  18. quoteMode_modelPatterns = g.Config().MustGet(gctx.New(), "quoteMode.modelPatterns").Strings()
  19. showOnlyOnce = [][]string{}
  20. )
  21. func init() {
  22. for _, v := range g.Config().MustGet(gctx.New(), "bidCommonwealth.showOnlyOnce").Array() {
  23. showOnlyOnce = append(showOnlyOnce, gconv.Strings(v))
  24. }
  25. }
  26. type Rule struct{}
  27. // 判断是否是联合体中标
  28. func (r *Rule) Execute(b *BidInfo) (bool, string, int) {
  29. bc := r.bidCommonwealth(b)
  30. quoteMode := r.quoteMode(b)
  31. if bc == -2 && quoteMode == "大模型识别" {
  32. _, quoteMode, bc = (&Model{}).Do(b, 2)
  33. } else if bc == -2 {
  34. _, _, bc = (&Model{}).Do(b, 3)
  35. } else if quoteMode == "大模型识别" {
  36. _, quoteMode, _ = (&Model{}).Do(b, 1)
  37. }
  38. if (quoteMode == "" || quoteMode == "无法识别") && b.Bidamount > 10000 {
  39. quoteMode = "整体报价模式"
  40. }
  41. log.Println(b.Id, "规则", "报价模式", quoteMode, "中标联合体", bc)
  42. return true, quoteMode, bc
  43. }
  44. // 识别中标联合体
  45. func (r *Rule) quoteMode(b *BidInfo) string {
  46. for _, line := range strings.Split(b.Detail, "\n") {
  47. for _, v := range quoteModeRules {
  48. vv := gvar.New(v).MapStrVar()
  49. if r.matchAnyPattern(line, vv["patterns"].Strings()) {
  50. return vv["mode"].String()
  51. }
  52. }
  53. }
  54. if r.matchAnyPattern(b.Detail, quoteMode_modelPatterns) {
  55. return "大模型识别"
  56. }
  57. return ""
  58. }
  59. // 识别中标联合体
  60. func (r *Rule) bidCommonwealth(b *BidInfo) int {
  61. if len(b.WinnerOrder) > 0 {
  62. if regexp.MustCompile(bidCommonwealth_firstWinnerOrder).MatchString(b.WinnerOrder[0]) {
  63. return 1
  64. }
  65. return -1
  66. }
  67. for _, v := range strings.Split(b.KvText, "\n") {
  68. if r.matchAnyPattern(v, bidCommonwealth_whitelistPatterns) {
  69. return 1
  70. }
  71. }
  72. v := b.Detail
  73. // Step 1: 排除黑名单
  74. if r.matchAnyPattern(v, bidCommonwealth_blacklistPatterns) {
  75. return -1
  76. }
  77. if r.matchOnlyOnce(v) {
  78. return 1
  79. }
  80. // Step 2: 精准匹配白名单
  81. if r.matchAnyPattern(v, bidCommonwealth_whitelistPatterns) {
  82. return 1
  83. }
  84. // Step 3: 检查“中标”附近是否有“联合体”
  85. index := strings.Index(v, "中标")
  86. if index != -1 {
  87. start := max(0, index-50)
  88. end := min(len(v), index+50)
  89. contextAroundWin := v[start:end]
  90. if strings.Contains(contextAroundWin, "联合体") {
  91. return 1
  92. }
  93. }
  94. if r.matchAnyPattern(v, bidCommonwealth_modelPatterns) {
  95. return -2
  96. }
  97. return -1
  98. }
  99. // 匹配任意模式
  100. func (r *Rule) matchAnyPattern(text string, patterns []string) bool {
  101. for _, pattern := range patterns {
  102. if strings.HasPrefix(pattern, "全部包含:") {
  103. vs := strings.Split(strings.TrimPrefix(pattern, "全部包含:"), "+")
  104. index := 0
  105. for _, v := range vs {
  106. if strings.Contains(text, v) {
  107. index++
  108. }
  109. }
  110. if index == len(vs) {
  111. log.Println(pattern)
  112. return true
  113. }
  114. continue
  115. }
  116. if matched, _ := regexp.MatchString(pattern, text); matched {
  117. log.Println(pattern)
  118. return true
  119. }
  120. }
  121. return false
  122. }
  123. func (r *Rule) matchOnlyOnce(text string) bool {
  124. for _, v := range showOnlyOnce {
  125. count := 0
  126. for _, vv := range v {
  127. if len(regexp.MustCompile(vv).FindAllString(text, -1)) != 1 {
  128. break
  129. }
  130. count++
  131. }
  132. if count > 0 && count == len(v) {
  133. return true
  134. }
  135. }
  136. return false
  137. }