#-*- 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.weight, item.sentence)
[((15, 7, 15, 25), 'textrank4zh.TextRank4Keyword', 'TextRank4Keyword', ({}, {}), '()', False, 'from textrank4zh import TextRank4Keyword, TextRank4Sentence\n'), ((28, 7, 28, 26), 'textrank4zh.TextRank4Sentence', 'TextRank4Sentence', ({}, {}), '()', False, 'from textrank4zh import TextRank4Keyword, TextRank4Sentence\n'), ((7, 4, 7, 35), 'sys.setdefaultencoding', 'sys.setdefaultencoding', ({(7, 27, 7, 34): '"""utf-8"""'}, {}), "('utf-8')", False, 'import sys\n'), ((14, 7, 14, 54), 'codecs.open', 'codecs.open', ({(14, 19, 14, 39): '"""../test/doc/01.txt"""', (14, 41, 14, 44): '"""r"""', (14, 46, 14, 53): '"""utf-8"""'}, {}), "('../test/doc/01.txt', 'r', 'utf-8')", False, 'import codecs\n')]