Beispiel #1
0
def getReceipt(sim_pred, resultMap, i):
    print('input receipt {}'.format(sim_pred))
    if sim_pred.find('年') > -1:
        return
    if priceUtils.checkMnyStr(sim_pred):
        return
    sim_pred = sim_pred.replace('ー', '-').replace('。', '.')
    sim_pred = jaconv.z2h(sim_pred, digit=True, ascii=True)
    if sim_pred.find('-') == -1:
        return
    # sim_pred = numberUtils.numberReplacement(sim_pred)
    if (sim_pred.find('-') == 0):
        sim_pred = str(1) + sim_pred
    tmpList = sim_pred.split('-')
    tmpHead = tmpList[0]
    tmpHead = tmpHead[-1]
    tmpHead = numberUtils.numberReplacement(tmpHead)

    lstHead = re.findall(r'\d+', tmpHead)
    tmpHead = ''.join(lstHead)
    if (tmpHead == ''):
        tmpHead = '1'
    tmpTail = tmpList[-1][:4]
    tmpTail = numberUtils.numberReplacement(tmpTail)
    lstTail = re.findall(r'\d+', tmpTail)
    tmpTail = ''.join(lstTail)
    if tmpTail == '':
        tmpTail = '1234'

    resultMap['6_receiptNO'] = tmpHead + 'ー' + tmpTail
    # resultMap['6_receiptNO']=tmpHead+'-'+tmpTail
    print('output receiptNO {}'.format(resultMap['6_receiptNO']))
Beispiel #2
0
def getCategoryAfter(tmpResult,resultMap,i):
  tmpResult = jaconv.z2h(tmpResult, digit=True, ascii=True)
  # not minus ー
  if(tmpResult.find('責')>-1 \
     or tmpResult.find('No')>-1 \
     or tmpResult.find('点')>-1 \
     or tmpResult.find('×')>-1 \
     # or tmpResult.find('-')>-1 \
     or tmpResult.find('ー')>-1 \
     or tmpResult.find(':')>-1 \
     or tmpResult.find('NO')>-1):
      return
  if not priceUtils.checkMnyStr(tmpResult):
      return
  print('input_{} category {}'.format(i,tmpResult))
  tmpResult=numberUtils.getMny(tmpResult)
  if(tmpResult==''):
    return

  tmpResult=numberUtils.numberReplacement(tmpResult)
  lstCatPrice=re.findall(r'\d+', tmpResult)
  sCatPrice=''.join(lstCatPrice)
  if(sCatPrice==''):
    iCatPrice=0
  else:
    iCatPrice=int(sCatPrice)

  if tmpResult.find('-')>-1:
      iCatPrice*=-1
  if(iCatPrice !=0):
    resultMap['suffix_catPrice'].append(iCatPrice)
    print('output iCatPrice------------- {}'.format(iCatPrice))
Beispiel #3
0
def getYear(sim_pred):
    sYear = sim_pred[0:sim_pred.find('年')]
    sYear = numberUtils.numberReplacement(sYear)
    lstYear = re.findall(r'\d+', sYear)
    sYear = ''.join(lstYear)
    if (len(sYear) != 4):
        sYear = '2017'
    if int(sYear) > 2019 or int(sYear) < 2016:
        sYear = '2017'
    return int(sYear)
Beispiel #4
0
def getTax(sim_pred, resultMap, i):
    print('input_{} tax {}'.format(i, sim_pred))
    resultMap['pos_tax'] = i
    sim_pred = numberUtils.getMny(sim_pred)
    sim_pred = numberUtils.numberReplacement(sim_pred)
    lstTax = re.findall(r'\d+', sim_pred)
    sTax = ''.join(lstTax)
    if (sTax == ''):
        iTax = 0
    else:
        iTax = int(sTax)
    resultMap['a_tax'] = iTax
    if (resultMap['a_tax'] > 0):
        print('output tax------------- {}'.format(resultMap['a_tax']))
