letiantian / TextRank4ZH

:deciduous_tree:从中文文本中自动提取关键词和摘要
MIT License
3.25k stars 844 forks source link

get_key_sentences获取出来的summary句子是乱序的 #12

Closed felinx closed 7 years ago

felinx commented 7 years ago

目前的实现get_key_sentences获取出来的summary句子顺序是根据pagerank的score值来排序的,虽然句子取对了,但是取出的句子顺序通常跟原文句子顺序不同。

理想的情况应该是取出前几的热门句子,最终的输出根据句子在原文中的顺序来排序,这样上下文才通畅。

TextRank4Sentence.get_key_sentences 和 utils.sort_sentences 可能要改造一下

letiantian commented 7 years ago

建议改动自己的项目代码就行了。判断句子在文本中的位置,根据位置排序。

felinx commented 7 years ago

sort_sentences 返回的信息里面没有了index信息,只能继承TextRank4Sentence用自己修改版的utils.sort_sentences了。

letiantian commented 7 years ago

这个可以有。我最近把这个添加一下。

letiantian commented 7 years ago

已经添加。示例(注意最后一句):

#-*- 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是权重