c_money.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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, bool) {
  67. isFindUnit := false
  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, isFindUnit
  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, isFindUnit
  136. }
  137. }
  138. //是否发现单位
  139. if strings.Contains(fmt.Sprint(data[0]), "万") || strings.Contains(fmt.Sprint(data[0]), "亿") {
  140. isFindUnit = true
  141. }
  142. ret := capitalMoney(data)[0]
  143. if ret.(float64) < float64(10000) || ret.(float64) > float64(50000000000) {
  144. ret2, _ := numMoney(data)
  145. //isfindUnit = b
  146. if ret2[0].(float64) > ret.(float64) {
  147. ret = ret2[0]
  148. }
  149. }
  150. f, _ := strconv.ParseFloat(strconv.FormatFloat(ret.(float64), 'f', 4, 64), 64)
  151. //if f < 1 {
  152. // f = 0
  153. //}
  154. //若果金额小于50,全文检索单位:万
  155. // if f < 50 && f > 0 && isfindUnit {
  156. // rep := contentUnit.FindAllStringIndex(fmt.Sprint(data[1]), -1)
  157. // if len(rep) > 0 {
  158. // f = f * 10000
  159. // }
  160. // }
  161. data[0] = f
  162. if f == 0 && !moneyUnitRegBool.MatchString(fmt.Sprint(tmpstr)) {
  163. data = append(data, false)
  164. return 0.0, isFindUnit
  165. }
  166. data = append(data, true)
  167. if len(data) > 0 {
  168. return util.Float64All(data[0]), isFindUnit
  169. } else {
  170. return 0.0, isFindUnit
  171. }
  172. }
  173. // 数字金额转换
  174. func numMoney(data []interface{}) ([]interface{}, bool) {
  175. tmp := fmt.Sprintf("%f", data[0])
  176. tmp = strings.ReplaceAll(tmp, "(不含税)", "")
  177. //费率转换% ‰
  178. flv := float64(1)
  179. if strings.HasSuffix(tmp, "%") {
  180. flv = 0.01
  181. } else if strings.HasSuffix(tmp, "‰") {
  182. flv = 0.001
  183. }
  184. repUnit := float64(1)
  185. if regQianw.MatchString(tmp) {
  186. tmp = strings.Replace(tmp, "千万", "万", -1)
  187. repUnit = float64(1000)
  188. }
  189. tmp = replaceSymbol(tmp, []string{",", ",", "(", ")", "(", ")", ":", "\n"})
  190. tmp = replaceString(tmp, []string{"万元", "亿元", "."}, []string{"万", "亿", "."})
  191. tmp = fmt.Sprint(CutAllSpace([]interface{}{tmp, data[1]})[0])
  192. rets := regNumFloat.FindAllString(tmp, -1)
  193. fnums := []float64{}
  194. unitstrs := []string{}
  195. if len(rets) > 0 {
  196. pindex := 0 //单位前置
  197. for k, v := range rets {
  198. f, err := strconv.ParseFloat(v, 64)
  199. if err == nil {
  200. fnums = append(fnums, f)
  201. index := strings.Index(tmp, v)
  202. //单位后置
  203. start := index + len(v)
  204. end := start + 3
  205. //log.Println("vvv", tmp, v, pindex, index, start)
  206. if k > 0 {
  207. if start >= pindex+3 {
  208. pstart := pindex + 3
  209. if pstart >= index {
  210. pstart = index
  211. }
  212. if len(tmp) > end {
  213. unitstrs = append(unitstrs, tmp[pstart:index]+tmp[start:end])
  214. } else {
  215. unitstrs = append(unitstrs, tmp[pstart:index]+tmp[start:])
  216. }
  217. } else {
  218. if len(tmp) > end {
  219. unitstrs = append(unitstrs, tmp[start:end])
  220. } else {
  221. unitstrs = append(unitstrs, tmp[start:])
  222. }
  223. }
  224. } else {
  225. if len(tmp) > end {
  226. if index-3 >= 0 {
  227. unitstrs = append(unitstrs, tmp[index-3:index]+tmp[start:end])
  228. } else {
  229. unitstrs = append(unitstrs, tmp[start:end])
  230. }
  231. } else {
  232. if index-3 >= 0 {
  233. unitstrs = append(unitstrs, tmp[index-3:index]+tmp[start:])
  234. } else {
  235. unitstrs = append(unitstrs, tmp[start:])
  236. }
  237. }
  238. }
  239. pindex = start
  240. }
  241. }
  242. }
  243. //log.Println("unitstrs", fnums, unitstrs)
  244. unit := float64(0)
  245. fnum := float64(0)
  246. for k, v := range fnums {
  247. fnum = v
  248. units := regStrUnit.FindAllString(unitstrs[k], -1)
  249. for _, v := range units {
  250. if moneyUnit[v] != 0 {
  251. unit = moneyUnit[v]
  252. break
  253. }
  254. }
  255. if unit != float64(0) { //取第一个
  256. break
  257. }
  258. }
  259. fnum = fnum * repUnit
  260. if unit == float64(0) {
  261. data[0] = fnum * flv
  262. } else {
  263. data[0] = fnum * unit * flv
  264. }
  265. if unit == 10000 {
  266. return data, false
  267. } else {
  268. return data, true
  269. }
  270. }
  271. // 大写数子金额转换
  272. func capitalMoney(data []interface{}) []interface{} {
  273. nodes := []float64{}
  274. node := float64(0)
  275. tmp := float64(0)
  276. decimals := 0.0
  277. ishaspoint := false //是否含小数点
  278. fnum := float64(0)
  279. end := false
  280. str := fmt.Sprint(data[0])
  281. //提取第一个大写信息
  282. if strings.Contains(str, "壹") {
  283. str = strings.ReplaceAll(str, "一", "壹")
  284. }
  285. strmatch := numCapitals.FindAllStringSubmatch(str, -1)
  286. if len(strmatch) > 0 {
  287. str = strmatch[0][0]
  288. }
  289. suffixUnit := float64(1)
  290. if strings.HasSuffix(str, "万") || strings.HasSuffix(str, "万元") || strings.HasSuffix(str, "万元整") {
  291. index := strings.LastIndex(str, "万")
  292. str = str[0:index]
  293. suffixUnit = float64(10000)
  294. }
  295. yy := false
  296. moneyRegChar.ReplaceAllStringFunc(str, func(key string) string {
  297. if key == "元" || key == "圆" || key == "点" {
  298. ishaspoint = true
  299. }
  300. if v, ok := moneyChar[key].(float64); ok && !end {
  301. if ishaspoint && v > 10 { //排除后面有其他的单位
  302. return ""
  303. }
  304. //fmt.Println(key, v, fnum)
  305. if v < 10 && v >= 0 {
  306. if ishaspoint { //小数部分
  307. if v >= 1 {
  308. fnum = v
  309. } else if v < 1 && v > 0 {
  310. decimals += fnum * v
  311. }
  312. } else {
  313. if tmp != float64(0) {
  314. node += tmp
  315. }
  316. tmp = float64(v)
  317. }
  318. } else if v == 10000 || v == 100000000 { //单位万、亿
  319. if tmp != float64(0) {
  320. node += tmp
  321. tmp = float64(0)
  322. }
  323. nodes = append(nodes, node*float64(v))
  324. if v == 100000000 {
  325. yy = true
  326. }
  327. node = float64(0)
  328. } else {
  329. if v == 10 && tmp == 0 {
  330. tmp = 1
  331. }
  332. tmp = tmp * float64(v)
  333. node += tmp
  334. tmp = float64(0)
  335. }
  336. }
  337. if key == "整" || key == "正" || key == "分" {
  338. end = true
  339. }
  340. return ""
  341. })
  342. if yy {
  343. nodes = append(nodes, node*suffixUnit, tmp)
  344. } else {
  345. nodes = append(nodes, node, tmp)
  346. }
  347. ret := float64(0)
  348. for _, v := range nodes {
  349. ret += v
  350. }
  351. if yy {
  352. return []interface{}{(ret + decimals), data[1]}
  353. } else {
  354. return []interface{}{(ret + decimals) * suffixUnit, data[1]}
  355. }
  356. }
  357. // 过滤符号
  358. func replaceSymbol(con string, rep []string) string {
  359. for _, v := range rep {
  360. con = strings.Replace(con, v, "", -1)
  361. }
  362. return con
  363. }
  364. // 符号替换
  365. func replaceString(con string, ret, rep []string) string {
  366. for k, v := range ret {
  367. if len(rep) > k {
  368. con = strings.Replace(con, v, rep[k], -1)
  369. }
  370. }
  371. return con
  372. }
  373. // 清理所有空白符
  374. func CutAllSpace(data []interface{}) []interface{} {
  375. tmp := cutAllSpace.ReplaceAllString(fmt.Sprint(data[0]), "")
  376. tmp = replaceSymbol(tmp, spaces)
  377. data[0] = tmp
  378. return data
  379. }