栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 前沿技术 > 人工智能 > NLP

自然语言处理NLP——提取文章摘要

NLP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

自然语言处理NLP——提取文章摘要

首先,在这里感谢下编写这些代码的大佬们!!!

我先声明下

为了方便使用,我这里只是整理(CV)了网上的几种提取摘要的的使用方法,不做任何代码解析,毕竟我也不懂算法。

这几种方法我都成功测试过了,但是提取出来数据是有差异的,这里建议这几种方法对比参考后再使用。

如果想深入了解逻辑的话,可以反编译下源码。

一、Java,使用Classifier4J

使用该方法,需要引入classifier4J.jar

下载链接:Classifier4J-0.6.zip_NLP自然语言处理提取摘要-Java工具类资源-CSDN下载

支持英文提取,不支持中文提取

import net.sf.classifier4J.summariser.ISummariser;
import net.sf.classifier4J.summariser.SimpleSummariser;

public class Classifier4J {
    public static void main1(String[] args) {
        String str= "Here is the content of the article";
        //SimpleSummariser s = new SimpleSummariser();
        ISummariser s = new SimpleSummariser();
        String result = s.summarise(str, 1);
        System.out.println(result);
    }
}
二、Java,使用HanLP

需要引入maven依赖



	com.hankcs
	hanlp
	portable-1.7.3

需要添加配置文件 hanlp.properties,本地添加字典库(hanlp.properties自定义路径),

文件下载链接:hanlp.properties_-Java文档类资源-CSDN下载

字典库太大,上传不上去,需要的话私信给我

支持中文、英文提取

注意:使用Hanlp这种方式提取摘要,Hanlp的字典库很大,电脑内存条得特别大才行,运行内存小于12G,就别碰了

import com.hankcs.hanlp.HanLP;

public class HanLP {
	public static void main(String[] args) {
		String str1= "此处为文章内容";
		String str2= "Here is the content of the article";
		List result1 = HanLP.extractSummary(str1, 1);//自定义需要摘要的长度
		System.out.println(result1.toString());		
	}
}
三、Python,使用NLTK

需要下载nltk及插件

# 下载nltk
pip install nltk

# 通过cmd进入python控制台 (cmd输入python进入)

# 下载punkt、stopwords
>>> import nltk
>>> nltk.download('punkt')
>>> nltk.download('stopwords')
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.corpus import stopwords
from collections import defaultdict
from string import punctuation
from heapq import nlargest

stopwords = set(stopwords.words('english') + list(punctuation))
max_cut = 0.9
min_cut = 0.1
"""
计算出每个词出现的频率
word_sent 是一个已经分好词的列表
返回一个词典freq[],
freq[w]代表了w出现的频率
"""
def compute_frequencies(word_sent):
    """
    defaultdict和普通的dict
    的区别是它可以设置default值
    参数是int默认值是0
    """
    freq = defaultdict(int)
    # 统计每个词出现的频率
    for s in word_sent:
        for word in s:
            # 注意stopwords
            if word not in stopwords:
                freq[word] += 1

    # 得出最高出现频次m
    m = float(max(freq.values()))
    # 所有单词的频次统除m
    for w in list(freq.keys()):
        freq[w] = freq[w] / m
        if freq[w] >= max_cut or freq[w] <= min_cut:
            del freq[w]
    # 最后返回的是
    # {key:单词, value: 重要性}
    return freq


def summarize(text, n):
    """
    用来总结的主要函数
    text是输入的文本
    n是摘要的句子个数
    返回包含摘要的列表
    """
    # 首先先把句子分出来
    sents = sent_tokenize(text)
    assert n <= len(sents)
    # 然后再分词
    word_sent = [word_tokenize(s.lower()) for s in sents]
    # freq是一个词和词重要性的字典
    freq = compute_frequencies(word_sent)
    # ranking则是句子和句子重要性的词典
    ranking = defaultdict(int)
    for i, word in enumerate(word_sent):
        for w in word:
            if w in freq:
                ranking[i] += freq[w]
    sents_idx = rank(ranking, n)
    return [sents[j] for j in sents_idx]
"""
考虑到句子比较多的情况
用遍历的方式找最大的n个数比较慢
我们这里调用heapq中的函数
创建一个最小堆来完成这个功能
返回的是最小的n个数所在的位置
"""
def rank(ranking, n):
    return nlargest(n, ranking, key=ranking.get)

# str.txt内容就是需要提取摘要的文章
if __name__ == '__main__':
    with open("str.txt", "r" , encoding="utf-8") as myfile:
        text = myfile.read()
        text = text.replace('n','')
    res = summarize(text, 1)
    for i in range(len(res)):
        print(res[i])

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/834447.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号