Package tipy :: Module db :: Class DatabaseConnector
[hide private]
[frames] | no frames]

Class DatabaseConnector

source code

object --+
         |
        DatabaseConnector
Known Subclasses:

Implement methods for accessing the databases.

Class Hierarchy for DatabaseConnector
Class Hierarchy for DatabaseConnector

Instance Methods [hide private]
 
__init__(self, dbname, maxN=1)
DababaseConnector creator.
source code
 
crt_ngram_table(self, n=1)
Creates a table in the database to store n-gram of a given n.
source code
 
drop_ngram_table(self, n=1)
Drop a n-gram table in the database.
source code
 
crt_index(self, n)
Create the index for the table storing n-gram of given n.
source code
 
dlt_index(self, n)
Drop the index for the table storing n-gram of given n.
source code
 
create_table_if_not_exists(self)
Create the database tables and indexes if they don't exists.
source code
tuple
ngrams(self, withCounts=False)
Returns all ngrams that are in every tables of the database.
source code
int
sum_ngrams_occ(self, n)
Compute the occurences sum of every n-grams of given n in database.
source code
int
ngrams_in_table(self, n)
Compute the number of n-grams in the n-grams table of given n.
source code
int
ngram_count(self, ngram)
Retrieve the number of occurences of a given ngram in the database.
source code
list
ngram_table_tp(self, ngram, limit=-1)
Retrieve the n-gram records which complete the given n-gram.
source code
 
insert_ngram(self, ngram, count)
Insert an n-gram with its number of occurences into the database.
source code
 
update_ngram(self, ngram, count)
Update an n-gram number of occurences in the database.
source code
 
remove_ngram(self, ngram)
Remove a given ngram from the databae.
source code
 
make_values_clause(self, ngram, count) source code
 
make_where_clause(self, ngram) source code
 
make_select_like_clause(self, n) source code
 
make_where_like_clause(self, ngram) source code
 
extract_first_integer(self, table) source code

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

Class Variables [hide private]
  singleQuoteRegex = re.compile(r'\'')
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, dbname, maxN=1)
(Constructor)

source code 

DababaseConnector creator.

Parameters:
  • dbname (str) - Path to the database file.
  • maxN (int) - The n in the longer database n-grams table.
Overrides: object.__init__

crt_ngram_table(self, n=1)

source code 

Creates a table in the database to store n-gram of a given n.

Parameters:
  • n (int) - The n in n-gram. A table called [n]_gram (where [n] is the n parameter) will be created.

drop_ngram_table(self, n=1)

source code 

Drop a n-gram table in the database.

Parameters:
  • n (int) - The n in n-gram.

crt_index(self, n)

source code 

Create the index for the table storing n-gram of given n.

Parameters:
  • n (int) - The n in n-gram.

dlt_index(self, n)

source code 

Drop the index for the table storing n-gram of given n.

Parameters:
  • n (int) - The n in n-gram.

create_table_if_not_exists(self)

source code 

Create the database tables and indexes if they don't exists.

This is usefull to avoid crash when trying to hit an unexisting table.

ngrams(self, withCounts=False)

source code 

Returns all ngrams that are in every tables of the database.

Parameters:
  • withCounts (bool) - Indicate if the ngrams counts (number of occurences) should be returned too.
Returns: tuple
The n-grams of each tables of the database.

Note: The result is convert to tuple before being returned because it is used as a dictionary key and only immutable types can be used as dictionary key, so list cannot.

sum_ngrams_occ(self, n)

source code 

Compute the occurences sum of every n-grams of given n in database.

Parameters:
  • n (int) - The n in n-gram.
Returns: int
The sum of the number of occurences of every n-grams in the n-grams table of given n in the database.

ngrams_in_table(self, n)

source code 

Compute the number of n-grams in the n-grams table of given n.

Parameters:
  • n (int) - The n in n-gram.
Returns: int
The number of n-grams in the n-grams table of given n in the database.

ngram_count(self, ngram)

source code 

Retrieve the number of occurences of a given ngram in the database.

Parameters:
  • ngram (list) - The n-gram for which number of occurences must be retrieved.
Returns: int
The number of occurences of the n-gram or 0 if the n-gram is not in the databade.

ngram_table_tp(self, ngram, limit=-1)

source code 

Retrieve the n-gram records which complete the given n-gram.

For instance, if ngram is:

   ['on', 'the', 'ta']

Then the returned records would be somthing like:

   [['on', 'the', 'table'  ], 5]
   [['on', 'the', 'take'   ], 1]
   [['on', 'the', 'taskbar'], 1]
Parameters:
  • ngram (list) - The n-gram words of the record to retrieve in the database.
  • limit (int) - Maximum number of records to retrieve.
Returns: list
Return the n-grams records (n-gram words + number of occurences) completing the n-gram in the database or an empty list if no n-grams have been found.

Note: the query makes sure the n-grams are returned in descending order according to their number of occurences. This is important because the predictors predict() methods which would call this method can limit their suggestion number so the most probable suggestions (which are based on most frequent n-grams) must be append to the list first.

insert_ngram(self, ngram, count)

source code 

Insert an n-gram with its number of occurences into the database.

Parameters:
  • ngram (list) - The n-gram to insert.
  • count (int) - The number of occurences of the n-gram to insert. It is usually 1 as the n-grams should be inserted in the database only if they are not already in it and they are added when they are read on the input buffer.

update_ngram(self, ngram, count)

source code 

Update an n-gram number of occurences in the database.

Parameters:
  • ngram (list) - The n-gram to update (!!! should be in the database !!!).
  • count (int) - The number of occurences of the n-gram.

Warning: The ngram has to be in the database, otherwise this method will fail.

remove_ngram(self, ngram)

source code 

Remove a given ngram from the databae.

Parameters:
  • ngram (list) - The n-gram to delete (!!! should be in the database !!!).

Warning: The ngram has to be in the database, otherwise this method will fail.