Beispiel #1
0
    def __init__(self):
        self.qiapplication = qi.Application()
        self.qiapplication.start()
        self.session = self.qiapplication.session

        # services
        self.memory = qiscript.MemoryHelper(self.session)
        self.services = qiscript.ServiceCache(self.session)
        signal.signal(signal.SIGTERM, self.exit)

        self.phrase = None
        self.language = None
        self.translated = None

        self.languages = {'English': 'en',
                          'French': 'fr',
                          'Spanish': 'es',
                          'German': 'de',
                          'Japanese': 'ja'}

        self.translator = Translator(
            'robot-translator',
            'KEy_HERE')
        translation = self.translator.translate('hello', 'fr')
        print translation
Beispiel #2
0
class App(object):
    "A sample standalone app, that demonstrates simple Python usage"
    def __init__(self):
        self.qiapplication = qi.Application()
        self.qiapplication.start()
        self.session = self.qiapplication.session

        # services
        self.memory = qiscript.MemoryHelper(self.session)
        self.services = qiscript.ServiceCache(self.session)
        signal.signal(signal.SIGTERM, self.exit)

        self.phrase = None
        self.language = None
        self.translated = None

        self.languages = {'English': 'en',
                          'French': 'fr',
                          'Spanish': 'es',
                          'German': 'de',
                          'Japanese': 'ja'}

        self.translator = Translator(
            'robot-translator',
            'KEy_HERE')
        translation = self.translator.translate('hello', 'fr')
        print translation

    def exit(self):
        self.memory.clear()
        self.qiapplication.stop()

    #
    # Run functions
    #
    def ask_translation(self):
        "Ask to be touched, waits, and exits."
        self.services.ALTextToSpeech.say("Give me something to translate")
        self.memory.connect("Babel/Phrase", self.set_phrase)
        self.memory.connect("Babel/Language", self.set_language)
        self.memory.connect("Babel/Exit", self.on_exit)
        self.qiapplication.run()

    def set_phrase(self, value):
        self.phrase = value
        print self.phrase
        if self.language is not None:
            self.say_translation()

    def set_language(self, value):
        self.language = self.languages[value.title()]
        print self.language
        if self.phrase is not None:
            self.say_translation()

    def on_exit(self, value):
        if value:
            self.exit()

    def say_translation(self):
        try:
            translation = self.translator.translate(self.phrase, self.language)
            print translation
        except TranslateApiException as err:
            translation = "I'm not sure"
            print err
        self.services.ALTextToSpeech.say(translation)
        self.phrase = None
        self.language = None