Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
chat-bot/keywords.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
106 lines (103 sloc)
3.57 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Code for assigning keywords | |
import string | |
from weatherAPI import getWeather | |
from covidCaseAPI import getStatsByCountry | |
from date import getDate | |
def readKeywords(): | |
keywords = [] | |
with open('keywords.txt', 'r') as reader: | |
for word in reader: | |
word = word.capitalize() | |
keywords.append(word.strip()) | |
return (keywords) | |
def readCountryCodes(country): | |
try: | |
code = [] | |
country = country.lower() | |
countryCodes = [] | |
with open('country-codes.csv', 'r') as reader: | |
for word in reader: | |
countryCodes.append(word.strip()) | |
for word in countryCodes: | |
word = word.replace(',', ' ') | |
word = word.lower() | |
if country in word: | |
word = word.split(' ') | |
code = word[len(word)-1] | |
break | |
return (code) | |
except UnboundLocalError: | |
return ('') | |
def readCountriesList(text): | |
try: | |
countries = [] | |
fileList = [] | |
text = text.title() | |
with open('worldcities.csv', 'r') as reader: | |
for word in reader: | |
fileList.append(word.strip()) | |
for word in fileList: | |
word = word.split(',') | |
countries.append(word[1]) | |
for word in countries: | |
if word in text: | |
userCountry = word | |
return userCountry | |
except UnboundLocalError: # If user doesn't input any country but inputs city, this part of code gets country by city's name | |
city = readCitiesList(text) | |
city = city.title() | |
cities = [] | |
text = text.title() | |
with open('worldcities.csv', 'r') as reader: | |
for word in reader: | |
fileList.append(word.strip()) | |
for word in fileList: | |
word = word.split(',') | |
cities.append(word[0]) | |
for word in cities: | |
if word == city: | |
idx = cities.index(word) | |
userCountry = fileList[idx].replace(city+',','') | |
return(userCountry) | |
return ('') | |
def readCitiesList(text): | |
try: | |
fileList = [] | |
cities = [] | |
text = text.title() | |
with open('worldcities.csv', 'r') as reader: | |
for word in reader: | |
fileList.append(word.strip()) | |
for word in fileList: | |
word = word.split(',') | |
cities.append(word[0]) | |
for word in cities: | |
if (' ' + word + ' ') in (' ' + text + ' '): | |
userCity = word | |
return (userCity) | |
except UnboundLocalError: | |
return ('') | |
def assignKeywords(text): | |
for char in string.punctuation: | |
text = text.replace(char, '') | |
text = text.title() | |
textList = text.split(' ') | |
keywords = readKeywords() | |
for word in textList: | |
if word == keywords[0] or word == keywords[1] or word == keywords[2]: | |
city = readCitiesList(text) | |
country = readCountriesList(city) | |
countryCode = readCountryCodes(country) | |
if city != '' and countryCode != '': | |
return(getWeather(city,countryCode)) | |
else: | |
return('I did not understand this city name') | |
if word == keywords[3] or word == keywords[4]: | |
return(getDate()) | |
if word == keywords[5] or word == keywords[6] or word == keywords[7]: | |
country = readCountriesList(text) | |
if country != '': | |
return (getStatsByCountry(country)) | |
else: | |
return('I did not understand this country name') | |
return('Sorry I did not quite understand you') |