wordfreq

repository·master·Indexed 23 days ago

https://github.com/rspeer/wordfreq

A Python library for looking up word frequencies across many languages using multiple data sources. It provides decimal and logarithmic (Zipf) frequency estimates, tools for tokenization (including CJK support), and functions to retrieve the most common words. Version 3.2.0 includes features such as frequency bins for precision, Benford's law-based number frequency estimation, and automatic transliteration for multi-script languages.

Tokens
4.6K
Snippets
8
Records
40
Agent score
82%

What's inside wordfreq

  1. Understand wordfreq wordlists and precision

    master

    Wordlist Types

    • 'small': Optimized for low memory usage. Covers words appearing at least once per million words.
    • 'large': More comprehensive. Covers words appearing at least once per 100 million words.
    • 'best': The default setting. It automatically selects 'large' if available for the requested language, falling back to 'small' otherwise.

    Precision and Frequency Bins

    To optimize loading speed and storage, wordfreq uses frequency bins. Instead of storing high-precision decimals, all words with the same Zipf frequency (rounded to the nearest hundredth) share the same frequency value. This ensures the frequency of any word is precise to within 1%.

  2. Understand wordfreq licensing and data sources

    master

    The wordfreq project is subject to multiple licenses depending on whether you are using the code or the data:

    • Code: Distributed under the Apache License.
    • Data Files: Distributed under the Creative Commons Attribution-ShareAlike 4.0 license.

    Data Source Attributions

    When redistributing or using the data, be aware of the following sources:

    • Google Books Ngrams & Syntactic Ngrams: Data is freely usable for any purpose. Acknowledgement of Google Books Ngram Viewer as the source and a link to http://books.google.com/ngrams is appreciated.
    • SUBTLEX word lists (US, UK, CH, DE, NL): Created by Marc Brysbaert et al. Any code derived from wordfreq must credit the SUBTLEX authors and clearly state that SUBTLEX is freely available data.
    • OpenSubtitles 2018: Data originates from the OpenSubtitles project and requires attribution to OpenSubtitles.
    • Other Sources: Includes data from the Leeds Internet Corpus, Wikipedia, ParaCrawl, and historical Twitter API statistics.
  3. How language code matching works

    master
    The wordfreq library uses the langcodes module to find the best match for a provided language code. This allows for flexible querying even when using highly specific language codes. For example, if you request word frequencies using the specific code cmn-Hans (Mandarin in Simplified Chinese), the library will automatically map this to the zh wordlist.
  4. How wordfreq handles numbers

    master

    To avoid massive wordlists, wordfreq uses an aggregation method for numbers based on their "shape" (e.g., ## or ####).

    When looking up a token containing multiple digits, the library calculates frequency by multiplying the aggregated entry's frequency by a distribution based on:

    1. The value of the first digit: Assigned probabilities via Benford's law.
    2. Year likelihood: A distribution that identifies 4-digit sequences likely to be years, with a probability plateau for the "present" (2019–2039).

    Single-digit numbers (0-9) have their own specific entries and are not aggregated.

    # Example number lookups
    word_frequency("2022", "en")
    word_frequency("1922", "en")
    word_frequency("1022", "en")
  5. How wordfreq handles multi-script languages

    master

    To prevent frequency discrepancies caused by different writing systems, wordfreq automatically transliterates certain languages:

    • Serbian (sr or sh): Cyrillic text is automatically converted to Latin. Note that requesting hr (Croatian) or bs (Bosnian) will not trigger transliteration.
    • Chinese (zh): All text is converted to an internal "Oversimplified Chinese" representation. This replaces all Traditional Chinese characters with their Simplified equivalents to unify frequencies across both scripts.
  6. Attribute wordfreq correctly

    master

    When using wordfreq, you must credit the author using her academic name to ensure proper attribution.

    Required Attribution Name: Robyn Speer

    Important Notes:

    • Do not credit her as Elia Robyn Lake.
    • Using any other name is a violation of the license and revokes your permission to use, copy, or redistribute the library.
    • If using wordfreq in academic work, you must cite it according to the instructions in the project's README.md.
  7. Install CJK support for Chinese, Japanese, and Korean

    master

    To correctly tokenize Chinese, Japanese, and Korean, you must install additional external dependencies. You can install these all at once by using the cjk extra feature with pip:

    pip install wordfreq[cjk]

    This installation handles the following requirements:

    • Chinese: depends on jieba.
    • Japanese: depends on mecab-python3 and ipadic.
    • Korean: depends on mecab-python3 and mecab-ko-dic.

    As of version 2.4.2, you no longer need to install dictionaries separately. If you are using Poetry, you can add wordfreq[cjk] to your [tool.poetry.dependencies] list.

  8. Tokenize Traditional Chinese while preserving original spans

    master

    When using jieba_tokenize(text) (with external_wordlist=False), the function performs a specific workflow to handle Traditional Chinese text:

    1. It simplifies the input text to Simplified Chinese internally.
    2. It tokenizes the simplified version.
    3. It returns the character spans from the original text.

    This allows you to get tokens from Traditional Chinese text that are compatible with wordfreq lookups while maintaining the original characters/spans.

  9. Tokenize text with tokenize()

    master

    The tokenize(text, lang) function splits a string into individual tokens (words) using the same logic used to build the wordfreq datasets. This ensures that your input text is processed consistently with the frequency data.

    Language-specific behaviors:

    • Arabic/Hebrew: Normalizes ligatures and removes combining marks.
    • Japanese/Korean: Uses mecab-python3 (requires libmecab-dev system package).
    • Chinese: Uses jieba.
    • Spanish/Portuguese: Allows words to end with @ or @s to support gender-neutral spelling.
    from wordfreq import tokenize
    tokenize('l@s niñ@s', 'es')
    # ['l@s', 'niñ@s']
  10. Get a dictionary of all frequencies with get_frequency_dict()

    master
    If you need to perform many lookups and want to avoid the overhead of the word_frequency wrapper, use get_frequency_dict(lang, wordlist='best'). This returns a dictionary mapping words to their frequencies for the specified language and wordlist.