Beispiel #5
0
def getTotalPrice(sim_pred, resultMap, i):
    print('input_{} totalPrice {}'.format(i, sim_pred))
    resultMap['pos_total'] = i
    sim_pred = numberUtils.getMny(sim_pred)
    sim_pred = numberUtils.numberReplacement(sim_pred)
    lstTotal = re.findall(r'\d+', sim_pred)
    sTotal = ''.join(lstTotal)
    if (sTotal == ''):
        iTotal = 0
    else:
        iTotal = int(sTotal)
    resultMap['5_total'] = iTotal
    if (resultMap['5_total'] > 0):
        print('output total------------- {}'.format(resultMap['5_total']))
Beispiel #6
0
def getMin(sim_pred):
    if (sim_pred.find(':') == -1):
        iMin = 0
    else:
        sMin = sim_pred[sim_pred.find(':') + 1:]
        sMin = numberUtils.numberReplacement(sMin)
        lstMin = re.findall(r'\d+', sMin)
        sMin = ''.join(lstMin)
        if (sMin == ''):
            iMin = 0
        else:
            iMin = int(sMin)
    if (iMin > 59):
        iMin = 0
    return iMin
Beispiel #7
0
def getHour(sim_pred):
    if (sim_pred.find(':') == -1):
        iHour = 0
    else:
        sHour = sim_pred[sim_pred.find(':') - 2:sim_pred.find(':')]
        sHour = numberUtils.numberReplacement(sHour)
        sHour = sHour[-3:]
        lstHour = re.findall(r'\d+', sHour)
        sHour = ''.join(lstHour)
        if (sHour == ''):
            iHour = 0
        else:
            iHour = int(sHour)
    if (iHour > 23):
        iHour = 0
    return iHour
Beispiel #8
0
def amendStaff(sim_pred, resultMap, i):
    if (not isStaffStr(sim_pred)):
        return
    print('input staffNo_amend {}'.format(sim_pred))
    staffNoOrigin = resultMap['7_staffNO']
    sim_pred = sim_pred.replace(',', '.').replace('。', '.').replace('黄', '責')
    if (sim_pred.find('.') > -1):
        tmpStrs = sim_pred.split('.')[-1]
    elif (sim_pred.find('責') > -1):
        tmpStrs = sim_pred.split('責')[-1]
    else:
        return
    tmpStrs = numberUtils.numberReplacement(tmpStrs)
    lstStaff = re.findall(r'\d+', tmpStrs)
    tmpStrs = ''.join(lstStaff)
    if (len(tmpStrs) >= 2):
        resultMap['7_staffNO'] = tmpStrs
        print('output staffNo_amend {}'.format(resultMap['7_staffNO']))
Beispiel #9
0
def getMonth(sim_pred):
    iPosYear = sim_pred.find('年')
    iPosMonth = sim_pred.find('月')
    if (iPosMonth == -1):
        iMonth = 1
    elif (iPosMonth > iPosYear + 3):  #maybe week
        iMonth = 1
    else:
        sMonth = sim_pred[iPosYear + 1:iPosMonth]
        sMonth = numberUtils.numberReplacement(sMonth)
        lstMonth = re.findall(r'\d+', sMonth)
        sMonth = ''.join(lstMonth)
        if (sMonth == ''):
            iMonth = 1
        else:
            iMonth = int(sMonth)
    if (iMonth > 12 or iMonth == 0):
        iMonth = 1
    return iMonth
Beispiel #10
0
def getDay(sim_pred):
    iPosYear = sim_pred.find('年')
    iPosDay = sim_pred.find('日')
    if (iPosDay == -1):
        iDay = 1
    elif (iPosDay > iPosYear + 6):  #maybe week
        iDay = 1
    else:
        sDay = sim_pred[iPosDay - 2:iPosDay]
        sDay = numberUtils.numberReplacement(sDay)
        lstDate = re.findall(r'\d+', sDay)
        sDay = ''.join(lstDate)
        if (sDay == ''):
            iDay = 1
        else:
            iDay = int(sDay)

    if (iDay > 31 or iDay == 0):
        iDay = 1
    return iDay
