Skip to content

Commit

Permalink
Setup Travis CI_resolved (vishakha-lall#98)
Browse files Browse the repository at this point in the history
* travis added

* added linting to travis

* updated unit testing

* added script for unit testing

* replaced unittest with pytest

* linting fixed
  • Loading branch information
Mansi145 authored Mar 29, 2020
1 parent 1a3f273 commit d9322f1
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 153 deletions.
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: python
python:
- "3.6"
cache: pip
install:
- pip install -r requirements.txt
script:
- flake8 .
- pytest
8 changes: 5 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@

app = Flask(__name__)


@app.route('/chatbot/<user_input>', methods=['GET'])
def chat(user_input):
try:
response = message_to_bot(user_input, clf, learn_response)
except:
return jsonify({'message': ('Unable to get response', learn_response)}, 500)
except: # noqa: E722
return jsonify({'message': ('Unable to get response', learn_response)}, 500) # noqa: E501

return jsonify({'message': response}, 200)


if __name__ == '__main__':
clf, learn_response = setup()
app.run(debug=False)
42 changes: 21 additions & 21 deletions chatbot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import utilities
import databaseconnect
import googleMapsApiModule
import googleMapsApiModule
import logging
import logger_config
location_dict = {"origin": "null", "destination": "null"}
Expand Down Expand Up @@ -29,23 +29,23 @@ def message_to_bot(H, clf, learn_response):
learn_response = 3
return B, learn_response
if learn_response == 3:
location_dict["destination"]=H
origin, destination = location_dict["origin"], location_dict["destination"]
googleMapsApiModule.direction(origin,destination)
location_dict["destination"] = H
origin, destination = location_dict["origin"], location_dict["destination"] # noqa: E501
googleMapsApiModule.direction(origin, destination)
B = "I will certainly help you with that."
learn_response = 0
return B, learn_response
if "bye" in H.lower().split(" "): #check in words within H
if "bye" in H.lower().split(" "): # check in words within H
B = "Bye! I'll miss you!"
return B, learn_response # exit loop
if not H:
B = "Please say something!"
return B, learn_response #empty input
# grammar parsing
B = "Please say something!"
return B, learn_response # empty input
# grammar parsing
subj = set()
obj = set()
verb = set()
triples,root = utilities.parse_sentence(H)
triples, root = utilities.parse_sentence(H)
triples = list(triples)
for t in triples:
if t[0][1][:2] == 'VB':
Expand All @@ -55,7 +55,7 @@ def message_to_bot(H, clf, learn_response):
subj.add(t[2][0])
if relation[-3:] == 'obj':
obj.add(t[2][0])
logging.debug("\t"+"Subject: "+str(subj)+"\n"+"\t"+"Object: "+str(obj)+"\n"+"\t"+"Topic: "+str(root)+"\n"+"\t"+"Verb: "+str(verb))
logging.debug("\t"+"Subject: "+str(subj)+"\n"+"\t"+"Object: "+str(obj)+"\n"+"\t"+"Topic: "+str(root)+"\n"+"\t"+"Verb: "+str(verb)) # noqa: E501
subj = list(subj)
obj = list(obj)
verb = list(verb)
Expand All @@ -67,33 +67,33 @@ def message_to_bot(H, clf, learn_response):
proper_nouns.add(t[2][0])
proper_nouns == list(proper_nouns)
logging.debug("\t"+"Proper Nouns: "+str(proper_nouns))
#classification
classification = utilities.classify_sentence(clf,H)
#logging.debug(classification)
# classification
classification = utilities.classify_sentence(clf, H)
# logging.debug(classification)
if learn_response == 0:
databaseconnect.add_to_database(classification, subj, root, verb, H)
if (classification == 'C'):
B = databaseconnect.get_chat_response()
elif (classification == 'Q'):
B, learn_response = databaseconnect.get_question_response(subj, root, verb)
if learn_response == 1 and (len(proper_nouns) == 0 or (len(proper_nouns) == 1 and H.split(" ", 1)[0] != "Where")):
databaseconnect.add_learnt_statement_to_database(subj, root, verb)
if learn_response == 1 and (len(proper_nouns) >= 2 or (len(proper_nouns) == 1 and H.split(" ", 1)[0] == "Where")):
B, learn_response = databaseconnect.get_question_response(subj, root, verb) # noqa: E501
if learn_response == 1 and (len(proper_nouns) == 0 or (len(proper_nouns) == 1 and H.split(" ", 1)[0] != "Where")): # noqa: E501
databaseconnect.add_learnt_statement_to_database(subj, root, verb) # noqa: E501
if learn_response == 1 and (len(proper_nouns) >= 2 or (len(proper_nouns) == 1 and H.split(" ", 1)[0] == "Where")): # noqa: E501
learn_response = 0
B = "I will certainly help you with that."
else:
B = "Oops! I'm not trained for this yet."
else:
B, learn_response = databaseconnect.learn_question_response(H)
if (len(proper_nouns) >= 2 or (len(proper_nouns) >= 1 and H.split(" ", 1)[0] == "Where")) and len(subj) != 0:
if (len(proper_nouns) >= 2 or (len(proper_nouns) >= 1 and H.split(" ", 1)[0] == "Where")) and len(subj) != 0: # noqa: E501
if subj[0] == "distance":
if len(proper_nouns) == 2:
location_dict["origin"] = proper_nouns.pop()
location_dict["destination"] = proper_nouns.pop()
origin, destination = location_dict["origin"], location_dict["destination"]
googleMapsApiModule.direction(origin,destination)
origin, destination = location_dict["origin"], location_dict["destination"] # noqa: E501
googleMapsApiModule.direction(origin, destination)
else:
B = "I didn't get that. Can you please give me the origin location?"
B = "I didn't get that. Can you please give me the origin location?" # noqa: E501
learn_response = 2
if len(proper_nouns) == 1:
location = proper_nouns.pop()
Expand Down
4 changes: 2 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
else:
load_dotenv('ENV/.env')

### MAKE SURE you have filled environment variables in `.env` files in `./ENV/` folder
''' MAKE SURE you have filled environment variables in `.env` files in `./ENV/` folder''' # noqa: E501

user = os.getenv("DB_USER")
password = os.getenv("DB_PASSWORD")
Expand All @@ -21,5 +21,5 @@
# your_path_to_stanford-corenlp-x.x.x-models.jar
stanford_path_to_models_jar = os.getenv("STANFORD_PATH_TO_MODELS_JAR")

# for eg. 'C:\\Program\ Files\\Java\\jdk1.8.0_201\\bin\\java.exe' or '/usr/local/openjdk-11/bin/java'
# for eg. 'C:\\Program\ Files\\Java\\jdk1.8.0_201\\bin\\java.exe' or '/usr/local/openjdk-11/bin/java' # noqa: E501
javahome = os.getenv("JAVAHOME")
2 changes: 1 addition & 1 deletion constants.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BASE_URL = {'direction': "https://www.google.com/maps/dir/?api=1", 'geocoding': "https://www.google.com/maps/search/?api=1&query"}
BASE_URL = {'direction': "https://www.google.com/maps/dir/?api=1", 'geocoding': "https://www.google.com/maps/search/?api=1&query"} # noqa: E501
34 changes: 17 additions & 17 deletions databaseconnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def connection_to_database():
# logging.debug("Connected")
logging.debug('MySQL connected')
return conn
except:
except: # noqa: E722
raise Exception("DATABASE NOT CONNECTED")


Expand All @@ -46,10 +46,10 @@ def connection_to_database():
def setup_database():
db = connection_to_database()
cur = db.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS chat_table(id INTEGER PRIMARY KEY AUTO_INCREMENT, root_word VARCHAR(40), subject VARCHAR(40), verb VARCHAR(40), sentence VARCHAR(200))")
cur.execute("CREATE TABLE IF NOT EXISTS statement_table(id INTEGER PRIMARY KEY AUTO_INCREMENT, root_word VARCHAR(40), subject VARCHAR(40), verb VARCHAR(40), sentence VARCHAR(200))")
cur.execute("CREATE TABLE IF NOT EXISTS question_table(id INTEGER PRIMARY KEY AUTO_INCREMENT, root_word VARCHAR(40), subject VARCHAR(40), verb VARCHAR(40), sentence VARCHAR(200))")
cur.execute("CREATE TABLE IF NOT EXISTS directions_table(id INTEGER PRIMARY KEY AUTO_INCREMENT, origin_location VARCHAR(100), destination_location VARCHAR(100))")
cur.execute("CREATE TABLE IF NOT EXISTS chat_table(id INTEGER PRIMARY KEY AUTO_INCREMENT, root_word VARCHAR(40), subject VARCHAR(40), verb VARCHAR(40), sentence VARCHAR(200))") # noqa: E501
cur.execute("CREATE TABLE IF NOT EXISTS statement_table(id INTEGER PRIMARY KEY AUTO_INCREMENT, root_word VARCHAR(40), subject VARCHAR(40), verb VARCHAR(40), sentence VARCHAR(200))") # noqa: E501
cur.execute("CREATE TABLE IF NOT EXISTS question_table(id INTEGER PRIMARY KEY AUTO_INCREMENT, root_word VARCHAR(40), subject VARCHAR(40), verb VARCHAR(40), sentence VARCHAR(200))") # noqa: E501
cur.execute("CREATE TABLE IF NOT EXISTS directions_table(id INTEGER PRIMARY KEY AUTO_INCREMENT, origin_location VARCHAR(100), destination_location VARCHAR(100))") # noqa: E501


@logger_config.logger
Expand All @@ -59,7 +59,7 @@ def add_to_database(classification, subject, root, verb, H):
cur = db.cursor()
cur = db.cursor(buffered=True)
if classification == 'C':
cur.execute(f"INSERT INTO chat_table(root_word,verb,sentence) VALUES ('{root}','{verb}','{H}')")
cur.execute(f"INSERT INTO chat_table(root_word,verb,sentence) VALUES ('{root}','{verb}','{H}')") # noqa: E501
db.commit()
elif classification == 'Q':
cur.execute("SELECT sentence FROM question_table")
Expand All @@ -71,7 +71,7 @@ def add_to_database(classification, subject, root, verb, H):
break
if exist == 0:
# do not add if question already exists
cur.execute(f"INSERT INTO question_table(subject,root_word,verb,sentence) VALUES ('{subject}','{root}','{verb}','{H}')")
cur.execute(f"INSERT INTO question_table(subject,root_word,verb,sentence) VALUES ('{subject}','{root}','{verb}','{H}')") # noqa: E501
db.commit()
else:
cur.execute("SELECT sentence FROM statement_table")
Expand All @@ -82,7 +82,7 @@ def add_to_database(classification, subject, root, verb, H):
exist = 1
break
if exist == 0: # do not add if question already exists
cur.execute(f"INSERT INTO statement_table(subject,root_word,verb,sentence) VALUES ('{subject}','{root}','{verb}','{H}')")
cur.execute(f"INSERT INTO statement_table(subject,root_word,verb,sentence) VALUES ('{subject}','{root}','{verb}','{H}')") # noqa: E501
db.commit()


Expand Down Expand Up @@ -116,7 +116,7 @@ def get_question_response(subject, root, verb):
found = 1
break
if found == 1:
cur.execute(f"SELECT sentence FROM statement_table WHERE verb='{verb}'")
cur.execute(f"SELECT sentence FROM statement_table WHERE verb='{verb}'") # noqa: E501
res = cur.fetchone()
B = res[0]
return B, 0
Expand All @@ -132,22 +132,22 @@ def get_question_response(subject, root, verb):
found = 1
break
if found == 1:
cur.execute(f"SELECT verb FROM statement_table WHERE subject='{subject}'")
cur.execute(f"SELECT verb FROM statement_table WHERE subject='{subject}'") # noqa: E501
res = cur.fetchone()
checkVerb = res[0] # checkVerb is a string while verb is a list. checkVerb ['verb']
checkVerb = res[0] # checkVerb is a string while verb is a list. checkVerb ['verb'] # noqa: E501
if checkVerb == '[]':
cur.execute(f"SELECT sentence FROM statement_table WHERE subject='{subject}'")
cur.execute(f"SELECT sentence FROM statement_table WHERE subject='{subject}'") # noqa: E501
res = cur.fetchone()
B = res[0]
return B, 0
else:
if checkVerb[2:-2] == verb[0]:
cur.execute(f"SELECT sentence FROM statement_table WHERE subject='{subject}'")
cur.execute(f"SELECT sentence FROM statement_table WHERE subject='{subject}'") # noqa: E501
res = cur.fetchone()
B = res[0]
return B, 0
else:
B = "Sorry I don't know the response to this. Please train me."
B = "Sorry I don't know the response to this. Please train me." # noqa: E501
return B, 1
else:
B = "Sorry I don't know the response to this. Please train me."
Expand All @@ -158,7 +158,7 @@ def get_question_response(subject, root, verb):
def add_learnt_statement_to_database(subject, root, verb):
db = connection_to_database()
cur = db.cursor()
cur.execute(f"INSERT INTO statement_table(subject,root_word,verb) VALUES ('{subject}','{root}','{verb}')")
cur.execute(f"INSERT INTO statement_table(subject,root_word,verb) VALUES ('{subject}','{root}','{verb}')") # noqa: E501
db.commit()


Expand All @@ -169,7 +169,7 @@ def learn_question_response(H):
cur.execute("SELECT id FROM statement_table ORDER BY id DESC")
res = cur.fetchone()
last_id = res[0]
cur.execute(f"UPDATE statement_table SET sentence='{H}' WHERE id={last_id}")
cur.execute(f"UPDATE statement_table SET sentence='{H}' WHERE id={last_id}") # noqa: E501
db.commit()
B = "Thank you! I have learnt this."
return B, 0
Expand All @@ -185,7 +185,7 @@ def clear_table(table_name):
for table in tables_to_be_cleaned:
describe_table(cur, table)

if input("Enter 'Y' to confirm cleaning of BOTH tables: ") in ("Y", "y"):
if input("Enter 'Y' to confirm cleaning of BOTH tables: ") in ("Y", "y"): # noqa: E501
for table in tables_to_be_cleaned:
cur.execute(f"DELETE FROM {table}")
db.commit()
Expand Down
Loading

0 comments on commit d9322f1

Please sign in to comment.