Skip to main content

A package for extracting causal chains

Project description

Causal Chain Extractor

This code implements a tool to extract causal chains from text by summarizing the text using the bart-cause-effect model from Hugging Face Transformers and then linking the causes and effects with cosine similarity calculated using the Sentence Transformer model.

image

Installation

Library can be installed with pip install causal-chains

Usage

  1. Initialize a CausalChain object with a list of chunks of text as input.
  2. Run the create_effects method to get the cause and effect pairs from the text and then link the events based on cosine similarity of their embeddings.
  3. Run the visualize method to see the largest chain.

Methods:

The class "CausalChain" has the following methods:

  1. create_effects: This method uses the pipeline from the transformers library to analyze the text for cause-effect relationships. The text is first split into chunks, with each chunk containing a maximum of 3 sentences. This is done to limit the output length and avoid memory issues. The pipeline then generates summaries of the cause-effect relationships in the text. The triggers and effects of these relationships are stored in separate lists.

  2. create_connections: This method uses the SentenceTransformer from the sentence-transformers library to encode the triggers and effects generated in the create_effects method. The method then calculates the cosine similarity between the triggers and effects to determine if there is a cause-effect relationship between them. If the cosine similarity score is above a certain threshold (default is 0.6), a connection is established between the trigger and effect. The connections between triggers and effects are stored in a dictionary.

  3. find_biggest_chain: This method finds the longest chain of cause-effect relationships in the text. It starts from an effect and follows the connections stored in the connections dictionary to find other effects that are connected to it. The method continues this process until there are no more connections or the chain reaches a certain length (default is 10). The longest chain found is stored in the biggest_chain attribute.

  4. visualize: This method displays a given chain using pydot.

Example Usage:

from causal_chain_extractor import CausalChain, util
import wikipedia 

text = wikipedia.page("ChristopherColumbus").content
chunks util.create_chunks(text)
cc = CausalChain(chunks,device=0)
cc.create_connections()
biggest_chain = cc.biggest_chain
cc.visualize(biggest_chain)

The display that this code produces is shown at the top of this page.

Google Colab Demo

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

causal_chains-1.0.7.tar.gz (466.7 kB view details)

Uploaded Source

Built Distribution

causal_chains-1.0.7-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file causal_chains-1.0.7.tar.gz.

File metadata

  • Download URL: causal_chains-1.0.7.tar.gz
  • Upload date:
  • Size: 466.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for causal_chains-1.0.7.tar.gz
Algorithm Hash digest
SHA256 3d395079e40982691adf73bda441acdf215dfb4ae44d9c5d1499b74be2dee20e
MD5 5d3674cecf07dc52712a90559890ba76
BLAKE2b-256 58fb7b0dd3bfe8865edb0b0616d57a49aa7fa0b132b0ac29b602ef814de7f8a3

See more details on using hashes here.

File details

Details for the file causal_chains-1.0.7-py3-none-any.whl.

File metadata

File hashes

Hashes for causal_chains-1.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 10e59a8180844630ac7856b1f7e1ea08a74dc394ba2527da91bca23ccff33b44
MD5 0cebe6bcc97644efcf8c9572a63d59c8
BLAKE2b-256 3976661f6659b88bb5abb7b79b32bff442f4fce5c863ef993c6d62df4b6bc07d

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