Beispiel #11
0
def getNo(sim_pred, resultMap, i):
    if (not isStaffStr(sim_pred)):
        return
    print('input staffNo {}'.format(sim_pred))
    # 7ー1784 No.0 8
    sim_pred = sim_pred.replace(',', '.').replace('。', '.').replace('黄', '責')
    sim_pred = jaconv.z2h(sim_pred, digit=True, ascii=True)
    if (sim_pred.find('.') > -1):
        tmpStrs = sim_pred.split('.')[-1]
    elif (sim_pred.find('責') > -1):
        tmpStrs = sim_pred.split('責')[-1]
    else:
        return
    tmpStrs = numberUtils.numberReplacement(tmpStrs)
    # lstStaff=re.findall(r'\d+', tmpStrs)
    # tmpStrs=''.join(lstStaff)
    resultMap['7_staffNO'] = tmpStrs
    # 08
    print('output staffNo {}'.format(resultMap['7_staffNO']))
    resultMap['pos_staff'] = i
Beispiel #12
0
def getTel(tmpResult, resultMap, i):
    print('input tel {}'.format(tmpResult))
    if (tmpResult.find('年') > -1):
        return
    if (resultMap['pos_tel_before'] > 0 and tmpResult.find(' ') > -1):
        print('tel_before {}'.format(resultMap['pos_tel_before']))
        telBefore = resultMap['tel_before'].split(':')
        if (len(telBefore) > 1):
            telBeforeNew = telBefore[1]
            telAfter = tmpResult.split(':')
            if (len(telAfter) > 1):
                telAfterNew = telAfter[1]
                lsStr1 = list(telAfterNew)
                for idx, val in enumerate(telAfterNew):
                    if (val == ' '):
                        lsStr1[idx] = telBeforeNew[idx]

    tmpResult = jaconv.z2h(tmpResult, digit=True, ascii=True)

    if (tmpResult.find('話') > -1):
        tmpResult = tmpResult.split('話')[1]
    if (tmpResult.find(':') > -1):
        tmpResult = tmpResult.split(':')[1]
    tmpResult = numberUtils.numberReplacement(tmpResult)
    strList = re.findall(r'\d+', tmpResult)
    tmpResult = ''.join(strList)
    if (len(tmpResult) > 10):
        tmpResult = tmpResult[-10:-1]
    if (resultMap['3_tel'] == 'none'):  #init value
        resultMap['3_tel'] = tmpResult
    elif (len(resultMap['3_tel']) < len(tmpResult)):
        resultMap['3_tel'] = tmpResult
    if len(tmpResult) == 9:
        tmpResult = str(0) + tmpResult
    if (resultMap['3_tel'] == tmpResult and len(tmpResult) > 6):
        print('output tel{} {}'.format('-' * 10, tmpResult))
        if (tmpResult != 'none'):
            resultMap['pos_tel_after'] = i
