pressagio.dbconnector

Classes to connect to databases.

class pressagio.dbconnector.DatabaseConnector(dbname, cardinality=1)[source]

Base class for all database connectors.

Methods

create_bigram_table(self) Creates a table for n-grams of cardinality 2.
create_index(self, cardinality) Create an index for the table with the given cardinality.
create_ngram_table(self, cardinality) Creates a table for n-gram of a give cardinality.
create_trigram_table(self) Creates a table for n-grams of cardinality 3.
create_unigram_table(self) Creates a table for n-grams of cardinality 1.
delete_index(self, cardinality) Delete index for the table with the given cardinality.
delete_ngram_table(self, cardinality) Deletes the table for n-gram of a give cardinality.
insert_ngram(self, ngram, count) Inserts a given n-gram with count into the database.
ngram_count(self, ngram) Gets the count for a given ngram from the database.
ngrams(self[, with_counts]) Returns all ngrams that are in the table.
remove_ngram(self, ngram) Removes a given ngram from the databae.
update_ngram(self, ngram, count) Updates a given ngram in the database.
close_database  
execute_sql  
increment_ngram_count  
ngram_like_table  
ngram_like_table_filtered  
open_database  
unigram_counts_sum  
__init__(self, dbname, cardinality=1)[source]

Constructor of the base class DababaseConnector.

Parameters:
dbname : str

path to the database file or database name

cardinality : int

default cardinality for n-grams

create_bigram_table(self)[source]

Creates a table for n-grams of cardinality 2.

create_index(self, cardinality)[source]

Create an index for the table with the given cardinality.

Parameters:
cardinality : int

The cardinality to create a index for.

create_ngram_table(self, cardinality)[source]

Creates a table for n-gram of a give cardinality. The table name is constructed from this parameter, for example for cardinality 2 there will be a table _2_gram created.

Parameters:
cardinality : int

The cardinality to create a table for.

create_trigram_table(self)[source]

Creates a table for n-grams of cardinality 3.

create_unigram_table(self)[source]

Creates a table for n-grams of cardinality 1.

delete_index(self, cardinality)[source]

Delete index for the table with the given cardinality.

Parameters:
cardinality : int

The cardinality of the index to delete.

delete_ngram_table(self, cardinality)[source]

Deletes the table for n-gram of a give cardinality. The table name is constructed from this parameter, for example for cardinality 2 there will be a table _2_gram deleted.

Parameters:
cardinality : int

The cardinality of the table to delete.

insert_ngram(self, ngram, count)[source]

Inserts a given n-gram with count into the database.

Parameters:
ngram : iterable of str

A list, set or tuple of strings.

count : int

The count for the given n-gram.

ngram_count(self, ngram)[source]

Gets the count for a given ngram from the database.

Parameters:
ngram : iterable of str

A list, set or tuple of strings.

Returns:
count : int

The count of the ngram.

ngrams(self, with_counts=False)[source]

Returns all ngrams that are in the table.

Parameters:
None
Returns:
ngrams : generator

A generator for ngram tuples.

remove_ngram(self, ngram)[source]

Removes a given ngram from the databae. The ngram has to be in the database, otherwise this method will stop with an error.

Parameters:
ngram : iterable of str

A list, set or tuple of strings.

update_ngram(self, ngram, count)[source]

Updates a given ngram in the database. The ngram has to be in the database, otherwise this method will stop with an error.

Parameters:
ngram : iterable of str

A list, set or tuple of strings.

count : int

The count for the given n-gram.

class pressagio.dbconnector.PostgresDatabaseConnector(dbname, cardinality=1, host='localhost', port=5432, user='postgres', password=None, connection=None)[source]

Database connector for postgres databases.

Methods

