testOne2.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #coding:utf8
  2. import re
  3. import time
  4. import psycopg2
  5. conn = psycopg2.connect(dbname="BiddingKM_test_10000",user="postgres",password="postgres",host="192.168.2.101")
  6. cursor = conn.cursor()
  7. def getDigitsDic(unit):
  8. DigitsDic = {"零":0, "壹":1, "贰":2, "叁":3, "肆":4, "伍":5, "陆":6, "柒":7, "捌":8, "玖":9,
  9. "〇":0, "一":1, "二":2, "三":3, "四":4, "五":5, "六":6, "七":7, "八":8, "九":9}
  10. return DigitsDic.get(unit)
  11. def getMultipleFactor(unit):
  12. MultipleFactor = {"兆":float(1000000000000),"亿":100000000,"万":10000,"仟":1000,"千":1000,"佰":100,"百":100,"拾":10,"十":10,"元":1,"角":0.1,"分":0.01}
  13. return MultipleFactor.get(unit)
  14. def getUnifyMoney(money):
  15. #print(money)
  16. money = re.sub("[,,]","",money)
  17. result = 0
  18. chnDigits = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"]
  19. chnFactorUnits = ["兆", "亿", "万", "仟", "佰", "拾","元","角","分"]
  20. lcChnDigits = ["〇", "一", "二", "三", "四", "五", "六", "七", "八", "九"]
  21. lcChnFactorUnits = ["兆", "亿", "万", "千", "百", "十","元","角","分"]
  22. DigitsDic = {"零":0, "壹":1, "贰":2, "叁":3, "肆":4, "伍":5, "陆":6, "柒":7, "捌":8, "玖":9,
  23. "〇":0, "一":1, "二":2, "三":3, "四":4, "五":5, "六":6, "七":7, "八":8, "九":9}
  24. MultipleFactor = {"兆":float(1000000000000),"亿":100000000,"万":10000,"仟":1000,"千":1000,"佰":100,"百":100,"拾":10,"十":10,"元":1,"角":0.1,"分":0.01}
  25. LowMoneypattern = re.compile("^(\d+,?)+(\.\d+)?$")
  26. BigMoneypattern = re.compile("^[%s]$"%("".join(chnDigits)))
  27. if re.search(LowMoneypattern,money) is not None:
  28. return float(money)
  29. elif re.search(BigMoneypattern,money) is not None:
  30. return DigitsDic.get(money)
  31. for factorUnit in chnFactorUnits:
  32. if re.search(re.compile(".*%s.*"%(factorUnit)),money) is not None:
  33. subMoneys = re.split(re.compile("%s(?!.*%s.*)"%(factorUnit,factorUnit)),money)
  34. if re.search(re.compile("^(\d+(,)?)+(\.\d+)?$"),subMoneys[0]) is not None:
  35. result += float(subMoneys[0])*MultipleFactor.get(factorUnit)
  36. elif len(subMoneys[0])==1:
  37. if re.search(re.compile("^[%s]$"%("".join(chnDigits))),subMoneys[0]) is not None:
  38. result += DigitsDic.get(subMoneys[0])*MultipleFactor.get(factorUnit)
  39. else:
  40. result += float(getUnifyMoney(subMoneys[0]))*MultipleFactor.get(factorUnit)
  41. if len(subMoneys)>1:
  42. if re.search(re.compile("^(\d+(,)?)+(\.\d+)?[百千万亿]?\s?(元)?$"),subMoneys[1]) is not None:
  43. result += float(subMoneys[1])
  44. elif len(subMoneys[1])==1:
  45. if re.search(re.compile("^[%s]$"%("".join(chnDigits))),subMoneys[1]) is not None:
  46. result += DigitsDic.get(subMoneys[1])
  47. else:
  48. result += float(getUnifyMoney(subMoneys[1]))
  49. break
  50. return result
  51. ''''''
  52. doc_id = "6cf0f360-9c7f-11e8-9b67-44a8424942f7"
  53. sql = " select tokens,sentence_index from sentences where doc_id='"+doc_id+"' order by sentence_index asc "
  54. cursor.execute(sql)
  55. rows = cursor.fetchall()
  56. for row in rows:
  57. #text = ",中标金额:人民币(万元):5,700,万,陆拾柒万玖仟陆佰伍拾元陆角柒分(¥679,650.00)"
  58. print(row[1])
  59. tokens = row[0]
  60. list_tokenbegin = []
  61. begin = 0
  62. for i in range(0,len(tokens)):
  63. list_tokenbegin.append(begin)
  64. begin += len(str(tokens[i]))
  65. list_tokenbegin.append(begin+1)
  66. #money_patten = re.compile("((?:#sp#)(?:\d+,?)+(?:.\d{2,4})+(?:#sp#)|(?:(?:#sp#)[零壹贰叁肆伍陆柒捌玖拾佰仟萬億十百千万亿元角分]{1,})+(?:#sp#))*")
  67. money_patten = re.compile("(([1-9],?(?:\d+,?)+(?:.\d{2,4})?[百千万亿]?[元整]+)|([零壹贰叁肆伍陆柒捌玖拾佰仟萬億十百千万亿元角分]{3,})|(?:[(\(]?([万]?)元[)\)]?[::]?|[¥¥]+,?)([1-9],?(?:\d+,?)+(?:.\d{2,4})?(?:,?)[百千万亿]?))*")
  68. all_match = re.findall(money_patten,"".join(tokens))
  69. index = 0
  70. for i in range(len(all_match)):
  71. if len(all_match[i][0])>0:
  72. unit = ""
  73. if len(all_match[i][1])>0:
  74. entity_text = all_match[i][1]
  75. elif len(all_match[i][2])>0:
  76. entity_text = all_match[i][2]
  77. else:
  78. entity_text = all_match[i][4]
  79. unit = all_match[i][3]
  80. index += len(all_match[i][0])-len(entity_text)
  81. #entity_text = getUnifyMoney(all_match[i])
  82. for j in range(len(list_tokenbegin)):
  83. if list_tokenbegin[j]==index:
  84. begin_index = j
  85. break
  86. elif list_tokenbegin[j]>index:
  87. begin_index = j-1
  88. break
  89. index += len(str(entity_text))
  90. for j in range(len(list_tokenbegin)):
  91. if list_tokenbegin[j]>=index:
  92. end_index = j-1
  93. break
  94. print("".join(tokens))
  95. print(entity_text)
  96. print(unit)
  97. if len(unit)>0:
  98. entity_text = getUnifyMoney(entity_text)*getMultipleFactor(unit)
  99. else:
  100. entity_text = getUnifyMoney(entity_text)
  101. print(entity_text,begin_index,end_index,index)
  102. else:
  103. index += 1