Created
June 30, 2014 17:54
-
-
Save calmofthestorm/b6c43579af93b8061697 to your computer and use it in GitHub Desktop.
Example of ListRef for pluggable vocab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aenea.proxy_nicknames import Grammar, MappingRule, Text, CompoundRule | |
grammar = Grammar('hello world') | |
import dragonfly | |
import json | |
import os | |
dlist = dragonfly.DictList('vocab list') | |
dynamic_vocabulary = {'multiedit.code': dragonfly.DictList('dynamic multiedit.code')} | |
class TestRule(MappingRule): | |
mapping = {'dynamic word <listie>': Text('%(listie)s')} | |
extras = [dragonfly.DictListRef('listie', dynamic_vocabulary['multiedit.code'])] | |
def update_dynamic_vocabulary(name, tags, vocab): | |
for t in tags: | |
if t not in dynamic_vocabulary: | |
dynamic_vocabulary[t] = dragonfly.DictList('dynamic %s' % t) | |
for spoken, action in vocab.iteritems(): | |
if isinstance(action, basestring): | |
dynamic_vocabulary[t][spoken] = action | |
print 'add ', spoken, ';', action, 'to', t | |
def load_dynamic_vocabulary(): | |
for tag in dynamic_vocabulary.itervalues(): | |
tag.clear() | |
for fn in os.listdir('E:\\aenea\\vocab'): | |
print fn | |
if fn.endswith('.json'): | |
try: | |
with open('E:\\aenea\\vocab\\%s' % fn) as fd: | |
v = json.load(fd) | |
update_dynamic_vocabulary(v['name'], v['tags'], v['vocabulary']) | |
except Exception: | |
print 'Error loading dynamic vocabulary file %s.' % fn | |
raise | |
class ReloadVocabularyRule(CompoundRule): | |
spec = 'reload vocabulary definition' | |
def _process_recognition(self, node, extras): | |
load_dynamic_vocabulary() | |
grammar.add_rule(TestRule()) | |
grammar.add_rule(ReloadVocabularyRule()) | |
grammar.load() | |
def unload(): | |
global grammar | |
if grammar: | |
grammar.unload() | |
grammar = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "python", | |
"tags": ["verbal_emacs.insertions.code", "multiedit.code"], | |
"vocabulary": { | |
"set atter": "setattr" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment