Skip to main content

license doc

Sent2Vec

How to Compute Sentence Embedding Fast and Flexible

In the past, we mostly encode text data using, for example, one-hot, term frequency, or TF-IDF (normalized term frequency). There are many challenges to these techniques. In recent years, the latest advancements give us the 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 various NLP tasks such as sentiment analysis and summarization. A flexible sentence embedding library is needed to prototype fast and contextualized. The open-source sent2vec Python package gives you the opportunity to do so. You currently have access to the standard encoders. More advanced techniques will be added in the later releases. Hope you can use this library in your exciting NLP projects.

Install

The sent2vec is developed to help you prototype faster. That is why it has many dependencies on other libraries. The module requires the following libraries:

  • gensim
  • numpy
  • spacy
  • transformers
  • torch

Then, it can be installed using pip:

pip3 install sent2vec

Usage

If you want to use the BERT language model (more specifically, distilbert-base-uncased) to encode sentences for downstream applications, 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()
vectorizer.bert(sentences)
vectors = vectorizer.vectors

Now, you can compute distance among sentences by using their vectors. In the example, as expected, the distance between vectors[0] and vectors[1] is less than the distance between vectors[0] and vectors[2].

from scipy import spatial

dist_1 = spatial.distance.cosine(vectors[0], vectors[1])
dist_2 = spatial.distance.cosine(vectors[0], vectors[2])
print('dist_1: {0}, dist_2: {1}'.format(dist_1, dist_2))
assert dist_1 < dist_2
# dist_1: 0.043, dist_2: 0.192

If you want to use a word2vec approach instead, you must first split sentences into lists of words using the sent2words method from the Splitter class. In this step, you can customize 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 word2vec method from the Vectorizer class. This method computes the average of vectors corresponding to the remaining words using the code below.

from sent2vec.vectorizer import Vectorizer
from sent2vec.splitter import Splitter

sentences = [
    "Alice is in the Wonderland.",
    "Alice is not in the Wonderland.",
]

splitter = Splitter()
splitter.sent2words(sentences=sentences, remove_stop_words=['not'], add_stop_words=[])
# print(splitter.words)
# [['alice', 'wonderland'], ['alice', 'not', 'wonderland']]
vectorizer = Vectorizer()
vectorizer.word2vec(splitter.words, pretrained_vectors_path= PRETRAINED_VECTORS_PATH)
vectors = vectorizer.vectors

As seen above, you can use different word2vec models by sending its path to the word2vec method. You can use a pre-trained model or a customized one.

And, that's pretty much it!

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sent2vec-0.2.4.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sent2vec-0.2.4-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file sent2vec-0.2.4.tar.gz.

File metadata

  • Download URL: sent2vec-0.2.4.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.5

File hashes

Hashes for sent2vec-0.2.4.tar.gz
Algorithm Hash digest
SHA256 00e5d35d7dc24530079764a281c4fec1fee3d301553a81bc6c8f7ea971614a5e
MD5 9d47384f56978c822e4130162f72b216
BLAKE2b-256 786a615c7ed2a32a9c031aeb044888f318928385ce2798d11b737dde8974f8ba

See more details on using hashes here.

File details

Details for the file sent2vec-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: sent2vec-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.5

File hashes

Hashes for sent2vec-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fa909c3f73cc01c0c18d5da1a75f2a504708feab836f44c9f9c7445be2dae20c
MD5 b1079a1d200842e064653b4ed8b871e7
BLAKE2b-256 d038c2b4f2ec022f32a4e84e115e86870e74e19efd94b7ad771b1a8e1a250cd8

See more details on using hashes here.

Release history Release notifications | RSS feed

0.3.0

2 files

This release

0.2.4 This release

2 files

0.2.3

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

0.0.2

2 files

0.0.1

2 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