### Install TextRank4ZH (System) Source: https://github.com/letiantian/textrank4zh/blob/master/README.md Install the library system-wide using sudo privileges. ```bash sudo python setup.py install ``` ```bash sudo pip install textrank4zh ``` -------------------------------- ### Install TextRank4ZH (User) Source: https://github.com/letiantian/textrank4zh/blob/master/README.md Install the library for the current user without root privileges. ```bash python setup.py install --user ``` ```bash pip install textrank4zh --user ``` -------------------------------- ### Get TopN key sentences for summarization Source: https://context7.com/letiantian/textrank4zh/llms.txt Retrieve a list of the most important sentences, sorted by weight in descending order. Filter sentences by minimum length. Each sentence includes its index, content, and weight. ```python import codecs from textrank4zh import TextRank4Sentence text = codecs.open('article.txt', 'r', 'utf-8').read() tr4s = TextRank4Sentence() tr4s.analyze(text=text, lower=True, source='all_filters') # 获取最重要的3个句子作为摘要(句子长度≥10) key_sentences = tr4s.get_key_sentences(num=3, sentence_min_len=10) for item in key_sentences: print(f'[位置 {item.index}] 权重={item.weight:.4f}') print(f' {item.sentence}') print() # 示例输出: # [位置 0] 权重=0.0710 # 中新网北京12月1日电(记者 张曦) 30日晚,高圆圆和赵又廷在京举行答谢宴... # # [位置 6] 权重=0.0541 # 高圆圆身穿粉色外套,看到大批记者在场露出娇羞神色... ``` -------------------------------- ### Uninstall TextRank4ZH Source: https://github.com/letiantian/textrank4zh/blob/master/README.md Remove the installed textrank4zh package. ```plain pip uninstall textrank4zh ``` -------------------------------- ### Get TopN keywords from analyzed text Source: https://context7.com/letiantian/textrank4zh/llms.txt Retrieve a list of the most important keywords, sorted by weight in descending order. Filter keywords by minimum length. Each keyword includes its word and weight. ```python from textrank4zh import TextRank4Keyword text = "中新网北京12月1日电,高圆圆和赵又廷在京举行答谢宴,诸多明星现身捧场,其中包括张杰、谢娜夫妇、何炅等。" tr4w = TextRank4Keyword() tr4w.analyze(text=text, lower=True, window=2) # 获取前10个长度≥2的关键词 keywords = tr4w.get_keywords(num=10, word_min_len=2) for item in keywords: print(f'{item.word} 权重: {item.weight:.6f}') # 示例输出: # 答谢 权重: 0.021559 # 高圆圆 权重: 0.020220 # 谢娜 权重: 0.013361 # 张杰 权重: 0.009558 # 赵又廷 权重: 0.014035 ``` -------------------------------- ### Get keyphrases from analyzed text Source: https://context7.com/letiantian/textrank4zh/llms.txt Extract multi-word keyphrases that appear frequently in the text. This function identifies phrases formed by adjacent keywords and requires a minimum occurrence count. ```python from textrank4zh import TextRank4Keyword text = """ 支持向量机是一种监督学习模型,支持向量机广泛应用于分类和回归分析。 支持向量机的核心是寻找最优超平面。支持向量是距离超平面最近的样本点。 """ tr4w = TextRank4Keyword() tr4w.analyze(text=text, lower=True, window=2) # 获取至少出现2次的关键短语,基于前20个关键词构造 keyphrases = tr4w.get_keyphrases(keywords_num=20, min_occur_num=2) print('关键短语:', keyphrases) # 示例输出:['支持向量机', '支持向量'] ``` -------------------------------- ### Initialize TextRank4Sentence with default settings Source: https://context7.com/letiantian/textrank4zh/llms.txt Create an instance of the sentence summarizer using default configurations for stop words and delimiters. ```python from textrank4zh import TextRank4Sentence # 默认配置 tr4s = TextRank4Sentence() ``` -------------------------------- ### Initialize TextRank4Keyword with default settings Source: https://context7.com/letiantian/textrank4zh/llms.txt Create an instance of the keyword extractor using default configurations for stop words and allowed part-of-speech tags. ```python from textrank4zh import TextRank4Keyword # 使用默认配置 tr4w = TextRank4Keyword() ``` -------------------------------- ### Initialize TextRank4Keyword with custom settings Source: https://context7.com/letiantian/textrank4zh/llms.txt Instantiate the keyword extractor with a custom stop words file, a specific list of allowed part-of-speech tags, and custom sentence delimiters. ```python from textrank4zh import TextRank4Keyword # 使用自定义停止词文件和词性白名单 tr4w_custom = TextRank4Keyword( stop_words_file='/path/to/my_stopwords.txt', allow_speech_tags=['n', 'nr', 'ns', 'v'], # 只保留名词和动词 delimiters=['。', '!', '?', '\n'] # 自定义分句符 ) ``` -------------------------------- ### TextRank4Keyword.__init__ Source: https://context7.com/letiantian/textrank4zh/llms.txt Initializes the keyword extraction instance. Allows customization of stop words file, allowed speech tags, and sentence delimiters. ```APIDOC ## TextRank4Keyword.__init__ ### Description Initializes the keyword extraction instance. Allows customization of stop words file, allowed speech tags, and sentence delimiters. ### Method __init__ ### Parameters - **stop_words_file** (str, optional) - Path to a custom stop words file. Defaults to an internal file. - **allow_speech_tags** (list[str], optional) - List of speech tags to allow. Defaults to a comprehensive list including nouns, verbs, etc. - **delimiters** (list[str], optional) - List of characters used to split sentences. Defaults to standard punctuation like '。', '!', '?', '\n'. ``` -------------------------------- ### TextRank4Sentence.__init__ Source: https://context7.com/letiantian/textrank4zh/llms.txt Initializes the sentence summarization instance. Similar to TextRank4Keyword, it allows customization of stop words, allowed speech tags, and sentence delimiters. ```APIDOC ## TextRank4Sentence.__init__ ### Description Initializes the sentence summarization instance. Similar to TextRank4Keyword, it allows customization of stop words, allowed speech tags, and sentence delimiters. ### Method __init__ ### Parameters - **stop_words_file** (str, optional) - Path to a custom stop words file. Defaults to an internal file. - **allow_speech_tags** (list[str], optional) - List of speech tags to allow. Defaults to a comprehensive list. - **delimiters** (list[str], optional) - List of characters used to split sentences. Defaults to standard punctuation. ``` -------------------------------- ### Extract Keywords, Keyphrases, and Summary Source: https://github.com/letiantian/textrank4zh/blob/master/README.md Demonstrates how to use TextRank4Keyword and TextRank4Sentence to extract keywords, keyphrases, and generate a summary from a given text. Ensure the text is properly encoded (UTF-8 for Python 2, bytes or str for Python 3). ```python #-*- encoding:utf-8 -*- from __future__ import print_function import sys try: reload(sys) sys.setdefaultencoding('utf-8') except: pass import codecs from textrank4zh import TextRank4Keyword, TextRank4Sentence text = codecs.open('../test/doc/01.txt', 'r', 'utf-8').read() tr4w = TextRank4Keyword() tr4w.analyze(text=text, lower=True, window=2) # py2中text必须是utf8编码的str或者unicode对象,py3中必须是utf8编码的bytes或者str对象 print( '关键词:' ) for item in tr4w.get_keywords(20, word_min_len=1): print(item.word, item.weight) print() print( '关键短语:' ) for phrase in tr4w.get_keyphrases(keywords_num=20, min_occur_num= 2): print(phrase) tr4s = TextRank4Sentence() tr4s.analyze(text=text, lower=True, source = 'all_filters') print() print( '摘要:' ) for item in tr4s.get_key_sentences(num=3): print(item.index, item.weight, item.sentence) # index是语句在文本中位置,weight是权重 ``` -------------------------------- ### Initialize TextRank4Sentence with custom delimiters Source: https://context7.com/letiantian/textrank4zh/llms.txt Instantiate the sentence summarizer with custom sentence delimiters, useful for handling non-standard text formats. It uses built-in stop words by default. ```python from textrank4zh import TextRank4Sentence # 自定义分句符(适合处理非标准文本) tr4s_custom = TextRank4Sentence( stop_words_file=None, # 使用内置停止词 delimiters=['。', '!', '?', ';', '\n'] ) ``` -------------------------------- ### Analyze text for sentence summarization Source: https://context7.com/letiantian/textrank4zh/llms.txt Process text to calculate sentence similarity, build a graph, and apply PageRank for sentence scoring. Options include specifying the word source for similarity calculation and providing a custom similarity function. ```python import codecs from textrank4zh import TextRank4Sentence text = codecs.open('article.txt', 'r', 'utf-8').read() tr4s = TextRank4Sentence() tr4s.analyze( text=text, lower=True, source='all_filters', # 使用词性过滤后的词计算相似度 pagerank_config={'alpha': 0.85} ) # 也可传入自定义相似度函数 from textrank4zh import util def my_sim(words1, words2): set1, set2 = set(words1), set(words2) if not set1 or not set2: return 0.0 return len(set1 & set2) / len(set1 | set2) # Jaccard相似度 tr4s.analyze(text=text, lower=True, sim_func=my_sim) ``` -------------------------------- ### TextRank4Keyword.analyze Source: https://context7.com/letiantian/textrank4zh/llms.txt Analyzes the input text to compute keyword weights using TextRank. Configurable parameters include window size, vertex/edge source for graph construction, and PageRank algorithm settings. ```APIDOC ## TextRank4Keyword.analyze ### Description Analyzes the input text to compute keyword weights using TextRank. Configurable parameters include window size, vertex/edge source for graph construction, and PageRank algorithm settings. ### Method analyze ### Parameters - **text** (str) - The input text to analyze. - **lower** (bool, optional) - Whether to convert English words to lowercase. Defaults to False. - **window** (int, optional) - The size of the co-occurrence window for building the graph. Defaults to 2. - **vertex_source** (str, optional) - Specifies the filtering level for words used as graph vertices. Options include 'all_filters', 'no_stop_words', 'no_filter'. Defaults to 'all_filters'. - **edge_source** (str, optional) - Specifies the filtering level for words used to form graph edges. Options include 'all_filters', 'no_stop_words', 'no_filter'. Defaults to 'no_stop_words'. - **pagerank_config** (dict, optional) - Configuration dictionary for the PageRank algorithm, e.g., {'alpha': 0.85}. ### Request Example ```python import codecs from textrank4zh import TextRank4Keyword text = codecs.open('article.txt', 'r', 'utf-8').read() tr4w = TextRank4Keyword() tr4w.analyze( text=text, lower=True, window=3, vertex_source='all_filters', edge_source='no_stop_words', pagerank_config={'alpha': 0.85} ) ``` ``` -------------------------------- ### Analyze text for keyword extraction Source: https://context7.com/letiantian/textrank4zh/llms.txt Process input text to perform word segmentation, graph construction, and PageRank scoring for keyword identification. Configure parameters like window size, vertex/edge sources, and PageRank settings. ```python import codecs from textrank4zh import TextRank4Keyword text = codecs.open('article.txt', 'r', 'utf-8').read() tr4w = TextRank4Keyword() tr4w.analyze( text=text, lower=True, # 英文转小写 window=3, # 窗口大小:3个单词内视为共现 vertex_source='all_filters', # 节点来源:经词性过滤的词 edge_source='no_stop_words', # 边来源:去停止词后的词 pagerank_config={'alpha': 0.85} # PageRank阻尼系数 ) # 分析后可直接访问中间结果 print('句子列表:', tr4w.sentences) # ['这间酒店位于北京东三环,里面摆放很多雕塑,文艺气息十足', '答谢宴于晚上8点开始'] print('无过滤分词:', tr4w.words_no_filter) # [['这', '间', '酒店', '位于', '北京', ...], ['答谢', '宴于', ...]] print('去停止词:', tr4w.words_no_stop_words) # [['间', '酒店', '位于', '北京', '东三环', ...], ...] print('词性过滤后:', tr4w.words_all_filters) # [['酒店', '位于', '北京', '东三环', '摆放', '雕塑', '文艺', '气息'], ...] ``` -------------------------------- ### TextRank4Keyword.get_keywords Source: https://context7.com/letiantian/textrank4zh/llms.txt Retrieves the top N keywords from the analyzed text, sorted by weight. Allows filtering by minimum word length. ```APIDOC ## TextRank4Keyword.get_keywords ### Description Retrieves the top N keywords from the analyzed text, sorted by weight. Allows filtering by minimum word length. ### Method get_keywords ### Parameters - **num** (int) - The number of top keywords to return. - **word_min_len** (int, optional) - The minimum length of words to be considered as keywords. Defaults to 1. ### Response #### Success Response (200) - **keywords** (list[AttrDict]) - A list of keyword objects, each containing 'word' (str) and 'weight' (float). ### Request Example ```python from textrank4zh import TextRank4Keyword text = "中新网北京12月1日电,高圆圆和赵又廷在京举行答谢宴,诸多明星现身捧场,其中包括张杰、谢娜夫妇、何炅等。" tr4w = TextRank4Keyword() tr4w.analyze(text=text, lower=True, window=2) keywords = tr4w.get_keywords(num=10, word_min_len=2) for item in keywords: print(f'{item.word} 权重: {item.weight:.6f}') ``` ``` -------------------------------- ### Sort Sentences by Weight with sort_sentences Source: https://context7.com/letiantian/textrank4zh/llms.txt Use the underlying sentence sorting function for advanced usage, allowing custom similarity functions and direct graph construction. Accepts sentences and their word segmentation results. ```python from textrank4zh.util import sort_sentences, get_similarity sentences = [ '高圆圆和赵又廷在京举行答谢宴', '诸多明星现身捧场', '宾客近百人,其中不少是女方同学' ] words = [ ['高圆圆', '赵又廷', '举行', '答谢宴'], ['明星', '现身', '捧场'], ['宾客', '女方', '同学'] ] ranked = sort_sentences( sentences=sentences, words=words, sim_func=get_similarity, pagerank_config={'alpha': 0.85} ) for item in ranked: print(f'[{item.index}] {item.sentence} 权重={item.weight:.4f}') # [0] 高圆圆和赵又廷在京举行答谢宴 权重=0.4012 # [2] 宾客近百人,其中不少是女方同学 权重=0.3201 # [1] 诸多明星现身捧场 权重=0.2787 ``` -------------------------------- ### Segment Text with Segmentation Module Source: https://context7.com/letiantian/textrank4zh/llms.txt Use the Segmentation module for underlying text segmentation, combining sentence and word splitting. It returns results in four formats. Customize delimiters and stop words. ```python from textrank4zh.Segmentation import Segmentation seg = Segmentation( stop_words_file=None, # 使用默认停止词 delimiters=['。', '!', '?', '\n'] ) result = seg.segment( text="这间酒店位于北京东三环,里面摆放很多雕塑,文艺气息十足。答谢宴于晚上8点开始。", lower=True ) print('sentences:', result.sentences) # ['这间酒店位于北京东三环,里面摆放很多雕塑,文艺气息十足', '答谢宴于晚上8点开始'] print('words_no_filter:', result.words_no_filter) # [['这', '间', '酒店', '位于', '北京', '东三环', '里面', '摆放', '很多', '雕塑', '文艺', '气息', '十足'], ...] print('words_no_stop_words:', result.words_no_stop_words) # [['间', '酒店', '位于', '北京', '东三环', '里面', '摆放', '很多', '雕塑', '文艺', '气息', '十足'], ...] print('words_all_filters:', result.words_all_filters) # [['酒店', '位于', '北京', '东三环', '摆放', '雕塑', '文艺', '气息'], ...] ``` -------------------------------- ### Analyze Text Segmentation Source: https://github.com/letiantian/textrank4zh/blob/master/README.md Illustrates how TextRank4Keyword processes text into different segments: sentences, words without filters, words without stop words, and words with all filters applied. This is useful for understanding the internal text processing stages. ```python #-*- encoding:utf-8 -*- from __future__ import print_function import codecs from textrank4zh import TextRank4Keyword, TextRank4Sentence import sys try: reload(sys) sys.setdefaultencoding('utf-8') except: pass text = "这间酒店位于北京东三环,里面摆放很多雕塑,文艺气息十足。答谢宴于晚上8点开始。" tr4w = TextRank4Keyword() tr4w.analyze(text=text, lower=True, window=2) print() print('sentences:') for s in tr4w.sentences: print(s) # py2中是unicode类型。py3中是str类型。 print() print('words_no_filter') for words in tr4w.words_no_filter: print('/'.join(words)) # py2中是unicode类型。py3中是str类型。 print() print('words_no_stop_words') for words in tr4w.words_no_stop_words: print('/'.join(words)) # py2中是unicode类型。py3中是str类型。 print() print('words_all_filters') for words in tr4w.words_all_filters: print('/'.join(words)) # py2中是unicode类型。py3中是str类型。 ``` -------------------------------- ### TextRank4Sentence.get_key_sentences Source: https://context7.com/letiantian/textrank4zh/llms.txt Retrieves the top N most important sentences from the analyzed text, which form the summary. Allows filtering by minimum sentence length. ```APIDOC ## TextRank4Sentence.get_key_sentences ### Description Retrieves the top N most important sentences from the analyzed text, which form the summary. Allows filtering by minimum sentence length. ### Method get_key_sentences ### Parameters - **num** (int) - The number of top sentences to return. - **sentence_min_len** (int, optional) - The minimum length of sentences to be considered. Defaults to 1. ### Response #### Success Response (200) - **key_sentences** (list[AttrDict]) - A list of sentence objects, each containing 'index' (int), 'sentence' (str), and 'weight' (float). ### Request Example ```python import codecs from textrank4zh import TextRank4Sentence text = codecs.open('article.txt', 'r', 'utf-8').read() tr4s = TextRank4Sentence() tr4s.analyze(text=text, lower=True, source='all_filters') key_sentences = tr4s.get_key_sentences(num=3, sentence_min_len=10) for item in key_sentences: print(f'[位置 {item.index}] 权重={item.weight:.4f}') print(f' {item.sentence}') print() ``` ``` -------------------------------- ### TextRank4Keyword.get_keyphrases Source: https://context7.com/letiantian/textrank4zh/llms.txt Extracts keyphrases from the text, which are sequences of adjacent keywords that appear frequently. Useful for multi-word terms. ```APIDOC ## TextRank4Keyword.get_keyphrases ### Description Extracts keyphrases from the text, which are sequences of adjacent keywords that appear frequently. Useful for multi-word terms. ### Method get_keyphrases ### Parameters - **keywords_num** (int) - The number of top keywords to consider when constructing potential keyphrases. - **min_occur_num** (int) - The minimum number of times a phrase must appear in the original text to be considered a keyphrase. ### Response #### Success Response (200) - **keyphrases** (list[str]) - A list of extracted keyphrases. ### Request Example ```python from textrank4zh import TextRank4Keyword text = """ 支持向量机是一种监督学习模型,支持向量机广泛应用于分类和回归分析。 支持向量机的核心是寻找最优超平面。支持向量是距离超平面最近的样本点。 """ tr4w = TextRank4Keyword() tr4w.analyze(text=text, lower=True, window=2) keyphrases = tr4w.get_keyphrases(keywords_num=20, min_occur_num=2) print('关键短语:', keyphrases) ``` ``` -------------------------------- ### TextRank4Sentence.analyze Source: https://context7.com/letiantian/textrank4zh/llms.txt Analyzes the text to compute sentence importance scores. It builds a graph based on sentence similarity and applies the PageRank algorithm. Custom similarity functions can be provided. ```APIDOC ## TextRank4Sentence.analyze ### Description Analyzes the text to compute sentence importance scores. It builds a graph based on sentence similarity and applies the PageRank algorithm. Custom similarity functions can be provided. ### Method analyze ### Parameters - **text** (str) - The input text to analyze. - **lower** (bool, optional) - Whether to convert English words to lowercase. Defaults to False. - **source** (str, optional) - Specifies the filtering level for words used in similarity calculation. Options include 'all_filters', 'no_stop_words', 'no_filter'. Defaults to 'all_filters'. - **sim_func** (callable, optional) - A custom function to calculate sentence similarity. It should accept two lists of words and return a float. - **pagerank_config** (dict, optional) - Configuration dictionary for the PageRank algorithm, e.g., {'alpha': 0.85}. ### Request Example ```python import codecs from textrank4zh import TextRank4Sentence text = codecs.open('article.txt', 'r', 'utf-8').read() tr4s = TextRank4Sentence() tr4s.analyze( text=text, lower=True, source='all_filters', pagerank_config={'alpha': 0.85} ) # Using a custom similarity function from textrank4zh import util def my_sim(words1, words2): set1, set2 = set(words1), set(words2) if not set1 or not set2: return 0.0 return len(set1 & set2) / len(set1 | set2) # Jaccard similarity tr4s.analyze(text=text, lower=True, sim_func=my_sim) ``` ``` -------------------------------- ### Segmentation.segment Source: https://context7.com/letiantian/textrank4zh/llms.txt Performs both sentence and word segmentation on the input text. It returns an AttrDict object containing sentences, words without filters, words without stop words, and words with all filters applied. ```APIDOC ## Segmentation.segment ### Description Performs sentence and word segmentation on the input text. ### Method `segment(text: str, lower: bool = False)` ### Parameters #### Path Parameters None #### Query Parameters - **text** (str) - Required - The input text to segment. - **lower** (bool) - Optional - If True, converts text to lowercase before segmentation. Defaults to False. ### Request Example ```python from textrank4zh.Segmentation import Segmentation seg = Segmentation( stop_words_file=None, delimiters=['。', '!', '?', '\n'] ) result = seg.segment( text="这间酒店位于北京东三环,里面摆放很多雕塑,文艺气息十足。答谢宴于晚上8点开始。", lower=True ) print('sentences:', result.sentences) print('words_no_filter:', result.words_no_filter) print('words_no_stop_words:', result.words_no_stop_words) print('words_all_filters:', result.words_all_filters) ``` ### Response #### Success Response (AttrDict) - **sentences** (list[str]) - A list of segmented sentences. - **words_no_filter** (list[list[str]]) - A list of lists, where each inner list contains words from a sentence without any filtering. - **words_no_stop_words** (list[list[str]]) - A list of lists, where each inner list contains words from a sentence after stop word filtering. - **words_all_filters** (list[list[str]]) - A list of lists, where each inner list contains words from a sentence after all filters (stop words and potentially others) are applied. #### Response Example ```json { "sentences": ["这间酒店位于北京东三环,里面摆放很多雕塑,文艺气息十足", "答谢宴于晚上8点开始"], "words_no_filter": [["这", "间", "酒店", "位于", "北京", "东三环", "里面", "摆放", "很多", "雕塑", "文艺", "气息", "十足"], ...], "words_no_stop_words": [["间", "酒店", "位于", "北京", "东三环", "里面", "摆放", "很多", "雕塑", "文艺", "气息", "十足"], ...], "words_all_filters": [["酒店", "位于", "北京", "东三环", "摆放", "雕塑", "文艺", "气息"], ...] } ``` ``` -------------------------------- ### util.sort_sentences Source: https://context7.com/letiantian/textrank4zh/llms.txt Sorts sentences based on PageRank algorithm using a custom similarity function. It takes a list of sentences and their corresponding word lists, returning sorted sentences with their weights. ```APIDOC ## util.sort_sentences ### Description Sorts sentences using the PageRank algorithm, allowing for custom similarity functions. ### Method `sort_sentences(sentences: list[str], words: list[list[str]], sim_func: callable, pagerank_config: dict = None) -> list[AttrDict]` ### Parameters #### Path Parameters None #### Query Parameters - **sentences** (list[str]) - Required - A list of sentences to be ranked. - **words** (list[list[str]]) - Required - A list of lists, where each inner list contains the words for the corresponding sentence. - **sim_func** (callable) - Required - The similarity function to use for calculating sentence similarity (e.g., `get_similarity`). - **pagerank_config** (dict) - Optional - Configuration parameters for the PageRank algorithm (e.g., {'alpha': 0.85}). ### Request Example ```python from textrank4zh.util import sort_sentences, get_similarity sentences = [ '高圆圆和赵又廷在京举行答谢宴', '诸多明星现身捧场', '宾客近百人,其中不少是女方同学' ] words = [ ['高圆圆', '赵又廷', '举行', '答谢宴'], ['明星', '现身', '捧场'], ['宾客', '女方', '同学'] ] ranked = sort_sentences( sentences=sentences, words=words, sim_func=get_similarity, pagerank_config={'alpha': 0.85} ) for item in ranked: print(f'[{item.index}] {item.sentence} 权重={item.weight:.4f}') ``` ### Response #### Success Response (list[AttrDict]) - **index** (int) - The original index of the sentence. - **sentence** (str) - The sentence text. - **weight** (float) - The calculated PageRank weight of the sentence. #### Response Example ```json [ {"index": 0, "sentence": "高圆圆和赵又廷在京举行答谢宴", "weight": 0.4012}, {"index": 2, "sentence": "宾客近百人,其中不少是女方同学", "weight": 0.3201}, {"index": 1, "sentence": "诸多明星现身捧场", "weight": 0.2787} ] ``` ``` -------------------------------- ### util.get_similarity Source: https://context7.com/letiantian/textrank4zh/llms.txt Calculates the similarity between two sentences based on word co-occurrence and sentence length. This is the default similarity function used by TextRank4Sentence and can be called directly. ```APIDOC ## util.get_similarity ### Description Calculates the similarity between two sentences based on word co-occurrence and sentence length. ### Method `get_similarity(words1: list[str], words2: list[str]) -> float` ### Parameters #### Path Parameters None #### Query Parameters - **words1** (list[str]) - Required - A list of words representing the first sentence. - **words2** (list[str]) - Required - A list of words representing the second sentence. ### Request Example ```python from textrank4zh.util import get_similarity words1 = ['北京', '酒店', '摆放', '雕塑', '文艺'] words2 = ['北京', '答谢宴', '晚上', '开始'] sim = get_similarity(words1, words2) print(f'相似度: {sim:.4f}') words3 = ['上海', '天气', '晴朗'] sim2 = get_similarity(words1, words3) print(f'相似度: {sim2:.4f}') ``` ### Response #### Success Response (float) - **similarity** (float) - The calculated similarity score between the two sentences. #### Response Example ```json { "similarity": 0.5000 } ``` ``` -------------------------------- ### Sort Words by Weight with sort_words Source: https://context7.com/letiantian/textrank4zh/llms.txt Directly call the underlying word sorting function for custom graph construction. Accepts a list of word lists for vertices and edges, returning words ranked by weight. ```python from textrank4zh.util import sort_words vertex_source = [['酒店', '北京', '雕塑', '文艺'], ['答谢', '宾客', '晚上']] edge_source = [['酒店', '北京', '雕塑', '文艺'], ['答谢', '宾客', '晚上']] ranked = sort_words( vertex_source=vertex_source, edge_source=edge_source, window=2, pagerank_config={'alpha': 0.85} ) for item in ranked: print(f'{item.word}: {item.weight:.4f}') # 酒店: 0.1823 # 北京: 0.1654 # ... ``` -------------------------------- ### util.sort_words Source: https://context7.com/letiantian/textrank4zh/llms.txt Sorts words based on PageRank algorithm. It takes a list of words as vertex source and edge source, returning a list of words sorted by weight in descending order. Useful for custom graph construction. ```APIDOC ## util.sort_words ### Description Sorts words using the PageRank algorithm based on provided vertex and edge sources. ### Method `sort_words(vertex_source: list[list[str]], edge_source: list[list[str]], window: int, pagerank_config: dict = None) -> list[AttrDict]` ### Parameters #### Path Parameters None #### Query Parameters - **vertex_source** (list[list[str]]) - Required - A list of lists of words representing the vertices of the graph. - **edge_source** (list[list[str]]) - Required - A list of lists of words representing the edges of the graph. - **window** (int) - Required - The window size for considering word co-occurrences. - **pagerank_config** (dict) - Optional - Configuration parameters for the PageRank algorithm (e.g., {'alpha': 0.85}). ### Request Example ```python from textrank4zh.util import sort_words vertex_source = [['酒店', '北京', '雕塑', '文艺'], ['答谢', '宾客', '晚上']] edge_source = [['酒店', '北京', '雕塑', '文艺'], ['答谢', '宾客', '晚上']] ranked = sort_words( vertex_source=vertex_source, edge_source=edge_source, window=2, pagerank_config={'alpha': 0.85} ) for item in ranked: print(f'{item.word}: {item.weight:.4f}') ``` ### Response #### Success Response (list[AttrDict]) - **word** (str) - The word. - **weight** (float) - The calculated PageRank weight of the word. #### Response Example ```json [ {"word": "酒店", "weight": 0.1823}, {"word": "北京", "weight": 0.1654}, ... ] ``` ``` -------------------------------- ### Calculate Sentence Similarity with get_similarity Source: https://context7.com/letiantian/textrank4zh/llms.txt Calculate sentence similarity using the default function based on word co-occurrence and sentence length. This function is used by TextRank4Sentence and can be replaced with custom functions. ```python from textrank4zh.util import get_similarity words1 = ['北京', '酒店', '摆放', '雕塑', '文艺'] words2 = ['北京', '答谢宴', '晚上', '开始'] sim = get_similarity(words1, words2) print(f'相似度: {sim:.4f}') # 相似度: 0.5000(共同词"北京"出现1次,log(5)+log(4)≈3.22,结果=1/3.22≈0.31) # 完全不相关的句子 words3 = ['上海', '天气', '晴朗'] sim2 = get_similarity(words1, words3) print(f'相似度: {sim2:.4f}') # 相似度: 0.0000 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.