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

@ -6,31 +6,31 @@ from dragonfly import (Grammar, CompoundRule, Dictation, Text, Key, AppContext,
class CPPEnabler(CompoundRule):
spec = "Enable CPP" # note: this command couldn't be "enable C++" because it causes problems with the grammar engine due to complexity.
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.
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
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):
mapping = {
"code block": Text("{") + Key("enter")+ Key("enter") + Text("}"),
"if": Text("if()") + Key("enter")+ Key("enter") + Text("}"),
@ -41,15 +41,15 @@ class CPPControlStructures(MappingRule):
"for loop": Text("for(;;) {") + Key("enter")+ Key("enter") + Text("}"),
"switch statement": Text("switch() {") + Key("enter")+ Key("enter") + Text("}"),
"try catch": Text("try {") + Key("enter")+ Key("enter") + Text("}") + Key("enter") + Text("catch() {") + Key("enter")+ Key("enter") + Text("}"),
}
#Some syntax rules for functions, classes, etc. in C++
class CPPFunctionsAndClassesSyntax(MappingRule):
mapping = {
#function syntax stuff
#function syntax stuff
"function prototype": Text("returnType functionName();"),
"function": Text("returnType functionName() {")+Key("enter")+ Key("enter")+ Text("}"),
#miscellaneous syntax stuff
@ -62,16 +62,16 @@ class CPPFunctionsAndClassesSyntax(MappingRule):
"class": Text("class ClassName {") + Key("enter")+ Text("public:")+ Key("enter") + Key("enter") + Text("};"),
"constructor": Text("();") + Key("left")+ Key("left")+ Key("left"),
"destructor": Text("~();") + Key("left")+ Key("left")+ Key("left"),
}
class CPPOperators(MappingRule):
mapping = {
"scope operator": Text("::"),
"plus plus": Text("++"),
"minus minus": Text("--"),
"plus": Text("+"),
@ -91,9 +91,9 @@ class CPPOperators(MappingRule):
"logical or": Text("||"),
"plus equals": Text("+="),
"minus equals": Text("-="),
}
}
#Handles the C++ commenting syntax
class CPPCommentsSyntax(MappingRule):
@ -101,7 +101,7 @@ class CPPCommentsSyntax(MappingRule):
mapping = {
"comment": Text("// "),
"multiline comment": Text("/*") + Key("enter") + Key("enter") + Text("*/") + Key("up")
}
#Some useful miscellaneous C++ functions
@ -111,8 +111,8 @@ class CPPUsefulFunctions(MappingRule):
"print statement": Text("cout << ;") + Key("left"),
"input statement": Text("cin >> ;") + Key("left"),
"end line": Text("endl"),
}
#Syntax for preprocessor directives in C++
@ -131,7 +131,7 @@ class CPPPreprocessorDirectives(MappingRule):
"directive undefine": Text("#undef "),
"directive error": Text("#error"),
"directive line": Text("#line ")
}
@ -152,7 +152,7 @@ class CPPDataTypes(MappingRule):
"constant": Text("const "),
"null": Text("NULL"),
#my attempt at a C++ script for shortcut float notation
#my attempt at a C++ script for shortcut float notation
"<n> point <n2> float": Text("%(n)d.%(n2)d") + Text("f"),
"<n> point <n2> <n3> float": Text("%(n)d.%(n2)d%(n3)d") + Text("f"),
"<n> point <n2> <n3> <n4> float": Text("%(n)d.%(n2)d%(n3)d%(n4)d") + Text("f"),
@ -170,11 +170,11 @@ class CPPEscapeSequences(MappingRule):
"escape quotes": Text("\ ")+ Key("left") + Text("\"") + Text("\ ")+ Key("left") + Text("\""),
"escape single quotes": Text("\ ")+ Key("left") + Text("\'") + Text("\ ")+ Key("left") + Text("\'"),
"escape line": Text("\ ")+ Key("left") + Text("n"),
"escape tab": Text("\ ")+ Key("left") + Text("t"),
"escape tab": Text("\ ")+ Key("left") + Text("t"),
"escape carriage return": Text("\ ")+ Key("left") + Text("r"),
}
}
CPPBootstrap = Grammar("C++ bootstrap") # Create a grammar to contain the command rule.
CPPBootstrap.add_rule(CPPEnabler())
@ -198,4 +198,4 @@ CPPGrammar.disable()
def unload():
global CPPGrammar
if CPPGrammar: CPPGrammar.unload()
CPPGrammar = None
CPPGrammar = None

