Skip to main content

WordWise

| |     / /___  _________/ / |     / (_)_______ 
| | /| / / __ \/ ___/ __  /| | /| / / / ___/ _ \
| |/ |/ / /_/ / /  / /_/ / | |/ |/ / (__  )  __/
|__/|__/\____/_/   \__,_/  |__/|__/_/____/\___/ 

WordWise is a minimal keyword extraction library that uses quality contextual embeddings generated by Sentence-BERT to extract keywords from blocks of text.

Installation

WordWise is available on PyPI.

pip install wordwise

To clone this repository, run

git clone https://github.com/jaketae/wordwise.git

Quickstart

At the core of WordWise is the Extractor class, which can be configured to generate keywords from some given text. The Extractor can be initialized and used out-of-the-box with minimal configuration as follows.

from wordwise import Extractor

extractor = Extractor()
keywords = extractor.generate(text)

For advanced users, the Extractor class, as well as the .generate() method, can be configured in a more granular fashion to induce specific behaviors. For instance, the underlying spaCy backend can be specified.

extractor = Extractor(spacy_model="en_core_web_lg")

By default, the Extractor will only generate the top 5 most relevant keywords. This behavior can be changed as follows.

# generate 10 keywords
keywords = extractor.generate(text, top_k=10)

Demo

Below is an example text adapted from the Wikipedia article on supervised learning.

text = """
         Supervised learning is the machine learning task of
         learning a function that maps an input to an output based
         on example input-output pairs.[1] It infers a function
         from labeled training data consisting of a set of
         training examples.[2] In supervised learning, each
         example is a pair consisting of an input object
         (typically a vector) and a desired output value (also
         called the supervisory signal). A supervised learning
         algorithm analyzes the training data and produces an
         inferred function, which can be used for mapping new
         examples. An optimal scenario will allow for the algorithm
         to correctly determine the class labels for unseen
         instances. This requires the learning algorithm to
         generalize from the training data to unseen situations
         in a 'reasonable' way (see inductive bias).
      """

The extractor selects the three most relevant keywords from the block of text.

>>> from wordwise import Extractor
>>> extractor = Extractor()
>>> keywords = extractor.generate(text, 3)
>>> print(keywords)
['algorithm', 'learning', 'supervised learning']

How it Works

Using spaCy, the Extractor object generates n-gram candidate noun phrases from the provided block of text. By default, it only considers uni-grams or bi-grams, since only rarely are keywords go beyond three words. Then, using a BERT model, it generates contextual embeddings for both the provided text and the n-gram keywords. Using cosine similarity as a distance function, it extracts the top_k candidate keywords that are most similar to the embedding of the inputted text.

For a more detailed write-up, please refer to my blog post here.

Credit

WordWise was largely inspired by KeyBERT, a library that similarly uses sentence embeddings for keyword extraction. WordWise also relies on NLP libraries, such as spaCy and HuggingFace transformers, without which its development would not have been possible.

License

Released under the MIT License.

Metadata

Release files for wordwise 0.0.5

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

Source distribution (sdist)

Source distribution for wordwise 0.0.5
File Size Uploaded Provenance
wordwise-0.0.5.tar.gz 5.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for wordwise 0.0.5
File Interpreter ABI Platform
wordwise-0.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 11.2 kB

Release files / wordwise-0.0.5.tar.gz

Download URL wordwise-0.0.5.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d24970ba2aa7d70a490b91e1da083eea3700c6902473e5c23654313980f77f25
BLAKE2b-256 checksum
How to use checksums
ab84f2ba805f3f4505435327f9a632ec35a4c342bfb50b7a0a84311113e2cde1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.7.9

Release files / wordwise-0.0.5-py3-none-any.whl

Download URL wordwise-0.0.5-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0d424b4b4e140c284186427494da21f79af533281ce0d2f513828821580e7ba8
BLAKE2b-256 checksum
How to use checksums
72d3f0adcc85c012940ddc06427389563735c6fec69ef4b8cacc087d4486b9c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.7.9

Security for wordwise 0.0.5

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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