使用NLP建立摘要

做者|Louis Teo
编译|VK
来源|Towards Data Sciencepython

你有没有读过不少的报告,而你只想对每一个报告作一个快速的总结摘要?你是否曾经遇到过这样的状况?git

摘要已成为21世纪解决数据问题的一种很是有帮助的方法。在本篇文章中,我将向你展现如何使用Python中的天然语言处理(NLP)建立我的文本摘要生成器。github

前言:我的文本摘要器不难建立——初学者能够轻松作到!web

什么是文本摘要

基本上,在保持关键信息的同时,生成准确的摘要,而不失去总体意义,这是一项任务。正则表达式

摘要有两种通常类型:算法

  • 抽象摘要>>从原文中生成新句子。
  • 提取摘要>>识别重要句子,并使用这些句子建立摘要。

应该使用哪一种总结方法

我使用提取摘要,由于我能够将此方法应用于许多文档,而没必要执行大量(使人畏惧)的机器学习模型训练任务。安全

此外,提取摘要法比抽象摘要具备更好的总结效果,由于抽象摘要必须从原文中生成新的句子,这是一种比数据驱动的方法提取重要句子更困难的方法。app

如何建立本身的文本摘要器

咱们将使用单词直方图来对句子的重要性进行排序,而后建立一个总结。这样作的好处是,你不须要训练你的模型来将其用于文档。机器学习

文本摘要工做流

下面是咱们将要遵循的工做流…ide

导入文本>>>>清理文本并拆分红句子>>删除停用词>>构建单词直方图>>排名句子>>选择前N个句子进行提取摘要

(1) 示例文本

我用了一篇新闻文章的文本,标题是苹果以5000万美圆收购AI初创公司,以推动其应用程序。你能够在这里找到原始的新闻文章:https://analyticsindiamag.com/apple-acquires-ai-startup-for-50-million-to-advance-its-apps/

你还能够从个人Github下载文本文档:https://github.com/louisteo9/personal-text-summarizer

(2) 导入库

# 天然语言工具包(NLTK)
import nltk
nltk.download('stopwords')

# 文本预处理的正则表达式
import re

# 队列算法求首句
import heapq

# 数值计算的NumPy
import numpy as np

# 用于建立数据帧的pandas
import pandas as pd

# matplotlib绘图
from matplotlib import pyplot as plt
%matplotlib inline

(3) 导入文本并执行预处理

有不少方法能够作到。这里的目标是有一个干净的文本,咱们能够输入到咱们的模型中。

# 加载文本文件
with open('Apple_Acquires_AI_Startup.txt', 'r') as f:
    file_data = f.read()

这里,咱们使用正则表达式来进行文本预处理。咱们将

(A)用空格(若是有的话…)替换参考编号,即[1]、[10]、[20],

(B)用单个空格替换一个或多个空格。

text = file_data
# 若是有,请用空格替换
text = re.sub(r'\[[0-9]*\]',' ',text) 

# 用单个空格替换一个或多个空格
text = re.sub(r'\s+',' ',text)

而后,咱们用小写(不带特殊字符、数字和额外空格)造成一个干净的文本,并将其分割成单个单词,用于词组分数计算和构词直方图。

造成一个干净文本的缘由是,算法不会把“理解”和“理解”做为两个不一样的词来处理。

# 将全部大写字符转换为小写字符
clean_text = text.lower()

# 用空格替换[a-zA-Z0-9]之外的字符
clean_text = re.sub(r'\W',' ',clean_text) 

# 用空格替换数字
clean_text = re.sub(r'\d',' ',clean_text) 

# 用单个空格替换一个或多个空格
clean_text = re.sub(r'\s+',' ',clean_text)

(4) 将文本拆分为句子

咱们使用NLTK sent_tokenize方法将文本拆分为句子。咱们将评估每一句话的重要性,而后决定是否应该将每一句都包含在总结中。

sentences = nltk.sent_tokenize(text)

(5) 删除停用词

停用词是指不给句子增长太多意义的英语单词。他们能够安全地被忽略,而不牺牲句子的意义。咱们已经下载了一个文件,其中包含英文停用词

这里,咱们将获得停用词的列表,并将它们存储在stop_word 变量中。

# 获取停用词列表
stop_words = nltk.corpus.stopwords.words('english')

(6) 构建直方图

让咱们根据每一个单词在整个文本中出现的次数来评估每一个单词的重要性。

咱们将经过(1)将单词拆分为干净的文本,(2)删除停用词,而后(3)检查文本中每一个单词的频率。

# 建立空字典以容纳单词计数
word_count = {}

# 循环遍历标记化的单词,删除停用单词并将单词计数保存到字典中
for word in nltk.word_tokenize(clean_text):
    # remove stop words
    if word not in stop_words:
        # 将字数保存到词典
        if word not in word_count.keys():
            word_count[word] = 1
        else:
            word_count[word] += 1

让咱们绘制单词直方图并查看结果。

plt.figure(figsize=(16,10))
plt.xticks(rotation = 90)
plt.bar(word_count.keys(), word_count.values())
plt.show()

让咱们把它转换成横条图,只显示前20个单词,下面有一个helper函数。