View File

@ -5,26 +5,26 @@ from dragonfly import (Grammar, CompoundRule, Dictation, Text, Key, AppContext,
class CSEnabler(CompoundRule):
spec = "Enable C sharp" # Spoken form of command.
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.
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):
@ -32,7 +32,7 @@ class CSCommentsSyntax(MappingRule):
mapping = {
"comment": Text("// "),
"multiline comment": Text("/*") + Key("enter") + Key("enter") + Text("*/") + Key("up")
}
# Deals with printing C# datatypes
@ -50,7 +50,7 @@ class CSDataTypes(MappingRule):
}
extras = [
Integer("n", 0, 64)
]
# This rule deals with the C# comparison operators
@ -63,7 +63,7 @@ class CSComparisonOperators(MappingRule):
"less than": Text("<"),
"less than or equal to": Text("<="),
"greater than or equal to": Text(">="),
}
# This rule deals with the C# Boolean operators
class CSBooleanOperators(MappingRule):
@ -74,12 +74,12 @@ class CSBooleanOperators(MappingRule):
"true": Text("true"),
"false": Text("false"),
"not": Text("!")
}
}
class CSControlStructures(MappingRule):
mapping = {
"code block": Text("{") + Key("enter")+ Key("enter") + Text("}"),
"if": Text("if() {") + Key("enter")+ Key("enter") + Text("}"),
@ -95,14 +95,14 @@ class CSControlStructures(MappingRule):
"interface": Text("<access modifier> interface InterfaceName {") + Key("enter")+ Key("enter") + Text("}"),
"enumeration": Text("<access modifier> enum EnumName {") + Key("enter")+ Key("enter") + Text("}"),
"method": Text("<datatype> methodName() {") + Key("enter") + Key("enter") + Text("}"),
}
# This rule provides some useful method calls in C#
class CSUsefulMethods(MappingRule):
mapping = {
"print statement": Text("Console.WriteLine()") + Key("left"),
"print statement": Text("Console.WriteLine()") + Key("left"),
}
# This rule deals with some of the C# arithmetic operators
class CSArithmeticOperators(MappingRule):
@ -115,7 +115,7 @@ class CSArithmeticOperators(MappingRule):
"minus": Text("-"),
"divided by": Text("/"),
"modulus": Text("%") #causes some weird problem in dragonfly so this doesn't work, use "percent sign" instead
}
class CSAssignmentOperators(MappingRule):
@ -125,16 +125,16 @@ class CSAssignmentOperators(MappingRule):
"minus equals": Text("-="),
"multiply equals": Text("*="),
"divide equals": Text("/="),
}
}
class CSMiscellaneousStuff(MappingRule):
mapping = {
"equals": Text(" = "),
"new": Text("new "),
}
class CSAccessModifiers(MappingRule):
@ -143,7 +143,7 @@ class CSAccessModifiers(MappingRule):
"public": Text("public "),
"private": Text("private "),
"protected": Text("protected ")
}
class CSEscapeSequences(MappingRule):
@ -152,12 +152,12 @@ class CSEscapeSequences(MappingRule):
"escape quotes": Text("\ ")+ Key("left") + Text("\"") + Text("\ ")+ Key("left") + Text("\""),
"escape single quotes": Text("\ ")+ Key("left") + Text("\'") + Text("\ ")+ Key("left") + Text("\'"),
"escape line": Text("\ ")+ Key("left") + Text("n"),
"escape tab": Text("\ ")+ Key("left") + Text("t"),
"escape tab": Text("\ ")+ Key("left") + Text("t"),
"escape carriage return": Text("\ ")+ Key("left") + Text("r"),
}
}
# The main C# grammar rules are activated here
csBootstrap = Grammar("C sharp bootstrap")
csBootstrap = Grammar("C sharp bootstrap")
csBootstrap.add_rule(CSEnabler())
csBootstrap.load()
@ -183,4 +183,4 @@ csGrammar.disable()
def unload():
global csGrammar
if csGrammar: csGrammar.unload()
csGrammar = None
csGrammar = None

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

