Skip to main content

A spaCy component for connecting entities and building relational graphs in text.

Project description

spaCy Aligner

The spacy_aligner is a custom spaCy component designed to connect entities in text and build relational graphs based on these connections. It utilizes both spaCy's powerful NLP capabilities and NetworkX for graph management, making it an excellent tool for complex entity relationship analysis in large texts.

Installation

To install spacy_aligner, you will need Python 3.7 or newer. You can install this package directly from PyPI (once uploaded) or through the repository if it is hosted on a site like GitHub.

pip install spacy_aligner

Or, if you have the source code:

git clone https://github.com/yourgithub/spacy_aligner
cd spacy_aligner
python setup.py install

Usage

Here's a quick start example to use spacy_aligner:

import spacy
from spacy_aligner.pipeline import Aligner
import json
import networkx as nx
import matplotlib.pyplot as plt

# Load external data


links = {
        "PERSON": {
            "Elizabeth": ["Liz", "Lizzie", "Beth", "Betsy", "Eliza"],
            "William": ["Will", "Bill", "Billy", "Liam"],
                }
        }

# Load the spaCy model
nlp = spacy.load("en_core_web_lg")

# Add the custom pipeline component to the spaCy pipeline
nlp.add_pipe("aligner", config={"links": links})

text = """Elizabeth Jenkins went to school. She works at Mattingly Autoparts.
        Liz is 20. She also goes by Lizzie. Mrs. Jenkins teaches students.
        Once she completes her PhD, she will be Dr. Elizabeth P. Jenkins.
        William also goes by Will.
        His full name is William Mattingly."""

# Process the text
doc = nlp(text)

# Access the generated graph
G = doc._.graph

Visualization

The spacy_aligner also includes a function to visualize the relationship graph generated:

def visualize_graph(G):
    pos = nx.spring_layout(G)
    nx.draw(G, pos, with_labels=True, node_color='lightblue', node_size=500, edge_color='gray', linewidths=1, font_size=12)
    plt.show()

visualize_graph(doc._.graph)

This function uses matplotlib to plot the graph, showing the relationships between detected entities based on the document text.

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

spacy_aligner-0.0.1.tar.gz (3.1 kB view hashes)

Uploaded Source

Built Distribution

spacy_aligner-0.0.1-py3-none-any.whl (3.4 kB view hashes)

Uploaded Python 3

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