file.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "os"
  7. "regexp"
  8. "strconv"
  9. "strings"
  10. util "utils"
  11. )
  12. var (
  13. CmmonDFA *DFA //常用字
  14. NotCommonDFA *DFA //不常用字
  15. TimesLimit int //常用字界限
  16. CmmLmt, NcmmLmt float64 //更新界限
  17. HanReg = regexp.MustCompile("[\u4e00-\u9fa5]+") //中文正则
  18. SpaceReg = regexp.MustCompile("[\\s\u3000\u2003\u00a0]+") //空格正则
  19. SpecialReg = regexp.MustCompile("图片(\\d)+") //
  20. )
  21. func InitFileInfo() {
  22. TimesLimit = 500
  23. CmmLmt = 0.5
  24. NcmmLmt = 0.1
  25. CmmonDFA = &DFA{}
  26. NotCommonDFA = &DFA{}
  27. LoadDict("common.txt") //初始化常用字典
  28. }
  29. //DFA
  30. type DFA struct {
  31. Link map[string]interface{}
  32. }
  33. func (d *DFA) AddWord(keys ...string) {
  34. d.AddWordAll(true, keys...)
  35. }
  36. func (d *DFA) AddWordAll(haskey bool, keys ...string) {
  37. if d.Link == nil {
  38. d.Link = make(map[string]interface{})
  39. }
  40. for _, key := range keys {
  41. nowMap := &d.Link
  42. for i := 0; i < len(key); i++ {
  43. kc := key[i : i+1]
  44. if v, ok := (*nowMap)[kc]; ok {
  45. nowMap, _ = v.(*map[string]interface{})
  46. } else {
  47. newMap := map[string]interface{}{}
  48. newMap["YN"] = "0"
  49. (*nowMap)[kc] = &newMap
  50. nowMap = &newMap
  51. }
  52. if i == len(key)-1 {
  53. (*nowMap)["YN"] = "1"
  54. if haskey {
  55. (*nowMap)["K"] = key
  56. }
  57. }
  58. }
  59. }
  60. }
  61. func (d *DFA) CheckSensitiveWord(src string) []string {
  62. res := make([]string, 0)
  63. for j := 0; j < len(src); j++ {
  64. nowMap := &d.Link
  65. for i := j; i < len(src); i++ {
  66. word := src[i : i+1]
  67. nowMap, _ = (*nowMap)[word].(*map[string]interface{})
  68. if nowMap != nil { // 存在,则判断是否为最后一个
  69. if "1" == util.ObjToString((*nowMap)["YN"]) {
  70. s := util.ObjToString((*nowMap)["K"])
  71. res = append(res, s)
  72. }
  73. } else {
  74. break
  75. }
  76. }
  77. }
  78. return res
  79. }
  80. //加载统计的常用词
  81. func LoadDict(path string) {
  82. dictFile, err := os.Open(path)
  83. if err != nil {
  84. util.Debug("Load Common.txt Error")
  85. os.Exit(-1)
  86. }
  87. defer dictFile.Close()
  88. reader := bufio.NewReader(dictFile)
  89. var (
  90. text string
  91. frequency int
  92. )
  93. // 逐行读入分词
  94. line := 0
  95. for {
  96. line++
  97. size, fsErr := fmt.Fscanln(reader, &text, &frequency) //读每行赋值
  98. if fsErr == io.EOF { //读取到结尾
  99. break
  100. }
  101. if size == 2 { //正确数据
  102. if frequency >= TimesLimit { //常用字
  103. CmmonDFA.AddWord(text)
  104. } else { //非常用字
  105. NotCommonDFA.AddWord(text)
  106. }
  107. } else {
  108. util.Debug("Read Line Error:", line)
  109. }
  110. }
  111. }
  112. //解析附件
  113. func AnalysisFile(filetext string) bool {
  114. defer util.Catch()
  115. //过滤空格
  116. filetextTmp := SpaceReg.ReplaceAllString(filetext, "")
  117. if filetextTmp == "" { //附件为空
  118. return false
  119. }
  120. //特殊情况:图片0 图片1
  121. filetextTmp = SpecialReg.ReplaceAllString(filetextTmp, "")
  122. if filetextTmp == "" { //附件为空
  123. return false
  124. }
  125. //中文匹配
  126. HanArr := HanReg.FindAllString(filetextTmp, -1)
  127. hanText := strings.Join(HanArr, "")
  128. hanTextLen := len([]rune(hanText))
  129. //长度过滤
  130. if hanTextLen <= 100 {
  131. return false
  132. }
  133. //qu.Debug(hanTextLen, hanText)
  134. commonArr := CmmonDFA.CheckSensitiveWord(hanText)
  135. commonLen := len(commonArr)
  136. //qu.Debug(commonLen, commonArr)
  137. //commonText := strings.Join(commonArr, "")
  138. notCommonArr := NotCommonDFA.CheckSensitiveWord(hanText)
  139. notCommonLen := len(notCommonArr)
  140. //qu.Debug(notCommonLen, notCommonArr)
  141. //解析常用字和非常用字占比(由于常用字或非常用字集不全,会导致比例相加不为100%)
  142. commonRatio := float64(commonLen) / float64(hanTextLen)
  143. commonRatio, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", commonRatio), 64)
  144. //qu.Debug(commonRatio)
  145. notCommonRatio := float64(notCommonLen) / float64(hanTextLen)
  146. notCommonRatio, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", notCommonRatio), 64)
  147. if commonRatio >= CmmLmt && notCommonRatio < NcmmLmt {
  148. return true
  149. }
  150. return false
  151. }
  152. //测试方法
  153. func AnalysisFileTest(detail string) (bool, string, int, float64, float64) {
  154. //qu.Debug(detail)
  155. defer util.Catch()
  156. //过滤空格
  157. filetextTmp := SpaceReg.ReplaceAllString(detail, "")
  158. if filetextTmp == "" { //附件为空
  159. return false, "", 0, 0, 0
  160. }
  161. //特殊情况:图片0 图片1
  162. filetextTmp = SpecialReg.ReplaceAllString(filetextTmp, "")
  163. if filetextTmp == "" { //附件为空
  164. return false, "", 1, 0, 0
  165. }
  166. //中文匹配
  167. HanArr := HanReg.FindAllString(filetextTmp, -1)
  168. hanText := strings.Join(HanArr, "")
  169. hanTextLen := len([]rune(hanText))
  170. //长度过滤
  171. if hanTextLen <= 100 {
  172. return false, "", 2, 0, 0
  173. }
  174. //qu.Debug(textLen, text)
  175. commonArr := CmmonDFA.CheckSensitiveWord(hanText)
  176. commonLen := len(commonArr)
  177. util.Debug(commonLen, commonArr)
  178. //commonText := strings.Join(commonArr, "")
  179. notCommonArr := NotCommonDFA.CheckSensitiveWord(hanText)
  180. notCommonLen := len(notCommonArr)
  181. util.Debug(notCommonLen, notCommonArr)
  182. //notCommonText := strings.Join(notCommonArr, "")
  183. //解析常用字和非常用字占比(由于常用字或非常用字集不全,会导致比例相加不为100%)
  184. commonRatio := float64(commonLen) / float64(hanTextLen)
  185. commonRatio, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", commonRatio), 64)
  186. notCommonRatio := float64(notCommonLen) / float64(hanTextLen)
  187. notCommonRatio, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", notCommonRatio), 64)
  188. return true, filetextTmp, 10, commonRatio, notCommonRatio
  189. }