Finished up primary focus of emojiParser. Now it can handle all weather inputs and parses the semantically similar emoji for the given weather condition.

This commit is contained in:
2017-07-30 10:33:14 +02:00
parent 870f00d65f
commit 7f2a3d3c05

View File

@@ -3,21 +3,21 @@
# @Author: KevinMidboe # @Author: KevinMidboe
# @Date: 2017-07-29 11:56:24 # @Date: 2017-07-29 11:56:24
# @Last Modified by: KevinMidboe # @Last Modified by: KevinMidboe
# @Last Modified time: 2017-07-29 18:36:55 # @Last Modified time: 2017-07-30 10:31:52
from fuzzywuzzy import process from fuzzywuzzy import process
# Find the first word, if it is a noun or a adjective. # Find the first word, if it is a noun or a adjective. ✔️
# Remove the adjective and split if there is a AND # Remove the adjective and split if there is a AND ✔️
# Then match the first noun to list and add that emoji # Then match the first noun to list and add that emoji ✔️
# and then match the second to list and add that emoji # and then match the second to list and add that emoji ✔️
# REGEX this bitch up # REGEX this bitch up
symbol_table = { symbol_table = {
'clear sky': '☀️', 'clear sky': '☀️',
'fair': '🌤', 'fair': '🌤',
'partly cloudy': '⛅️', 'partly cloudy': '⛅️',
'cloudy': '☁️', 'cloudy': ' ☁️ ',
'thunder': '⚡️', 'thunder': '⚡️',
'rain showers': '🌦', 'rain showers': '🌦',
@@ -35,14 +35,14 @@ symbol_table = {
} }
severity = { severity = {
'rain': ['💧', ' ☂️', ' ☔️'], 'rain': ['', ' ☂️', ' ☔️'],
'sleet': [' 💦 ', ' 💧 ', ' 💧 💦 '], 'sleet': [' 💦 ', ' 💧 ', ' 💧 💦 '],
'snow': [' ❄️ ', ' ❄️ ❄️ ', ' ❄️ ❄️ ❄️ '] 'snow': [' ❄️ ', ' ❄️ ❄️ ', ' ❄️ ❄️ ❄️ ']
} }
class EmojiParser(object): class EmojiParser(object):
def __init__(self, condition_text): def __init__(self, condition_text):
self.condition_expression = condition_text self.condition_expression = condition_text.lower()
self.severity = None self.severity = None
self.nouns = [] self.nouns = []
@@ -55,20 +55,33 @@ class EmojiParser(object):
# Splits and lowers the condition text for easier parsing # Splits and lowers the condition text for easier parsing
def splitCondition(self, condition): def splitCondition(self, condition):
condition = condition.lower()
return condition.split() return condition.split()
# Takes a input or uses condition_expression to find adjective in sentence
def findAdjective(self, sentence=None): def findAdjective(self, sentence=None):
if sentence is None: if sentence is None:
sentence = self.condition_expression sentence = self.condition_expression
# Splits and iterates over each word in sentence
expression = self.splitCondition(sentence) expression = self.splitCondition(sentence)
for word in expression: for word in expression:
if word in self.weather_adjectives: if word in self.weather_adjectives:
# Return the word if matched with weather_adjectives
return word return word
return None return None
# Removes the first adjective in the a given sentence
def removeAdjective(self):
adjective = self.findAdjective()
if adjective: # Adjective is not None
expression = self.splitCondition(self.condition_expression)
expression.remove(adjective)
return ' '.join(expression)
else:
return self.condition_expression
def severityValue(self): def severityValue(self):
adjective = self.findAdjective() adjective = self.findAdjective()
@@ -77,61 +90,64 @@ class EmojiParser(object):
else: else:
self.severity = self.weather_adjectives['normal'] self.severity = self.weather_adjectives['normal']
def removeAdjective(self):
adjective = self.findAdjective()
if adjective:
expression = self.splitCondition(self.condition_expression)
expression.remove(adjective)
return ' '.join(expression)
else:
return self.condition_expression
def findWeatherTokens(self): def findWeatherTokens(self):
# If present removes the leading adjective
sentence = self.removeAdjective() sentence = self.removeAdjective()
# If multiple tokens/weather_nouns split all between the 'and'
if 'and' in sentence: if 'and' in sentence:
self.nouns = sentence.split(' and ') self.nouns = sentence.split(' and ')
else: else:
self.nouns = [sentence] self.nouns = [sentence]
# Use the symbol_table to convert the forcast name to emoji
def emojify(self, noun): def emojify(self, noun):
return symbol_table[noun] return symbol_table[noun]
# Does as emojify above, but iterates over a list if multiple elements
def emojifyList(self, noun_list): def emojifyList(self, noun_list):
returnList = [] returnList = []
# TODO use more like a map function?
for noun in noun_list: for noun in noun_list:
returnList.append(self.emojify(noun)) returnList.append(self.emojify(noun))
return ' '.join(returnList) return ' '.join(returnList)
def findPrimaryForcast(self):
# Copies the contents not the refrence to the list
noun_list = list(self.nouns)
forcast = noun_list.pop(0)
# Translates to emoji once here instead of twice below
forcast_emoji = self.emojify(forcast)
if forcast in severity:
return ('%s %s' % (forcast_emoji, severity[forcast]))
else:
return forcast_emoji
# Trying to analyze the semantics of the condition text # Trying to analyze the semantics of the condition text
def emojifyWeatherForecast(self): def emojifyWeatherForecast(self):
# Finds the tokens/nouns of weather for the given input text and severity value
self.findWeatherTokens() self.findWeatherTokens()
self.severityValue()
noun_list = self.nouns
primary_forcast = noun_list.pop(0) primary_forcast = self.findPrimaryForcast()
primary_severity = severity[primary_forcast][self.severity] secondary_forcast = self.emojifyList(self.nouns[1:])
secondary_forcast = self.emojifyList(noun_list)
return ('%s %s %s' % (self.emojify(primary_forcast), primary_severity, secondary_forcast)) return ('%s %s' % (primary_forcast, secondary_forcast))
def convertSematicsToEmoji(self): def convertSematicsToEmoji(self):
self.severityValue() return self.emojifyWeatherForecast()
emojiForcast = self.emojifyWeatherForecast()
return emojiForcast
def main(): def main():
emojiParser = EmojiParser('Light rain') emojiParser = EmojiParser('Cloudy')
emojiParser.convertSematicsToEmoji() print(emojiParser.convertSematicsToEmoji())
print(emojiParser)
if __name__ == '__main__': if __name__ == '__main__':