util.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package main
  2. import (
  3. "regexp"
  4. "strconv"
  5. "strings"
  6. )
  7. type TagMatching struct {
  8. tagName string // 标签名称
  9. tagCode string // 标签值(保存)
  10. matchField []string //关键词匹配字段
  11. matchKey string //匹配词
  12. matchKeyReg []*RegexpInfo
  13. addField []string //附加词匹配字段
  14. addKey string // 附件词匹配词
  15. addKeyReg []*RegexpInfo
  16. excludeField []string //排除词
  17. excludeKey string //排除词匹配词
  18. excludeKeyReg []*RegexpInfo
  19. //clearField []string // 清理词匹配字段
  20. clearKey []string //清理词匹配字段跟关键词一样
  21. }
  22. type RegexpInfo struct {
  23. keyStr string
  24. regs *regexp.Regexp
  25. }
  26. func GetRegex(key string) []*RegexpInfo {
  27. var infos []*RegexpInfo
  28. for _, s := range strings.Split(key, ",") {
  29. if strings.Contains(s, "&&") {
  30. info := &RegexpInfo{
  31. keyStr: s,
  32. regs: nil,
  33. }
  34. infos = append(infos, info)
  35. } else {
  36. info := &RegexpInfo{
  37. keyStr: s,
  38. regs: regexp.MustCompile(".*(?i)" + s + ".*"),
  39. }
  40. infos = append(infos, info)
  41. }
  42. }
  43. return infos
  44. }
  45. var (
  46. regNumFloat, _ = regexp.Compile(`([1-9]\d*|0)(\.\d+)?`)
  47. regStrUnit, _ = regexp.Compile(`[元|万|亿]`)
  48. contentUnit, _ = regexp.Compile(`(万元|单位/万)`)
  49. numCapitals, _ = regexp.Compile(`([〇|零|点|壹|贰|叁|肆|伍|陆|柒|捌|玖|拾|百|佰|千|仟|万|亿|億|元|圆|角|分|整|正]{4,40})`)
  50. regStrChar = `[〇|零|点|壹|贰|叁|肆|伍|陆|柒|捌|玖|拾|百|佰|千|仟|万|亿|億|元|圆|角|分|整|正]`
  51. moneyRegChar, _ = regexp.Compile(regStrChar)
  52. regQianw, _ = regexp.Compile(`\d{1,2}千万`)
  53. cutAllSpace, _ = regexp.Compile(`\s*`)
  54. spaces = []string{"\u3000", "\u2003", "\u00a0", "\t", "\r", "\n"}
  55. moneyChar = map[string]interface{}{ //"〇": "0", "零": "0",
  56. "一": float64(1), "壹": float64(1), "二": float64(2), "贰": float64(2), "三": float64(3), "叁": float64(3), "四": float64(4), "肆": float64(4), "五": float64(5), "伍": float64(5),
  57. "六": float64(6), "陆": float64(6), "七": float64(7), "柒": float64(7), "八": float64(8), "捌": float64(8), "九": float64(9), "玖": float64(9), "十": float64(10), "拾": float64(10),
  58. "百": float64(100), "佰": float64(100), "千": float64(1000), "仟": float64(1000), "万": float64(10000), "亿": float64(100000000), "億": float64(100000000),
  59. "零": float64(0), "点": ".", "角": float64(0.1), "分": float64(0.01),
  60. }
  61. moneyUnit = map[string]float64{
  62. "元": float64(1), "万": float64(10000), "亿": float64(100000000), "億": float64(100000000), //单位
  63. }
  64. )
  65. var currencyItem = map[string]string{
  66. "人民币": "人民币",
  67. "rmb": "人民币",
  68. "RMB": "人民币",
  69. "$": "美元",
  70. "$": "美元",
  71. "美元": "美元",
  72. "港元": "港币",
  73. "港币": "港币",
  74. "澳币": "澳币",
  75. "澳元": "澳币",
  76. }
  77. //获取币种
  78. func GetCurrency(text string) (currency string) {
  79. if text == "" {
  80. return
  81. }
  82. currency = "人民币"
  83. for k, v := range currencyItem {
  84. if strings.Contains(text, k) {
  85. currency = v
  86. return
  87. }
  88. }
  89. return
  90. }
  91. //金额转换
  92. func ObjToMoney(text string) float64 {
  93. isfindUnit := true
  94. ret := capitalMoney(text)
  95. if ret < float64(10000) || ret > float64(50000000000) {
  96. ret2, b := numMoney(text)
  97. isfindUnit = b
  98. if ret2 > ret {
  99. ret = ret2
  100. }
  101. }
  102. f, _ := strconv.ParseFloat(strconv.FormatFloat(ret, 'f', 4, 64), 64)
  103. // if f < 1 {
  104. // f = 0
  105. // }
  106. //如果金额小于50,全文检索单位:万
  107. if f < 50 && f > 0 && isfindUnit {
  108. rep := contentUnit.FindAllStringIndex(text, -1)
  109. if len(rep) > 0 {
  110. f = f * 10000
  111. }
  112. }
  113. return f
  114. }
  115. func capitalMoney(text string) float64 {
  116. nodes := []float64{}
  117. node := float64(0)
  118. tmp := float64(0)
  119. decimals := 0.0
  120. ishaspoint := false //是否含小数点
  121. fnum := float64(0)
  122. end := false
  123. //str := fmt.Sprint(data[0])
  124. //提取第一个大写信息
  125. strmatch := numCapitals.FindAllStringSubmatch(text, -1)
  126. if len(strmatch) > 0 {
  127. text = strmatch[0][0]
  128. }
  129. suffixUnit := float64(1)
  130. if strings.HasSuffix(text, "万") || strings.HasSuffix(text, "万元") || strings.HasSuffix(text, "万元整") {
  131. index := strings.LastIndex(text, "万")
  132. text = text[0:index]
  133. suffixUnit = float64(10000)
  134. }
  135. moneyRegChar.ReplaceAllStringFunc(text, func(key string) string {
  136. if key == "元" || key == "圆" || key == "点" {
  137. ishaspoint = true
  138. }
  139. if v, ok := moneyChar[key].(float64); ok && !end {
  140. if ishaspoint && v > 10 { //排除后面有其他的单位
  141. return ""
  142. }
  143. //fmt.Println(key, v, fnum)
  144. if v < 10 && v >= 0 {
  145. if ishaspoint { //小数部分
  146. if v >= 1 {
  147. fnum = v
  148. } else if v < 1 && v > 0 {
  149. decimals += fnum * v
  150. }
  151. } else {
  152. if tmp != float64(0) {
  153. node += tmp
  154. }
  155. tmp = float64(v)
  156. }
  157. } else if v == 10000 || v == 100000000 { //单位万、亿
  158. if tmp != float64(0) {
  159. node += tmp
  160. tmp = float64(0)
  161. }
  162. nodes = append(nodes, node*float64(v))
  163. node = float64(0)
  164. } else {
  165. if v == 10 && tmp == 0 {
  166. tmp = 1
  167. }
  168. tmp = tmp * float64(v)
  169. node += tmp
  170. tmp = float64(0)
  171. }
  172. }
  173. if key == "整" || key == "正" || key == "分" {
  174. end = true
  175. }
  176. return ""
  177. })
  178. nodes = append(nodes, node, tmp)
  179. ret := float64(0)
  180. for _, v := range nodes {
  181. ret += v
  182. }
  183. return (ret + decimals) * suffixUnit
  184. }
  185. //数字金额转换
  186. func numMoney(text string) (moneyFloat float64, flag bool) {
  187. //tmp := fmt.Sprintf("%f", data[0])
  188. repUnit := float64(1)
  189. if regQianw.MatchString(text) {
  190. text = strings.Replace(text, "千万", "万", -1)
  191. repUnit = float64(1000)
  192. }
  193. text = replaceSymbol(text, []string{",", ",", "(", ")", "(", ")", ":", "\n"})
  194. text = replaceString(text, []string{"万元", "亿元", "."}, []string{"万", "亿", "."})
  195. text = CutAllSpace(text)
  196. rets := regNumFloat.FindAllString(text, -1)
  197. fnums := []float64{}
  198. unitstrs := []string{}
  199. if len(rets) > 0 {
  200. pindex := 0 //单位前置
  201. for k, v := range rets {
  202. f, err := strconv.ParseFloat(v, 64)
  203. if err == nil {
  204. fnums = append(fnums, f)
  205. index := strings.Index(text, v)
  206. //单位后置
  207. start := index + len(v)
  208. end := start + 3
  209. //log.Println("vvv", tmp, v, pindex, index, start)
  210. if k > 0 {
  211. if start >= pindex+3 {
  212. pstart := pindex + 3
  213. if pstart >= index {
  214. pstart = index
  215. }
  216. if len(text) > end {
  217. unitstrs = append(unitstrs, text[pstart:index]+text[start:end])
  218. } else {
  219. unitstrs = append(unitstrs, text[pstart:index]+text[start:])
  220. }
  221. } else {
  222. if len(text) > end {
  223. unitstrs = append(unitstrs, text[start:end])
  224. } else {
  225. unitstrs = append(unitstrs, text[start:])
  226. }
  227. }
  228. } else {
  229. if len(text) > end {
  230. if index-3 >= 0 {
  231. unitstrs = append(unitstrs, text[index-3:index]+text[start:end])
  232. } else {
  233. unitstrs = append(unitstrs, text[start:end])
  234. }
  235. } else {
  236. if index-3 >= 0 {
  237. unitstrs = append(unitstrs, text[index-3:index]+text[start:])
  238. } else {
  239. unitstrs = append(unitstrs, text[start:])
  240. }
  241. }
  242. }
  243. pindex = start
  244. }
  245. }
  246. }
  247. //log.Println("unitstrs", fnums, unitstrs)
  248. unit := float64(0)
  249. fnum := float64(0)
  250. for k, v := range fnums {
  251. fnum = v
  252. units := regStrUnit.FindAllString(unitstrs[k], -1)
  253. for _, v := range units {
  254. if moneyUnit[v] != 0 {
  255. unit = moneyUnit[v]
  256. break
  257. }
  258. }
  259. if unit != float64(0) { //取第一个
  260. break
  261. }
  262. }
  263. fnum = fnum * repUnit
  264. if unit == float64(0) {
  265. moneyFloat = fnum
  266. } else {
  267. moneyFloat = fnum * unit
  268. }
  269. if unit == 10000 {
  270. flag = false
  271. } else {
  272. flag = true
  273. }
  274. return
  275. }
  276. //清理所有空白符
  277. func CutAllSpace(text string) string {
  278. tmp := cutAllSpace.ReplaceAllString(text, "")
  279. tmp = replaceSymbol(tmp, spaces)
  280. return tmp
  281. }
  282. //符号替换
  283. func replaceString(con string, ret, rep []string) string {
  284. for k, v := range ret {
  285. if len(rep) > k {
  286. con = strings.Replace(con, v, rep[k], -1)
  287. }
  288. }
  289. return con
  290. }
  291. //过滤符号
  292. func replaceSymbol(con string, rep []string) string {
  293. for _, v := range rep {
  294. con = strings.Replace(con, v, "", -1)
  295. }
  296. return con
  297. }