Error running a skill with regex

I want to build a skill that translate a word or a sentence to Chinese.
Here is my code so far: https://github.com/oren/mycroft-web/blob/master/skills/skill_translate/init.py

I have work.rk:

translate (?P<Word>.*)

and init.py:

 word = message.data["Word"]

Here is my skills log when I type “translate hi”

2017-08-28 22:17:10,981 - Display Manager (mycroft.client.enclosure) - DEBUG - Setting active skill to TranslateSkill
2017-08-28 22:17:10,982 - mycroft.skills.core - ERROR - An error occurred while processing a request in TranslateSkill
Traceback (most recent call last):
  File "/home/oren/mycroft-core/mycroft/skills/core.py", line 390, in wrapper
    handler(message)
  File "/home/oren/mycroft-core/jarbas_skills/skill_translate/__init__.py", line 51, in handle_translate_intent
    word = message.data["Word"]
KeyError: 'Word'

I am not sure about the syntax for the regex. maybe that’s the problem?

Could you try to use another identifier for regex group? Could be “word” a key word?

From your link, i see that you named your regex file "work.rx"
I think that

message.data[“Word”]

don’t look for the group name but the file name.

I think the file name was the problem. I renamed it to word.rx but not i always get ‘zaijian’ even when I type: “translate hi” or “translate hello”. Here is my skills log:

