TextRank4ZH Documentation

repository·master·Indexed 25 days ago

https://github.com/letiantian/textrank4zh

A Python implementation of the TextRank algorithm optimized for Chinese text. Provides functionality for keyword extraction, keyphrase extraction, and automatic text summarization via the TextRank4Keyword and TextRank4Sentence classes.

Tokens
722
Snippets
2
Records
6
Agent score
36%

What's inside textrank4zh

  1. Install TextRank4ZH

    master

    You can install TextRank4ZH using pip or by running the setup.py script. For Python 3, ensure you use python3 and pip3 commands.

    Using pip (Recommended):

    # Install for current user
    pip3 install textrank4zh --user
    
    # System-wide install
    sudo pip3 install textrank4zh

    Using setup.py:

    # Install for current user
    python3 setup.py install --user
    
    # System-wide install
    sudo python3 setup.py install
  2. Generate Summaries with TextRank4Sentence

    master

    Use the TextRank4Sentence class to extract the most important sentences from a text to create a summary.

    Methods:

    • analyze(text, lower=True, source='all_filters'): Processes the input text.
    • get_key_sentences(num=3): Returns a list of sentence objects. Each object contains:
      • .index: The position of the sentence in the original text.
      • .weight: The importance weight of the sentence.
      • .sentence: The actual sentence text.
  3. Extract Keywords and Keyphrases with TextRank4Keyword

    master

    Use the TextRank4Keyword class to extract important words and adjacent keyword groups (keyphrases) from Chinese text.

    Methods:

    • analyze(text, lower=True, window=2): Processes the input text.
      • In Python 2, text must be a str (UTF-8) or unicode object.
      • In Python 3, text must be a bytes or str object.
    • get_keywords(num, word_min_len=1): Returns a list of keyword objects containing .word and .weight.
    • get_keyphrases(keywords_num=20, min_occur_num=2): Returns a list of keyphrase strings.
    from textrank4zh import TextRank4Keyword
    
    text = "你的中文文本"
    tr4w = TextRank4Keyword()
    tr4w.analyze(text=text, lower=True, window=2)
    
    # Get keywords
    for item in tr4w.get_keywords(20, word_min_len=1):
        print(item.word, item.weight)
    
    # Get keyphrases
    for phrase in tr4w.get_keyphrases(keywords_num=20, min_occur_num=2):
        print(phrase)
  4. Access Processed Text Formats

    master

    Both TextRank4Keyword and TextRank4Sentence break down the input text into four internal formats after calling .analyze():

    1. sentences: A list of sentences.
    2. words_no_filter: A 2D list of words (tokenized sentences) without any filtering.
    3. words_no_stop_words: A 2D list of words after removing stop words.
    4. words_all_filters: A 2D list of words after removing stop words and filtering by specific parts of speech (POS).