diff --git a/exec/findsynonyms.py b/exec/findsynonyms.py new file mode 100644 index 0000000..17cdbd3 --- /dev/null +++ b/exec/findsynonyms.py @@ -0,0 +1,16 @@ +import nltk # Used for natural language recognition +from nltk.stem import WordNetLemmatizer # Used to lemmatize (find root word) +from nltk.corpus import wordnet # Wordnet for finding synonyms +from nltk.tokenize import sent_tokenize, word_tokenize + + +# Finds synonyms of word - returns array of synonyms +def findSynonyms(entryWord): + # Array which will contain synonyms + synonyms = [] + for syn in wordnet.synsets(entryWord): + # Iterates through synonyms + for l in syn.lemmas(): + # Iterates through possible lemmas, appends to synonyms array + synonyms.append(l.name()) + return synonyms diff --git a/exec/friendly_drink.py b/exec/friendly_drink.py index 6de2a8b..ef0d6fe 100644 --- a/exec/friendly_drink.py +++ b/exec/friendly_drink.py @@ -5,8 +5,9 @@ from nltk.corpus import wordnet # Wordnet for finding synonyms from nltk.tokenize import sent_tokenize, word_tokenize +from findsynonyms import findSynonyms from location import lctnwthrgt - +from stateofmind import fnchk from random import randint # Used to generate random integer import ASCII_Store @@ -73,51 +74,14 @@ def stateofmind(): usedWords = [] # Contains all the words used to make decisions on what response to make wrdarr = word_tokenize(tempstore['sttofmnd']) # Array of sentence tokens= nltk.pos_tag(wrdarr) - fnchk(tokens) - if sombin==1: + + if fnchk(tokens): reveal ("That is Amazing.") - elif sombin==2: + sombin=1 + else: reveal ("I hope you feel better") - - -def fnchk(sentence): - fnSynonyms= findSynonyms("fine")+ findSynonyms("good") - ntSynonyms= findSynonyms("not") - sdSynonyms= findSynonyms("sad")+findSynonyms("sick")+findSynonyms("bad") - for i in range (0, (len(sentence)-1)): - ki=0; - if sentence[i][1]=='JJ': - for syns in fnSynonyms: - if syns.lower() == sentence[i][0].lower(): - ki=ki+1 - if i==0: - sombin=1 - else: - if sentence[i-1][0].lower()=="not": - sombin=0 - else: - for ntsyns in ntSynonyms: - if sentence[i-1][0].lower()==ntsyns.lower(): - sombin=0 - else: - sombin=1 - - if ki!=0: - for syns in sdSynonyms: - if syns.lower() == sentence[i][0].lower(): - if i==0: - sombin=0 - else: - if sentence[i-1][0].lower()=="not": - sombin=1 - else: - for ntsyns in ntSynonyms: - if sentence[i-1][0].lower()==ntsyns.lower(): - sombin=1 - else: - sombin=0 - return + sombin=0 @@ -167,16 +131,6 @@ def getTime(locale): reveal("The time in %s, %s is:" % (searchParam.title(), countryName)) reveal(time.strftime("%H:%M:%S, %a, %d %b %Y ", time.gmtime(checkTime))) -# Finds synonyms of word - returns array of synonyms -def findSynonyms(entryWord): - # Array which will contain synonyms - synonyms = [] - for syn in wordnet.synsets(entryWord): - # Iterates through synonyms - for l in syn.lemmas(): - # Iterates through possible lemmas, appends to synonyms array - synonyms.append(l.name()) - return synonyms # Checks if asking to flip a coin def checkToFlipCoin(POS_tagged_sentence): @@ -205,10 +159,6 @@ def checkToFlipCoin(POS_tagged_sentence): if WordNetLemmatizer().lemmatize(word[0]) == 'tail': # Proceeds if sentence contains 'tail' return True -<<<<<<< HEAD:exec/friendly_drink.py -======= - ->>>>>>> My-Branch:exec/friendly_drink.py # Flips coin, prints string showing answer def flipCoin(): @@ -229,7 +179,7 @@ def weathercheck(sentence): return True def weatherout(): - weather='The temperature in %s is %5.2f with wind speed of %i' %(userData['city'], userData['temperature'], userData['wndspd']) + weather='The temperature in %s is %5.2f with wind speed of %4.1f' %(userData['city'], userData['temperature'], userData['wndspd']) reveal(weather) # tokenizes string to determine if user is asking to flip a coin @@ -242,11 +192,8 @@ def searchQ(inString): if checkToFlipCoin(tags): usedWords.extend(['flip','coin']) flipCoin() -<<<<<<< HEAD:exec/friendly_drink.py -======= elif weathercheck(tags): weatherout() ->>>>>>> My-Branch:exec/friendly_drink.py else: dbsearch.search(inString) diff --git a/exec/location.py b/exec/location.py index b3f45ea..f93d689 100644 --- a/exec/location.py +++ b/exec/location.py @@ -21,6 +21,7 @@ def lctnwthrgt (): return (temperature, wnd['speed'], sky['id'], str(location['city']) ) + #weatherid: #20X thunder storm stuff #30X drizzle diff --git a/exec/stateofmind.py b/exec/stateofmind.py new file mode 100644 index 0000000..0bcd310 --- /dev/null +++ b/exec/stateofmind.py @@ -0,0 +1,55 @@ +import nltk # Used for natural language recognition +from nltk.stem import WordNetLemmatizer # Used to lemmatize (find root word) +from nltk.corpus import wordnet # Wordnet for finding synonyms +from nltk.tokenize import sent_tokenize, word_tokenize + +from findsynonyms import findSynonyms + +def fnchk(sentence): + stom=0 + fnSynonyms= findSynonyms("fine")+ findSynonyms("good")+findSynonyms("happy") + ntSynonyms= findSynonyms("not") + sdSynonyms= findSynonyms("sad")+findSynonyms("sick")+findSynonyms("bad") + + for i in range (0, (len(sentence))): + ki=0 + if (len(sentence))==1: + li=0 + for syns in fnSynonyms: + li=1 + if sentence[i][0].lower()==syns: + return True + break + if li==0: + for syns in sdSynonyms: + if sentence[i][0].lower()==syns: + return False + break + + elif sentence[i][1]=='JJ': + for syns in fnSynonyms: + if syns == sentence[i][0].lower(): + ki=1 + if i==0: + return True + else: + for k in range (0, (i)): + if (sentence[k][0].lower()=="not" or sentence[k][0].lower()=="non"): + return False + else: + continue + return True + break + if ki==0: + for syns in sdSynonyms: + if syns == sentence[i][0]: + if i==0: + return False + else: + for k in range (0, (i)): + if (sentence[k][0].lower()=="not" or sentence[k][0].lower()=="non"): + return True + else: + continue + return False + break