Skip to content

Commit

Permalink
Improvments for domogik 0.5.0 and new translator
Browse files Browse the repository at this point in the history
  • Loading branch information
fritz-smh committed Nov 24, 2015
1 parent 56f03a0 commit a0f580f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 19 deletions.
6 changes: 6 additions & 0 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
Changelog
=========

1.2
===

* Domogik 0.5 compatibility : handle butler logger
* New translator : textblob

1.1
===

Expand Down
4 changes: 2 additions & 2 deletions info.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
}
],
"description": "Butler package to use Wolfram Alpha, an online knowledge database",
"domogik_min_version": "0.4.1",
"domogik_min_version": "0.5.0",
"name": "wolfram",
"type": "brain",
"version": "1.1"
"version": "1.2"
},
"json_version": 2
}
65 changes: 50 additions & 15 deletions lib/wolfram.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,23 @@

# imports
import traceback
# translatros
import goslate
from textblob import TextBlob
# wolfram
import tungsten

WOLFRAM_LANG="en"

# choose the translator
TRANSLATOR = "textblob"
#TRANSLATOR = "goslate"


# translation for non "us" languages as wolfram works only in english
class Wolfram():

def __init__(self, cfg_i18n):
def __init__(self, cfg_i18n, log = None):
""" constructor
@param cfg_i18n : a dictionnary of i18n data
{ 'LANG' : ...,
Expand All @@ -27,6 +36,7 @@ def __init__(self, cfg_i18n):
'ERROR_MSG_CONFIG' : ... }
"""
self.cfg_i18n = cfg_i18n
self.log = log

def query(self, args):
""" Do the query
Expand All @@ -53,34 +63,58 @@ def query(self, args):
return response_lang

def translate_query(self, text):
#print("Translate query : {0}".format(text))
if self.cfg_i18n['LANG'] == "us":
self.log.info(u"Translate query : '{0}' from lang {1} in lang : {2}".format(text, self.cfg_i18n['LANG'], WOLFRAM_LANG))
self.log.info(u"Translator engine is : {0}".format(TRANSLATOR))

if self.cfg_i18n['LANG'] == WOLFRAM_LANG:
return text

# translation in "us"
gs = goslate.Goslate()
translated = gs.translate(text, "us", self.cfg_i18n['LANG'])
return translated
if TRANSLATOR == "goslate":
gs = goslate.Goslate()
#gs = goslate.Goslate(service_urls = ['http://translate.google.de'])
#translated = gs.translate("bonjour", "en", "fr")
#translated = gs.translate("bonjour", WOLFRAM_LANG, self.cfg_i18n['LANG'])
translated = gs.translate(text, WOLFRAM_LANG, self.cfg_i18n['LANG'])
elif TRANSLATOR == "textblob":
q_blob = TextBlob(u"{0}".format(text))
translated = q_blob.translate(from_lang = self.cfg_i18n['LANG'], to = WOLFRAM_LANG)
else:
self.log.warning(u"No translation engine used!")
translated = text
self.log.info(u"Translated : {0}".format(translated))
return u"{0}".format(translated)

def translate_response(self, text):
#print("Translate response : {0}".format(text))
if self.cfg_i18n['LANG'] == "us":
self.log.info(u"Translate response : '{0}' from lang {1} in lang : {2}".format(text, WOLFRAM_LANG, self.cfg_i18n['LANG']))
self.log.info(u"Translator engine is : {0}".format(TRANSLATOR))

if self.cfg_i18n['LANG'] == WOLFRAM_LANG:
return text

# translation in LANG
gs = goslate.Goslate()
translated = gs.translate(text, self.cfg_i18n['LANG'])
return translated
if TRANSLATOR == "goslate":
gs = goslate.Goslate()
translated = gs.translate(text, self.cfg_i18n['LANG'], WOLFRAM_LANG)
elif TRANSLATOR == "textblob":
q_blob = TextBlob(u"{0}".format(text))
translated = q_blob.translate(from_lang = WOLFRAM_LANG, to = self.cfg_i18n['LANG'])
else:
self.log.warning(u"No translation engine used!")
translated = text
self.log.info(u"Translated : {0}".format(translated))
return u"{0}".format(translated)

# request wolfram for an answer
def ask_wolfram(self, text):
try:
response = u""
#print("Call Wolfram for : {0}".format(text))
self.log.info(u"Call Wolfram for : {0}".format(text))
client = tungsten.Tungsten(self.WOLFRAMALPHA_APPID)
result = client.query(text)
if result.success == False:
#print(result.error)
self.log.error(u"Wolfram error : {0}".format(result.error))
self.log.error(u"Result is : {0}".format(result))
if result.error == None:
return u"{0}".format(self.cfg_i18n['ERROR_MSG_NONE'])
else:
Expand All @@ -90,7 +124,7 @@ def ask_wolfram(self, text):
# if so, we only take this one in account
for pod in result.pods:
if pod.id == "Result":
#print(pod.format)
self.log.info(u"Wolfram response : {0}".format(pod.format['plaintext']))
return u"{0}".format(self.process_response(pod.format['plaintext']))

# if no result, process returned data
Expand All @@ -110,9 +144,10 @@ def ask_wolfram(self, text):
response = u"{0}\n{1}. {2}".format(response, pod.title, pod_result)
else:
response = u"{0}\n{2}".format(response, pod_result)
self.log.info(u"Wolfram response : {0}".format(response))
return response
except:
print(u"Wolfram Error : {0}".format(traceback.format_exc()))
self.log.error(u"Wolfram Error : {0}".format(traceback.format_exc()))
return u"{0} : {1}".format(self.cfg_i18n['ERROR_MSG'], traceback.format_exc())

def process_response(self, tab_text):
Expand Down
2 changes: 1 addition & 1 deletion rs/en_US/wolfram.rive
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
'ERROR_MSG_CONFIG' : u"An error occured during the configuration reading."
}

w = Wolfram(cfg_i18n)
w = Wolfram(cfg_i18n, log = rs.log)
return w.query(args)
< object

Expand Down
2 changes: 1 addition & 1 deletion rs/fr_FR/wolfram.rive
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
'ERROR_MSG_CONFIG' : u"Une erreur est survenue pendant la récupération de la configuration."
}

w = Wolfram(cfg_i18n)
w = Wolfram(cfg_i18n, log = rs.log)
return w.query(args)
< object

Expand Down

0 comments on commit a0f580f

Please sign in to comment.