Skip to main content

Creates an SQLite database ngrams.

Project description

Create an SQLite database from the Google ngrams database.

Travis CI Build Status PyPI Python 3.6

Creates an SQLite database of the one million n-gram datasets from Google. This code downloads the n-gram data sets corpus and then creates an SQLite database file with the contents. It also provides a simple API for n-gram look ups.

Table of Contents

Installation

  1. To make life easier, install GNU Make. If you do not, you'll need to follow the steps given in the makefile.
  2. Download the 1 million n-gram data sets: make download. This should take a few minutes with a good Internet connection.
  3. Un-compress the load files: make uncompress.
  4. Create and load the SQLite database from the downloaded corpus: make load. Depending on the processor speed, this should take about an hour and creates a file in data/eng-1gram.db that takes 18G on disk.
  5. Install from the command line either from source (make install) or from pip.

If you want to use the program on the command line (as opposed to an API), create a the following file in ~/.ngramdbrc with the contents:

[default]
[ngram_db]
data_dir=${HOME}/path/to/eng-1gram.db

Data Size

As mentioned, the SQLite database file take 18G on disk. This is because it keeps occurrences over several decades. In many cases, older n-grams are not needed and queries can take a while given the size of the data. The data can be minimized with the following SQL in any SQLite interface (i.e. MacOS has sqlite3 on the command line):

delete from ngram where yr < 1990;

In this example, all n-grams recorded from publications before the year 1990 are expunged.

Usage

This project can be used either from the command line or as an API.

Command Line

To use from the command line:

% ngramdb query -g the -y 2005
631362690 0.56880%

This gives the number of unigrams (assuming unigrams were built) found since 2005 and the percentage of that unigram to all words in the corpus.

Programmatic Interface

As in the installation section, create the ~/.ngramdbrc configuration file. Also note that the API is configured to easily work with other Python projects that use the zensols.actioncli configuration API.

from zensols.ngramdb import AppConfig, Query
conf = AppConfig.instance().app_config
query = Query(conf)
stash = query.stash
n_occurs = stash['The']
print(f'{n_occurs} {100 * n_occurs / len(stash):.5f}%')

=> 631362690 0.56880%

Data Analysis with Pandas

The stash access is nice for specific use cases where a subset of the corpus counts are necessary. However, the intention of creating a selectable data format was to allow for data analysis as well. Here's an example of how Pandas can be used directly against the created SQLite file learn about the corpus:

from zensols.actioncli.time import time
import pandas as pd
import sqlite3 as s

# "connect" to the SQLite database file
db = '/path/to/data/directory/eng-1gram.db'
conn = s.connect(db)

# create a dataframe with all entries on or after 1990
sql = 'select grams, match_count as cnt from ngram where yr >= 1990'
with time('{rc} rows read'):
    df = pd.read_sql_query(sql, conn)
    rc = len(df)
#=> 28150989 rows read finished in 60.8s

# create a data frame with a ngram text and number of match counts per row
with time('groupby of {rc} rows'):
    dfg = df.groupby(['grams'], as_index=False).agg({'cnt': 'sum'})
    rc = len(df)
#=> group of 28150989 rows finished in 7.6s

# get the number of counts on 'the'
dfg[dfg['grams'] == 'the']
#=>         grams        cnt
#=> 2819462   the  621594750

# all token occurrences
all_cnt = df['cnt'].sum()
all_cnt
#=> 13269089201

# calculate at the poulation of a few words
for word in 'the The . cat dog phone iPhone'.split():
	occ = dfg[dfg['grams'] == word].cnt.item()
	pop = occ / all_cnt
	print(f'word \'{word}\' found {occ} times, which is {pop * 100:.5f}% of the corpus')

#=> word 'the' found 621594750 times, which is 4.68453% of the corpus
#=> word 'The' found 77576794 times, which is 0.58464% of the corpus
#=> word '.' found 641792317 times, which is 4.83675% of the corpus
#=> word 'cat' found 247075 times, which is 0.00186% of the corpus
#=> word 'dog' found 453789 times, which is 0.00342% of the corpus
#=> word 'phone' found 522190 times, which is 0.00394% of the corpus
#=> word 'iPhone' found 178 times, which is 0.00000% of the corpus

with time('pickled data frame'):
    df.to_pickle('df.dat')
#=> pickled data frame finished in 8.0s

with time('write to csv'):
    df.to_csv('df.csv')
#=> write to csv finished in 44.3s

with time('read data from'):
    df = pd.read_pickle('df.dat')
#=> read data from finished in 2.4s

Obtaining

The easist way to install the command line program is via the pip installer:

pip3 install zensols.ngramdb

Binaries are also available on pypi.

Changelog

An extensive changelog is available here.

License

Copyright (c) 2019 Paul Landes

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for zensols.ngramdb, version 0.0.2
Filename, size File type Python version Upload date Hashes
Filename, size zensols.ngramdb-0.0.2-py3.7.egg (24.5 kB) File type Egg Python version 3.7 Upload date Hashes View
Filename, size zensols.ngramdb-0.0.2-py3-none-any.whl (15.3 kB) File type Wheel Python version py3 Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page