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 format. The word2vec model was one of them. Plus, the BERT language model provides us a powerful sentence encoders.

The sentence encoding is an important step of many NLP projects. Plus, we believe that a flexible sent2vec library is needed to build a prototype 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. Note that we can use encoding, embedding, or vectorizing interchangeably in this context.

Library

The package requires the following libraries:

  • gensim
  • numpy
  • pandas
  • spacy
  • transformers
  • torch

This package is developed to make the prototyping become faster. That is why it has many dependencies on other libraries.

Install

It can be installed using pip:

pip3 install sent2vec

Usage

This is how to initialize the library and provide the data.

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(sentences)

If you want to use the the BERT language model to compute sentence embedding, you must use the code below.

vectors = vectorizer.bert()

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].

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(sentences)
vectorizer.sent2words(remove_stop_words=['not'], add_stop_words=[])
vectors = vectorizer.w2v()

And, that's pretty much it!

Metadata

Release files for sent2vec 0.1.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 sent2vec 0.1.0
File Size Uploaded
sent2vec-0.1.0.tar.gz 4.3 kB Details

Built distribution (wheel)

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

Total release size: 8.9 kB

Release files / sent2vec-0.1.0.tar.gz

Download URL sent2vec-0.1.0.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
836bb5763bc54c4bb2baa302182c6630685df10c9a3d4b8cfa670d27912e051a
BLAKE2b-256 checksum
How to use checksums
70a5ab94db966c24d417fff9ec68c863b26ab673c338e838d3a41962f1d6b741
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.0-py3-none-any.whl

Download URL sent2vec-0.1.0-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
76be02eff5214eafe9311b1a5ce46ba1eaadf6e5eea041b74a3dd46364527767
BLAKE2b-256 checksum
How to use checksums
70b4764e39df260f22a8a0b200cf0e516206fbb803fd3965978839a63fc0dd39
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 history Release notifications | RSS feed

0.3.0

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

This release

0.1.0 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

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