pytextrank

repository·main·Indexed 24 days ago

https://github.com/derwenai/pytextrank

A Python implementation of TextRank designed as a spaCy pipeline extension. It provides graph-based natural language processing capabilities, including phrase extraction, extractive summarization, and concept inference. The library implements several textgraph algorithms such as TextRank, PositionRank, Biased TextRank, and TopicRank, leveraging spaCy 3.x for preprocessing.

Tokens
12.1K
Snippets
29
Records
81
Agent score
79%

What's inside pytextrank

  1. Overview of PyTextRank

    main

    PyTextRank is a Python implementation of the TextRank algorithm designed as a spaCy pipeline extension. It uses graph-based natural language processing (textgraph algorithms) to perform tasks such as:

    • Phrase Extraction: Identifying the top-ranked phrases within a text document.
    • Extractive Summarization: Providing low-cost summaries of text documents.
    • Concept Inference: Helping transform unstructured text into more structured representations.

    It implements several textgraph algorithms, including TextRank, PositionRank, Biased TextRank, and TopicRank. It leverages spaCy 3.x for preprocessing (like noun chunking and NER) and uses lemmatization instead of stemming.

  2. Extractive Summarization in PyTextRank

    main

    PyTextRank provides an implementation of extractive summarization.

    While more advanced (and often higher-cost) methods like abstractive summarization (which uses deep learning and knowledge graphs) exist, PyTextRank offers a lower-cost alternative that may better suit specific engineering and policy trade-offs in various use cases.

  3. How PyTextRank works using a Lemma Graph

    main

    PyTextRank operates by constructing a lemma graph to represent links between candidate phrases (such as unrecognized entities) and supporting language within a text.

    The Process:

    1. Annotation: It relies on spaCy pipeline annotations, specifically part-of-speech and lemmatized tokens. When combined with disambiguated word sense, tokens can be mapped to concepts.
    2. Graph Construction: The TextRank algorithm applies a sliding window across tokens in a parsed sentence. It constructs a graph where lemmatized tokens that are neighbors within the window are linked. Each unique lemma in the graph collects links from repeated instances.
    3. Ranking: A centrality measure is calculated for each node in the graph, allowing nouns to be ranked in descending order.
    4. Phrase Agglomeration: An additional pass uses both noun chunks and named entities to group adjacent nouns into ranked phrases.
  4. Improving results by enriching the Lemma Graph

    main

    The quality of phrase ranking in PyTextRank can be improved by enriching the lemma graph before the ranking stage.

    Common methods for enrichment include:

    • Coreference Resolution: Linking different mentions of the same entity.
    • Semantic Relations: Using knowledge graphs or thesauri to infer links between words that are not explicitly linked in the text (e.g., using hyponymy/hypernymy).

    Examples of external resources that can be used for this purpose include WordNet and DBpedia.

  5. How TopicRank works

    main

    The TopicRank algorithm identifies key topics in a document through a multi-stage process. It is deployed as a spaCy pipeline component.

    Algorithm Steps:

    1. Preprocessing: Sentence segmentation, word tokenization, and POS tagging.
    2. Candidate extraction: Extracts noun chunks (sequences of nouns and adjectives).
    3. Candidate clustering: Uses Hierarchical Agglomerative Clustering (with average linking) based on lemma overlap (similarity threshold > 25%).
    4. Candidate ranking: Applies TextRank on a complete graph where topics are nodes, with edge weights favoring topics appearing closer together.
    5. Candidate selection: Selects the first occurring keyphrase from each topic to represent it.

    Note: TopicRank is not instantiated directly; you must use its factory class.

  6. Understand Summarization types: Extractive vs Abstractive

    main

    PyTextRank relates to the field of summarization, which can be categorized into two main approaches:

    • extractive summarization: Summarizing source text by identifying a subset of the most important sentences as excerpts and generating them verbatim.
    • abstractive summarization: Generating a short, concise summary that captures salient ideas, potentially using new phrases and sentences that do not appear in the original source text.
  7. Understand TextRank concepts: Lemma Graph and Phrase Extraction

    main

    In the context of the TextRank algorithm used by PyTextRank, two key concepts are used:

    • phrase extraction: The process of selecting representative phrases from a document as its characteristic entities (distinct from simple keyword analysis).
    • lemma graph: A graph data structure used to represent links among phrases extracted from a source text during the operation of the TextRank algorithm.
  8. How PyTextRank works as a spaCy extension

    main
    PyTextRank is implemented as a spaCy pipeline extension. It integrates graph-based natural language algorithms into the standard spaCy nlp workflow. By adding the textrank component to your pipeline using nlp.add_pipe("textrank"), the library populates the doc._.phrases attribute with ranked linguistic entities, enabling tasks like phrase extraction, extractive summarization, and concept inference.
  9. Understand textgraphs in NLP

    main
    In the context of Natural Language Processing (NLP), textgraphs refer to the use of graph algorithms applied to a graph representation of a source text. This approach allows for analyzing text structure through graph-based relationships.
  10. Use pytextrank to extract ranked phrases

    main

    PyTextRank integrates directly into the spaCy pipeline. After adding the textrank component to your nlp object, you can process text and access ranked phrases via the doc._.phrases attribute. Each phrase object provides access to its text, rank, frequency count, and associated noun chunks.

    import spacy
    import pytextrank
    
    text = "Compatibility of systems of linear constraints over the set of natural numbers..."
    
    # Load a spaCy model
    nlp = spacy.load("en_core_web_sm")
    
    # Add PyTextRank to the spaCy pipeline
    nlp.add_pipe("textrank")
    
    # Process the text
    doc = nlp(text)
    
    # Examine the top-ranked phrases
    for phrase in doc._.phrases:
        print(phrase.text)
        print(phrase.rank, phrase.count)
        print(phrase.chunks)
  11. Set up the local development environment

    main

    If you are contributing to the pytextrank library, you can set up the build environment by installing the development requirements and configuring pre-commit hooks.

    1. Install development dependencies: python3 -m pip install -r requirements-dev.txt

    2. Install pre-commit hooks: pre-commit install git config --local core.hooksPath .git/hooks/

    python3 -m pip install -r requirements-dev.txt
    pre-commit install
    git config --local core.hooksPath .git/hooks/