dfa.go 3.1 KB

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