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
CausalChainobject with a list of chunks of text as input. - Run the
create_effectsmethod to get the cause and effect pairs from the text and then link the events based on cosine similarity of their embeddings. - Run the
visualizemethod 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.
Metadata
Release files for causal-chains 1.0.12
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| causal_chains-1.0.12.tar.gz | 4.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| causal_chains-1.0.12-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.4 kB
Release files / causal_chains-1.0.12.tar.gz
| Download URL | causal_chains-1.0.12.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9109dcb43afc25ee1882db4520e469199337e325b5d0f32a512efc2568b1638f
|
|
BLAKE2b-256 checksum How to use checksums |
f77f5c8f412411fca8a7f928a74a6a15d4a9d0dbb43ca6232b5fd806ef53f177
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.13
|
Release files / causal_chains-1.0.12-py3-none-any.whl
| Download URL | causal_chains-1.0.12-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2468dad2d99c579d8b56227b9ec343910d38844bd329594cb2758e26fc602c78
|
|
BLAKE2b-256 checksum How to use checksums |
d63fe2877f6e9918d2adfb9bd1dea58c3eee5ee5cf95af99d0f174cbc47f8250
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.13
|