google-ngrams 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- google_ngrams/__init__.py +19 -0
- google_ngrams/data/__init__.py +14 -0
- google_ngrams/data/googlebooks_eng_all_totalcounts_20120701.parquet +0 -0
- google_ngrams/data/googlebooks_eng_gb_all_totalcounts_20120701.parquet +0 -0
- google_ngrams/data/googlebooks_eng_us_all_totalcounts_20120701.parquet +0 -0
- google_ngrams/ngrams.py +341 -0
- google_ngrams/scatter_helpers.py +187 -0
- google_ngrams/vnc.py +518 -0
- google_ngrams/vnc_helpers.py +809 -0
- google_ngrams-0.2.0.dist-info/METADATA +144 -0
- google_ngrams-0.2.0.dist-info/RECORD +14 -0
- google_ngrams-0.2.0.dist-info/WHEEL +5 -0
- google_ngrams-0.2.0.dist-info/licenses/LICENSE +162 -0
- google_ngrams-0.2.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,144 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: google_ngrams
|
3
|
+
Version: 0.2.0
|
4
|
+
Summary: Fetch and analyze Google Ngram data for specified word forms.
|
5
|
+
Author-email: David Brown <dwb2@andrew.cmu.edu>
|
6
|
+
Maintainer-email: David Brown <dwb2@andrew.cmu.edu>
|
7
|
+
License-Expression: MIT
|
8
|
+
Project-URL: Documentation, https://browndw.github.io/google_ngrams
|
9
|
+
Project-URL: Homepage, https://github.com/browndw/google_ngrams
|
10
|
+
Keywords: nlp,language
|
11
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
15
|
+
Classifier: Intended Audience :: Science/Research
|
16
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
17
|
+
Classifier: Operating System :: OS Independent
|
18
|
+
Requires-Python: >=3.10
|
19
|
+
Description-Content-Type: text/x-rst
|
20
|
+
License-File: LICENSE
|
21
|
+
Requires-Dist: numpy>=1.22
|
22
|
+
Requires-Dist: matplotlib>=3.5
|
23
|
+
Requires-Dist: polars>=1.17
|
24
|
+
Provides-Extra: test
|
25
|
+
Requires-Dist: pytest>=7.0; extra == "test"
|
26
|
+
Requires-Dist: pytest-cov>=4.0; extra == "test"
|
27
|
+
Provides-Extra: dev
|
28
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
29
|
+
Requires-Dist: pytest-cov>=4.0; extra == "dev"
|
30
|
+
Requires-Dist: build>=1.0.0; extra == "dev"
|
31
|
+
Requires-Dist: twine>=5.0.0; extra == "dev"
|
32
|
+
Dynamic: license-file
|
33
|
+
|
34
|
+
|
35
|
+
google_ngrams
|
36
|
+
=======================================================================================================
|
37
|
+
|pypi| |pypi_downloads| |tests|
|
38
|
+
|
39
|
+
This package has functions for processing `Google’s Ngram repositories <http://storage.googleapis.com/books/ngrams/books/datasetsv2.html>`_ 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.
|
40
|
+
|
41
|
+
The main function uses `scan_csv from the polars <https://docs.pola.rs/api/python/dev/reference/api/polars.scan_csv.html>`_ 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.
|
42
|
+
|
43
|
+
vnc
|
44
|
+
---
|
45
|
+
|
46
|
+
To analyze the returned data, the package also contains functions based on the work of Gries and Hilpert (2012) for `Variability-Based Neighbor Clustering <https://www.oxfordhandbooks.com/view/10.1093/oxfordhb/9780199922765.001.0001/oxfordhb-9780199922765-e-14>`_.
|
47
|
+
|
48
|
+
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 <http://global.oup.com/us/companion.websites/fdscontent/uscompanion/us/static/companion.websites/nevalainen/Gries-Hilpert_web_final/vnc.individual.html>`_.
|
49
|
+
|
50
|
+
Distances, therefore, are calculated in sums of standard deviations and coefficients of variation, according to their stated method.
|
51
|
+
|
52
|
+
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.
|
53
|
+
|
54
|
+
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.
|
55
|
+
|
56
|
+
Lightweight Implementation
|
57
|
+
--------------------------
|
58
|
+
|
59
|
+
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.
|
60
|
+
|
61
|
+
|
62
|
+
Installation
|
63
|
+
------------
|
64
|
+
|
65
|
+
You can install the released version of google_ngrams from `PyPI <https://pypi.org/project/google_ngrams/>`_:
|
66
|
+
|
67
|
+
.. code-block:: install-google_ngrams
|
68
|
+
|
69
|
+
pip install google-ngrams
|
70
|
+
|
71
|
+
|
72
|
+
Usage
|
73
|
+
-----
|
74
|
+
|
75
|
+
To use the google_ngrams package, import :code:`google_ngram` to fetch data and :code:`TimeSeries` for analysis.
|
76
|
+
|
77
|
+
.. code-block:: import
|
78
|
+
|
79
|
+
from google_ngrams import google_ngram, TimeSeries
|
80
|
+
|
81
|
+
|
82
|
+
Fetching n-gram data
|
83
|
+
^^^^^^^^^^^^^^^^^^^^
|
84
|
+
|
85
|
+
The :code:`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:
|
86
|
+
|
87
|
+
The following would return counts for the word *x-ray* in US English by year:
|
88
|
+
|
89
|
+
.. code-block:: by_year
|
90
|
+
|
91
|
+
xray_year = google_ngram(word_forms = ["x-ray"], variety = "us", by = "year")
|
92
|
+
|
93
|
+
Alternatively, the following would return counts of the combined forms *xray* and *xrays* in British English by decade:
|
94
|
+
|
95
|
+
.. code-block:: by_decade
|
96
|
+
|
97
|
+
xray_decade = google_ngram(word_forms = ["x-ray", "x-rays"], variety = "gb", by = "decade")
|
98
|
+
|
99
|
+
The function returns a polars DataFrame with either a time interval column (either :code:`Year` or :code:`Decade`) and columns for :code:`Token`, :code:`AF` (absolute frequency) and :code:`RF` (relative frequency).
|
100
|
+
|
101
|
+
The returned DataFrame, then, can be manipulated using the polars API:
|
102
|
+
|
103
|
+
.. code-block:: filtering
|
104
|
+
|
105
|
+
import polars as pl
|
106
|
+
|
107
|
+
xray_filtered = xray_decade.filter(pl.col("Decade") >= 1900)
|
108
|
+
|
109
|
+
|
110
|
+
Analyzing time series data
|
111
|
+
^^^^^^^^^^^^^^^^^^^^^^^^^^
|
112
|
+
|
113
|
+
To analyze the data, use :code:`TimeSeries`, specifying a column of time intervals and a column of relative frequencies:
|
114
|
+
|
115
|
+
.. code-block:: time_series
|
116
|
+
|
117
|
+
xray_ts = TimeSeries(xray_filtered, time_col="Decade", values_col="RF")
|
118
|
+
|
119
|
+
VNC dendrograms can then be plotted with a variety of options:
|
120
|
+
|
121
|
+
.. code-block:: dendrogram
|
122
|
+
|
123
|
+
xray_ts.timeviz_vnc()
|
124
|
+
|
125
|
+
For additional information, consult the `documentation <https://browndw.github.io/google_ngrams/>`_.
|
126
|
+
|
127
|
+
|
128
|
+
License
|
129
|
+
-------
|
130
|
+
|
131
|
+
Code licensed under `MIT License <https://opensource.org/licenses/MIT>`_.
|
132
|
+
See `LICENSE <https://github.com/browndw/google_ngrams/blob/main/LICENSE>`_ file.
|
133
|
+
|
134
|
+
.. |pypi| image:: https://badge.fury.io/py/google_ngrams.svg
|
135
|
+
:target: https://badge.fury.io/py/google_ngrams
|
136
|
+
:alt: PyPI Version
|
137
|
+
|
138
|
+
.. |pypi_downloads| image:: https://img.shields.io/pypi/dm/google_ngrams
|
139
|
+
:target: https://pypi.org/project/google_ngrams/
|
140
|
+
:alt: Downloads from PyPI
|
141
|
+
|
142
|
+
.. |tests| image:: https://github.com/browndw/google_ngrams/actions/workflows/test.yml/badge.svg
|
143
|
+
:target: https://github.com/browndw/google_ngrams/actions/workflows/test.yml
|
144
|
+
:alt: Test Status
|
@@ -0,0 +1,14 @@
|
|
1
|
+
google_ngrams/__init__.py,sha256=01F_2COa0bIM7j6u-TM5bDQ27OPAlU_Jmz2FHRc6cTs,379
|
2
|
+
google_ngrams/ngrams.py,sha256=cMGlZQXyE8DUPf3TPO0OYiJATLd3HNbyhxxaHYkMZNU,12192
|
3
|
+
google_ngrams/scatter_helpers.py,sha256=JlMrUe-8PZIu68C1Yp06AH1KHb_8PLxzAbuuNcGJbEQ,5657
|
4
|
+
google_ngrams/vnc.py,sha256=unsIZqKP4Yh3iA3vJvmKIAVvDGQUGhkuBPOKlKBcFds,17018
|
5
|
+
google_ngrams/vnc_helpers.py,sha256=1YxN9dxAYDXIqd5KBmkWa7sz5S_LzYryYqGUaJNUKQ8,25123
|
6
|
+
google_ngrams/data/__init__.py,sha256=HGJ8SSIb7oP17ZtqwBVE4MzrbHEkR7Wkr2yleI-qTUM,420
|
7
|
+
google_ngrams/data/googlebooks_eng_all_totalcounts_20120701.parquet,sha256=z39hgtEE18o3qAdOc_HpFJOFCPjdgVWDef1rAbZRndQ,12000
|
8
|
+
google_ngrams/data/googlebooks_eng_gb_all_totalcounts_20120701.parquet,sha256=oxcdjSOEFuvt19CiaD0kPapy6Z8lkij0rIDuiJuJUVc,12251
|
9
|
+
google_ngrams/data/googlebooks_eng_us_all_totalcounts_20120701.parquet,sha256=CQi0n-vovKbKYQ7nUWEn7gerGHm8pAusGVpGIx1m9Go,11063
|
10
|
+
google_ngrams-0.2.0.dist-info/licenses/LICENSE,sha256=gCVdJQkFNFbywKo3EZ1w6kV-Zt5jkXozXV0wWjDed1g,9122
|
11
|
+
google_ngrams-0.2.0.dist-info/METADATA,sha256=EE4Yw3AOQCFgn_1cQGAlthgIu9el-x0pmg60nUIUq2k,6273
|
12
|
+
google_ngrams-0.2.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
13
|
+
google_ngrams-0.2.0.dist-info/top_level.txt,sha256=IjVijaqC11yDFtc5yqqDR_ikNpUJT0zu_6AaDpHs0SA,14
|
14
|
+
google_ngrams-0.2.0.dist-info/RECORD,,
|
@@ -0,0 +1,162 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 David Brown
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
23
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
24
|
+
on behalf of whom a Contribution has been received by Licensor and
|
25
|
+
subsequently incorporated within the Work.
|
26
|
+
|
27
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
28
|
+
this License, each Contributor hereby grants to You a perpetual,
|
29
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
30
|
+
copyright license to reproduce, prepare Derivative Works of,
|
31
|
+
publicly display, publicly perform, sublicense, and distribute the
|
32
|
+
Work and such Derivative Works in Source or Object form.
|
33
|
+
|
34
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
35
|
+
this License, each Contributor hereby grants to You a perpetual,
|
36
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
37
|
+
(except as stated in this section) patent license to make, have made,
|
38
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
39
|
+
where such license applies only to those patent claims licensable
|
40
|
+
by such Contributor that are necessarily infringed by their
|
41
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
42
|
+
with the Work to which such Contribution(s) was submitted. If You
|
43
|
+
institute patent litigation against any entity (including a
|
44
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
45
|
+
or a Contribution incorporated within the Work constitutes direct
|
46
|
+
or contributory patent infringement, then any patent licenses
|
47
|
+
granted to You under this License for that Work shall terminate
|
48
|
+
as of the date such litigation is filed.
|
49
|
+
|
50
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
51
|
+
Work or Derivative Works thereof in any medium, with or without
|
52
|
+
modifications, and in Source or Object form, provided that You
|
53
|
+
meet the following conditions:
|
54
|
+
|
55
|
+
(a) You must give any other recipients of the Work or
|
56
|
+
Derivative Works a copy of this License; and
|
57
|
+
|
58
|
+
(b) You must cause any modified files to carry prominent notices
|
59
|
+
stating that You changed the files; and
|
60
|
+
|
61
|
+
(c) You must retain, in the Source form of any Derivative Works
|
62
|
+
that You distribute, all copyright, patent, trademark, and
|
63
|
+
attribution notices from the Source form of the Work,
|
64
|
+
excluding those notices that do not pertain to any part of
|
65
|
+
the Derivative Works; and
|
66
|
+
|
67
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
68
|
+
distribution, then any Derivative Works that You distribute must
|
69
|
+
include a readable copy of the attribution notices contained
|
70
|
+
within such NOTICE file, excluding those notices that do not
|
71
|
+
pertain to any part of the Derivative Works, in at least one
|
72
|
+
of the following places: within a NOTICE text file distributed
|
73
|
+
as part of the Derivative Works; within the Source form or
|
74
|
+
documentation, if provided along with the Derivative Works; or,
|
75
|
+
within a display generated by the Derivative Works, if and
|
76
|
+
wherever such third-party notices normally appear. The contents
|
77
|
+
of the NOTICE file are for informational purposes only and
|
78
|
+
do not modify the License. You may add Your own attribution
|
79
|
+
notices within Derivative Works that You distribute, alongside
|
80
|
+
or as an addendum to the NOTICE text from the Work, provided
|
81
|
+
that such additional attribution notices cannot be construed
|
82
|
+
as modifying the License.
|
83
|
+
|
84
|
+
You may add Your own copyright statement to Your modifications and
|
85
|
+
may provide additional or different license terms and conditions
|
86
|
+
for use, reproduction, or distribution of Your modifications, or
|
87
|
+
for any such Derivative Works as a whole, provided Your use,
|
88
|
+
reproduction, and distribution of the Work otherwise complies with
|
89
|
+
the conditions stated in this License.
|
90
|
+
|
91
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
92
|
+
any Contribution intentionally submitted for inclusion in the Work
|
93
|
+
by You to the Licensor shall be under the terms and conditions of
|
94
|
+
this License, without any additional terms or conditions.
|
95
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
96
|
+
the terms of any separate license agreement you may have executed
|
97
|
+
with Licensor regarding such Contributions.
|
98
|
+
|
99
|
+
6. Trademarks. This License does not grant permission to use the trade
|
100
|
+
names, trademarks, service marks, or product names of the Licensor,
|
101
|
+
except as required for reasonable and customary use in describing the
|
102
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
103
|
+
|
104
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
105
|
+
agreed to in writing, Licensor provides the Work (and each
|
106
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
107
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
108
|
+
implied, including, without limitation, any warranties or conditions
|
109
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
110
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
111
|
+
appropriateness of using or redistributing the Work and assume any
|
112
|
+
risks associated with Your exercise of permissions under this License.
|
113
|
+
|
114
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
115
|
+
whether in tort (including negligence), contract, or otherwise,
|
116
|
+
unless required by applicable law (such as deliberate and grossly
|
117
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
118
|
+
liable to You for damages, including any direct, indirect, special,
|
119
|
+
incidental, or consequential damages of any character arising as a
|
120
|
+
result of this License or out of the use or inability to use the
|
121
|
+
Work (including but not limited to damages for loss of goodwill,
|
122
|
+
work stoppage, computer failure or malfunction, or any and all
|
123
|
+
other commercial damages or losses), even if such Contributor
|
124
|
+
has been advised of the possibility of such damages.
|
125
|
+
|
126
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
127
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
128
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
129
|
+
or other liability obligations and/or rights consistent with this
|
130
|
+
License. However, in accepting such obligations, You may act only
|
131
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
132
|
+
of any other Contributor, and only if You agree to indemnify,
|
133
|
+
defend, and hold each Contributor harmless for any liability
|
134
|
+
incurred by, or claims asserted against, such Contributor by reason
|
135
|
+
of your accepting any such warranty or additional liability.
|
136
|
+
|
137
|
+
END OF TERMS AND CONDITIONS
|
138
|
+
|
139
|
+
APPENDIX: How to apply the Apache License to your work.
|
140
|
+
|
141
|
+
To apply the Apache License to your work, attach the following
|
142
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
143
|
+
replaced with your own identifying information. (Don't include
|
144
|
+
the brackets!) The text should be enclosed in the appropriate
|
145
|
+
comment syntax for the file format. We also recommend that a
|
146
|
+
file or class name and description of purpose be included on the
|
147
|
+
same "printed page" as the copyright notice for easier
|
148
|
+
identification within third-party archives.
|
149
|
+
|
150
|
+
Copyright [yyyy] [name of copyright owner]
|
151
|
+
|
152
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
153
|
+
you may not use this file except in compliance with the License.
|
154
|
+
You may obtain a copy of the License at
|
155
|
+
|
156
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
157
|
+
|
158
|
+
Unless required by applicable law or agreed to in writing, software
|
159
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
160
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
161
|
+
See the License for the specific language governing permissions and
|
162
|
+
limitations under the License.
|
@@ -0,0 +1 @@
|
|
1
|
+
google_ngrams
|