@ -8,42 +8,36 @@
Command-module for **Firefox**
============================================================================
This module offers direct control of the `Firefox
<http://www.mozilla.com/en-US/firefox/>`_ web browser. It
requires the `mouseless-browsing
<https://addons.mozilla.org/en-US/firefox/addon/879>`_
This module offers direct control of the `Firefox
<http://www.mozilla.com/en-US/firefox/>`_ web browser. It
requires the `mouseless-browsing
<https://addons.mozilla.org/en-US/firefox/addon/879>`_
(mlb) add-on for reliable access to hyperlinks.
This module includes direct searching using Firefox's
search bar and Firefox's keyword searching. It also
allows single-utterance submitting of text into form text
This module includes direct searching using Firefox's
search bar and Firefox's keyword searching. It also
allows single-utterance submitting of text into form text
fields.
Installation
----------------------------------------------------------------------------
If you are using DNS and Natlink, simply place this file in you Natlink
macros directory. It will then be automatically loaded by Natlink when
If you are using DNS and Natlink, simply place this file in you Natlink
macros directory. It will then be automatically loaded by Natlink when
you next toggle your microphone or restart Natlink.
Customization
----------------------------------------------------------------------------
Users should customize this module by editing its
configuration file. In this file they should edit the
``search.searchbar`` and ``search.keywords`` settings to
match their own personal search preferences. These
variables map *what you say* to which *search engines* to
Users should customize this module by editing its
configuration file. In this file they should edit the
``search.searchbar`` and ``search.keywords`` settings to
match their own personal search preferences. These
variables map *what you say* to which *search engines* to
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

@ -5,26 +5,26 @@ from dragonfly import (Grammar, CompoundRule, Dictation, Text, Key, AppContext,
class JavaEnabler(CompoundRule):
spec = "Enable Java" # Spoken form of command.
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.
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):
@ -32,7 +32,7 @@ class JavaCommentsSyntax(MappingRule):
mapping = {
"comment": Text("// "),
"multiline comment": Text("/*") + Key("enter") #+ Key("enter") + Text("*/") + Key("up")
}
# Deals with printing Java datatypes
@ -58,7 +58,7 @@ class JavaComparisonOperators(MappingRule):
"less than": Text("<"),
"less than or equal to": Text("<="),
"greater than or equal to": Text(">="),
}
# This rule deals with the Java Boolean operators
class JavaBooleanOperators(MappingRule):
@ -69,9 +69,9 @@ class JavaBooleanOperators(MappingRule):
"true": Text("true"),
"false": Text("false"),
"not": Text("!")
}
}
class JavaControlStructures(MappingRule):
#note: the last curly braces were commented out so that these commands properly work with the auto formatting of the eclipse IDE.
@ -89,14 +89,14 @@ class JavaControlStructures(MappingRule):
"interface": Text("<access modifier> interface InterfaceName {") + Key("enter")+ Key("enter"), #+ Text("}"),
"enumeration": Text("<access modifier> enum EnumName {") + Key("enter")+ Key("enter"), #+ Text("}")
"method": Text("<datatype> methodName() {") + Key("enter")+ Key("enter"), #+ Text("}"),
}
# This rule provides some useful method calls in Java
class JavaUsefulMethods(MappingRule):
mapping = {
"print statement": Text("System.out.println()") + Key("left")
"print statement": Text("System.out.println()") + Key("left")
}
# This rule deals with some of the Java arithmetic operators
class JavaArithmeticOperators(MappingRule):
@ -109,7 +109,7 @@ class JavaArithmeticOperators(MappingRule):
"minus": Text("-"),
"divided by": Text("/"),
"modulus": Text("%") #causes some weird problem in dragonfly so this doesn't work, use "percent sign" instead
}
class JavaAssignmentOperators(MappingRule):
@ -119,9 +119,9 @@ class JavaAssignmentOperators(MappingRule):
"minus equals": Text("-="),
"multiply equals": Text("*="),
"divide equals": Text("/="),
}
}
class JavaMiscellaneousStuff(MappingRule):
@ -129,7 +129,7 @@ class JavaMiscellaneousStuff(MappingRule):
"equals": Text(" = "),
"import": Text("import ;") + Key("left"),
"new": Text("new "),
}
class JavaAccessModifiers(MappingRule):
@ -138,7 +138,7 @@ class JavaAccessModifiers(MappingRule):
"public": Text("public "),
"private": Text("private "),
"protected": Text("protected ")
}
class JavaEscapeSequences(MappingRule):
@ -147,12 +147,12 @@ class JavaEscapeSequences(MappingRule):
"escape quotes": Text("\ ")+ Key("left") + Text("\"") + Text("\ ")+ Key("left") + Text("\""),
"escape single quotes": Text("\ ")+ Key("left") + Text("\'") + Text("\ ")+ Key("left") + Text("\'"),
"escape line": Text("\ ")+ Key("left") + Text("n"),
"escape tab": Text("\ ")+ Key("left") + Text("t"),
"escape tab": Text("\ ")+ Key("left") + Text("t"),
"escape carriage return": Text("\ ")+ Key("left") + Text("r"),
}
}
# The main Java grammar rules are activated here
javaBootstrap = Grammar("java bootstrap")
javaBootstrap = Grammar("java bootstrap")
javaBootstrap.add_rule(JavaEnabler())
javaBootstrap.load()
@ -178,4 +178,4 @@ javaGrammar.disable()
def unload():
global javaGrammar
if javaGrammar: javaGrammar.unload()
javaGrammar = None
javaGrammar = None

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

