Package tipy :: Module cntxt :: Class ContextMonitor
[hide private]
[frames] | no frames]

Class ContextMonitor

source code

object --+
         |
        ContextMonitor

Monitire user current context.

This class monitore the input buffers in order to:

Class Hierarchy for ContextMonitor
Class Hierarchy for ContextMonitor

Instance Methods [hide private]
 
__init__(self, config, predictorRegistry, callback)
ContextMonitor creator.
source code
bool
context_change(self)
Check if a context change occure.
source code
 
update(self)
Check if context changes occure and learn what need to be learnt.
source code
 
learn(self, string)
Learn n-grams from the input buffers.
source code
str
prefix(self)
Return the token just before the cursor.
source code
str
suffix(self)
Return the token just after the cursor.
source code
str
left_token(self, index)
Return the token at a given index in the left input buffer.
source code
str
right_token(self, index)
Return the token at a given index in the right input buffer.
source code
str
previous_tokens(self, index, change)
Return the token just before the change token (if any).
source code
str
left_buffer(self)
Use the callback to get the value of the left buffer.
source code
str
right_buffer(self)
Use the callback to get the value of the right buffer.
source code
 
make_completion(self, suggestion)
Compute the completion string given a suggested word.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, config, predictorRegistry, callback)
(Constructor)

source code 

ContextMonitor creator.

Parameters:
  • config (drvr.Configuration) - It is used to retrieve the ContextMonitor settings from the configuration file.
  • predictorRegistry (PredictorRegistry) - It is used to access the predictors's learn() methods. Also, the ContextMonitor is used by the predictors to access the input buffers.
  • callback (Callback) - As the callback hold the input buffers and the ContextMonitor operate on these buffers, it is used to access the input buffers from inside the ContextMonitor.
Overrides: object.__init__

context_change(self)

source code 

Check if a context change occure.

Returns: bool
Return True or False weither a context change occure.

update(self)

source code 

Check if context changes occure and learn what need to be learnt.

This method is called by Driver.predict() after the predictions have been computed. It check if a context change occure in the input buffers and if so, it learn the words that need to be learnt if the predictor's learning mode is ON. Finaly, it update the monitored scope.

learn(self, string)

source code 

Learn n-grams from the input buffers.

Trigger the learn() method of each predictor of the registry. This method use the input buffers to create n-grams and add them to the predictors's databases or memory so that the program learn from the user input.

Parameters:
  • string (str) - The string to learn.

prefix(self)

source code 

Return the token just before the cursor.

Returns: str
The token just before the cursor or an empty string if there is none.

suffix(self)

source code 

Return the token just after the cursor.

Returns: str
The token just after the cursor or the empty string if there is none.

left_token(self, index)

source code 

Return the token at a given index in the left input buffer.

Parameters:
  • index (int) - The index of the token to retrieve in the left input buffer.
Returns: str
The token at index 'index' in the left input buffer or an empty string if the token dosen't exists.

right_token(self, index)

source code 

Return the token at a given index in the right input buffer.

Parameters:
  • index (int) - The index of the token to retrieve in the right input buffer.
Returns: str
The token at index 'index' in the right input buffer or an empty string if the token dosen't exists.

previous_tokens(self, index, change)

source code 

Return the token just before the change token (if any).

This method is called in some predictors's learn() method. It retrieve the token that appear just before the change token and has already been learnt before (or should have). The previous token is used to fill the n-grams.

Parameters:
  • index (int) - Index of the previous token.
  • change (str) - The change token.
Returns: str
The token just before the change token or an empty string if there is none.

left_buffer(self)

source code 

Use the callback to get the value of the left buffer.

Returns: str
The left input buffer.

right_buffer(self)

source code 

Use the callback to get the value of the right buffer.

Returns: str
The right input buffer.

make_completion(self, suggestion)

source code 

Compute the completion string given a suggested word.

This method compute and return the completion string using the token just before the cursor (prefix) and the suggested word (suggestion). The suggestion should be the word that the user choose from the suggested words list.

For instance, if the prefix is:

   "wor"

And the suggestion is:

   "world"

Then this method will compute the completion:

   "ld"

If the character before the cursor is a blankspace or a separator then the prefix should be empty:

   ""

Then if the suggestion is:

   "guilty"

This method will compute the completion:

   "guilty"

If the suggestion and the prefix don't match then False is returned. This should never happen as suggestions completing an input word should always match it. Still, I prefer to check it at the cost of some lower() and startswith() calls.

Parameters:
  • suggestion (str) - The suggested word from which to compute the completion.