-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathturtle_listens.py
118 lines (91 loc) · 4.11 KB
/
turtle_listens.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -*- coding: utf-8 -*-
#Copyright (c) 2014, Rodrigo Parra
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import os
import glob
from gettext import gettext as _
from plugins.plugin import Plugin
from TurtleArt.tapalette import (make_palette, define_logo_function)
from TurtleArt.talogo import (primitive_dictionary, logoerror)
from TurtleArt.tautils import (debug_output, get_path, data_to_string,
hat_on_top, listify, data_from_file)
from TurtleArt.taprimitive import (ArgSlot, ConstantArg, Primitive)
from TurtleArt.tatype import (TYPE_BOOL, TYPE_BOX, TYPE_CHAR, TYPE_INT,
TYPE_FLOAT, TYPE_OBJECT, TYPE_STRING,
TYPE_NUMBER)
from TurtleArt.taturtle import Turtle
from sugarlistens import helper
from sugarlistens import utils
import logging
def after(self):
self.private = self.private + 1
class Turtle_listens(Plugin):
""" A class for defining an extra palette for speech recognition in Turtle Blocks
"""
def __init__(self, turtle_window):
Plugin.__init__(self)
self.tw = turtle_window
self.command = None
def setup(self):
palette = make_palette('listens',
colors=["#FFC000", "#A08000"],
help_string=_('Palette for speech recognition'))
palette.add_block('turtle-listen-to',
style='boolean-1arg-block-style',
label=_('listen to'),
prim_name='listen_to',
value_block=True,
help_string=_('Listen to'))
self.tw.lc.def_prim('listen_to', 1,
Primitive(self.listen_to,
return_type=TYPE_BOOL,
arg_descs=[ArgSlot(TYPE_STRING)]))
def start(self):
self.__path = os.path.dirname(os.path.abspath(__file__))
lines = []
with open(self.__path + '/speech/en/language.gram.base', 'r') as myfile:
lines = myfile.readlines()
commands = []
for block in self.tw.block_list.get_similar_blocks('block', 'turtle-listen-to'):
commands.append(block.connections[-1].values[0].lower())
rule = '<comando> = ' + ' | '.join(commands) + ';\n'
lines.append(rule)
with open(self.__path + '/speech/en/language.gram', 'a+') as myfile:
for line in lines:
myfile.write(line)
utils.jsgf2fsg(self.__path + '/speech/en/language.gram')
try:
self.__recognizer = helper.RecognitionHelper(self.__path)
idx = self.__recognizer.listen(self.final_result)
self.__recognizer.start_listening()
except RuntimeError:
self.set_colors_speech_blocks(['#A0A0A0', '#808080'])
def stop(self):
self.set_colors_speech_blocks(["#FFC000", "#A08000"])
os.remove(self.__path + '/speech/en/language.gram')
os.remove(self.__path + '/speech/en/language.fsg')
self.__recognizer.stop_listening()
def final_result(self, text):
self.command = text
def listen_to(self, text):
flag = False
if self.command and self.command == text:
self.command = None
flag = True
return flag
def set_colors_speech_blocks(self, colors):
for block in self.tw.block_list.get_similar_blocks('block', 'turtle-listen-to'):
block.set_colors(colors)