mirror of
https://github.com/KevinMidboe/termForecast.git
synced 2025-10-29 09:50:18 +00:00
Can now handle all the outputs of yr weather forcast with additions that there can be multiple conditions and finds the correct emoji for the tokens.
This commit is contained in:
127
emojiParser.py
127
emojiParser.py
@@ -3,40 +3,135 @@
|
||||
# @Author: KevinMidboe
|
||||
# @Date: 2017-07-29 11:56:24
|
||||
# @Last Modified by: KevinMidboe
|
||||
# @Last Modified time: 2017-07-29 12:34:58
|
||||
# @Last Modified time: 2017-07-29 18:26:06
|
||||
|
||||
from fuzzywuzzy import process
|
||||
|
||||
weather_nouns = ['cleary sky', 'fair', 'cloudy', 'rain showers', 'rain', 'sleet',
|
||||
'sleet showers', 'snow showers', 'thunder', 'sleet', 'snow']
|
||||
|
||||
# Find the first word, if it is a noun or a adjective.
|
||||
# Remove the adjective and split if there is a AND
|
||||
# Then match the first noun to list and add that emoji
|
||||
# and then match the second to list and add that emoji
|
||||
# REGEX this bitch up
|
||||
|
||||
# Splits and lowers the condition text for easier parsing
|
||||
def splitCondition(condition):
|
||||
symbol_table = {
|
||||
'clear sky': '☀️',
|
||||
'fair': '🌤',
|
||||
'partly cloudy': '⛅️',
|
||||
'cloudy': '☁️',
|
||||
'thunder': '⚡️',
|
||||
|
||||
'rain showers': '🌦',
|
||||
'rain': '🌧',
|
||||
'sleet showers': '🌦 💦',
|
||||
'sleet': '🌨 💦',
|
||||
'snow showers': '⛅ ❄️',
|
||||
'snow': '🌨',
|
||||
|
||||
'rain': '🌧',
|
||||
'sleet': '🌧',
|
||||
'snow': '🌨',
|
||||
|
||||
'showers': '🌤'
|
||||
}
|
||||
|
||||
severity = {
|
||||
'rain': ['💧', ' ☂️', ' ☔️'],
|
||||
'sleet': [' 💦 ', ' 💧 ', ' 💧 💦 '],
|
||||
'snow': [' ❄️ ', ' ❄️ ❄️ ', ' ❄️ ❄️ ❄️ ']
|
||||
}
|
||||
|
||||
class EmojiParser(object):
|
||||
def __init__(self, condition_text):
|
||||
self.condition_expression = condition_text
|
||||
self.severity = None
|
||||
self.nouns = []
|
||||
|
||||
self.weather_nouns = ['cleary sky', 'fair', 'cloudy', 'rain', 'rain showers', 'sleet',
|
||||
'sleet showers', 'snow showers', 'thunder', 'snow']
|
||||
self.weather_adjectives = {'light': 0, 'normal': 1, 'heavy': 2}
|
||||
|
||||
def __str__(self):
|
||||
return str([self.condition_expression, self.severity, self.nouns])
|
||||
|
||||
# Splits and lowers the condition text for easier parsing
|
||||
def splitCondition(self, condition):
|
||||
condition = condition.lower()
|
||||
return condition.split()
|
||||
|
||||
# Trying to analyze the semantics of the condition text
|
||||
def findConditionContext(condition_text):
|
||||
condition_expression = splitCondition(condition_text)
|
||||
def findAdjective(self, sentence=None):
|
||||
if sentence is None:
|
||||
sentence = self.condition_expression
|
||||
|
||||
# Iterate over each word and find what matches 100%
|
||||
for expression_value in condition_expression:
|
||||
noun_matches = process.extract(expression_value, weather_nouns)
|
||||
print(expression_value + ': ' + str(noun_matches))
|
||||
expression = self.splitCondition(sentence)
|
||||
for word in expression:
|
||||
if word in self.weather_adjectives:
|
||||
return word
|
||||
|
||||
return None
|
||||
|
||||
def severityValue(self):
|
||||
adjective = self.findAdjective()
|
||||
|
||||
if adjective:
|
||||
self.severity = self.weather_adjectives[adjective]
|
||||
else:
|
||||
self.severity = self.weather_adjectives['normal']
|
||||
|
||||
|
||||
def emojiParser(condition_text):
|
||||
findConditionContext(condition_text)
|
||||
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):
|
||||
sentence = self.removeAdjective()
|
||||
|
||||
if 'and' in sentence:
|
||||
self.nouns = sentence.split(' and ')
|
||||
else:
|
||||
self.nouns = [sentence]
|
||||
|
||||
|
||||
def emojify(self, noun):
|
||||
return symbol_table[noun]
|
||||
|
||||
def emojifyList(self, noun_list):
|
||||
returnList = []
|
||||
|
||||
for noun in noun_list:
|
||||
returnList.append(self.emojify(noun))
|
||||
|
||||
return ' '.join(returnList)
|
||||
|
||||
|
||||
|
||||
# Trying to analyze the semantics of the condition text
|
||||
def emojifyWeatherForecast(self):
|
||||
self.findWeatherTokens()
|
||||
|
||||
noun_list = self.nouns
|
||||
print(noun_list)
|
||||
|
||||
primary_forcast = noun_list.pop(0)
|
||||
primary_severity = severity[primary_forcast][self.severity]
|
||||
secondary_forcast = self.emojifyList(noun_list)
|
||||
|
||||
print('%s %s %s' % (self.emojify(primary_forcast), primary_severity, secondary_forcast))
|
||||
|
||||
|
||||
def convertSematicsToEmoji(self):
|
||||
self.severityValue()
|
||||
self.emojifyWeatherForecast()
|
||||
|
||||
|
||||
def main():
|
||||
emojiParser('Rain showers')
|
||||
emojiParser = EmojiParser('Light rain')
|
||||
emojiParser.convertSematicsToEmoji()
|
||||
print(emojiParser)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user