Updated to use WSR and python 3

master
Jeremy Hayes 2021-02-12 12:26:10 -06:00
parent 924ea4902f
commit 34214ca2aa
17 changed files with 269 additions and 194 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
__pycache__/ __pycache__/
*.py[cod] *.py[cod]
dfly-loader-wsr.py

View File

@ -16,7 +16,7 @@ class AngularEnabler(CompoundRule):
AngularBootstrap.disable() AngularBootstrap.disable()
AngularGrammar.enable() AngularGrammar.enable()
s = "Angular JS grammar activated" s = "Angular JS grammar activated"
print s print (s)
speaker.Speak(s) speaker.Speak(s)
class AngularDisabler(CompoundRule): class AngularDisabler(CompoundRule):
@ -26,14 +26,14 @@ class AngularDisabler(CompoundRule):
AngularGrammar.disable() AngularGrammar.disable()
AngularBootstrap.enable() AngularBootstrap.enable()
s = "Angular JS grammar deactivated" s = "Angular JS grammar deactivated"
print s print (s)
speaker.Speak(s) speaker.Speak(s)
class AngularTestRule(CompoundRule): class AngularTestRule(CompoundRule):
spec = "test Angular" # Spoken form of command. spec = "test Angular" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
print "Angular JS grammar tested" print ("Angular JS grammar tested")
class AngularControlStructures(MappingRule): class AngularControlStructures(MappingRule):

View File

@ -10,7 +10,7 @@ class CPPEnabler(CompoundRule):
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
CPPBootstrap.disable() CPPBootstrap.disable()
CPPGrammar.enable() CPPGrammar.enable()
print "C++ grammar enabled" print ("C++ grammar enabled")
class CPPDisabler(CompoundRule): class CPPDisabler(CompoundRule):
spec = "switch language" # Spoken form of command. spec = "switch language" # Spoken form of command.
@ -18,7 +18,7 @@ class CPPDisabler(CompoundRule):
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
CPPGrammar.disable() CPPGrammar.disable()
CPPBootstrap.enable() CPPBootstrap.enable()
print "C++ grammar disabled" print ("C++ grammar disabled")
# test rule to see if this script is working # test rule to see if this script is working
@ -26,7 +26,7 @@ class CPPTestRule(CompoundRule):
spec = "test CPP" # Spoken form of command. spec = "test CPP" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
print "C++ grammar tested" print ("C++ grammar tested")
#Rules for implementing control structures in C++ #Rules for implementing control structures in C++
class CPPControlStructures(MappingRule): class CPPControlStructures(MappingRule):

View File

@ -9,7 +9,7 @@ class CSEnabler(CompoundRule):
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
csBootstrap.disable() csBootstrap.disable()
csGrammar.enable() csGrammar.enable()
print "C# grammar enabled" print ("C# grammar enabled")
class CSDisabler(CompoundRule): class CSDisabler(CompoundRule):
spec = "switch language" # Spoken form of command. spec = "switch language" # Spoken form of command.
@ -17,14 +17,14 @@ class CSDisabler(CompoundRule):
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
csGrammar.disable() csGrammar.disable()
csBootstrap.enable() csBootstrap.enable()
print "C# grammar disabled" print ("C# grammar disabled")
# This is a test rule to see if the C# grammar is enabled # This is a test rule to see if the C# grammar is enabled
class CSTestRule(CompoundRule): class CSTestRule(CompoundRule):
spec = "test C sharp" # Spoken form of command. spec = "test C sharp" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
print "C# grammar tested" print ("C# grammar tested")
# Handles C# commenting syntax # Handles C# commenting syntax
class CSCommentsSyntax(MappingRule): class CSCommentsSyntax(MappingRule):

View File

