Activating and Deactivating Rules
Hi All
I hope you can help me. I am trying to activate and deactivate rules based on what the user has said. For example, if they say "math mode on" then the rule expr will be activated and deactivated if they say "math mode off". This works fine but now I want to add another rule, which will activate document when the user says "document mode on". Now, depending on what I say first, this messes up the other mode. For example if I say "document mode on" first, it doesnt activate expr when I say "math mode on". Whereas if I say "math mode on" first and "document mode on" after it doesnt activate document.
This is my grammar (simplified version):
gramSpec="""
<math_mode_on> exported = math mode on;
<math_mode_off> exported = math mode off;
<document_mode_on> exported = document mode on;
<document_mode_off> exported = document mode off;
<document> exported = begin [document | itemize | quotation | abstract | equation];
<expr> exported = (<operator>|<letter>)+;
"""This is my Python file (simplified version):
def initialize(self):
self.load(self.gramSpec)
self.mathMode = 0
self.documentMode = 0
def gotResults_math_mode_on(self,words, fullResults):
if not self.mathMode:
natlink.playString('$')
self.mathMode = 1
def gotResults_math_mode_off(self,words, fullResults):
if self.mathMode:
self.mathMode = 0
natlink.playString('$')
def gotResults_document_mode_on(self,words, fullResults):
self.documentMode = 1
def gotResults_document_mode_off(self,words, fullResults):
self.documentMode = 0
def gotBegin(self,moduleInfo):
winHandle=matchWindow(moduleInfo,'winedt','WinEdt')
if winHandle:
self.activate('math_mode_on', window=winHandle, noError=1)
self.activate('math_mode_off', window=winHandle, noError=1)
self.activate('document_mode_on', window=winHandle, noError=1)
self.activate('document_mode_off', window=winHandle, noError=1)
if (self.documentMode==1):
self.activate('document', noError=1)
else:
self.deactivate('document', noError=1)
if (self.mathMode==1):
self.activate('expr', noError=1)
else:
self.deactivate('expr', noError=1)I have also tried if, elif and else statements and even activating the rules in the gotResults methods but the same thing happens.
I hope I have provided enough information.
Thanks for any help you can provide.
Angela



Angela,
Angela,
In the Pro version at least these commands are superfluous because they already exist amongst the Natural Commands, but I'm not sure about Standard and Preferred versions.
I don't know anything about Python, but it seems to me that for entering a new mode it's probably a good idea to switch back to normal mode first -- that gives you a known state from which to work. Obviously I don't know how to do this in Python, but I'm confident that you do.
Bruce
PS: It may be the way to Set Normal Mode On is to set each of the special modes off, which could be done in what I would call a subroutine of function that could be called whenever you need it.
Hi Angela, welcom to
Hi Angela, welcom to natlink/natpython. Maybe you could study unimacro too, for examples and possibly grammars that do (about) the same as what you want to do.
Here I miss the gotResuls_document(...) and the gotResults_expr(...) functions, that should do the relevant recognitions, just in case they are activated by the other rules.
Succes, Quintijn.