pressagio.dbconnector

Classes to connect to databases.

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

Base class for all database connectors.

Methods

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

Creates a table for n-grams of cardinality 2.

create_index(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(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()[source]

Creates a table for n-grams of cardinality 3.

create_unigram_table()[source]

Creates a table for n-grams of cardinality 1.

delete_index(cardinality)[source]

Delete index for the table with the given cardinality.

Parameters:

cardinality : int

The cardinality of the index to delete.

delete_ngram_table(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(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(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(with_counts=False)[source]

Returns all ngrams that are in the table.

Parameters:

None

Returns:

ngrams : generator

A generator for ngram tuples.

remove_ngram(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(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=u'localhost', port=5432, user=u'postgres', password=None, connection=None)[source]

Database connector for postgres databases.

Methods

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

Closes the sqlite database.

commit()[source]

Sends a commit to the database.

create_database()[source]

Creates an empty database if not exists.

create_index(cardinality)[source]

Create an index for the table with the given cardinality.

Parameters:

cardinality : int

The cardinality to create a index for.

delete_index(cardinality)[source]

Delete index for the table with the given cardinality.

Parameters:

cardinality : int

The cardinality of the index to delete.

execute_sql(query)[source]

Executes a given query string on an open postgres database.

open_database()[source]

Opens the sqlite database.

reset_database()[source]

Re-create an empty database.

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

Database connector for sqlite databases.

Methods

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

Closes the sqlite database.

commit()[source]

Sends a commit to the database.

execute_sql(query)[source]

Executes a given query string on an open sqlite database.

open_database()[source]

Opens the sqlite database.