Beispiel #13
0
def parseResult(result, resultMap, im_name):
    for i in result:  # for trim string, convert string_______step 1
        sim_pred = str(result[i][1]).strip()
        sim_pred = numberUtils.removeQuote(sim_pred)
        sim_pred = jaconv.z2h(sim_pred, digit=True, ascii=True)
        result[i][1] = sim_pred

    # TODO # get all key position
    # ___________________________________________________step 2__prepare_pos
    lingUtils.getPosLing(resultMap, result)
    pos_ling_after = resultMap['pos_ling_after']

    for i in result:
        if pos_ling_after > 0 and i <= pos_ling_after:
            continue
        if resultMap['pos_mny_after'] > 0:
            break
        sim_pred = str(result[i][1])
        if sim_pred.find('¥') > -1:
            resultMap['pos_mny_after'] = i
    pos_mny = resultMap['pos_mny_after']

    for i in result:
        if pos_ling_after > 0 and i <= pos_ling_after:
            continue
        if resultMap['pos_tax_after'] > 0:
            break
        sim_pred = str(result[i][1])
        if (i > 8 and taxUtils.checkIsTaxStr(sim_pred)):
            resultMap['pos_tax_after'] = i
    pos_tax = resultMap['pos_tax_after']

    if pos_tax > 0 and pos_mny > 0 and pos_mny > pos_tax:
        pos_mny = 0
    if pos_tax > 0 and pos_ling_after > 0 and pos_tax < pos_ling_after:
        pos_ling_after = 0

    for i in result:
        if pos_mny > 0 and i >= pos_mny:
            break
        if pos_tax > 0 and i >= pos_tax:
            break
        if resultMap['pos_time_after'] > 0:
            break
        sim_pred = str(result[i][1])
        if (i > 3 and resultMap['pos_time_after'] == 0):
            if (sim_pred.find('年') > -1):
                resultMap['pos_time_after'] = i
    pos_time = resultMap['pos_time_after']

    # timeUtils.amendYear(result[pos_time][1], resultMap) #FIXME do nothing
    if (pos_time > 0):
        timeUtils.amendHour(result[pos_time - 1][1], resultMap)
        timeUtils.amendHour(result[pos_time][1], resultMap)
        timeUtils.amendHour(result[pos_time + 1][1], resultMap)

    if pos_ling_after > 0 and pos_time > 0 and pos_ling_after < pos_time:  # ling and then year,match family
        resultMap['type_shop'] = 1  # two shop type

    for i in result:
        if pos_ling_after > 0 and i <= pos_ling_after:
            continue
        if pos_time > 0 and i <= pos_time:
            continue
        sim_pred = str(result[i][1])
        if (sim_pred.find('No.') > -1):
            staffNoUtils.getNo(sim_pred, resultMap, i)
        if (resultMap['pos_staff'] > 0):
            break
    pos_staff = resultMap['pos_staff']

    if pos_staff > 0:  # receipt
        sim_pred = str(result[pos_staff - 1][1])
        staffNoUtils.getReceipt(sim_pred, resultMap,
                                i)  #get from current line same with staff
        if pos_staff + 1 < len(result):
            sim_pred = str(result[pos_staff + 1][1]).strip()
            staffNoUtils.getReceipt(sim_pred, resultMap, i)

    for i in result:
        sim_pred = str(result[i][1])
        if (i > pos_time and i < pos_tax - 3):
            categoryUtils.getCategoryAfter(sim_pred, resultMap, i)

    cardUtils.getCardPos(resultMap, result)
    cardUtils.parseCard(resultMap, result)

    resultMap['9_category'] = len(resultMap['suffix_catPrice'])
    catTotalMny = 0
    taxMny = resultMap['a_tax']
    subtotal = resultMap['b_subtotal']
    catCount = len(resultMap['suffix_catPrice'])
    for price in resultMap['suffix_catPrice']:
        catTotalMny += price
    if (taxMny > 0):
        if (taxMny / 0.07 + 50 < catTotalMny and catCount > 1):
            catTotalMny -= resultMap['suffix_catPrice'][-1]
            resultMap['9_category'] = len(resultMap['suffix_catPrice']) - 1
            # if(resultMap['5_total']==0):
            # if(subtotal>0)
    else:
        if (catCount > 1):
            lastMny = resultMap['suffix_catPrice'][-1]
            if (catTotalMny - lastMny == lastMny):
                catTotalMny = lastMny
                resultMap['9_category'] = catCount - 1
    if (resultMap['5_total'] < 100):
        if (subtotal > 0):
            resultMap['5_total'] = subtotal
        else:
            resultMap['5_total'] = catTotalMny

    for i in result:  # tel parser at last
        sim_pred = result[i][1]
        if (resultMap['pos_tel_after'] == 0):
            telUtils.getTel(sim_pred, resultMap, i)
            if (resultMap['pos_tel_after'] != 0):
                break
            if pos_time > 0 and i > pos_time:
                break
            if pos_ling_after > 0 and i > pos_ling_after:
                break
            if pos_tax > 0 and i > pos_tax:
                break
            if pos_staff > 0 and i > pos_staff:
                break

    for i in result:  # shop name parser at last
        sim_pred = result[i][1]
        if (resultMap['1_shopName'] == 'none'):
            shopNameUtils.getShopName(sim_pred, resultMap)
            if (resultMap['1_shopName'] != 'none'):
                break
            if pos_time > 0 and i > pos_time:
                break
            if pos_ling_after > 0 and i > pos_ling_after:
                break
            if pos_tax > 0 and i > pos_tax:
                break
            if pos_staff > 0 and i > pos_staff:
                break
    # city
    for i in result:  # city parser at last
        sim_pred = result[i][1]
        if (resultMap['2_city'] == 'none'):
            cityUtils.getCity(sim_pred, resultMap)
            if (resultMap['2_city'] != 'none'):
                break
            if pos_ling_after > 0 and i > pos_ling_after:
                break
            if pos_time > 0 and i > pos_time:
                break
            if pos_tax > 0 and i > pos_tax:
                break
            if pos_staff > 0 and i > pos_staff:
                break

    if (resultMap['1_shopName'] == 'ファミリマート'):
        if (resultMap['pos_time'] + 1 == resultMap['pos_staff']):
            for i in range(pos_time + 1, pos_time + 4):
                sim_pred = str(result[i][1]).strip()
                if (sim_pred.find('-') == 1 and len(sim_pred) <= 8):
                    sim_pred = numberUtils.numberReplacement(sim_pred)
                    if (sim_pred[0].isdigit()
                            and sim_pred[0] != resultMap['6_receiptNO'][0]):
                        resultMap['6_receiptNO'] = sim_pred[0] + resultMap[
                            '6_receiptNO'][1:]
                if (len(sim_pred) <= 8):
                    staffNoUtils.amendStaff(sim_pred, resultMap, i)

    # set default at last--------------vvvvv
    if pos_tax>0 and \
        (result[pos_tax][1].find('づ')>-1 \
         or result[pos_tax][1].find('う')>-1):
        resultMap['1_shopName'] = 'サンクス'
    if resultMap['1_shopName'] == 'none':
        resultMap['1_shopName'] = 'サンクス'

    if len(resultMap['2_city'].strip()) == 0:
        resultMap['2_city'] = '東京都'
    if resultMap['3_tel'] == 'none':
        resultMap['test_tel'] = 'unknown_tel'
    if len(resultMap['3_tel'].strip()) == 0:
        resultMap['3_tel'] = 'none'
        resultMap['test_tel'] = 'unknown_tel'

    if resultMap['6_receiptNO'] == 'none':
        resultMap['test_receiptNO'] = 'unknown_receipt'
    if resultMap['7_staffNO'] == 'none':
        resultMap['test_staffNO'] = 'unknown_staff'
    if len(resultMap['7_staffNO']) == 0:
        resultMap['7_staffNO'] = 'none'
    if resultMap['8_pointcard'] == 'none':
        resultMap['test_Card'] = 'unknown_card'
    # --------------------^^^^^^^
    for key in sorted(resultMap):
        print('{} = {}'.format(key, resultMap[key]))

    base_name = im_name.split('/')[-1]
    with open('./' + 'result_submit.tsv', 'a+') as f:
        f.write(
            base_name.split('.')[0] + '.jpg-----------' +
            str(datetime.datetime.now().time()) + '\r\n')
        resultStr = ''
        i = 0
        for key in sorted(resultMap):
            if (i > 9):
                break
            resultStr += '{}\r\n'.format(resultMap.get(key))
            i += 1
        f.write(resultStr)