自动提取摘要

本文介绍了使用TF-IDF技术进行关键词提取和摘要生成的方法。首先,通过TF-IDF计算关键词,然后在摘要提取中,基于关键词匹配和评分来选择关键句子,以实现对对话文本的有效摘要。经过文本预处理、关键词提取和句子匹配,逐步优化摘要质量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1.TF-IDF提取关键词
2.摘要提取
2.1 基于关键词匹配的摘要提取
2.2基于关键词评分的摘要提取

1.TF-IDF提取关键词

TF-IDF是Term Frequency - Inverse Document Frequency的缩写,即“词频-逆文本频率”。它由两部分组成,TF和IDF。TF为词频,即某个词在文章中出现的次数。
IDF为逆文档频率:
在这里插入图片描述
TF-IDF的计算为:
在这里插入图片描述
TF_IDF提取关键词的步骤就是,对文本先进行分词处理,再对每一个词计算TF-IDF值,然后按降序排序,取排在最前面的几个词。
参考文献:TF-IDF与余弦相似性的应用(一):自动提取关键词 阮一峰

2.摘要提取

2.1 基于关键词匹配的摘要提取

算法思想来自于阮一峰 TF-IDF与余弦相似性的应用(三):自动摘要
我们要做的是对对话文本进行摘要提取。
对话文本实例:
在这里插入图片描述
(1)文本预处理
文本预处理时将比较短的文本过滤掉,我们设置的字符个数为7,然后每一个对话句子作为一个sentence.
(2)TF-IDF提取关键词
我们直接调用的结巴的接口,提取了6个关键词
(3)匹配句子
根据关键词去匹配每一个sentence,并且只考虑关键词首先出现的句子。最多提取5个句子。

import json
import jieba
import jieba.analyse
import re
def search_sentences(sentences,word):
    for sentence in sentences:
        if re.search(word,sentence):
            return sentence
    return word   
file = "./data/mendian_class1/司内投诉.txt"
with open(file,"r",encoding="utf-8") as f:
    lines = f.readlines()
for line in lines:
    res = line.split("|")[1]
    res = res.replace("\'","\"")
    res = json.loads(res)
    sentences_list = []
    sentences = ""
    sentences_dict = {}
    count = 0
    if res is not None and "sentences" in res:
        if res["sentences"]:
            for index,sent in enumerate(res["sentences"]):
                tex = sent["text"]
                if len(tex) >= 7:
                    sentences_list.append(tex)
                    sentences += tex
                    count += 1

    #print("sentences_list:",sentences_list)
    #print("sentences:",sentences)

    hotwords = jieba.analyse.extract_tags(sentences, topK=6, allowPOS=( 'n', 'vn', 'v'))   
    print(hotwords)

    set_summary_sentences = set()
    for word in hotwords:
        #print(word)
        first_match_sentence = search_sentences(sentences_list,word)
        set_summary_sentences.add(first_match_sentence)
        if len(set_summary_sentences) == 3:
            break

    print("set_summary_sentences:",set_summary_sentences)
    summary = ""
    for sentence in set_summary_sentences:
        summary = summary + " " + sentence + "/"
    print("summary:",summary)

运行结果:

Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\xiang\AppData\Local\Temp\jieba.cache
Loading model cost 1.280 seconds.
Prefix dict has been built succesfully.
['理赔', '污染', '保价', '肯定', '不了', '公司']
set_summary_sentences: {'喂,你好,就是关于之前的一个胡萝卜住两天胡萝卜素,那个理赔呃,现在公司逐步给到的一个金额,是理赔,并是2000元。', '他这个是量筒的话有一同学坏的吗,换的话就是因为我们这边理赔的,是按照保价比例啊,还有坏掉一个程度,所以说是理赔的异同的那个
成本是2000元。', '如果说是外面污染被污染掉的话,这个清洗完了之后,它里面的东西,实际上也没有,会不会受到影响吗?'}
summary:  喂,你好,就是关于之前的一个胡萝卜住两天胡萝卜素,那个理赔呃,现在公司逐步给到的一个金额,是理赔,并是2000元。/ 他这个是量筒的话有一同学坏的吗,换的话就是因为我们这边理赔的,是按照保价比例啊,还有坏掉一个程度,所以说是理赔的异同的那个成本是2000元。/ 如
果说是外面污染被污染掉的话,这个清洗完了之后,它里面的东西,实际上也没有,会不会受到影响吗?/
PS G:\debang_item_20190614\abstract_extract>

可以看到提取的摘要语句比较长,也比较杂乱,对此我们进行了改进。
(1)文本预处理
文本预处理时将比较短的文本过滤掉,我们设置的字符个数为7,然后每一个对话句子以逗号为分隔符切分为短句子,并进一步过滤,将字符数少于5个的过滤掉,最后将得到的每个短句子作为一个sentence.
(2)TF-IDF提取关键词
我们直接调用的结巴的接口,提取了6个关键词
(3)匹配句子
根据关键词去匹配每一个sentence,并且只考虑关键词首先出现的句子。最多提取5个句子。

import json
import jieba
import jieba.analyse
import re
import time
#创建停用词list
def stop_word_list(path):
    stopwords = [line.strip() for line in open(path, 'r', encoding='utf-8').readlines()] 
    return stopwords
#预处理文本
def preprocess(text):
    text_with_spaces=""
    textcut = jieba.cut(text.strip()) 
    stopwords = stop_word_list("./data/stop_words.txt")
    for word in textcut:
        if word not in stopwords:
            if word != '\t':
                text_with_spaces += word + " "
    return text_with_spaces

def search_sentences(sentences,word):
    for sentence in sentences:
        if re.search(word,sentence):
            return sentence
    return word 

def sentence_preprocess(res):
    res = res.replace("\'","\"")
    res = json.loads(res)
    text_all = ""
    if res is not None and "sentences" in res:
        if res["sentences"]:
            for index,sent in enumerate(res["sentences"]):
                    tex = sent["text"]
                    if len(tex) >= 7:
                        #print(tex)
                        text_all = text_all + "#".join(tex.split(","))+"#"
                        #print(text_all)
    
    text_all = text_all.split("#")
    text_all_list = []
    text_old = []
    for tex in text_all:
        if len(tex) <= 5:
            continue
        else:
            tex = ''.join(tex.split('?'))
            tex = ''.join(tex.split('?'))
            tex = ''.joi
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值