Skip to main content

No project description provided

Project description

to_paragraphs

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
import numpy as np
from transformers import AutoTokenizer, AutoModel


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):
    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)

    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)

Project details


Download files

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

Source Distribution

to_paragraphs-0.1.4.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

to_paragraphs-0.1.4-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

Details for the file to_paragraphs-0.1.4.tar.gz.

File metadata

  • Download URL: to_paragraphs-0.1.4.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.10.6 Linux/5.15.0-69-generic

File hashes

Hashes for to_paragraphs-0.1.4.tar.gz
Algorithm Hash digest
SHA256 c7f6f2ad8bc384e1b39ce2b6811b1aabd2eb1c54b0837afe022424dc2f6b8bd0
MD5 fac25d2b31dbe8e1818f5c4fe3d4ed48
BLAKE2b-256 7f42a96a432631e8b88af58e2a891c0fa4fc1d77c0684f4d86e2438616ca6358

See more details on using hashes here.

File details

Details for the file to_paragraphs-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: to_paragraphs-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 3.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.10.6 Linux/5.15.0-69-generic

File hashes

Hashes for to_paragraphs-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d666a8eb693257f84b7eb4f92ee1b2f6d3eace681a61c483b5650cf0f9386fc3
MD5 62fc42f83b81bf53de4f63ed92c2610c
BLAKE2b-256 67d931f851d8c768bc04f9020610f6fa4cda38868a42ac2ad743473ed6a3ad3e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page