python nlp文本摘要_理解文本摘要并用python创建你自己的摘要器

我们都与使用文本摘要的应用程序进行交互。 这些应用程序中的许多应用程序都是用于发布有关每日新闻,娱乐和体育的文章的平台。 由于我们的日程安排很忙,因此我们决定在阅读全文之前先阅读这些文章的摘要。 阅读摘要有助于我们确定感兴趣的领域,并提供故事的简要背景信息。

33cdf145b772

摘要图示.png

摘要可以定义为在保持关键信息和整体含义的同时,提供简洁明了的摘要的任务。

影响力

汇总系统通常具有其他证据,可用于指定最重要的文档主题。 例如,在总结博客时,博客文章后会有一些讨论或评论,这些信息或信息是确定博客的哪些部分至关重要且有趣的信息。

在科学论文摘要中,存在大量信息,例如被引用的论文和会议信息,可以用来识别原始论文中的重要句子。

文本摘要的类型

通常,摘要有两种类型,Abstractive 和 Extractive summarization.

Abstractive Summarization: 抽象方法是基于语义理解来选择单词的,即使这些单词没有出现在源文档中。 它旨在以新的方式生产重要的材料。 他们使用先进的自然语言技术解释和检查文本,以便生成新的较短文本,从而传达原始文本中最关键的信息。

它可以与人类阅读文本文章或博客文章,然后以自己的单词进行摘要的方式相关。

输入文档→了解上下文→语义→创建自己的摘要

Extractive Summarization:摘录方法尝试通过选择保留最重要要点的单词子集来对文章进行摘要。

这种方法对句子的重要部分进行加权,并使用它们来构成摘要。 使用不同的算法和技术来定义句子的权重,并根据彼此之间的重要性和相似性对它们进行排名。

输入文档→句子相似度→加权句子→选择排名更高的句子

有限的研究可用于抽象总结,因为与提取方法相比,它需要对文本有更深刻的理解。

与Extractive Summarization摘要相比,Abstractive Summarization摘要通常可以提供更好的结果。 这是因为,Abstractive Summarization方法可以应对语义表示,推理和自然语言生成,这比数据驱动的方法(例如句子提取)要难得多。

有许多技术可用于生成提取摘要。 为简单起见,我将使用一种无监督的学习方法来查找句子相似度并对其进行排名。 这样的好处之一是,您无需在开始将其用于项目时就进行训练和构建模型。

最好理解余弦相似度,以充分利用您将要看到的代码。 余弦相似度是度量内部乘积空间的两个非零向量之间相似度的度量,该向量测量两个向量之间的夹角余弦。 由于我们将句子表示为向量束,因此我们可以使用它来找到句子之间的相似性。 它测量向量之间夹角的余弦值。 如果句子相似,角度将为0。

接下来,下面是我们用于生成摘要文本的流程:

输入文章→分解为句子→删除停用词→建立相似度矩阵→基于矩阵生成等级→选择最前面的N个句子进行汇总

#Import all necessary libraries

from nltk.corpus import stopwords

from nltk.cluster.util import cosine_distance

import numpy as np

import networkx as nx

import re

#Generate clean sentences

def read_article(file_name):

file = open(file_name, "r")

filedata = file.readlines()

article = filedata[0].split(". ")

sentences = []

for sentence in article:

print(sentence)

sentences.append(re.sub("[^a-zA-Z]", " ", sentence).split(" "))

sentences.pop()

return sentences

def sentence_similarity(sent1, sent2, stopwords=None):

if stopwords is None:

stopwords = []

sent1 = [w.lower() for w in sent1]

sent2 = [w.lower() for w in sent2]

all_words = list(set(sent1 + sent2))

vector1 = [0] * len(all_words)

vector2 = [0] * len(all_words)

# build the vector for the first sentence

for w in sent1:

if w in stopwords:

continue

vector1[all_words.index(w)] += 1

# build the vector for the second sentence

for w in sent2:

if w in stopwords:

continue

vector2[all_words.index(w)] += 1

return 1 - cosine_distance(vector1, vector2)

#Similarity matrix

#This is where we will be using cosine similarity to find similarity between sentences.

def build_similarity_matrix(sentences, stop_words):

# Create an empty similarity matrix

similarity_matrix = np.zeros((len(sentences), len(sentences)))

for idx1 in range(len(sentences)):

for idx2 in range(len(sentences)):

if idx1 == idx2: #ignore if both are same sentences

continue

similarity_matrix[idx1][idx2] = sentence_similarity(sentences[idx1], sentences[idx2], stop_words)

return similarity_matrix

#Generate Summary Method

def generate_summary(file_name, top_n=5):

stop_words = stopwords.words('english')

summarize_text = []

# Step 1 - Read text and tokenize

sentences = read_article(file_name)

# Step 2 - Generate Similary Martix across sentences

sentence_similarity_martix = build_similarity_matrix(sentences, stop_words)

# Step 3 - Rank sentences in similarity martix

sentence_similarity_graph = nx.from_numpy_array(sentence_similarity_martix)

scores = nx.pagerank(sentence_similarity_graph)

# Step 4 - Sort the rank and pick top sentences

ranked_sentence = sorted(((scores[i],s) for i,s in enumerate(sentences)), reverse=True)

print("Indexes of top ranked_sentence order are ", ranked_sentence)

for i in range(top_n):

summarize_text.append(" ".join(ranked_sentence[i][1]))

# Step 5 - Offcourse, output the summarize texr

print("Summarize Text: \n", ". ".join(summarize_text))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值