From c7648de3bce74b0e38e2de684c1bbb83373c9c16 Mon Sep 17 00:00:00 2001 From: Vivek Teega Date: Fri, 15 Feb 2019 17:41:53 +0530 Subject: [PATCH] Cleaning the code --- .gitignore | 1 + app.py | 18 ++--- errors.db | Bin 12288 -> 0 bytes flo.py | 17 ----- parse.py | 121 ------------------------------- parse-mod.py => parse_flodata.py | 88 +++++++++++----------- parse_incorp.py | 111 ---------------------------- test.db | 0 8 files changed, 52 insertions(+), 304 deletions(-) delete mode 100644 errors.db delete mode 100644 flo.py delete mode 100644 parse.py rename parse-mod.py => parse_flodata.py (54%) delete mode 100644 parse_incorp.py delete mode 100644 test.db diff --git a/.gitignore b/.gitignore index 2483976..c713bfc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea/ __pycache__/ +*.db diff --git a/app.py b/app.py index 9023dd3..10b771f 100644 --- a/app.py +++ b/app.py @@ -4,7 +4,7 @@ from flask_wtf import Form from wtforms import StringField from wtforms.validators import DataRequired from wtforms.widgets import TextArea -import parse_incorp +import parse_flodata import sqlite3 class MyForm(Form): @@ -27,18 +27,16 @@ def textparse(): errorform = ReportError() if form.validate_on_submit(): - g.parsed_data = parse_incorp.parse_flodata(form.flodata.data) - return render_template('index.html', form=form, parsed_data= g.parsed_data, errorform=errorform) + parsed_data = parse_flodata.parse_flodata(form.flodata.data) + return render_template('index.html', form=form, parsed_data= parsed_data, errorform=errorform) if errorform.validate_on_submit(): - #conn = sqlite3.connect('test.db') - #sqlquery = 'INSERT INTO errorlogs (flodata, comments) VALUES ({},{})'.format( g.parsed_data['flodata'], errorform.comments.data) - #conn.execute(sqlquery) - print(g.parsed_data) - #conn.close() - return render_template('index.html', form=form, parsed_data= g.parsed_data, errorform=errorform) + conn = sqlite3.connect('test.db') + sqlquery = 'INSERT INTO errorlogs (flodata, comments) VALUES ({},{})'.format( g.parsed_data['flodata'], errorform.comments.data) + conn.execute(sqlquery) + conn.close() + return render_template('index.html', form=form, parsed_data= parsed_data, errorform=errorform) return render_template('index.html', form=form, errorform=errorform) app.run(debug=True) - diff --git a/errors.db b/errors.db deleted file mode 100644 index 6c82df41a0b03201fd3a0d4716c24be179e873e7..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI#KTE?v7{~FuRCJQMbrJfEEiEp614cQd*v1+om`P%Ll|uf|Bo2-az8_zrmtqTc zakhLPxa5A}?jWC=o`?I%sQY20rQK+`v8^T_ZG(F^`LELI;{5B> zt@7F<)&6u33IPNVKmY**5I_I{1Q0*~f&U>e^Nu@%fsX6sCAHN$b^Y^mW!G;upYO-k zb!hy^NaSBljO?xL3H9<6#Wm+tfI+4gcxil}b%;t5qiT$QGcTGQq00IagfB*sr pAb 1: - return 'od' - if count == 1 and (returnval is None): - returnval = marker - return returnval - - -def extractOperation(text, operationList): - count = 0 - returnval = None - text = text.lower() - for operation in operationList: - operation = operation.lower() - - count = count + text.count(operation) - if count > 1: - return 'od' - if count == 1 and (returnval is None): - returnval = operation - return returnval - - -def extractAmount(text): - count = 0 - returnval = None - text = text.lower() - splitText = re.split("\W+", text) - - for word in splitText: - word = word.replace('rmt','') - try: - float(word) - count = count + 1 - returnval = float(word) - except ValueError: - pass - - if count > 1: - return 'od' - return returnval - -# Combine test -operationList = ['send','transfer','give'] -markerList = ['ranchimall','rmt'] -for string in testcases.testStrings: - marker = extractOperation(string, markerList) - operation = extractOperation(string, operationList) - amount = extractAmount(string) - - print('text - ' + string) - print('Marker - '+str(marker)) - print('Operation - '+str(operation)) - print('Amount - '+str(amount)+'\n\n') - - -# Marker test -'''markerList = ['ranchimall','rmt'] -for string in testcases.testStrings: - returnval = extractMarkers(string, markerList) - if returnval is not None: - if returnval == 'od': - print('text - ' + string) - print('Transaction reject\nMore than one marker present\n\n') - else: - print('text - ' + string) - print('Marker - '+str(returnval)+'\n\n') - else: - print('text - ' + string) - print('Marker not found\n\n') - -# Operator test -operationList = ['send','transfer','give'] -for string in testcases.testStrings: - returnval = extractOperation(string, operationList) - if returnval is not None: - if returnval == 'od': - print('text - ' + string) - print('Transaction reject\nMore than one operation present\n\n') - else: - print('text - ' + string) - print('Operation - '+str(returnval)+'\n\n') - else: - print('text - ' + string) - print('Operation not found\n\n')''' - - -''' -GRAVEYARD ----------- - -def extractAddress(text): - count = 0 - returnval = None - - for operation in operationList: - operation = operation.lower() - - count = count + text.count(operation) - if count > 1: - return 'od' - if count == 1 and (returnval is None): - returnval = operation - return returnval''' diff --git a/parse-mod.py b/parse_flodata.py similarity index 54% rename from parse-mod.py rename to parse_flodata.py index 0a29b35..572737e 100644 --- a/parse-mod.py +++ b/parse_flodata.py @@ -1,35 +1,35 @@ import re -import testcases -marker=None -operation=None -address=None -amount=None +marker = None +operation = None +address = None +amount = None -def extractMarkers(text): - returnval = None - text = text.lower() - text = ' '.join(text.split()) - textlst = text.split(' ') - - for part in textlst: - if part[-1] == '#' and len(part)>1: - if returnval is not None: - return 'od' - returnval = part - return returnval +def isTransfer(text): + wordlist = ['transfer','send','give'] #keep everything lowercase + textList = text.split(' ') + for word in wordlist: + if word in textList: + return True + return False -def extractOperation(text, operationList): +def isIncorp(text): + wordlist = ['incorporate','create','start'] # keep everything lowercase + textList = text.split(' ') + for word in wordlist: + if word in textList: + return True + return False + +def extractOperation(text): + operationList = ['send', 'transfer', 'give'] # keep everything lowercase count = 0 returnval = None - text = text.lower() for operation in operationList: - operation = operation.lower() - count = count + text.count(operation) if count > 1: - return 'od' + return 'Too many' if count == 1 and (returnval is None): returnval = operation return returnval @@ -38,7 +38,6 @@ def extractOperation(text, operationList): def extractAmount(text): count = 0 returnval = None - text = text.lower() splitText = re.split("\W+", text) for word in splitText: @@ -51,21 +50,12 @@ def extractAmount(text): pass if count > 1: - return 'od' + return 'Too many' return returnval -def isIncorp(text): - wordlist = ['incorporate','create','start'] - cleantext = re.sub(' +', ' ',text) - textList = cleantext.split(' ') - for word in wordlist: - if word in textList: - return True - return False -def extractIncMarker(text): - cleantext = re.sub(' +', ' ',text) - textList = cleantext.split(' ') +def extractMarker(text): + textList = text.split(' ') for word in textList: if word[-1] == '#': return word @@ -73,8 +63,7 @@ def extractIncMarker(text): def extractInitTokens(text): base_units = {'thousand':10**3 , 'million':10**6 ,'billion':10**9, 'trillion':10**12} - cleantext = re.sub(' +', ' ',text) - textList = cleantext.split(' ') + textList = text.split(' ') for idx,word in enumerate(textList): try: result = float(word) @@ -84,19 +73,28 @@ def extractInitTokens(text): except: continue + # Combine test def parse_flodata(string): - if not isIncorp(string): - operationList = ['send', 'transfer', 'give'] - marker = extractMarkers(string) - operation = extractOperation(string, operationList) - amount = extractAmount(string) + if string[0:5] == 'text:': + string = string.split('text:')[1] + + cleanstring = re.sub(' +', ' ', string) + cleanstring = cleanstring.lower() + + if isTransfer(cleanstring): + marker = extractMarker(cleanstring) + operation = extractOperation(cleanstring) + amount = extractAmount(cleanstring) parsed_data = {'type': 'transfer', 'flodata': string, 'marker': marker, 'operation': operation, 'amount': amount} + elif isIncorp(cleanstring): + incMarker = extractMarker(cleanstring) + initTokens = extractAmount(cleanstring) + parsed_data = {'type': 'incorporation', 'flodata': string, 'marker': incMarker, 'initTokens': initTokens} else: - incMarker = extractIncMarker(string) - initTokens = extractInitTokens(string) - parsed_data = {'type': 'incorporation', 'flodata': string, 'marker': marker, 'initTokens': initTokens} + parsed_data = {'type': 'noise'} + return parsed_data diff --git a/parse_incorp.py b/parse_incorp.py deleted file mode 100644 index f62d6ef..0000000 --- a/parse_incorp.py +++ /dev/null @@ -1,111 +0,0 @@ -import re -import testcases - -marker=None -operation=None -address=None -amount=None - -def extractMarkers(text): - returnval = None - text = text.lower() - textlst = text.split(' ') - - for part in textlst: - if part[-1] == '#' and len(part)>1: - if returnval is not None: - return 'od' - returnval = part - return returnval - - -def extractOperation(text, operationList): - count = 0 - returnval = None - text = text.lower() - for operation in operationList: - operation = operation.lower() - - count = count + text.count(operation) - if count > 1: - return 'od' - if count == 1 and (returnval is None): - returnval = operation - return returnval - - -def extractAmount(text): - count = 0 - returnval = None - text = text.lower() - splitText = re.split("\W+", text) - - for word in splitText: - word = word.replace('rmt','') - try: - float(word) - count = count + 1 - returnval = float(word) - except ValueError: - pass - - if count > 1: - return 'od' - return returnval - -def isIncorp(text): - wordlist = ['incorporate','create','start'] - cleantext = re.sub(' +', ' ',text) - cleantext= cleantext.lower() - textList = cleantext.split(' ') - for word in wordlist: - if word in textList: - return True - return False - -def extractIncMarker(text): - cleantext = re.sub(' +', ' ',text) - textList = cleantext.split(' ') - for word in textList: - if word[-1] == '#': - return word - return False - -def extractInitTokens(text): - base_units = {'thousand':10**3 , 'million':10**6 ,'billion':10**9, 'trillion':10**12, 'lakh':10**5, 'crore':10**7} - cleantext = re.sub(' +', ' ',text) - textList = cleantext.split(' ') - for idx,word in enumerate(textList): - try: - result = float(word) - if textList[idx+1] in base_units: - return result*base_units[textList[idx+1]] - return result - except: - continue - - -# Combine test -def parse_flodata(string): - - if string[0:5] == 'text:': - string = string.split('text:')[1] - - string = string.lower() - - if not isIncorp(string): - operationList = ['send', 'transfer', 'give'] - marker = extractMarkers(string) - operation = extractOperation(string, operationList) - amount = extractAmount(string) - parsed_data = {'type': 'transfer', 'flodata': string, 'marker': marker, 'operation': operation, - 'amount': amount} - else: - incMarker = extractIncMarker(string) - initTokens = extractInitTokens(string) - parsed_data = {'type': 'incorporation', 'flodata': string, 'marker': incMarker, 'initTokens': initTokens} - - return parsed_data - - - diff --git a/test.db b/test.db deleted file mode 100644 index e69de29..0000000