2017-08-29 08:08:04,893 - Skills - DEBUG - {"type": "recognizer_loop:utterance", "data": {"utterances": ["\"translate hello\""]}, "context": {"source": "webchat_:2", "user": "webchat 2", "mute": true}}
2017-08-29 08:08:04,899 - Skills - DEBUG - {"type": "skill.converse.request", "data": {"lang": "en-us", "skill_id": 33, "utterances": ["\"translate hello\""]}, "context": null}
2017-08-29 08:08:04,903 - Skills - DEBUG - {"type": "skill.converse.response", "data": {"skill_id": 33, "result": false}, "context": null}
2017-08-29 08:08:05,007 - Skills - DEBUG - {"type": "33:TranslateIntent", "data": {"confidence": 0.5, "Word": "hello \"", "target": null, "TranslateKeyword": "translate", "intent_type": "33:TranslateIntent", "__tags__": [{"end_token": 1, "start_token": 1, "from_context": false, "entities": [{"confidence": 1.0, "data": [["translate", "TranslateKeyword"]], "key": "translate", "match": "translate"}], "key": "translate", "match": "translate"}, {"end_token": 3, "start_token": 2, "confidence": 0.5, "from_context": false, "entities": [{"confidence": 0.5, "data": [["hello \"", "Word"]], "key": "hello \"", "match": "hello \""}], "key": "hello \"", "match": "hello \""}], "utterance": "\"translate hello\""}, "context": {"source": "server_skills", "user": "webchat 2", "mute": true, "target": null, "destinatary": "webchat_:2"}}
2017-08-29 08:08:05,008 - AgainSkill - INFO - Tracking last executed intent: 33:TranslateIntent
----------------------
hello "
----------------------
2017-08-29 08:08:05,009 - Display Manager (mycroft.client.enclosure) - DEBUG - Setting active skill to TranslateSkill
2017-08-29 08:08:05,015 - Skills - DEBUG - {"type": "intent.execution.start", "data": {"status": "start", "intent": "33:TranslateIntent"}, "context": null}
2017-08-29 08:08:05,016 - Skills - DEBUG - {"type": "speak", "data": {"expect_response": false, "utterance": "zaijian", "metadata": {"url": "http:://foo.com"}}, "context": {"source": "TranslateSkill", "destinatary": "webchat_:2", "more_speech": false, "target": null, "mute": true}}
2017-08-29 08:08:05,051 - Skills - DEBUG - {"type": "add_context", "data": {"word": "zaijian", "context": "Last_Speech"}, "context": null}
2017-08-29 08:08:05,053 - Skills - DEBUG - {"type": "add_context", "data": {"word": "http:://foo.com", "context": "url"}, "context": null}
2017-08-29 08:08:05,055 - Skills - DEBUG - {"type": "intent.execution.end", "data": {"status": "executed", "intent": "33:TranslateIntent"}, "context": null}
2017-08-29 08:08:05,057 - Skills - DEBUG - {"type": "enclosure.eyes.blink", "data": {"side": "b"}, "context": null}
2017-08-29 08:08:05,058 - Skills - DEBUG - {"type": "recognizer_loop:audio_output_start", "data": {}, "context": null}
2017-08-29 08:08:05,059 - mycroft.skills.media - DEBUG - Lower volume not implemented in Playback Control Skill
2017-08-29 08:08:05,059 - mycroft.skills.media - DEBUG - Lower volume not implemented in Playback Control Skill
2017-08-29 08:08:06,173 - Skills - DEBUG - {"type": "recognizer_loop:audio_output_end", "data": {}, "context": null}
2017-08-29 08:08:06,174 - mycroft.skills.media - DEBUG - Restore volume not implemented in Playback Control Skill
2017-08-29 08:08:06,176 - Skills - DEBUG - {"type": "enclosure.eyes.blink", "data": {"side": "b"}, "context": null}
2017-08-29 08:09:39,223 - Skills - DEBUG - {"type": "recognizer_loop:utterance", "data": {"utterances": ["\"translate hi\""]}, "context": {"source": "webchat_:2", "user": "webchat 2", "mute": true}}
2017-08-29 08:09:39,227 - Skills - DEBUG - {"type": "skill.converse.request", "data": {"lang": "en-us", "skill_id": 33, "utterances": ["\"translate hi\""]}, "context": null}
2017-08-29 08:09:39,232 - Skills - DEBUG - {"type": "skill.converse.response", "data": {"skill_id": 33, "result": false}, "context": null}
2017-08-29 08:09:39,332 - Skills - DEBUG - {"type": "33:TranslateIntent", "data": {"confidence": 0.5, "Word": "hi \"", "target": null, "TranslateKeyword": "translate", "intent_type": "33:TranslateIntent", "__tags__": [{"end_token": 1, "start_token": 1, "from_context": false, "entities": [{"confidence": 1.0, "data": [["translate", "TranslateKeyword"]], "key": "translate", "match": "translate"}], "key": "translate", "match": "translate"}, {"end_token": 3, "start_token": 2, "confidence": 0.5, "from_context": false, "entities": [{"confidence": 0.5, "data": [["hi \"", "Word"]], "key": "hi \"", "match": "hi \""}], "key": "hi \"", "match": "hi \""}], "utterance": "\"translate hi\""}, "context": {"source": "server_skills", "user": "webchat 2", "mute": true, "target": null, "destinatary": "webchat_:2"}}
2017-08-29 08:09:39,333 - AgainSkill - INFO - Tracking last executed intent: 33:TranslateIntent
----------------------
hi "
----------------------
2017-08-29 08:09:39,336 - Display Manager (mycroft.client.enclosure) - DEBUG - Setting active skill to TranslateSkill
2017-08-29 08:09:39,338 - Skills - DEBUG - {"type": "intent.execution.start", "data": {"status": "start", "intent": "33:TranslateIntent"}, "context": null}
2017-08-29 08:09:39,375 - Skills - DEBUG - {"type": "speak", "data": {"expect_response": false, "utterance": "zaijian", "metadata": {"url": "http:://foo.com"}}, "context": {"source": "TranslateSkill", "destinatary": "webchat_:2", "more_speech": false, "target": null, "mute": true}}
2017-08-29 08:09:39,376 - Skills - DEBUG - {"type": "add_context", "data": {"word": "zaijian", "context": "Last_Speech"}, "context": null}
2017-08-29 08:09:39,376 - Skills - DEBUG - {"type": "add_context", "data": {"word": "http:://foo.com", "context": "url"}, "context": null}
2017-08-29 08:09:39,377 - Skills - DEBUG - {"type": "intent.execution.end", "data": {"status": "executed", "intent": "33:TranslateIntent"}, "context": null}
2017-08-29 08:09:39,378 - Skills - DEBUG - {"type": "recognizer_loop:audio_output_start", "data": {}, "context": null}
2017-08-29 08:09:39,378 - mycroft.skills.media - DEBUG - Lower volume not implemented in Playback Control Skill
2017-08-29 08:09:39,378 - mycroft.skills.media - DEBUG - Lower volume not implemented in Playback Control Skill
2017-08-29 08:09:40,467 - Skills - DEBUG - {"type": "recognizer_loop:audio_output_end", "data": {}, "context": null}
2017-08-29 08:09:40,468 - mycroft.skills.media - DEBUG - Restore volume not implemented in Playback Control Skill

take a look into jcasofts translate skill https://github.com/jcasoft/TranslateSkill

i think there is no chinese support, but at least can help you with the regex