# helper 函数,用于绘制最上面的单词。
def plot_top_words(word_count_dict, show_top_n=20):
    word_count_table = pd.DataFrame.from_dict(word_count_dict, orient = 'index').rename(columns={0: 'score'})
    
    word_count_table.sort_values(by='score').tail(show_top_n).plot(kind='barh', figsize=(10,10))
    plt.show()

让咱们展现前20个单词。

plot_top_words(word_count, 20)

从上面的图中,咱们能够看到“ai”和“apple”两个词出如今顶部。这是有道理的,由于这篇文章是关于苹果收购一家人工智能初创公司的。

(7) 根据分数排列句子

如今,咱们将根据句子得分对每一个句子的重要性进行排序。咱们将:

  • 删除超过30个单词的句子,认识到长句未必老是有意义的;

  • 而后,从构成句子的每一个单词中加上分数,造成句子分数。

高分的句子将排在前面。前面的句子将造成咱们的总结。

注意:根据个人经验,任何25到30个单词均可以给你一个很好的总结。

# 建立空字典来存储句子分数
sentence_score = {}

# 循环经过标记化的句子,只取少于30个单词的句子,而后加上单词分数来造成句子分数
for sentence in sentences:
    # 检查句子中的单词是否在字数字典中
    for word in nltk.word_tokenize(sentence.lower()):
        if word in word_count.keys():
            # 只接受少于30个单词的句子
            if len(sentence.split(' ')) < 30:
                # 把单词分数加到句子分数上
                if sentence not in sentence_score.keys():
                    sentence_score[sentence] = word_count[word]
                else:
                    sentence_score[sentence] += word_count[word]

咱们将句子-分数字典转换成一个数据框,并显示sentence_score

注意:字典不容许根据分数对句子进行排序,所以须要将字典中存储的数据转换为DataFrame。

df_sentence_score = pd.DataFrame.from_dict(sentence_score, orient = 'index').rename(columns={0: 'score'})
df_sentence_score.sort_values(by='score', ascending = False)

(8) 选择前面的句子做为摘要

咱们使用堆队列算法来选择前3个句子,并将它们存储在best_quences变量中。

一般3-5句话就足够了。根据文档的长度,能够随意更改要显示的最上面的句子数。

在本例中,我选择了3,由于咱们的文本相对较短。

# 展现最好的三句话做为总结         
best_sentences = heapq.nlargest(3, sentence_score, key=sentence_score.get)

让咱们使用print和for loop函数显示摘要文本。

print('SUMMARY')
print('------------------------')

# 根据原文中的句子顺序显示最上面的句子
for sentence in sentences:
    if sentence in best_sentences:
        print (sentence)

这是到个人Github的连接以获取Jupyter笔记本。你还将找到一个可执行的Python文件,你能够当即使用它来总结你的文本:https://github.com/louisteo9/personal-text-summarizer

让咱们看看算法的实际操做!

如下是一篇题为“苹果以5000万美圆收购人工智能创业公司(Apple Acquire AI Startup)以推动其应用程序”的新闻文章的原文(原文可在此处找到):https://analyticsindiamag.com/apple-acquires-ai-startup-for-50-million-to-advance-its-apps/

In an attempt to scale up its AI portfolio, Apple has acquired Spain-based AI video startup — Vilynx for approximately $50 million.

Reported by Bloomberg, the AI startup — Vilynx is headquartered in Barcelona, which is known to build software using computer vision to analyse a video’s visual, text, and audio content with the goal of “understanding” what’s in the video. This helps it categorising and tagging metadata to the videos, as well as generate automated video previews, and recommend related content to users, according to the company website.

Apple told the media that the company typically acquires smaller technology companies from time to time, and with the recent buy, the company could potentially use Vilynx’s technology to help improve a variety of apps. According to the media, Siri, search, Photos, and other apps that rely on Apple are possible candidates as are Apple TV, Music, News, to name a few that are going to be revolutionised with Vilynx’s technology.

With CEO Tim Cook’s vision of the potential of augmented reality, the company could also make use of AI-based tools like Vilynx.

The purchase will also advance Apple’s AI expertise, adding up to 50 engineers and data scientists joining from Vilynx, and the startup is going to become one of Apple’s key AI research hubs in Europe, according to the news.

Apple has made significant progress in the space of artificial intelligence over the past few months, with this purchase of UK-based Spectral Edge last December, Seattle-based Xnor.ai for $200 million and Voysis and Inductiv to help it improve Siri. With its habit of quietly purchasing smaller companies, Apple is making a mark in the AI space. In 2018, CEO Tim Cook said in an interview that the company had bought 20 companies over six months, while only six were public knowledge.

摘要以下:

SUMMARY
------------------------
In an attempt to scale up its AI portfolio, Apple has acquired Spain-based AI video startup — Vilynx for approximately $50 million.
With CEO Tim Cook’s vision of the potential of augmented reality, the company could also make use of AI-based tools like Vilynx.
With its habit of quietly purchasing smaller companies, Apple is making a mark in the AI space.

结尾

祝贺你!你已经在Python中建立了你的我的文本摘要器。我但愿,摘要看起来很不错。

原文连接:https://towardsdatascience.com/report-is-too-long-to-read-use-nlp-to-create-a-summary-6f5f7801d355

欢迎关注磐创AI博客站:
http://panchuang.net/

sklearn机器学习中文官方文档:
http://sklearn123.com/

欢迎关注磐创博客资源汇总站:
http://docs.panchuang.net/

相关文章
相关标签/搜索