使用 Python 进行文本总结

引言

文本摘要涉及减少文本中的单词数量,同时保持其意义。它提高了效率,减少了阅读多篇文章的时间。本文我们将演示如何使用 Python 实现文本摘要自动化。

阅读文章中的所有文字并提取摘要是一项耗时而乏味的工作。幸运的是,我们可以使用NLP模型自动生成文本摘要。而且越来越多的媒体平台使用NLP进行文本摘要生成。本文我们将演示如何使用Python对文章进行总结。

什么是文本摘要?

本质上,任务是将文本作为输入并输出其摘要。关键是确保输入文本的整体含义保留在摘要文本中。

有两种关于文本总结的技术。其中一种技术称为提取文本摘要技术。它涉及从文本中提取最重要的单词。另一种技术称为抽象摘要,涉及使用已知的学习词汇来解释输入文本。

在本文中,我将专注于提取摘要技术。

获取文本

几乎所有主要机构都在利用自然处理语言 (NLP) 模型来总结文本。例如,我们可以在社交媒体平台上找到对一家公司的数千甚至数百万条评论。使用文本摘要器可以实现对某个主题的公正看法。文本摘要的一种方法可以像删除不重要的单词一样直接,对每个单词进行评分并只保留包含最重要单词的句子。

详细来说,Twitter 是最大的微博社交媒体平台之一。我们可以尝试获取一段时间内关于某个主题的所有推文,并将它们与来自 Google 的新闻文章结合起来。这可能会为我们提供对某个主题的公正看法。

一旦组合文本准备就绪,我们就可以使用文本摘要器为我们总结文本。我们可以在去除文本中的噪声后对每个单词进行排名,然后根据构成句子的单词的排名对每个句子进行排名,最后取排名最高的句子。

如何总结文本摘要?

我们将专注于提取摘要技术。它涉及从文本中提取最重要的单词。这意味着我们需要计算每个单词的重要性分数。有时,数据可能包含大量噪声。 因此,我们的第一个目标是删除那些没有增加价值的词语。

关键是专注于关键信息并去除噪音。下面的代码执行以下关键步骤:

1. 软件包下载完成后,第一步就是通过执行间歇处理、去掉标点符号和停止文字来对文本进行预处理。

2. 计算机理解数字。我们需要将文本转换为数字。下一步是根据每个单词的频率对其进行评分或排名,然后对频率分数进行归一化。然后我们将创建一个map,其中map的键是单词,值是分数。

3. 然后,通过将构成句子的单个单词的分数相加,为每个句子赋予一个重要性分数。

4. 最后返回前 3 个句子来总结文本。

第一步: 安装软件包

pip install spacy

第二步: 下载 Spacy 模块

import sys
!{sys.executable} -m spacy download en

第三步: 导入包

import spacy
from spacy.lang.en.stop_words import STOP_WORDS
from string import punctuation
import string
from spacy.lang.en.stop_words import STOP_WORDS
from spacy.lang.en import English
from heapq import nlargest
punctuations = string.punctuation
from spacy.language import Language
nlp = English()
nlp.add_pipe('sentencizer') # updated
parser = English()

第四步: 预处理文字以消除噪音

def pre_process(document):
    clean_tokens = [ token.lemma_.lower().strip() for token in document ]
    clean_tokens = [ token for token in clean_tokens if token not in STOP_WORDS and token not in punctuations ]
    tokens = [token.text for token in document]
    lower_case_tokens = list(map(str.lower, tokens))
    
    return lower_case_tokens

第五步: 从文本生成数字矢量

def generate_numbers_vector(tokens):
    frequency = [tokens.count(token) for token in tokens]
    token_dict = dict(list(zip(tokens,frequency)))
    maximum_frequency=sorted(token_dict.values())[-1]
    normalised_dict = {token_key:token_dict[token_key]/maximum_frequency for token_key in token_dict.keys()}
    return normalised_dict

第六步: 生成句子重要性得分

def sentences_importance(text, normalised_dict):
    importance ={}
    for sentence in nlp(text).sents:
        for token in sentence:
            target_token = token.text.lower()
            if target_token in normalised_dict.keys():
                if sentence in importance.keys():
                    importance[sentence]+=normalised_dict[target_token]
                else:
                    importance[sentence]=normalised_dict[target_token]
    return importance

第七步: 生成摘要

def generate_summary(rank, text):
    target_document = parser(text)
    importance = sentences_importance(text, generate_numbers_vector(pre_process(target_document)))
    summary = nlargest(rank, importance, key=importance.get)
    return summary

第八步: 主函数

·  END  ·

HAPPY LIFE

5d9770b123cdbee3a8c64f094f159154.png

猜你喜欢

转载自blog.csdn.net/weixin_38739735/article/details/128451087