it uses google and speaks the translated sentences much better than if you just use standard TTS (which works well in 1 language only usually)

Hi @oren I’m going to test your skill later today in more detail but I think there may be an issue with overlapping vocabulary.

Since translate is used as a keyword and in the regex I think there will be a collision.

require(‘TranslateKeyword’) consumes the translate part of the sentence and then the regex can’t get a match (since it requires translate).

Edit: Reading further I notice your intent is working :slight_smile:

Testing it more it works for me, hello returns ‘hello’ everything else returns zaijian

I think my issue was a fork I was using that had a different requirements.txt file. Now it’s working (except for ‘translate hi’. from some reason it’s not working on ‘hi’).

Also it was super easy to open a tab in the browser:

import webbrowser

word = translate(word)
self.speak(word)
webbrowser.open('http://google.com/?q=' + word)

https://github.com/oren/translate-skill/blob/361488bb3ef148c3982b45a44fc17168475b56a5/init.py#L58

now all i have to do is find a way to actually do the translation (:

1 Like

the webbrowser module just open the page, you can’t get the result back into your code

you should check jcasofts translate skill i linked before to see how he does it, you would need something like requests/beuatifull soup or selenium

From the logs you posted the speak message is there, everything seems to be working, exactly how was it failing?

you can do pip install mtranslate and in your code

from mtranslate import translate

translated = translate(sentence, lang)

translate hi is failing how? maybe it is triggering hello world skill?

I can’t remember the reason for the failure. sorry!

Thanks for recommending the ‘translate’ library!

def translate(sentence):
    translated = translate(sentence, "zh")
    return translated

Now I am getting

 translate hello                                                                                                              
 >> Sorry, I didn't catch that. Please rephrase your request. 

Here is my code.

what happens in the logs? it seems the STT didn’t catch what you said

screen -r 10959.mycroft-skills

2017-09-11 08:25:13,060 - Display Manager (mycroft.client.enclosure) - DEBUG - Setting active skill to TimeSkill
2017-09-11 08:25:16,886 - Skills - DEBUG - {"type": "recognizer_loop:utterance", "data": {"lang": "en-us", "utterances": ["translate hello"]}, "context": null}
2017-09-11 08:25:16,891 - mycroft.skills.intent_service - ERROR - 
Traceback (most recent call last):
  File "/home/oren/mycroft-core/mycroft/skills/intent_service.py", line 231, in handle_utterance
    context_manager=self.context_manager))
StopIteration
2017-09-11 08:25:16,896 - Skills - DEBUG - {"type": "intent_failure", "data": {"lang": "en-us", "utterance": "translate hello"}, "context": null}
2017-09-11 08:25:16,897 - mycroft.skills.padatious_service - DEBUG - Padatious fallback attempt: translate hello
2017-09-11 08:25:16,897 - mycroft.skills.core - INFO - Exception in fallback: max() arg is an empty sequence
2017-09-11 08:25:16,898 - fallback-wolfram-alpha__init__ - DEBUG - WolframAlpha fallback attempt: translate hello
2017-09-11 08:25:16,898 - fallback-wolfram-alpha__init__ - DEBUG - Unknown intent: translate hello
2017-09-11 08:25:16,898 - mycroft.skills.core - WARNING - No fallback could handle intent.
2017-09-11 08:25:16,902 - Skills - DEBUG - {"type": "complete_intent_failure", "data": {}, "context": null}
2017-09-11 08:25:16,939 - Skills - DEBUG - {"type": "speak", "data": {"utterance": "Sorry, I didn't catch that. Please rephrase your request."}, "context": null}
2017-09-11 08:25:16,949 - Skills - DEBUG - {"type": "enclosure.eyes.blink", "data": {"side": "b"}, "context": null}
2017-09-11 08:25:16,952 - Skills - DEBUG - {"type": "recognizer_loop:audio_output_start", "data": {}, "context": null}
2017-09-11 08:25:18,064 - Display Manager (mycroft.client.enclosure) - DEBUG - Setting active skill to TimeSkill
2017-09-11 08:25:18,064 - Display Manager (mycroft.client.enclosure) - DEBUG - Setting active skill to TimeSkill
2017-09-11 08:25:18,065 - Display Manager (mycroft.client.enclosure) - DEBUG - Setting active skill to TimeSkill
2017-09-11 08:25:18,066 - Display Manager (mycroft.client.enclosure) - DEBUG - Setting active skill to TimeSkill
2017-09-11 08:25:18,067 - Display Manager (mycroft.client.enclosure) - DEBUG - Setting active skill to TimeSkill
2017-09-11 08:25:18,068 - Display Manager (mycroft.client.enclosure) - DEBUG - Setting active skill to TimeSkill
2017-09-11 08:25:18,069 - Display Manager (mycroft.client.enclosure) - DEBUG - Setting active skill to TimeSkill

