interestanalysis.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. *兴趣分析
  3. *
  4. */
  5. package dfa
  6. import (
  7. "log"
  8. "strings"
  9. )
  10. //DFA实现
  11. type DFA struct {
  12. link map[string]interface{} //存放or
  13. linkAnd map[string]int //存放and
  14. linkAndWord map[string]interface{} //存放and中的拆分词
  15. }
  16. //添加词组,用于初始化,该方法是可以调用多次的
  17. func (d *DFA) AddWord(words ...string) {
  18. if d.link == nil {
  19. d.link = make(map[string]interface{})
  20. d.linkAnd = make(map[string]int)
  21. d.linkAndWord = make(map[string]interface{})
  22. }
  23. var nowMap *map[string]interface{}
  24. for _, key := range words {
  25. keys := strings.Split(key, "+")
  26. lenkeys := len(keys)
  27. if lenkeys > 1 {
  28. d.linkAnd[key] = lenkeys
  29. for k := 0; k < lenkeys; k++ {
  30. minKey := keys[k]
  31. nowMap = &d.linkAndWord
  32. for i := 0; i < len(minKey); i++ {
  33. kc := minKey[i : i+1]
  34. if v, ok := (*nowMap)[kc]; ok {
  35. nowMap, _ = v.(*map[string]interface{})
  36. } else {
  37. newMap := map[string]interface{}{}
  38. newMap["YN"] = "N"
  39. (*nowMap)[kc] = &newMap
  40. nowMap = &newMap
  41. }
  42. if i == len(minKey)-1 {
  43. (*nowMap)["YN"] = "Y"
  44. if (*nowMap)["key"] == nil {
  45. (*nowMap)["key"] = make(map[string]int)
  46. }
  47. (*nowMap)["key"].(map[string]int)[key] = k
  48. }
  49. }
  50. }
  51. } else {
  52. nowMap = &d.link
  53. for i := 0; i < len(key); i++ {
  54. kc := key[i : i+1]
  55. if v, ok := (*nowMap)[kc]; ok {
  56. nowMap, _ = v.(*map[string]interface{})
  57. } else {
  58. newMap := map[string]interface{}{}
  59. newMap["YN"] = "N"
  60. (*nowMap)[kc] = &newMap
  61. nowMap = &newMap
  62. }
  63. if i == len(key)-1 {
  64. (*nowMap)["YN"] = "Y"
  65. }
  66. }
  67. }
  68. }
  69. }
  70. func (d *DFA) Clear() {
  71. d.link = nil
  72. }
  73. //从给定的内容中找出匹配上的关键词
  74. func (d *DFA) Analy(src string) []string {
  75. if d.link == nil {
  76. log.Println("请先添加词组")
  77. return []string{}
  78. }
  79. keywords := []string{}
  80. tempMap := make(map[string][]bool)
  81. for i := 0; i < len(src); i++ {
  82. nowMap := &d.link
  83. length := 0 // 匹配标识数默认为0
  84. //flag := false // 敏感词结束标识位:用于敏感词只有1位的情况
  85. for j := i; j < len(src); j++ {
  86. word := src[j : j+1]
  87. nowMap, _ = (*nowMap)[word].(*map[string]interface{})
  88. if nowMap != nil {
  89. length = length + 1
  90. tag, _ := (*nowMap)["YN"].(string)
  91. if "Y" == tag {
  92. //flag = true
  93. keywords = append(keywords, src[i:i+length])
  94. }
  95. } else {
  96. break
  97. }
  98. }
  99. nowMap = &d.linkAndWord
  100. length = 0
  101. for j := i; j < len(src); j++ {
  102. word := src[j : j+1]
  103. nowMap, _ = (*nowMap)[word].(*map[string]interface{})
  104. if nowMap != nil {
  105. length = length + 1
  106. tag, _ := (*nowMap)["YN"].(string)
  107. if "Y" == tag {
  108. mkeys := (*nowMap)["key"].(map[string]int)
  109. for k, v := range mkeys {
  110. tempBool := tempMap[k]
  111. if tempBool == nil {
  112. tempBool = make([]bool, d.linkAnd[k])
  113. tempMap[k] = tempBool
  114. }
  115. tempBool[v] = true
  116. }
  117. }
  118. } else {
  119. break
  120. }
  121. }
  122. }
  123. for k, v := range tempMap {
  124. ball := true
  125. for _, m := range v {
  126. if !m {
  127. ball = false
  128. break
  129. }
  130. }
  131. if ball {
  132. keywords = append(keywords, k)
  133. }
  134. }
  135. return keywords
  136. }