Skip to main content

A Generic Sentence Embedding Library

In natural language processing, we need to encode text data. In the past, we mostly use encoders such as one-hot, term frequency, or TF-IDF (normalized term frequency). There are many challenges with these techniques. In the recent years, the latest advancements give us opportunity to encode sentences or words in more meaningful formats. The word2vec technique and BERT language model are two important ones.

The sentence embedding is an important step of many NLP projects from sentiment analysis to summarization. We believe that a flexible sentence embedding library is needed to build prototypes fast. That is why we have initiated this project. In the early releases, you will have access to the standard encoders. We will add more curated techniques in the later releases. Hope you can use this library in your exciting NLP projects.

Library

The package requires the following libraries:

  • gensim
  • numpy
  • spacy
  • transformers
  • torch

The sent2vec package is developed to help you prototype faster. That is why it has many dependencies on other libraries.

Install

It can be installed using pip:

pip3 install sent2vec

Usage

If you want to use the the BERT language model (more specifically, distilbert-base-uncased) to compute sentence embedding, you must use the code below.

from sent2vec.vectorizer import Vectorizer

sentences = [
    "This is an awesome book to learn NLP.",
    "DistilBERT is an amazing NLP model.",
    "We can interchangeably use embedding, encoding, or vectorizing.",
]
vectorizer = Vectorizer()
vectors = vectorizer.bert(sentences)

Having the corresponding vectors, you can compute distance among vectors. Here, as expected, the distance between vectors[0] and vectors[1] is less than the distance between vectors[0] and vectors[2].

dist_1 = cosine_distance(vectors[0], vectors[1])
dist_2 = cosine_distance(vectors[0], vectors[2])

print('dist_1: {}'.format(dist_1), 'dist_2: {}'.format(dist_2))
dist_1: 0.043, dist_2: 0.192

If you want to use a word2vec approach instead, you must first split sentences to lists of words using the sent2words method. In this stage, you can customized the list of stop-words by adding or removing to/from the default list. When you extract the most important words in sentences, you can compute the sentence embeddings using the w2v method. This method computes the average of vectors corresponding to the remaining words using the code bleow.

sentences = [
    "Alice is in the Wonderland.",
    "Alice is not in the Wonderland.",
]
vectorizer = Vectorizer()
words = vectorizer.sent2words(sentences, remove_stop_words=['not'], add_stop_words=[])
vectors = vectorizer.w2v(words)

And, that's pretty much it!

Metadata

Release files for sent2vec 0.1.3

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

Source distribution (sdist)

Source distribution for sent2vec 0.1.3
File Size Uploaded Provenance Actions
sent2vec-0.1.3.tar.gz 4.3 kB Without provenance Details for sent2vec-0.1.3.tar.gz

Built distribution (wheel)

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

Total release size: 8.9 kB

Release files / sent2vec-0.1.3.tar.gz

Download URL sent2vec-0.1.3.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
187e035110401291bc4d47a0eee92d1e3f515d534def858bd0ea2ad4d370993a
BLAKE2b-256 checksum
How to use checksums
5835dce56361945f43d365c3071858feb9df98ab3f90dd443ed62685c87c4029
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0.post20200714 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.3

Release files / sent2vec-0.1.3-py3-none-any.whl

Download URL sent2vec-0.1.3-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
be8811ab4407ccb32897902152a892234aa9bd086cf7efb812565c90e44b2a17
BLAKE2b-256 checksum
How to use checksums
c747de9f72a6eae0a30304b81f927fc848991a9aa341d3ac503381b17c91d42d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0.post20200714 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.3

Security for sent2vec 0.1.3

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