from the CLI

Display Manager (mycroft.client.enclosure) - DEBUG - Setting active skill to TranslateSkill
- DEBUG - {"type": "speak", "data": {"expect_response": false, "utterance": "foobar"}, "context": null}
- DEBUG - {"type": "enclosure.eyes.blink", "data": {"side": "b"}, "context": null}
- DEBUG - {"type": "recognizer_loop:audio_output_start", "data": {}, "context": null}
- DEBUG - {"type": "recognizer_loop:audio_output_end", "data": {}, "context": null}
Created new window in existing browser session.
[20241:20315:0911/084938.808965:ERROR:browser_gpu_channel_host_factory.cc(103)] Failed to launch GPU process.
- DEBUG - Reloading Skill: translate-skill
mycroft.skills.core - INFO - ATTEMPTING TO LOAD SKILL: translate-skill with ID 17
mycroft.skills.core - ERROR - Failed to load skill: translate-skill
Traceback (most recent call last):
File "/home/oren/mycroft-core/mycroft/skills/core.py", line 133, in load_skill
skill_descriptor["name"] + MainModule, *skill_descriptor["info"])
File "/opt/mycroft/skills/translate-skill/__init__.py", line 23, in <module>
from translate import Translator
ImportError: No module named translate
- DEBUG - {"type": "detach_skill", "data": {"skill_id": "17:"}, "context": null}
  ^--- NEWEST ---^ 

I never used python, is this the correct docs for this library? https://pypi.python.org/pypi/translate

I tried both

from translate import Translator

and what you told me to try:

from mtranslate import translate

Also, should i look at the log of other screen sessions or is the information I provided is enough? If i remove the import line and everything that is related to this library, things are fine.

Thanks!

maybe i installed the wrong package. I’ll take a look tonight.
also, what about this one? https://pypi.python.org/pypi/googletrans? I’ll try it as well and update the thread.

I tested the code outside of mycroft and the translation works. but when i try it with mycroft I am getting an error. Here is the log:

mycroft.skills.intent_service - ERROR -
Traceback (most recent call last):
File "/home/oren/mycroft-core/mycroft/skills/intent_service.py", line 231, in handle_utterance
context_manager=self.context_manager))
StopIteration
- DEBUG - {"type": "intent_failure", "data": {"lang": "en-us", "utterance": "translate hi"}, "context": null}
mycroft.skills.padatious_service - DEBUG - Padatious fallback attempt: translate hi
mycroft.skills.core - INFO - Exception in fallback: max() arg is an empty sequence
fallback-wolfram-alpha__init__ - DEBUG - WolframAlpha fallback attempt: translate hi
fallback-wolfram-alpha__init__ - DEBUG - Unknown intent: translate hi
mycroft.skills.core - WARNING - No fallback could handle intent.
- DEBUG - {"type": "complete_intent_failure", "data": {}, "context": null}
- DEBUG - {"type": "speak", "data": {"utterance": "Sorry, I didn't catch that. Please rephrase your request."}, "context": null}
- DEBUG - {"type": "enclosure.eyes.blink", "data": {"side": "b"}, "context": null}
- DEBUG - {"type": "recognizer_loop:audio_output_start", "data": {}, "context": null}
SpeechClient - INFO - Failed to find intent.
- DEBUG - {"type": "recognizer_loop:audio_output_end", "data": {}, "context": null}
  ^--- NEWEST ---^ 

History ===================================================================================================================================    Log Output Legend ============================================================================================================ Mic Level ==
 translate hi                                                                                                                                  DEBUG output                                                                                                                               
 >> Sorry, I didn't catch that. Please rephrase your request.                                                                                  mycroft-skills.log, other                                                                                                                  
 translate hi                                                                                                                                  mycroft-voice.log                                                                                                                --- 519.92
 >> Sorry, I didn't catch that. Please rephrase your request.                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                          
                                                                 

And here is the code: https://github.com/oren/translate-skill/blob/40fcd01f6224af8a5e9b73915c9329d8416dd3fb/init.py