Skip to main content

google_ngrams

PyPI Version Downloads from PyPI Test Status

This package has functions for processing Google’s Ngram repositories without having to download them locally. These repositories vary in their size, but the larger ones (like th one for the letter s or common bigrams) contain multiple gigabytes.

The main function uses scan_csv from the polars package to reduce memory load. Still, depending on the specific word forms being searched, loading and processing the data tables can sometimes take a few minutes if they are large.

vnc

To analyze the returned data, the package also contains functions based on the work of Gries and Hilpert (2012) for Variability-Based Neighbor Clustering.

The idea is to use hierarchical clustering to aid “bottom up” periodization of language change. The python functions are built on their original R code.

Distances, therefore, are calculated in sums of standard deviations and coefficients of variation, according to their stated method.

Dendrograms are plotted using matplotlib, with custom implementations for hierarchical clustering that maintain the plotting order of the leaves according to the requirements of the method.

The package also has a custom implementation of dendrogram truncation that consolidates leaves under a specified number of time periods (or clusters) while also maintaining the leaf order to facilitate the reading and interpretation of large dendrograms.

Lightweight Implementation

Starting with version 0.2.0, google_ngrams uses lightweight, custom implementations for statistical computations instead of heavy dependencies like scipy and statsmodels. This design choice reduces installation overhead while maintaining full functionality for the core VNC methodology and smoothing operations.

Installation

You can install the released version of google_ngrams from PyPI:

pip install google-ngrams

Usage

To use the google_ngrams package, import google_ngram to fetch data and TimeSeries for analysis.

from google_ngrams import google_ngram, TimeSeries

Fetching n-gram data

The google_ngram function supports different varieties of English (e.g., British, American) and allows aggregation by year or decade. Word forms (even a single word form) must be formatted as a list:

The following would return counts for the word x-ray in US English by year:

xray_year = google_ngram(word_forms = ["x-ray"], variety = "us", by = "year")

Alternatively, the following would return counts of the combined forms xray and xrays in British English by decade:

xray_decade = google_ngram(word_forms = ["x-ray", "x-rays"], variety = "gb", by = "decade")

The function returns a polars DataFrame with either a time interval column (either Year or Decade) and columns for Token, AF (absolute frequency) and RF (relative frequency).

The returned DataFrame, then, can be manipulated using the polars API:

import polars as pl

xray_filtered = xray_decade.filter(pl.col("Decade") >= 1900)

Analyzing time series data

To analyze the data, use TimeSeries, specifying a column of time intervals and a column of relative frequencies:

xray_ts = TimeSeries(xray_filtered, time_col="Decade", values_col="RF")

VNC dendrograms can then be plotted with a variety of options:

xray_ts.timeviz_vnc()

For additional information, consult the documentation.

License

Code licensed under MIT License. See LICENSE file.

Metadata

Release files for google-ngrams 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for google-ngrams 0.2.0
File Size Uploaded Provenance Actions
google_ngrams-0.2.0.tar.gz 49.2 kB With provenance Details for google_ngrams-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for google-ngrams 0.2.0
File Interpreter ABI Platform Actions
google_ngrams-0.2.0-py3-none-any.whl Python 3 none any Details for google_ngrams-0.2.0-py3-none-any.whl

Total release size: 95.8 kB

Release files / google_ngrams-0.2.0.tar.gz

Download URL google_ngrams-0.2.0.tar.gz
Size 49.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8ea9925ebd942b3205746525ae46194df5b8bcc4b333f1ce76f106b116d28fa0
BLAKE2b-256 checksum
How to use checksums
aec10398ed616bbc595543f9a3d01c8c0286351ae63df13cdc69d276301461a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 11, 2025.

Transparency log

Release files / google_ngrams-0.2.0-py3-none-any.whl

Download URL google_ngrams-0.2.0-py3-none-any.whl
Size 46.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e370a1205f0283498cedfc7fdb654f44223cc33a1bf5cdbb7eea767057539ecc
BLAKE2b-256 checksum
How to use checksums
2d3d9cb21ec29cb0d41fa9d8d27e27f02f8c7acc6134e505c241bee7e034f9c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 11, 2025.

Transparency log

Security for google-ngrams 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for google_ngrams-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 11, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page