to_paragraphs
Note, the code is currently ~slow and messy. It's a work in progress.
Extracts paragraphs from a string, using the semantic similarity between sentences to determine paragraph boundaries. This tends to work better than the naive approach of splitting on newlines.
Installation
pip install to_paragraphs
Example
from to_paragraphs import to_paragraphs
text = """
The biosphere includes everything living on Earth it is also known as ecosphere. Currently the biosphere has a biomass (or amount of living things) at around 1900 gigatonnes of carbon. It is not certain exactly how thick the biosphere is, though scientists predict that it is around 12,500 meters. The biosphere extends to the upper areas of the atmosphere, including birds and insects.
Pizza is an Italian food that was created in Italy (The Naples area). It is made with different toppings. Some of the most common toppings are cheese, sausages, pepperoni, vegetables, tomatoes, spices and herbs and basil. These toppings are added over a piece of bread covered with sauce. The sauce is most often tomato-based, but butter-based sauces are used, too. The piece of bread is usually called a "pizza crust". Almost any kind of topping can be put over a pizza. The toppings used are different in different parts of the world. Pizza comes from Italy from Neapolitan cuisine. However, it has become popular in many parts of the world.
"""
def embed_fn(content):
# ... some function that returns an embedding for a string
pass
paragraphs = to_paragraphs(text, embed_fn=embed_fn)
for paragraph in paragraphs:
print(paragraph) # prints the paragraph about the biosphere, then the paragraph about pizza
Example embedding function
import torch
import torch.nn.functional as F
from transformers import AutoTokenizer, AutoModel
CACHE_PATH = '/tmp/transformers'
TOKENIZER = AutoTokenizer.from_pretrained('sentence-transformers/all-MiniLM-L6-v2', cache_dir=CACHE_PATH)
MODEL = AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L6-v2', cache_dir=CACHE_PATH)
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0] # First element of model_output contains all token embeddings
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
def embed(sentences):
encoded_input = TOKENIZER(sentences, padding=True, truncation=True, return_tensors='pt')
with torch.no_grad():
model_output = MODEL(**encoded_input)
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
return F.normalize(sentence_embeddings, p=2, dim=1)
Metadata
Release files for to-paragraphs 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| to_paragraphs-0.2.1.tar.gz | 3.0 kB | Without provenance | Details for to_paragraphs-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| to_paragraphs-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for to_paragraphs-0.2.1-py3-none-any.whl |
Total release size: 6.5 kB
Release files / to_paragraphs-0.2.1.tar.gz
| Download URL | to_paragraphs-0.2.1.tar.gz |
|---|---|
| Size | 3.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
193ba22d2f369e279af5b48c61a0f1a03c79d49cf24aebbeac8a0d0ace5be920
|
|
BLAKE2b-256 checksum How to use checksums |
82a0cb460e7d08957180c470a97a3e5725df0fbd54e19443dae0889dd67507b8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.10.6 Linux/5.15.0-70-generic
|
Release files / to_paragraphs-0.2.1-py3-none-any.whl
| Download URL | to_paragraphs-0.2.1-py3-none-any.whl |
|---|---|
| Size | 3.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
eab61baa5727ef11ed5c8efa08b0146be1c0c0472b8c88b99f24eb24d1fafa01
|
|
BLAKE2b-256 checksum How to use checksums |
8b336a7cf56410e16a28215ff4b1c6e1fdc23c566db027b72b3d16874d77fb6d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.10.6 Linux/5.15.0-70-generic
|
Security for to-paragraphs 0.2.1
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.