@ -16,7 +16,7 @@ class CSSEnabler(CompoundRule):
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
cssBootstrap.disable() cssBootstrap.disable()
cssGrammar.enable() cssGrammar.enable()
print "CSS grammar enabled" print ("CSS grammar enabled")
class CSSDisabler(CompoundRule): class CSSDisabler(CompoundRule):
spec = "switch language" # Spoken form of command. spec = "switch language" # Spoken form of command.
@ -24,14 +24,14 @@ class CSSDisabler(CompoundRule):
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
cssGrammar.disable() cssGrammar.disable()
cssBootstrap.enable() cssBootstrap.enable()
print "CSS grammar disabled" print ("CSS grammar disabled")
class CSSTestRule(CompoundRule): class CSSTestRule(CompoundRule):
spec = "test CSS" # Spoken form of command. spec = "test CSS" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
print "CSS grammar tested" print ("CSS grammar tested")
class CSSSelectors(MappingRule): class CSSSelectors(MappingRule):
mapping = { mapping = {

View File

@ -38,12 +38,6 @@ use.
""" """
try:
import pkg_resources
pkg_resources.require("dragonfly >= 0.6.5beta1.dev-r76")
except ImportError:
pass
from dragonfly import * from dragonfly import *

View File

@ -7,13 +7,13 @@ import win32com.client
speaker = win32com.client.Dispatch("SAPI.SpVoice") speaker = win32com.client.Dispatch("SAPI.SpVoice")
class HTMLEnabler(CompoundRule): class HTMLEnabler(CompoundRule):
spec = "Activate HTML" # Spoken form of command. spec = "Activate html" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
htmlBootstrap.disable() htmlBootstrap.disable()
htmlGrammar.enable() htmlGrammar.enable()
s = "HTML grammar activated" s = "HTML grammar activated"
print s print (s)
speaker.Speak(s) speaker.Speak(s)
class HTMLDisabler(CompoundRule): class HTMLDisabler(CompoundRule):
spec = "switch language" # Spoken form of command. spec = "switch language" # Spoken form of command.
@ -22,14 +22,14 @@ class HTMLDisabler(CompoundRule):
htmlGrammar.disable() htmlGrammar.disable()
htmlBootstrap.enable() htmlBootstrap.enable()
s = "HTML grammar deactivated" s = "HTML grammar deactivated"
print s print (s)
speaker.Speak(s) speaker.Speak(s)
class HTMLTestRule(CompoundRule): class HTMLTestRule(CompoundRule):
spec = "test HTML" # Spoken form of command. spec = "test HTML" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
print "HTML grammar tested" print ("HTML grammar tested")
class HTMLTags(MappingRule): class HTMLTags(MappingRule):

View File

@ -9,7 +9,7 @@ class JavaEnabler(CompoundRule):
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
javaBootstrap.disable() javaBootstrap.disable()
javaGrammar.enable() javaGrammar.enable()
print "Java grammar enabled" print ("Java grammar enabled")
class JavaDisabler(CompoundRule): class JavaDisabler(CompoundRule):
spec = "switch language" # Spoken form of command. spec = "switch language" # Spoken form of command.
@ -17,14 +17,14 @@ class JavaDisabler(CompoundRule):
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
javaGrammar.disable() javaGrammar.disable()
javaBootstrap.enable() javaBootstrap.enable()
print "Java grammar disabled" print ("Java grammar disabled")
# This is a test rule to see if the Java grammar is enabled # This is a test rule to see if the Java grammar is enabled
class JavaTestRule(CompoundRule): class JavaTestRule(CompoundRule):
spec = "test Java" # Spoken form of command. spec = "test Java" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
print "Java grammar tested" print ("Java grammar tested")
# Handles Java commenting syntax # Handles Java commenting syntax
class JavaCommentsSyntax(MappingRule): class JavaCommentsSyntax(MappingRule):

View File

@ -16,7 +16,7 @@ class JavaScriptEnabler(CompoundRule):
JavaScriptBootstrap.disable() JavaScriptBootstrap.disable()
JavaScriptGrammar.enable() JavaScriptGrammar.enable()
s = "JavaScript grammar activated" s = "JavaScript grammar activated"
print s print (s)
speaker.Speak(s) speaker.Speak(s)
@ -27,14 +27,14 @@ class JavaScriptDisabler(CompoundRule):
JavaScriptGrammar.disable() JavaScriptGrammar.disable()
JavaScriptBootstrap.enable() JavaScriptBootstrap.enable()
s = "JavaScript grammar deactivated" s = "JavaScript grammar deactivated"
print s print (s)
speaker.Speak(s) speaker.Speak(s)
class JavaScriptTestRule(CompoundRule): class JavaScriptTestRule(CompoundRule):
spec = "test JavaScript" # Spoken form of command. spec = "test JavaScript" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
print "JavaScript grammar tested" print ("JavaScript grammar tested")
class JavaScriptControlStructures(MappingRule): class JavaScriptControlStructures(MappingRule):

View File

@ -2,6 +2,8 @@
# This script is to be used for programming in the Julia programming language # This script is to be used for programming in the Julia programming language
from dragonfly import (Grammar, CompoundRule, Dictation, Text, Key, AppContext, MappingRule) from dragonfly import (Grammar, CompoundRule, Dictation, Text, Key, AppContext, MappingRule)
import win32com.client
speaker = win32com.client.Dispatch("SAPI.SpVoice")
class JuliaEnabler(CompoundRule): class JuliaEnabler(CompoundRule):
spec = "Enable Julia" # Spoken command to enable the Julia grammar. spec = "Enable Julia" # Spoken command to enable the Julia grammar.
@ -9,7 +11,9 @@ class JuliaEnabler(CompoundRule):
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
JuliaBootstrap.disable() JuliaBootstrap.disable()
JuliaGrammar.enable() JuliaGrammar.enable()
print "Julia grammar enabled" s = "Julia grammar enabled"
print (s)
speaker.Speak(s)
class JuliaDisabler(CompoundRule): class JuliaDisabler(CompoundRule):
spec = "switch language" # spoken command to disable the Julia grammar. spec = "switch language" # spoken command to disable the Julia grammar.
@ -17,22 +21,23 @@ class JuliaDisabler(CompoundRule):
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
JuliaGrammar.disable() JuliaGrammar.disable()
JuliaBootstrap.enable() JuliaBootstrap.enable()
print "Julia grammar disabled" s = "Julia grammar disabled"
print (s)
speaker.Speak(s)
# This is a test rule to see if the Julia grammar is enabled # This is a test rule to see if the Julia grammar is enabled
class JuliaTestRule(CompoundRule): class JuliaTestRule(CompoundRule):
spec = "test Julia" # Spoken form of command. spec = "test Julia" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
print "Julia grammar tested" print ("Julia grammar tested")
# Handles Julia commenting syntax # Handles Julia commenting syntax
class JuliaCommentsSyntax(MappingRule): class JuliaCommentsSyntax(MappingRule):
mapping = { mapping = {
"comment": Text("# "), "comment": Text("# "),
} }
@ -42,11 +47,8 @@ class JuliaControlStructures(MappingRule):
"if": Text("if condition:") + Key("enter"), "if": Text("if condition:") + Key("enter"),
"while loop": Text("while condition:") + Key("enter"), "while loop": Text("while condition:") + Key("enter"),
"for loop": Text("for something in something:") + Key("enter"), "for loop": Text("for something in something:") + Key("enter"),
"function": Text("function functionName()") + Key("enter") + Key("enter") + Text("end"), "function": Text("function functionName()") + Key("enter") + Key("enter") + Text("end"),
"class": Text("class className(inheritance):") + Key("enter"), "class": Text("class className(inheritance):") + Key("enter"),
} }

