安装TextRank4ZH
pip install TextRank4ZH
依赖环境
jieba版本号至少0.35
numpy版本号至少1.7.1
networkx版本号至少1.9.1
示例
不多bb,看代码:
# -*- coding:utf-8 -*-
from textrank4zh import TextRank4Keyword, TextRank4Sentence
with open('./test.txt','r') as fileread:
text = fileread.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,