-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreespeech.py
executable file
·677 lines (626 loc) · 28.9 KB
/
freespeech.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# FreeSpeech
# Continuous realtime speech recognition and control via pocketsphinx
# Copyright (c) 2013, 2014 Henry Kroll III, http://www.TheNerdShow.com
# 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 3 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, see <http://www.gnu.org/licenses/>.
import pygtk
pygtk.require('2.0')
import gtk
import pygst
pygst.require('0.10')
import gobject
gobject.threads_init()
import gst
import subprocess
import platform, os, shutil, sys, codecs
import re
import json
from send_key import *
""" global variables """
appname = 'FreeSpeech'
refdir = 'lm'
# hmmm, where to put files? How about XDG_CONFIG_HOME?
# This will work on most Linux
if os.environ.has_key('XDG_CONFIG_HOME'):
confhome = os.environ['XDG_CONFIG_HOME']
confdir = os.path.join(confhome, appname)
else:
if os.environ.has_key('HOME'):
confhome = os.path.join(os.environ['HOME'],".config")
confdir = os.path.join(confhome, appname)
else:
confdir = refdir
# reference files written by this application
lang_ref= os.path.join(confdir, 'freespeech.ref.txt')
vocab = os.path.join(confdir, 'freespeech.vocab')
idngram = os.path.join(confdir, 'freespeech.idngram')
arpa = os.path.join(confdir, 'freespeech.arpa')
dmp = os.path.join(confdir, 'freespeech.dmp')
cmdtext = os.path.join(confdir, 'freespeech.cmd.txt')
cmdjson = os.path.join(confdir, 'freespeech.cmd.json')
class freespeech(object):
"""GStreamer/PocketSphinx Continuous Speech Recognition"""
def __init__(self):
"""Initialize a freespeech object"""
# place to store the currently open file name, if any
self.open_filename=''
# create confdir if not exists
if not os.access(confdir, os.R_OK):
os.mkdir(confdir)
# copy lang_ref to confdir if not exists
if not os.access(lang_ref, os.R_OK):
lang_ref_orig = os.path.join(refdir, 'freespeech.ref.txt')
shutil.copy(lang_ref_orig, lang_ref)
# initialize components
self.init_gui()
self.init_errmsg()
self.init_prefs()
self.init_file_chooser()
self.init_gst()
def init_gui(self):
self.undo = [] # Say "Scratch that" or "Undo that"
"""Initialize the GUI components"""
self.window = gtk.Window()
# Change to executable's dir
if os.path.dirname(sys.argv[0]):
os.chdir(os.path.dirname(sys.argv[0]))
self.icon = gtk.gdk.pixbuf_new_from_file(appname+".png")
self.window.connect("delete-event", gtk.main_quit)
self.window.set_default_size(400, 200)
self.window.set_border_width(10)
self.window.set_icon(self.icon)
self.window.set_title(appname)
vbox = gtk.VBox()
hbox = gtk.HBox(homogeneous=True)
self.textbuf = gtk.TextBuffer()
self.text = gtk.TextView(self.textbuf)
self.text.set_wrap_mode(gtk.WRAP_WORD)
self.scroller = gtk.ScrolledWindow(None, None)
self.scroller.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
self.scroller.add(self.text)
vbox.pack_start(self.scroller, True, True, 5)
vbox.pack_end(hbox, False, False)
self.button0 = gtk.Button("Learn")
self.button0.connect('clicked', self.learn_new_words)
self.button1 = gtk.ToggleButton("Send keys")
self.button1.connect('clicked', self.toggle_echo)
self.button2 = gtk.Button("Show commands")
self.button2.connect('clicked', self.show_commands)
self.button3 = gtk.ToggleButton("Mute")
self.button3.connect('clicked', self.mute)
hbox.pack_start(self.button0, True, False, 0)
hbox.pack_start(self.button1, True, False, 0)
hbox.pack_start(self.button2, True, False, 0)
hbox.pack_start(self.button3, True, False, 0)
self.window.add(vbox)
self.window.show_all()
def init_file_chooser(self):
self.file_chooser = gtk.FileChooserDialog(title="File Chooser",
parent=self.window, action=gtk.FILE_CHOOSER_ACTION_OPEN,
buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
gtk.STOCK_OK, gtk.RESPONSE_ACCEPT), backend=None)
def init_commands(self):
self.commands = {'file quit': 'gtk.main_quit',
'file open': 'self.file_open',
'file save': 'self.file_save',
'file save as': 'self.file_save_as',
'show commands': 'self.show_commands',
'editor clear': 'self.clear_edits',
'clear edits': 'self.clear_edits',
'file close': 'self.clear_edits',
'delete': 'self.delete',
'select': 'self.select',
'send keys' : 'self.toggle_keys',
'insert': 'self.insert',
'go to the end': 'self.done_editing',
'done editing': 'self.done_editing',
'scratch that': 'self.scratch_that',
'back space': 'self.backspace',
'new paragraph': 'self.new_paragraph',
}
self.write_prefs()
try:
self.prefsdialog.checkbox.set_active(False)
except:
pass
def init_prefs(self):
"""Initialize new GUI components"""
me = self.prefsdialog = gtk.Dialog("Command Preferences", None,
gtk.DIALOG_DESTROY_WITH_PARENT,
(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
me.set_default_size(400, 300)
if not os.access(cmdjson, os.R_OK):
#~ write some default commands to a file if it doesn't exist
self.init_commands()
else:
self.read_prefs()
me.label = gtk.Label( \
"Double-click to change command wording.\n\
If new commands don't work click the learn button to train them.")
me.vbox.pack_start(me.label)
me.checkbox=gtk.CheckButton("Restore Defaults")
me.checkbox.show()
me.action_area.pack_start(me.checkbox)
me.liststore=gtk.ListStore(str, str)
me.liststore.set_sort_column_id(0, gtk.SORT_ASCENDING)
me.tree=gtk.TreeView(me.liststore)
editable = gtk.CellRendererText()
fixed = gtk.CellRendererText()
editable.set_property('editable', True)
editable.connect('edited', self.edited_cb)
me.connect("expose-event", self.prefs_expose)
me.connect("response", self.prefs_response)
column = gtk.TreeViewColumn("Spoken command",editable,text=0)
me.tree.append_column(column)
column = gtk.TreeViewColumn("What it does",fixed,text=1)
me.tree.append_column(column)
me.vbox.pack_end(me.tree)
me.label.show()
me.tree.show()
self.commands_old = self.commands
me.show_all()
def prefs_expose(self, me, event):
""" callback when prefs window is shown """
# populate commands list with documentation
me.liststore.clear()
for x,y in self.commands.items():
me.liststore.append([x,eval(y).__doc__])
def write_prefs(self):
""" write command list to file """
with codecs.open(cmdjson, encoding='utf-8', mode='w') as f:
f.write(json.dumps(self.commands))
# write commands text, so we don't have to train each time
with codecs.open(cmdtext, encoding='utf-8', mode='w') as f:
for j in self.commands.keys():
f.write('<s> '+j+' </s>\n')
def read_prefs(self):
""" read command list from file """
with codecs.open(cmdjson, encoding='utf-8', mode='r') as f:
self.commands=json.loads(f.read())
def prefs_response(self, me, event):
""" make prefs dialog non-modal by using response event
instead of run() method, which waited for input """
if me.checkbox.get_active():
self.init_commands()
else:
if event!=gtk.RESPONSE_ACCEPT:
self.commands = self.commands_old
else:
self.write_prefs()
me.hide()
def edited_cb(self, cellrenderertext, path, new_text):
""" callback activated when treeview text edited """
#~ self.prefsdialog.tree.path=new_text
liststore=self.prefsdialog.liststore
treeiter = liststore.get_iter(path)
old_text = liststore.get_value(treeiter,0)
if not self.commands.has_key(new_text):
liststore.set_value(treeiter,0,new_text)
self.commands[new_text]=self.commands[old_text]
del(self.commands[old_text])
#~ print(old_text, new_text)
def init_errmsg(self):
me = self.errmsg = gtk.Dialog("Error", None,
gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
me.set_default_size(400, 200)
me.label = gtk.Label("Nice label")
me.vbox.pack_start(me.label)
me.label.show()
def init_gst(self):
"""Initialize the speech components"""
self.pipeline = gst.parse_launch('autoaudiosrc ! audioconvert ! audioresample '
+ '! vader name=vad auto-threshold=true '
+ '! pocketsphinx name=asr ! fakesink')
asr = self.pipeline.get_by_name('asr')
"""Load custom dictionary and language model"""
asr.set_property('dict', 'custom.dic')
# The language model that came with pocketsphinx works OK...
# asr.set_property('lm', '/usr/share/pocketsphinx/model/lm/en_US/wsj0vp.5000.DMP')
# but it does not contain editing commands, so we make our own
if not os.access(dmp, os.R_OK): # create if not exists
self.learn_new_words(None)
asr.set_property('lm', dmp)
# Adapt pocketsphinx to your voice for better accuracy.
# See http://cmusphinx.sourceforge.net/wiki/tutorialadapt
# asr.set_property('hmm', '../sphinx/hub4wsj_sc_8kadapt')
#fixme: write an acoustic model trainer
asr.connect('partial_result', self.asr_partial_result)
asr.connect('result', self.asr_result)
asr.set_property('configured', True)
bus = self.pipeline.get_bus()
bus.add_signal_watch()
bus.connect('message::application', self.application_message)
#self.pipeline.set_state(gst.STATE_PAUSED)
self.pipeline.set_state(gst.STATE_PLAYING)
def learn_new_words(self, button):
""" Learn new words, jargon, or other language
1. Add the word(s) to the dictionary, if necessary.
2. Type or paste sentences containing the word(s).
2. Use the word(s) differently in at least 3 sentences.
3. Click the "Learn" button. """
# prepare a text corpus from the textbox
corpus = self.prepare_corpus(self.textbuf)
# append it to the language reference
with codecs.open(lang_ref, encoding='utf-8', mode='a+') as f:
for line in corpus:
if line:
f.write(line + '\n')
# cat command
if platform.system()=='Windows':
catcmd = 'type '
else:
catcmd = 'cat '
# compile a vocabulary
# http://www.speech.cs.cmu.edu/SLM/toolkit_documentation.html#text2wfreq
if subprocess.call(catcmd + cmdtext + ' ' + cmdtext + ' ' + cmdtext + ' ' + cmdtext + ' ' + lang_ref + '|text2wfreq -verbosity 2' \
+ ' |wfreq2vocab -top 20000 -records 100000 > ' + vocab, shell=True):
self.err('Trouble writing ' + vocab)
# update the idngram\
# http://www.speech.cs.cmu.edu/SLM/toolkit_documentation.html#text2idngram
if subprocess.call('text2idngram -vocab ' + vocab + \
' -n 3 < ' + lang_ref + ' > ' + idngram, shell=True):
self.err('Trouble writing ' + idngram)
# (re)build arpa language model
# http://drupal.cs.grinnell.edu/~stone/courses/computational-linguistics/ngram-lab.html
if subprocess.call('idngram2lm -idngram -n 3 -verbosity 2 ' + idngram + \
' -vocab ' + vocab + ' -arpa ' + arpa + ' -vocab_type 1' \
' -good_turing', shell=True):
self.err('Trouble writing ' + arpa)
# convert to dmp
if subprocess.call('sphinx_lm_convert -i ' + arpa + \
' -o ' + dmp + ' -ofmt dmp', shell=True):
self.err('Trouble writing ' + dmp)
# load the dmp
asr = self.pipeline.get_by_name('asr')
self.pipeline.set_state(gst.STATE_PAUSED)
asr.set_property('configured', False)
asr.set_property('lm', dmp)
asr.set_property('configured', True)
self.pipeline.set_state(gst.STATE_PLAYING)
def mute(self, button):
"""Handle button presses."""
if not button.get_active():
button.set_label("Mute")
self.pipeline.set_state(gst.STATE_PLAYING)
else:
button.set_label("Speak")
vader = self.pipeline.get_by_name('vad')
vader.set_property('silent', True)
self.pipeline.set_state(gst.STATE_PAUSED)
def toggle_echo(self, button):
""" echo keystrokes to the desktop """
if not button.get_active():
button.set_label("Send keys")
button.set_active(False)
else:
button.set_label("Stop sending")
button.set_active(True)
def toggle_keys(self):
""" echo keystrokes to the desktop """
self.button1.set_active(True - self.button1.get_active())
return True
def collapse_punctuation(self, hyp, started):
index = 0
start = self.textbuf.get_iter_at_mark(self.textbuf.get_insert())
words = hyp.split()
# remove the extra text to the right of the punctuation mark
while True:
if (index >= len(words)):
break
word = words[index]
if (re.match("^\W\w", word)):
words[index] = word[0]
index += 1
hyp = " ".join(words)
hyp = hyp.replace(" ...ellipsis", " ...")
hyp = re.sub(r" ([^\w\s]+)\s*", r"\1 ", hyp)
hyp = re.sub(r"([({[]) ", r" \1", hyp).strip()
if not start.inside_sentence():
hyp = hyp[0].capitalize() + hyp[1:]
if re.match(r"\w", hyp[0]) and started:
space = " "
else:
space = ""
return space + hyp
def expand_punctuation(self, corpus):
# tweak punctuation to match dictionary utterances
for ind, line in enumerate(corpus):
line = re.sub(r'--', r'--dash', line)
line = re.sub(r'- ', r'-hyphen ', line)
line = re.sub(r'`', r'`agrave', line)
line = re.sub(r'=', r'=equals-sign', line)
line = re.sub(r'>', r'>greater-than-symbol', line)
line = re.sub(r'<', r'<less-than-symbol', line)
line = re.sub(r'\|', r'\|pipe-symbol', line)
line = re.sub(r'\. \. \.', r'...ellipsis', line)
line = re.sub(r' \. ', r' .dot ', line)
line = re.sub(r'\.$', r'.period', line)
line = re.sub(r',', r',comma', line)
line = re.sub(r':', r':colon', line)
line = re.sub(r'\?', r'?question-mark', line)
line = re.sub(r'"', r'"quote', line)
line = re.sub(r'([\w]) \' s', r"\1's", line)
line = re.sub(r" '", r" 'single-quote", line)
line = re.sub(r'\(', r'(left-paren', line)
line = re.sub(r'\)', r')right-paren', line)
line = re.sub(r'\[', r'[left-bracket', line)
line = re.sub(r'\]', r']right-bracket', line)
line = re.sub(r'{', r'{left-brace', line)
line = re.sub(r'}', r'}right-brace', line)
line = re.sub(r'!', r'!exclamation-point', line)
line = re.sub(r';', r';semi-colon', line)
line = re.sub(r'/', r'/slash', line)
line = re.sub(r'%', r'%percent', line)
line = re.sub(r'#', r'#sharp-sign', line)
line = re.sub(r'@', r'@at-symbol', line)
line = re.sub(r'\*', r'*asterisk', line)
line = re.sub(r'\^', r'^circumflex', line)
line = re.sub(r'&', r'&ersand', line)
line = re.sub(r'\$', r'$dollar-sign', line)
line = re.sub(r'\+', r'+plus-symbol', line)
line = re.sub(r'§', r'§section-sign', line)
line = re.sub(r'¶', r'¶paragraph-sign', line)
line = re.sub(r'¼', r'¼and-a-quarter', line)
line = re.sub(r'½', r'½and-a-half', line)
line = re.sub(r'¾', r'¾and-three-quarters', line)
line = re.sub(r'¿', r'¿inverted-question-mark', line)
line = re.sub(r'×', r'×multiplication-sign', line)
line = re.sub(r'÷', r'÷division-sign', line)
line = re.sub(r'° ', r'°degree-sign ', line)
line = re.sub(r'©', r'©copyright-sign', line)
line = re.sub(r'™', r'™trademark-sign', line)
line = re.sub(r'®', r'®registered-sign', line)
line = re.sub(r'_', r'_underscore', line)
line = re.sub(r'\\', r'\backslash', line)
line = re.sub(r'^(.)', r'<s> \1', line)
line = re.sub(r'(.)$', r'\1 </s>', line)
corpus[ind] = line
return corpus
def prepare_corpus(self, txt):
txt.begin_user_action()
self.bounds = self.textbuf.get_bounds()
text = txt.get_text(self.bounds[0], self.bounds[1])
# break on end of sentence
text = re.sub(r'(\w[.:;?!])\s+(\w)', r'\1\n\2', text)
text = re.sub(r'\n+', r'\n', text)
corpus= re.split(r'\n', text)
for ind, tex in enumerate(corpus):
# try to remove blank lines
tex = tex.strip()
if len(tex) == 0:
try:
corpus.remove(ind)
except:
pass
continue;
# lower case maybe
if len(tex) > 1 and tex[1] > 'Z':
tex = tex[0].lower() + tex[1:]
# separate punctuation marks into 'words'
# by adding spaces between them
tex = re.sub(r'\s*([^\w\s]|[_])\s*', r' \1 ', tex)
# except apostrophe followed by lower-case letter
tex = re.sub(r"(\w) ' ([a-z])", r"\1'\2", tex)
tex = re.sub(r'\s+', ' ', tex)
# fixme: needs more unicode -> dictionary replacements
# or we could convert the rest of the dictionary to utf-8
# and use the ʼunicode charactersʼ
tex = tex.replace(u"ʼ", "'apostrophe")
tex = tex.strip()
corpus[ind] = tex
return self.expand_punctuation(corpus)
def asr_partial_result(self, asr, text, uttid):
"""Forward partial result signals on the bus to the main thread."""
struct = gst.Structure('partial_result')
struct.set_value('hyp', text)
struct.set_value('uttid', uttid)
asr.post_message(gst.message_new_application(asr, struct))
def asr_result(self, asr, text, uttid):
"""Forward result signals on the bus to the main thread."""
struct = gst.Structure('result')
struct.set_value('hyp', text)
struct.set_value('uttid', uttid)
asr.post_message(gst.message_new_application(asr, struct))
def application_message(self, bus, msg):
"""Receive application messages from the bus."""
msgtype = msg.structure.get_name()
if msgtype == 'partial_result':
self.partial_result(msg.structure['hyp'],
msg.structure['uttid'])
elif msgtype == 'result':
self.final_result(msg.structure['hyp'],
msg.structure['uttid'])
#self.pipeline.set_state(gst.STATE_PAUSED)
#self.button.set_active(False)
def partial_result(self, hyp, uttid):
"""Show partial result on tooltip."""
self.text.set_tooltip_text(hyp)
def final_result(self, hyp, uttid):
"""Insert the final result into the textbox."""
# All this stuff appears as one single action
self.textbuf.begin_user_action()
self.text.set_tooltip_text(hyp)
# get bounds of text buffer
self.bounds = self.textbuf.get_bounds()
# Fix punctuation
hyp = self.collapse_punctuation(hyp, \
not self.bounds[1].is_start())
# handle commands
if not self.do_command(hyp):
self.undo.append(hyp)
self.textbuf.delete_selection(True, self.text.get_editable())
self.textbuf.insert_at_cursor(hyp)
# send keystrokes to the desktop?
if self.button1.get_active():
send_string(hyp)
display.sync()
print(hyp)
ins = self.textbuf.get_insert()
iter = self.textbuf.get_iter_at_mark(ins)
self.text.scroll_to_iter(iter, 0, False)
self.textbuf.end_user_action()
"""Process spoken commands"""
def err(self, errormsg):
self.errmsg.label.set_text(errormsg)
self.errmsg.run()
self.errmsg.hide()
def show_commands(self, argument=None):
""" show these command preferences """
me=self.prefsdialog
self.commands_old = self.commands
me.show_all()
me.present()
return True # command completed successfully!
def clear_edits(self):
""" close file and start over without saving """
self.textbuf.set_text('')
self.open_filename=''
self.window.set_title("FreeSpeech")
self.undo = []
return True # command completed successfully!
def backspace(self):
""" delete one character """
start = self.textbuf.get_iter_at_mark(self.textbuf.get_insert())
self.textbuf.backspace(start, False, True)
return True # command completed successfully!
def select(self,argument=None):
""" select [text/all/to end] """
if argument:
if re.match("^to end", argument):
start = self.textbuf.get_iter_at_mark(self.textbuf.get_insert())
end = self.bounds[1]
self.textbuf.select_range(start, end)
return True # success
search_back = self.searchback(self.bounds[1], argument)
if re.match("^all", argument):
self.textbuf.select_range(self.bounds[0], self.bounds[1])
return True # success
search_back = self.searchback(self.bounds[1], argument)
if None == search_back:
return True
# also select the space before it
search_back[0].backward_char()
self.textbuf.select_range(search_back[0], search_back[1])
return True # command completed successfully!
return False
def delete(self,argument=None):
""" delete [text] or erase selection """
if argument:
# print("del "+argument)
if re.match("^to end", argument):
start = self.textbuf.get_iter_at_mark(self.textbuf.get_insert())
end = self.bounds[1]
self.textbuf.delete(start, end)
return True # success
search_back = self.searchback(self.bounds[1], argument)
if None == search_back:
return True
# also select the space before it
search_back[0].backward_char()
self.textbuf.delete(search_back[0], search_back[1])
return True # command completed successfully!
self.textbuf.delete_selection(True, self.text.get_editable())
return True # command completed successfully!
def insert(self,argument=None):
""" insert after [text] """
if re.match("^after", argument):
argument = re.match(u'\w+(.*)', argument).group(1)
search_back = self.searchback(self.bounds[1], argument)
if None == search_back:
return True
self.textbuf.place_cursor(search_back[1])
return True # command completed successfully!
def done_editing(self):
""" place cursor at end """
self.textbuf.place_cursor(self.bounds[1])
return True # command completed successfully!
def scratch_that(self):
""" erase recent text """
if self.undo:
scratch = self.undo.pop(-1)
search_back = self.bounds[1].backward_search( \
scratch, gtk.TEXT_SEARCH_TEXT_ONLY)
if search_back:
self.textbuf.select_range(search_back[0], search_back[1])
self.textbuf.delete_selection(True, self.text.get_editable())
if self.button1.get_active():
b="".join(["\b" for x in range(0,len(scratch))])
send_string(b)
display.sync()
return True # command completed successfully!
return False
def new_paragraph(self):
""" start a new paragraph """
self.textbuf.insert_at_cursor('\n')
return True # command completed successfully!
def file_open(self):
""" open file dialog """
response=self.file_chooser.run()
if response==gtk.RESPONSE_ACCEPT:
self.open_filename=self.file_chooser.get_filename()
with codecs.open(self.open_filename, encoding='utf-8', mode='r') as f:
self.textbuf.set_text(f.read())
self.file_chooser.hide()
self.window.set_title("FreeSpeech | "+ os.path.basename(self.open_filename))
return True # command completed successfully!
def file_save(self):
""" save text buffer to disk """
if not self.open_filename:
response=self.file_chooser.run()
if response==gtk.RESPONSE_ACCEPT:
self.open_filename=self.file_chooser.get_filename()
self.file_chooser.hide()
self.window.set_title("FreeSpeech | "+ os.path.basename(self.open_filename))
if self.open_filename:
with codecs.open(self.open_filename, encoding='utf-8', mode='w') as f:
f.write(self.textbuf.get_text(self.bounds[0],self.bounds[1]))
return True # command completed successfully!
def file_save_as(self):
""" save under a different name """
self.open_filename=''
return self.file_save()
def do_command(self, hyp):
"""decode spoken commands"""
hyp = hyp.strip()
hyp = hyp[0].lower() + hyp[1:]
# editable commands
commands=self.commands
# process editable commands
if commands.has_key(hyp):
return eval(commands[hyp])()
try:# separate command and arguments
reg = re.match(u'(\w+) (.*)', hyp)
command = reg.group(1)
argument = reg.group(2)
return eval(commands[command])(argument)
except:
pass # didn't work; not a command
return False
def searchback(self, iter, argument):
"""helper function to search backwards in text buffer"""
search_back = iter.backward_search( \
argument, gtk.TEXT_SEARCH_TEXT_ONLY)
if None == search_back:
search_back = iter.backward_search( \
argument.capitalize(), gtk.TEXT_SEARCH_TEXT_ONLY)
if None == search_back:
return None
return search_back
if __name__ == "__main__":
app = freespeech()
gtk.main()