View File

@ -63,11 +63,6 @@ within this callback is very simple:
""" """
try:
import pkg_resources
pkg_resources.require("dragonfly >= 0.6.5beta1.dev-r99")
except ImportError:
pass
from dragonfly import * from dragonfly import *

View File

@ -10,7 +10,7 @@ class PHPEnabler(CompoundRule):
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
phpBootstrap.disable() phpBootstrap.disable()
phpGrammar.enable() phpGrammar.enable()
print "PHP grammar enabled" print ("PHP grammar enabled")
class PHPDisabler(CompoundRule): class PHPDisabler(CompoundRule):
spec = "switch language" # Spoken form of command. spec = "switch language" # Spoken form of command.
@ -18,14 +18,14 @@ class PHPDisabler(CompoundRule):
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
phpGrammar.disable() phpGrammar.disable()
phpBootstrap.enable() phpBootstrap.enable()
print "PHP grammar disabled" print ("PHP grammar disabled")
class PHPTestRule(CompoundRule): class PHPTestRule(CompoundRule):
spec = "test PHP" # Spoken form of command. spec = "test PHP" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
print "PHP grammar tested" print ("PHP grammar tested")
# Deals with printing PHP datatypes # Deals with printing PHP datatypes
class PHPDataTypes(MappingRule): class PHPDataTypes(MappingRule):

View File

@ -2,14 +2,18 @@
# This script is to be used for programming in the Python programming language # This script is to be used for programming in the Python programming language
from dragonfly import (Grammar, CompoundRule, Dictation, Text, Key, AppContext, MappingRule) from dragonfly import (Grammar, CompoundRule, Dictation, Text, Key, AppContext, MappingRule)
import win32com.client
speaker = win32com.client.Dispatch("SAPI.SpVoice")
class PythonEnabler(CompoundRule): class PythonEnabler(CompoundRule):
spec = "Enable Python" # Spoken command to enable the Python grammar. spec = "Enable Python" # Spoken command to enable the Python grammar.
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
pythonBootstrap.disable() pythonBootstrap.disable()
pythonGrammar.enable() pythonGrammar.enable()
print "Python grammar enabled" print ("Python grammar enabled")
s = "Python grammar enabled"
print (s)
speaker.Speak(s)
class PythonDisabler(CompoundRule): class PythonDisabler(CompoundRule):
spec = "switch language" # spoken command to disable the Python grammar. spec = "switch language" # spoken command to disable the Python grammar.
@ -17,14 +21,14 @@ class PythonDisabler(CompoundRule):
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
pythonGrammar.disable() pythonGrammar.disable()
pythonBootstrap.enable() pythonBootstrap.enable()
print "Python grammar disabled" print ("Python grammar disabled")
# This is a test rule to see if the Python grammar is enabled # This is a test rule to see if the Python grammar is enabled
class PythonTestRule(CompoundRule): class PythonTestRule(CompoundRule):
spec = "test Python" # Spoken form of command. spec = "test Python" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
print "Python grammar tested" print ("Python grammar tested")
# Handles Python commenting syntax # Handles Python commenting syntax
class PythonCommentsSyntax(MappingRule): class PythonCommentsSyntax(MappingRule):

