A package for extracting causal relationships
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.
Installation
Library can be installed with pip install causal-chains
Usage
- Initialize a
CausalChain
object with a list of chunks of text as input. - 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. - Run the
visualize
method to see the largest chain.
Methods:
The class "CausalChain" has the following methods:
-
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. -
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. -
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. -
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file causal_chains-1.0.2.tar.gz
.
File metadata
- Download URL: causal_chains-1.0.2.tar.gz
- Upload date:
- Size: 2.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4dd398d9a864b975f9981edef616c979aa7b52a8b97ec3e9d7aa4846cc8dd84b |
|
MD5 | 63cab4ddf47b9edfd60318d6fbedbccd |
|
BLAKE2b-256 | e72350b7d5f97bcea89612f0228466cf4880635e382d31c043038531559d71f9 |
File details
Details for the file causal_chains-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: causal_chains-1.0.2-py3-none-any.whl
- Upload date:
- Size: 2.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 367b986336cf7f564a0f904bf320d7f30200a6306fa08350e146f593b7e816ea |
|
MD5 | 67e6edfe63bf7b7988467da988498a7a |
|
BLAKE2b-256 | d6be9aea973dd147a1afd9e6cdf67ec3f0ff2bf0768bf51a1f3421156ce23391 |