c_money.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. package clean
  2. import (
  3. "fmt"
  4. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  5. "math"
  6. "regexp"
  7. "strconv"
  8. "strings"
  9. "unicode/utf8"
  10. )
  11. var (
  12. moneyReg1 = regexp.MustCompile("([\\s ,]+)")
  13. moneyReg2 = regexp.MustCompile("^([0-9.]+)E([1-7])$")
  14. numReg1 = regexp.MustCompile("([0-9\\.]+)")
  15. )
  16. var unpkvBidamountReg = regexp.MustCompile("^([Xx]\\+[1-9\\.]+元/每)")
  17. var specBidamountReg = regexp.MustCompile("^([0-9.]+)E([1-7])$")
  18. var regUnitMoneyClean = regexp.MustCompile("^(.*单价[0-9.]+元[/][袋|块])[,,](含税总价[0-9.]+[万元]+)[.。]$")
  19. var blackMoneyClean = regexp.MustCompile("^([0-9.]+以下[万]?|分)$")
  20. var impactMoneyClean = regexp.MustCompile("(分二串口|分站模块)")
  21. // 大写金额补充
  22. var impactMoneyeplenish = regexp.MustCompile("^([壹贰叁肆伍陆柒捌玖]分)")
  23. // 特殊金额-格式-重置
  24. var resetAamountReg = regexp.MustCompile("[.](0|00)[.](0|00)")
  25. var regPercentMoney, _ = regexp.Compile(`[0-9.]+[((]?[%|%][))]?`)
  26. var regQianw, _ = regexp.Compile(`\d{1,2}千万`)
  27. var kxjsReg = regexp.MustCompile("[0-9][E|e]{1}[-—+]{1}[0-9]{1,2}")
  28. var regOperator, _ = regexp.Compile(`[*|+|)*)]`)
  29. var regNumFloat, _ = regexp.Compile(`([1-9]\d*|0)(\.\d+)?`)
  30. var regStrUnit, _ = regexp.Compile(`[元|万|亿]`)
  31. var regStrJe = regexp.MustCompile(`([1-9]\d*|0)(\.\d_+)?[\s|元|万|亿]{0,3}`)
  32. var regStrChar = `[〇|零|点|壹|贰|叁|肆|伍|陆|柒|捌|玖|拾|百|佰|千|仟|万|亿|億|元|圆|角|分|整|正]`
  33. var moneyRegChar, _ = regexp.Compile(regStrChar)
  34. var contentUnit, _ = regexp.Compile(`(万元|单位/万)`)
  35. var numCapitals, _ = regexp.Compile(`([〇|零|点|壹|贰|叁|肆|伍|陆|柒|捌|玖|拾|百|佰|千|仟|万|亿|億|元|圆|角|分|整|正]{4,40})`)
  36. var moneyUnitRegBool = regexp.MustCompile(`(中标金额|成交金额|合同金额|中标价|成交价|成交价格|中标\(成交\)金额|投标报价|中标标价|成交结果)?[::\s]?(0|零|0.0|¥0)+(0|\.)*[\s]?(万|元|){0,2}[\s]?((人民币))?$`)
  37. var cutAllSpace, _ = regexp.Compile(`\s*`)
  38. var spaces = []string{"\u3000", "\u2003", "\u00a0", "\t", "\r", "\n", "\u0001"}
  39. var moneyClearSpidercode map[string]interface{}
  40. var moneyChar = map[string]interface{}{ //"〇": "0", "零": "0",壹贰叁肆伍陆柒捌玖
  41. "一": float64(1), "壹": float64(1), "二": float64(2), "贰": float64(2), "三": float64(3), "叁": float64(3), "四": float64(4), "肆": float64(4), "五": float64(5), "伍": float64(5),
  42. "六": float64(6), "陆": float64(6), "七": float64(7), "柒": float64(7), "八": float64(8), "捌": float64(8), "九": float64(9), "玖": float64(9), "十": float64(10), "拾": float64(10),
  43. "百": float64(100), "佰": float64(100), "千": float64(1000), "仟": float64(1000), "万": float64(10000), "亿": float64(100000000), "億": float64(100000000),
  44. "零": float64(0), "点": ".", "角": float64(0.1), "分": float64(0.01),
  45. }
  46. var NumChar = map[string]interface{}{
  47. "一": 1, "二": 1, "三": 1, "四": 1, "五": 1, "六": 1, "七": 1, "八": 1, "久": 1, "十": 1,
  48. }
  49. var moneyUnit = map[string]float64{
  50. "元": float64(1), "万": float64(10000), "亿": float64(100000000), "億": float64(100000000), //单位
  51. }
  52. func init() {
  53. regOperator, _ = regexp.Compile(`[*|+|)*)]`)
  54. regNumFloat, _ = regexp.Compile(`([1-9]\d*|0)(\.\d+)?`)
  55. regStrUnit, _ = regexp.Compile(`[元|万|亿]`)
  56. regStrJe = regexp.MustCompile(`([1-9]\d*|0)(\.\d_+)?[\s|元|万|亿]{0,3}`)
  57. regStrChar = `[〇|零|点|壹|贰|叁|肆|伍|陆|柒|捌|玖|拾|百|佰|千|仟|万|亿|億|元|圆|角|分|整|正]`
  58. moneyRegChar, _ = regexp.Compile(regStrChar)
  59. contentUnit, _ = regexp.Compile(`(万元|单位/万)`)
  60. numCapitals, _ = regexp.Compile(`([〇|零|点|壹|贰|叁|肆|伍|陆|柒|捌|玖|拾|百|佰|千|仟|万|亿|億|元|圆|角|分|整|正]{4,40})`)
  61. regQianw, _ = regexp.Compile(`\d{1,2}千万`)
  62. kxjsReg = regexp.MustCompile("[0-9][E|e]{1}[-—+]{1}[0-9]{1,2}")
  63. regPercentMoney, _ = regexp.Compile(`[0-9.]+[((]?[%|%][))]?`)
  64. }
  65. // 金额转换
  66. func CleanMoney(data []interface{}) float64 {
  67. //isfindUnit := true
  68. tmpstr := (data)[0]
  69. totmpstr := ""
  70. if _, ok := tmpstr.(float64); ok {
  71. totmpstr = fmt.Sprintf("%f", tmpstr)
  72. } else {
  73. totmpstr = util.ObjToString(tmpstr)
  74. }
  75. //去除空格
  76. totmpstr = strings.ReplaceAll(totmpstr, " ", "")
  77. (data)[0] = totmpstr
  78. //特殊转换-科学计数法
  79. if specBidamountReg.MatchString(totmpstr) {
  80. price := util.Float64All(specBidamountReg.ReplaceAllString(totmpstr, "${1}"))
  81. if unit := util.Float64All(specBidamountReg.ReplaceAllString(totmpstr, "${2}")); unit > 0.0 && price > 0.0 {
  82. totmpstr = fmt.Sprintf("%f", math.Pow(10, unit)*price)
  83. (data)[0] = totmpstr
  84. }
  85. }
  86. //异常替换
  87. if unpkvBidamountReg.MatchString(totmpstr) {
  88. totmpstr = unpkvBidamountReg.ReplaceAllString(totmpstr, "")
  89. (data)[0] = totmpstr
  90. }
  91. if resetAamountReg.MatchString(totmpstr) {
  92. totmpstr = resetAamountReg.ReplaceAllString(totmpstr, ".0")
  93. (data)[0] = totmpstr
  94. }
  95. //单位指定
  96. if regUnitMoneyClean.MatchString(totmpstr) {
  97. totmpstr = regUnitMoneyClean.ReplaceAllString(totmpstr, "$2")
  98. (data)[0] = totmpstr
  99. }
  100. //特殊替换
  101. if impactMoneyClean.MatchString(totmpstr) {
  102. totmpstr = impactMoneyClean.ReplaceAllString(totmpstr, "")
  103. (data)[0] = totmpstr
  104. }
  105. //大写金额补充
  106. if impactMoneyeplenish.MatchString(totmpstr) {
  107. totmpstr = "零元" + totmpstr
  108. (data)[0] = totmpstr
  109. }
  110. //黑名单
  111. if blackMoneyClean.MatchString(totmpstr) {
  112. totmpstr = ""
  113. (data)[0] = totmpstr
  114. }
  115. //未含税总价1454400.00元,税率6%,含税总价1541664.00元
  116. Percent := regPercentMoney.FindAllString(totmpstr, -1)
  117. for _, v := range Percent {
  118. totmpstr = strings.ReplaceAll(totmpstr, v, "")
  119. }
  120. totmpstr = strings.ReplaceAll(totmpstr, "_", "")
  121. (data)[0] = totmpstr //过滤到%相关数字
  122. if utf8.RuneCountInString(totmpstr) > 100 { //过长-字符无有效金额
  123. (data)[0] = 0
  124. data = append(data, false)
  125. return 0.0
  126. }
  127. if utf8.RuneCountInString(totmpstr) > 20 {
  128. if numCapitals.MatchString(totmpstr) {
  129. tmpstr = numCapitals.FindString(totmpstr)
  130. } else if regStrJe.MatchString(totmpstr) {
  131. tmpstr = regStrJe.FindString(totmpstr)
  132. } else {
  133. (data)[0] = 0
  134. data = append(data, false)
  135. return 0.0
  136. }
  137. }
  138. ret := capitalMoney(data)[0]
  139. if ret.(float64) < float64(10000) || ret.(float64) > float64(50000000000) {
  140. ret2, _ := numMoney(data)
  141. //isfindUnit = b
  142. if ret2[0].(float64) > ret.(float64) {
  143. ret = ret2[0]
  144. }
  145. }
  146. f, _ := strconv.ParseFloat(strconv.FormatFloat(ret.(float64), 'f', 4, 64), 64)
  147. //if f < 1 {
  148. // f = 0
  149. //}
  150. //若果金额小于50,全文检索单位:万
  151. // if f < 50 && f > 0 && isfindUnit {
  152. // rep := contentUnit.FindAllStringIndex(fmt.Sprint(data[1]), -1)
  153. // if len(rep) > 0 {
  154. // f = f * 10000
  155. // }
  156. // }
  157. data[0] = f
  158. if f == 0 && !moneyUnitRegBool.MatchString(fmt.Sprint(tmpstr)) {
  159. data = append(data, false)
  160. return 0.0
  161. }
  162. data = append(data, true)
  163. if len(data) > 0 {
  164. return util.Float64All(data[0])
  165. } else {
  166. return 0.0
  167. }
  168. }
  169. // 数字金额转换
  170. func numMoney(data []interface{}) ([]interface{}, bool) {
  171. tmp := fmt.Sprintf("%f", data[0])
  172. tmp = strings.ReplaceAll(tmp, "(不含税)", "")
  173. //费率转换% ‰
  174. flv := float64(1)
  175. if strings.HasSuffix(tmp, "%") {
  176. flv = 0.01
  177. } else if strings.HasSuffix(tmp, "‰") {
  178. flv = 0.001
  179. }
  180. repUnit := float64(1)
  181. if regQianw.MatchString(tmp) {
  182. tmp = strings.Replace(tmp, "千万", "万", -1)
  183. repUnit = float64(1000)
  184. }
  185. tmp = replaceSymbol(tmp, []string{",", ",", "(", ")", "(", ")", ":", "\n"})
  186. tmp = replaceString(tmp, []string{"万元", "亿元", "."}, []string{"万", "亿", "."})
  187. tmp = fmt.Sprint(CutAllSpace([]interface{}{tmp, data[1]})[0])
  188. rets := regNumFloat.FindAllString(tmp, -1)
  189. fnums := []float64{}
  190. unitstrs := []string{}
  191. if len(rets) > 0 {
  192. pindex := 0 //单位前置
  193. for k, v := range rets {
  194. f, err := strconv.ParseFloat(v, 64)
  195. if err == nil {
  196. fnums = append(fnums, f)
  197. index := strings.Index(tmp, v)
  198. //单位后置
  199. start := index + len(v)
  200. end := start + 3
  201. //log.Println("vvv", tmp, v, pindex, index, start)
  202. if k > 0 {
  203. if start >= pindex+3 {
  204. pstart := pindex + 3
  205. if pstart >= index {
  206. pstart = index
  207. }
  208. if len(tmp) > end {
  209. unitstrs = append(unitstrs, tmp[pstart:index]+tmp[start:end])
  210. } else {
  211. unitstrs = append(unitstrs, tmp[pstart:index]+tmp[start:])
  212. }
  213. } else {
  214. if len(tmp) > end {
  215. unitstrs = append(unitstrs, tmp[start:end])
  216. } else {
  217. unitstrs = append(unitstrs, tmp[start:])
  218. }
  219. }
  220. } else {
  221. if len(tmp) > end {
  222. if index-3 >= 0 {
  223. unitstrs = append(unitstrs, tmp[index-3:index]+tmp[start:end])
  224. } else {
  225. unitstrs = append(unitstrs, tmp[start:end])
  226. }
  227. } else {
  228. if index-3 >= 0 {
  229. unitstrs = append(unitstrs, tmp[index-3:index]+tmp[start:])
  230. } else {
  231. unitstrs = append(unitstrs, tmp[start:])
  232. }
  233. }
  234. }
  235. pindex = start
  236. }
  237. }
  238. }
  239. //log.Println("unitstrs", fnums, unitstrs)
  240. unit := float64(0)
  241. fnum := float64(0)
  242. for k, v := range fnums {
  243. fnum = v
  244. units := regStrUnit.FindAllString(unitstrs[k], -1)
  245. for _, v := range units {
  246. if moneyUnit[v] != 0 {
  247. unit = moneyUnit[v]
  248. break
  249. }
  250. }
  251. if unit != float64(0) { //取第一个
  252. break
  253. }
  254. }
  255. fnum = fnum * repUnit
  256. if unit == float64(0) {
  257. data[0] = fnum * flv
  258. } else {
  259. data[0] = fnum * unit * flv
  260. }
  261. if unit == 10000 {
  262. return data, false
  263. } else {
  264. return data, true
  265. }
  266. }
  267. // 大写数子金额转换
  268. func capitalMoney(data []interface{}) []interface{} {
  269. nodes := []float64{}
  270. node := float64(0)
  271. tmp := float64(0)
  272. decimals := 0.0
  273. ishaspoint := false //是否含小数点
  274. fnum := float64(0)
  275. end := false
  276. str := fmt.Sprint(data[0])
  277. //提取第一个大写信息
  278. if strings.Contains(str, "壹") {
  279. str = strings.ReplaceAll(str, "一", "壹")
  280. }
  281. strmatch := numCapitals.FindAllStringSubmatch(str, -1)
  282. if len(strmatch) > 0 {
  283. str = strmatch[0][0]
  284. }
  285. suffixUnit := float64(1)
  286. if strings.HasSuffix(str, "万") || strings.HasSuffix(str, "万元") || strings.HasSuffix(str, "万元整") {
  287. index := strings.LastIndex(str, "万")
  288. str = str[0:index]
  289. suffixUnit = float64(10000)
  290. }
  291. yy := false
  292. moneyRegChar.ReplaceAllStringFunc(str, func(key string) string {
  293. if key == "元" || key == "圆" || key == "点" {
  294. ishaspoint = true
  295. }
  296. if v, ok := moneyChar[key].(float64); ok && !end {
  297. if ishaspoint && v > 10 { //排除后面有其他的单位
  298. return ""
  299. }
  300. //fmt.Println(key, v, fnum)
  301. if v < 10 && v >= 0 {
  302. if ishaspoint { //小数部分
  303. if v >= 1 {
  304. fnum = v
  305. } else if v < 1 && v > 0 {
  306. decimals += fnum * v
  307. }
  308. } else {
  309. if tmp != float64(0) {
  310. node += tmp
  311. }
  312. tmp = float64(v)
  313. }
  314. } else if v == 10000 || v == 100000000 { //单位万、亿
  315. if tmp != float64(0) {
  316. node += tmp
  317. tmp = float64(0)
  318. }
  319. nodes = append(nodes, node*float64(v))
  320. if v == 100000000 {
  321. yy = true
  322. }
  323. node = float64(0)
  324. } else {
  325. if v == 10 && tmp == 0 {
  326. tmp = 1
  327. }
  328. tmp = tmp * float64(v)
  329. node += tmp
  330. tmp = float64(0)
  331. }
  332. }
  333. if key == "整" || key == "正" || key == "分" {
  334. end = true
  335. }
  336. return ""
  337. })
  338. if yy {
  339. nodes = append(nodes, node*suffixUnit, tmp)
  340. } else {
  341. nodes = append(nodes, node, tmp)
  342. }
  343. ret := float64(0)
  344. for _, v := range nodes {
  345. ret += v
  346. }
  347. if yy {
  348. return []interface{}{(ret + decimals), data[1]}
  349. } else {
  350. return []interface{}{(ret + decimals) * suffixUnit, data[1]}
  351. }
  352. }
  353. // 过滤符号
  354. func replaceSymbol(con string, rep []string) string {
  355. for _, v := range rep {
  356. con = strings.Replace(con, v, "", -1)
  357. }
  358. return con
  359. }
  360. // 符号替换
  361. func replaceString(con string, ret, rep []string) string {
  362. for k, v := range ret {
  363. if len(rep) > k {
  364. con = strings.Replace(con, v, rep[k], -1)
  365. }
  366. }
  367. return con
  368. }
  369. // 清理所有空白符
  370. func CutAllSpace(data []interface{}) []interface{} {
  371. tmp := cutAllSpace.ReplaceAllString(fmt.Sprint(data[0]), "")
  372. tmp = replaceSymbol(tmp, spaces)
  373. data[0] = tmp
  374. return data
  375. }