TextRank算法可以用来从文本中提取关键词和摘要。
TextRank4ZH是针对中文文本的TextRank算法的Python算法实现。
而在TextRank4ZH库中,类TextRank4Keyword、TextRank4Sentence在处理一段文本时,会将文本拆分成4种格式:
(1)sentences:由句子组成的列表。
(2)words_no_filter:对sentences中每个句子分词而得到的两级列表。
(3)words_no_stop_words:去掉words_no_filter中的停止词而得到的二维列表。
(4)words_all_filters:保留words_no_stop_words中指定词性的单词而得到的二维列表。
因此,我们可以根据自己的项目需求,应用上述的一种或几种方法对目标文本进行处理,以得到相应的结果。
那么,接下来将分别举例说明TextRank4ZH的使用方法:
1.TextRank4Keyword.sentences
目标:将文本拆分成句子。
文本内容:<2018年2月,深圳率先获批在前海启动资本项目外汇收入支付便利化试点,银行审核企业材料时间由试点前的几小时大幅缩短至试点后的几分钟。今年深圳外管局将资本项目外汇收入支付便利化试点扩大至整个前海蛇口自贸片区,并降低了企业试点门槛,受益范围进一步扩大。>
代码如下:
from textrank4zh import TextRank4Keyword, TextRank4Sentence
test_text="2018年2月,深圳率先获批在前海启动资本项目外汇收入支付便利化试点,银行审核企业材料时间由试点前的几小时大幅缩短至试点后的几分钟。今年深圳外管局将资本项目外汇收入支付便利化试点扩大至整个前海蛇口自贸片区,并降低了企业试点门槛,受益范围进一步扩大。"
tr4w=TextRank4Keyword()
tr4w.analyze(text=test_text, lower=True, window=2)
for s in tr4w.sentences:
print(s)
运行结果:

可以看出,该方法根据句号“。”对文本进行拆分,最终拆分成了两句话。因此,拆分文本成句子可采用TextRank4Keyword.sentences这个方法。
2.TextRank4Keyword.words_no_filter
目标:对sentences中每个句子分词。
代码如下:
from textrank4zh import TextRank4Keyword, TextRank4Sentence
test_text="2018年2月,深圳率先获批在前海启动资本项目外汇收入支付便利化试点,银行审核企业材料时间由试点前的几小时大幅缩短至试点后的几分钟。今年深圳外管局将资本项目外汇收入支付便利化试点扩大至整个前海蛇口自贸片区,并降低了企业试点门槛,受益范围进一步扩大。"
tr4w=TextRank4Keyword()
tr4w.analyze(text=test_text, lower=True, window=2)
for words in tr4w.words_no_filter:
print('/'.join(words))
运行结果:

可以看出,该方法在分句后的基础上。对每句话进行了分词。
3.TextRank4Keyword.words_no_stop_words
目标:去掉words_no_filter中的停止词。
代码如下:
from textrank4zh import TextRank4Keyword, TextRank4Sentence
test_text="2018年2月,深圳率先获批在前海启动资本项目外汇收入支付便利化试点,银行审核企业材料时间由试点前的几小时大幅缩短至试点后的几分钟。今年深圳外管局将资本项目外汇收入支付便利化试点扩大至整个前海蛇口自贸片区,并降低了企业试点门槛,受益范围进一步扩大。"
tr4w=TextRank4Keyword()
tr4w.analyze(text=test_text, lower=True, window=2)
for words in tr4w.words_no_stop_words:
print('/'.join(words))
运行结果:

可以看出,该方法是在分句、分词后的基础上,去除了停用词。
4.TextRank4Keyword.words_all_filters
目标:保留words_no_stop_words中指定词性的单词。
代码如下:
from textrank4zh import TextRank4Keyword, TextRank4Sentence
test_text="2018年2月,深圳率先获批在前海启动资本项目外汇收入支付便利化试点,银行审核企业材料时间由试点前的几小时大幅缩短至试点后的几分钟。今年深圳外管局将资本项目外汇收入支付便利化试点扩大至整个前海蛇口自贸片区,并降低了企业试点门槛,受益范围进一步扩大。"
tr4w=TextRank4Keyword()
tr4w.analyze(text=test_text, lower=True, window=2)
for words in tr4w.words_all_filters:
print('/'.join(words))
运行结果:

可以看出,该方法是在分句、分词、去除停用词的基础上,去除了其它词性的单词,保留了指定词性的单词。
因此,我们拆分文本的时候,一般可以直接使用words_all_filters这个方法,一步到位将文本拆分为词语。
扩展知识:
在上述的代码 tr4w.analyze(text=test_text, lower=True, window=2) 中
函数analyze的具体参数详解:
analyze(text, window=2, lower=False, vertex_source ='all_filters', edge_source='no_stop_words', pagerank_ config={'alpha': 0.85})
text -- 文本内容,字符串。
window --窗口大小,int,用来构造单词之间的边。默认为2。
lower -- 是否将文本转换为小写。默认为False。
vertex_source -- 选择使用words_no_filter, words_ no_stop_words, words_all_filters中的哪一个来构造pagerank对应的图中的节点。默认值为'all_filters',可选值为'no_ filter', 'no_stop_words', 'all_filters'。关键词也来自vertex_source。
edge_source --选用words_no_filter, words_no_stop_words,words_all_filters中的哪一个来构造pagerank对应的图中的节点之间的边。 默认值为'no_stop_words',可选值为'no_ filter', 'no_stop_words', 'all_filters'。边的构造要结合window参数。
网友评论