close_database(self) Closes the sqlite database.
commit(self) Sends a commit to the database.
create_bigram_table(self) Creates a table for n-grams of cardinality 2.
create_database(self) Creates an empty database if not exists.
create_index(self, cardinality) Create an index for the table with the given cardinality.
create_ngram_table(self, cardinality) Creates a table for n-gram of a give cardinality.
create_trigram_table(self) Creates a table for n-grams of cardinality 3.
create_unigram_table(self) Creates a table for n-grams of cardinality 1.
delete_index(self, cardinality) Delete index for the table with the given cardinality.
delete_ngram_table(self, cardinality) Deletes the table for n-gram of a give cardinality.
execute_sql(self, query) Executes a given query string on an open postgres database.
insert_ngram(self, ngram, count) Inserts a given n-gram with count into the database.
ngram_count(self, ngram) Gets the count for a given ngram from the database.
ngrams(self[, with_counts]) Returns all ngrams that are in the table.
open_database(self) Opens the sqlite database.
remove_ngram(self, ngram) Removes a given ngram from the databae.
reset_database(self) Re-create an empty database.
update_ngram(self, ngram, count) Updates a given ngram in the database.
increment_ngram_count  
ngram_like_table  
ngram_like_table_filtered  
unigram_counts_sum  
__init__(self, dbname, cardinality=1, host='localhost', port=5432, user='postgres', password=None, connection=None)[source]

Constructor for the postgres database connector.

Parameters:
dbname : str

the database name

cardinality : int

default cardinality for n-grams

host : str

hostname of the postgres database

port : int

port number of the postgres database

user : str

user name for the postgres database

password: str

user password for the postgres database

connection : connection

an open database connection

close_database(self)[source]

Closes the sqlite database.

commit(self)[source]

Sends a commit to the database.

create_database(self)[source]

Creates an empty database if not exists.

create_index(self, cardinality)[source]

Create an index for the table with the given cardinality.

Parameters:
cardinality : int

The cardinality to create a index for.

delete_index(self, cardinality)[source]

Delete index for the table with the given cardinality.

Parameters:
cardinality : int

The cardinality of the index to delete.

execute_sql(self, query)[source]

Executes a given query string on an open postgres database.

open_database(self)[source]

Opens the sqlite database.

reset_database(self)[source]

Re-create an empty database.

class pressagio.dbconnector.SqliteDatabaseConnector(dbname, cardinality=1)[source]

Database connector for sqlite databases.

Methods

close_database(self) Closes the sqlite database.
commit(self) Sends a commit to the database.
create_bigram_table(self) Creates a table for n-grams of cardinality 2.
create_index(self, cardinality) Create an index for the table with the given cardinality.
create_ngram_table(self, cardinality) Creates a table for n-gram of a give cardinality.
create_trigram_table(self) Creates a table for n-grams of cardinality 3.
create_unigram_table(self) Creates a table for n-grams of cardinality 1.
delete_index(self, cardinality) Delete index for the table with the given cardinality.
delete_ngram_table(self, cardinality) Deletes the table for n-gram of a give cardinality.
execute_sql(self, query) Executes a given query string on an open sqlite database.
insert_ngram(self, ngram, count) Inserts a given n-gram with count into the database.
ngram_count(self, ngram) Gets the count for a given ngram from the database.
ngrams(self[, with_counts]) Returns all ngrams that are in the table.
open_database(self) Opens the sqlite database.
remove_ngram(self, ngram) Removes a given ngram from the databae.
update_ngram(self, ngram, count) Updates a given ngram in the database.
increment_ngram_count  
ngram_like_table  
ngram_like_table_filtered  
unigram_counts_sum  
__init__(self, dbname, cardinality=1)[source]

Constructor for the sqlite database connector.

Parameters:
dbname : str

path to the database file

cardinality : int

default cardinality for n-grams

close_database(self)[source]

Closes the sqlite database.

commit(self)[source]

Sends a commit to the database.

execute_sql(self, query)[source]

Executes a given query string on an open sqlite database.

open_database(self)[source]

Opens the sqlite database.