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__/
*.py[cod]
dfly-loader-wsr.py

View File

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

View File

@ -10,7 +10,7 @@ class CPPEnabler(CompoundRule):
def _process_recognition(self, node, extras): # Callback when command is spoken.
CPPBootstrap.disable()
CPPGrammar.enable()
print "C++ grammar enabled"
print ("C++ grammar enabled")
class CPPDisabler(CompoundRule):
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.
CPPGrammar.disable()
CPPBootstrap.enable()
print "C++ grammar disabled"
print ("C++ grammar disabled")
# test rule to see if this script is working
@ -26,7 +26,7 @@ class CPPTestRule(CompoundRule):
spec = "test CPP" # Spoken form of command.
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++
class CPPControlStructures(MappingRule):

View File

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

View File

@ -16,7 +16,7 @@ class CSSEnabler(CompoundRule):
def _process_recognition(self, node, extras): # Callback when command is spoken.
cssBootstrap.disable()
cssGrammar.enable()
print "CSS grammar enabled"
print ("CSS grammar enabled")
class CSSDisabler(CompoundRule):
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.
cssGrammar.disable()
cssBootstrap.enable()
print "CSS grammar disabled"
print ("CSS grammar disabled")
class CSSTestRule(CompoundRule):
spec = "test CSS" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken.
print "CSS grammar tested"
print ("CSS grammar tested")
class CSSSelectors(MappingRule):
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 *

View File

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

View File

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

View File

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

View File

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

View File

@ -56,18 +56,13 @@ called when this voice command is recognized. The logic
within this callback is very simple:
1. Retrieve the sequence of actions from the element with
the name "sequence".
the name "sequence".
2. Retrieve the repeat count from the element with the name
"n".
"n".
3. Execute the actions the specified number of times.
"""
try:
import pkg_resources
pkg_resources.require("dragonfly >= 0.6.5beta1.dev-r99")
except ImportError:
pass
from dragonfly import *

View File

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

View File

@ -2,14 +2,18 @@
# 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")
class PythonEnabler(CompoundRule):
spec = "Enable Python" # Spoken command to enable the Python grammar.
def _process_recognition(self, node, extras): # Callback when command is spoken.
pythonBootstrap.disable()
pythonGrammar.enable()
print "Python grammar enabled"
print ("Python grammar enabled")
s = "Python grammar enabled"
print (s)
speaker.Speak(s)
class PythonDisabler(CompoundRule):
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.
pythonGrammar.disable()
pythonBootstrap.enable()
print "Python grammar disabled"
print ("Python grammar disabled")
# This is a test rule to see if the Python grammar is enabled
class PythonTestRule(CompoundRule):
spec = "test Python" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken.
print "Python grammar tested"
print ("Python grammar tested")
# Handles Python commenting syntax
class PythonCommentsSyntax(MappingRule):

View File

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

View File

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