View File

@ -13,7 +13,7 @@ class TerminalEnabler(CompoundRule):
TerminalBootstrap.disable() TerminalBootstrap.disable()
TerminalGrammar.enable() TerminalGrammar.enable()
s = "Terminal grammar activated" s = "Terminal grammar activated"
print s print (s)
speaker.Speak(s) speaker.Speak(s)
class TerminalDisabler(CompoundRule): class TerminalDisabler(CompoundRule):
spec = "switch language" # Spoken form of command. spec = "switch language" # Spoken form of command.
@ -22,14 +22,14 @@ class TerminalDisabler(CompoundRule):
TerminalGrammar.disable() TerminalGrammar.disable()
TerminalBootstrap.enable() TerminalBootstrap.enable()
s = "Terminal grammar deactivated" s = "Terminal grammar deactivated"
print s print (s)
speaker.Speak(s) speaker.Speak(s)
class TerminalTestRule(CompoundRule): class TerminalTestRule(CompoundRule):
spec = "test Terminal" # Spoken form of command. spec = "test Terminal" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
print "Terminal grammar tested" print ("Terminal grammar tested")
class TerminalTags(MappingRule): class TerminalTags(MappingRule):

View File

@ -16,7 +16,7 @@ class VueEnabler(CompoundRule):
VueBootstrap.disable() VueBootstrap.disable()
VueGrammar.enable() VueGrammar.enable()
s = "Vue JS grammar activated" s = "Vue JS grammar activated"
print s print (s)
speaker.Speak(s) speaker.Speak(s)
class VueDisabler(CompoundRule): class VueDisabler(CompoundRule):
@ -26,7 +26,7 @@ class VueDisabler(CompoundRule):
VueGrammar.disable() VueGrammar.disable()
VueBootstrap.enable() VueBootstrap.enable()
s = "Vue JS grammar deactivated" s = "Vue JS grammar deactivated"
print s print (s)
speaker.Speak(s) speaker.Speak(s)
class VueTestRule(CompoundRule): class VueTestRule(CompoundRule):
@ -34,7 +34,7 @@ class VueTestRule(CompoundRule):
def _process_recognition(self, node, extras): # Callback when command is spoken. def _process_recognition(self, node, extras): # Callback when command is spoken.
s = "Vue JS grammar tested" s = "Vue JS grammar tested"
print s print (s)
speaker.Speak(s) speaker.Speak(s)
class VueControlStructures(MappingRule): class VueControlStructures(MappingRule):

79
dfly-loader-wsr.py Normal file
View File

@ -0,0 +1,79 @@
#
# This file is a command-module for Dragonfly.
# (c) Copyright 2008 by Christo Butcher
# Licensed under the LGPL, see <http://www.gnu.org/licenses/>
#
"""
Command-module loader for WSR
=============================
This script can be used to look Dragonfly command-modules
for use with Window Speech Recognition. It scans the
directory it's in and loads any ``_*.py`` it finds.
"""
import os.path
import sys
import six
from dragonfly import get_engine
from dragonfly.loader import CommandModuleDirectory
from dragonfly.log import setup_log
#---------------------------------------------------------------------------
# Set up basic logging.
setup_log()
# logging.getLogger("compound.parse").setLevel(logging.INFO)
#---------------------------------------------------------------------------
# Main event driving loop.
def main():
try:
path = os.path.dirname(__file__)
except NameError:
# The "__file__" name is not always available, for example
# when this module is run from PythonWin. In this case we
# simply use the current working directory.
path = os.getcwd()
__file__ = os.path.join(path, "dfly-loader-wsr.py")
# Initialize and connect the engine.
# Set any configuration options here as keyword arguments.
engine = get_engine("sapi5inproc")
engine.connect()
# Load grammars.
directory = CommandModuleDirectory(path, excludes=[__file__])
directory.load()
# Define recognition callback functions.
def on_begin():
print("Speech start detected.")
def on_recognition(words):
message = u"Recognized: %s" % u" ".join(words)
# This only seems to be an issue with Python 2.7 on Windows.
if six.PY2:
encoding = sys.stdout.encoding or "ascii"
message = message.encode(encoding, errors='replace')
print(message)
def on_failure():
print("Sorry, what was that?")
# Recognize from WSR in a loop.
try:
engine.do_recognition(on_begin, on_recognition, on_failure)
except KeyboardInterrupt:
pass
if __name__ == "__main__":
main()