@ -8,17 +8,17 @@
Command-module for cursor movement and **editing**
============================================================================
This module allows the user to control the cursor and
efficiently perform multiple text editing actions within a
This module allows the user to control the cursor and
efficiently perform multiple text editing actions within a
single phrase.
Example commands
----------------------------------------------------------------------------
*Note the "/" characters in the examples below are simply
to help the reader see the different parts of each voice
command. They are not present in the actual command and
*Note the "/" characters in the examples below are simply
to help the reader see the different parts of each voice
command. They are not present in the actual command and
should not be spoken.*
Example: **"up 4 / down 1 page / home / space 2"**
@ -31,43 +31,38 @@ Example: **"left 7 words / backspace 3 / insert hello Cap world"**
the text "hello World".
Example: **"home / space 4 / down / 43 times"**
This command will insert 4 spaces at the beginning of
of this and the next 42 lines. The final "43 times"
This command will insert 4 spaces at the beginning of
of this and the next 42 lines. The final "43 times"
repeats everything in front of it that many times.
Discussion of this module
----------------------------------------------------------------------------
This command-module creates a powerful voice command for
editing and cursor movement. This command's structure can
This command-module creates a powerful voice command for
editing and cursor movement. This command's structure can
be represented by the following simplified language model:
- *CommandRule* -- top-level rule which the user can say
- *repetition* -- sequence of actions (name = "sequence")
- *KeystrokeRule* -- rule that maps a single
- *KeystrokeRule* -- rule that maps a single
spoken-form to an action
- *optional* -- optional specification of repeat count
- *integer* -- repeat count (name = "n")
- *literal* -- "times"
The top-level command rule has a callback method which is
called when this voice command is recognized. The logic
The top-level command rule has a callback method which is
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".
1. Retrieve the sequence of actions from the element with
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 *
@ -177,12 +172,12 @@ else:
#---------------------------------------------------------------------------
# Here we define the keystroke rule.
# This rule maps spoken-forms to actions. Some of these
# include special elements like the number with name "n"
# or the dictation with name "text". This rule is not
# This rule maps spoken-forms to actions. Some of these
# include special elements like the number with name "n"
# or the dictation with name "text". This rule is not
# exported, but is referenced by other elements later on.
# It is derived from MappingRule, so that its "value" when
# processing a recognition will be the right side of the
# It is derived from MappingRule, so that its "value" when
# processing a recognition will be the right side of the
# mapping: an action.
# Note that this rule does not execute these actions, it
# simply returns them when it's value() method is called.
@ -202,10 +197,10 @@ class KeystrokeRule(MappingRule):
defaults = {
"n": 1,
}
# Note: when processing a recognition, the *value* of
# this rule will be an action object from the right side
# of the mapping given above. This is default behavior
# of the MappingRule class' value() method. It also
# Note: when processing a recognition, the *value* of
# this rule will be an action object from the right side
# of the mapping given above. This is default behavior
# of the MappingRule class' value() method. It also
# substitutes any "%(...)." within the action spec
# with the appropriate spoken values.
@ -236,10 +231,10 @@ sequence = Repetition(single_action, min=1, max=16, name="sequence")
#---------------------------------------------------------------------------
# Here we define the top-level rule which the user can say.
# This is the rule that actually handles recognitions.
# When a recognition occurs, it's _process_recognition()
# method will be called. It receives information about the
# recognition in the "extras" argument: the sequence of
# This is the rule that actually handles recognitions.
# When a recognition occurs, it's _process_recognition()
# method will be called. It receives information about the
# recognition in the "extras" argument: the sequence of
# actions and the number of times to repeat them.
class RepeatRule(CompoundRule):

