首页 > 技术文章 > 自动摘要

xuehuiping 2019-12-27 14:13 原文

每天都要发两三次的企业信息咨询,所以来一个自动摘要试试看吧!

用conda,在服务器端启动jupyter notebook服务。
为了对比效果,选择多个自动摘要模块。

  1. 安装依赖
!pip install snownlp
!pip install summa
!pip install textrank4zh
  1. 读取文件
    这里没有使用爬虫,直接加载文本文件
file_name = 'text.txt'
text = open(file_name).read()
  1. 定义函数,抽取摘要并打印出来
from snownlp import SnowNLP
from summa.summarizer import summarize
from textrank4zh import TextRank4Keyword, TextRank4Sentence

def get_summary(text):

    s = SnowNLP(text)
    summary = s.summary(5)
    print(summary)
    print("***"*20)

    r = summarize(text, ratio=0.2)
    print(r)
    print("***"*20)

    tr4s = TextRank4Sentence()
    tr4s.analyze(text=text, lower=True, source='all_filters')
    for item in tr4s.get_key_sentences(num=5):
       print(item.sentence+'。')
    print("***"*20)
  1. 运行查看效果
get_summary(text)

推荐阅读