dragonfly-grammars/_python_grammar.py

80 lines
2.7 KiB
Python
Raw Normal View History

2019-08-02 15:25:33 -05:00
# Author:Brandon Lovrien
# This script is to be used for programming in the Python programming language
from dragonfly import (Grammar, CompoundRule, Dictation, Text, Key, AppContext, MappingRule)
# import win32com.client
# speaker = win32com.client.Dispatch("SAPI.SpVoice")
import os
2019-08-02 15:25:33 -05:00
class PythonEnabler(CompoundRule):
spec = "Enable Python" # Spoken command to enable the Python grammar.
2021-02-12 12:26:10 -06:00
2019-08-02 15:25:33 -05:00
def _process_recognition(self, node, extras): # Callback when command is spoken.
pythonBootstrap.disable()
pythonGrammar.enable()
2021-02-12 12:26:10 -06:00
print ("Python grammar enabled")
s = "Python grammar enabled"
print (s)
# speaker.Speak(s)
os.system('echo "{0}" | festival --tts'.format(s))
2019-08-02 15:25:33 -05:00
class PythonDisabler(CompoundRule):
spec = "switch language" # spoken command to disable the Python grammar.
2021-02-12 12:26:10 -06:00
2019-08-02 15:25:33 -05:00
def _process_recognition(self, node, extras): # Callback when command is spoken.
pythonGrammar.disable()
pythonBootstrap.enable()
2021-02-12 12:26:10 -06:00
print ("Python grammar disabled")
s = "Python grammar disabled"
os.system('echo "{0}" | festival --tts'.format(s))
2019-08-02 15:25:33 -05:00
# This is a test rule to see if the Python grammar is enabled
class PythonTestRule(CompoundRule):
spec = "test Python" # Spoken form of command.
2021-02-12 12:26:10 -06:00
2019-08-02 15:25:33 -05:00
def _process_recognition(self, node, extras): # Callback when command is spoken.
2021-02-12 12:26:10 -06:00
print ("Python grammar tested")
2019-08-02 15:25:33 -05:00
# Handles Python commenting syntax
class PythonCommentsSyntax(MappingRule):
mapping = {
"comment": Text("# "),
2021-02-12 12:26:10 -06:00
2019-08-02 15:25:33 -05:00
}
# handles Python control structures
class PythonControlStructures(MappingRule):
mapping = {
"if": Text("if condition:") + Key("enter"),
"while loop": Text("while condition:") + Key("enter"),
"for loop": Text("for something in something:") + Key("enter"),
2021-02-12 12:26:10 -06:00
2019-08-02 15:25:33 -05:00
"function": Text("def functionName():") + Key("enter"),
"class": Text("class className(inheritance):") + Key("enter"),
2021-02-12 12:26:10 -06:00
}
2019-08-02 15:25:33 -05:00
# The main Python grammar rules are activated here
2021-02-12 12:26:10 -06:00
pythonBootstrap = Grammar("python bootstrap")
2019-08-02 15:25:33 -05:00
pythonBootstrap.add_rule(PythonEnabler())
pythonBootstrap.load()
pythonGrammar = Grammar("python grammar")
pythonGrammar.add_rule(PythonTestRule())
pythonGrammar.add_rule(PythonCommentsSyntax())
pythonGrammar.add_rule(PythonControlStructures())
pythonGrammar.add_rule(PythonDisabler())
pythonGrammar.load()
pythonGrammar.disable()
# Unload function which will be called by natlink at unload time.
def unload():
global pythonGrammar
if pythonGrammar: pythonGrammar.unload()
2021-02-12 12:26:10 -06:00
pythonGrammar = None