View File

@ -6,26 +6,26 @@ from dragonfly import (Grammar, CompoundRule, Dictation, Text, Key, AppContext,
class PHPEnabler(CompoundRule):
spec = "Enable PHP" # Spoken form of command.
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.
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):
@ -43,7 +43,7 @@ class PHPVariableDeclarations(MappingRule):
mapping = {
"variable": Text("$")
}
# Handles PHP commenting syntax
@ -52,7 +52,7 @@ class PHPCommentsSyntax(MappingRule):
mapping = {
"comment": Text("// "),
"multiline comment": Text("/*") + Key("enter") + Key("enter") + Text("*/") + Key("up")
}
class PHPSuperGlobals(MappingRule):
@ -83,8 +83,8 @@ class PHPControlStructures(MappingRule):
"try catch": Text("try {") + Key("enter")+ Key("enter") + Text("}") + Key("enter") + Text("catch(<ExceptionClass> e) {") + Key("enter")+ Key("enter") + Text("}"),
"function": Text("function functionName() {") + Key("enter")+ Key("enter") + Text("}"),
"class": Text("class ClassName {") + Key("enter")+ Key("enter") + Text("}")
}
}
class PHPAccessModifiers(MappingRule):
@ -92,14 +92,14 @@ class PHPAccessModifiers(MappingRule):
"public": Text("public "),
"private": Text("private "),
"static": Text("static ")
}
class PHPUsefulMethods(MappingRule):
mapping = {
"print statement": Text("echo \"\"") + Key("left")
"print statement": Text("echo \"\"") + Key("left")
}
class PHPLogicalOperators(MappingRule):
@ -109,7 +109,7 @@ class PHPLogicalOperators(MappingRule):
"logical or": Text("||"),
"true": Text("true"),
"false": Text("false"),
"not": Text("!"),
"not": Text("!"),
"logical exclusive or": Text("xor")
}
@ -121,7 +121,7 @@ class PHPAssignmentOperators(MappingRule):
"multiply equals": Text("*="),
"divide equals": Text("/="),
".equals": Text(".=")
}
}
class PHPArithmeticOperators(MappingRule):
@ -140,7 +140,7 @@ class PHPEscapeSequences(MappingRule):
"escape quotes": Text("\ ")+ Key("left") + Text("\"") + Text("\ ")+ Key("left") + Text("\""),
"escape single quotes": Text("\ ")+ Key("left") + Text("\'") + Text("\ ")+ Key("left") + Text("\'"),
"escape line": Text("\ ")+ Key("left") + Text("n"),
"escape tab": Text("\ ")+ Key("left") + Text("t"),
"escape tab": Text("\ ")+ Key("left") + Text("t"),
"escape carriage return": Text("\ ")+ Key("left") + Text("r"),
}
@ -155,22 +155,22 @@ class PHPComparisonOperators(MappingRule):
"less than": Text("<"),
"less than or equal to": Text("<="),
"greater than or equal to": Text(">="),
}
class PHPMiscellaneousStuff(MappingRule):
mapping = {
"PHP block": Text("<?php ?>"),
"require": Text("require ('');"),
"require once": Text("require_once ('');"),
"include": Text("include ('');"),
"include once": Text("require_once ('');"),
"equals": Text(" = "),
}
}
phpBootstrap = Grammar("php bootstrap") # Create a grammar to contain the command rule.
phpBootstrap.add_rule(PHPEnabler())
@ -200,4 +200,4 @@ phpGrammar.disable()
def unload():
global phpGrammar
if phpGrammar: phpGrammar.unload()
phpGrammar = None
phpGrammar = None

View File

@ -2,37 +2,41 @@
# 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.
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):
mapping = {
"comment": Text("# "),
}
@ -42,16 +46,16 @@ class PythonControlStructures(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("def functionName():") + Key("enter"),
"class": Text("class className(inheritance):") + Key("enter"),
}
}
# The main Python grammar rules are activated here
pythonBootstrap = Grammar("python bootstrap")
pythonBootstrap = Grammar("python bootstrap")
pythonBootstrap.add_rule(PythonEnabler())
pythonBootstrap.load()
@ -68,4 +72,4 @@ pythonGrammar.disable()
def unload():
global pythonGrammar
if pythonGrammar: pythonGrammar.unload()
pythonGrammar = None
pythonGrammar = None

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()