美文网首页
基于NLP的摘要生成工具(java)

基于NLP的摘要生成工具(java)

作者: Deo_Prime | 来源:发表于2020-11-23 17:01 被阅读0次

使用了三种工具, 做一下笔记, 分别为: 百度ai开放平台, nlpir(中科院), hanlp(开源软件)
这三个平台都提供了基于nlp的非常全面的场景解决方案, 这里只讨论摘要生成这一个场景.

百度ai开放平台

需要注意: 个人用户累计只有50w次调用机会, 且qps为限制为2[意为一秒调用不得超过2次]
入口地址: https://ai.baidu.com/tech/nlp_apply/news_summary
api地址: https://ai.baidu.com/ai-doc/NLP/Nk6z52ci5#%E6%96%B0%E9%97%BB%E6%91%98%E8%A6%81%E6%8E%A5%E5%8F%A3
使用过程:

  1. 注册帐号
  2. 新增应用(获得appid, key, secret)
  3. 开发程序
pom.xml添加:
        <dependency>
            <groupId>com.baidu.aip</groupId>
            <artifactId>java-sdk</artifactId>
            <version>4.15.1</version>
        </dependency>

application.properties:
nlp.nlpir.dataPath=E:/TEMP2/lib
digest.length = 200

java程序:
    @Value("${digest.length}")
    private int length;

    @ResponseBody
    @RequestMapping("/baidu")
    public String baidu(String title, String src) {
        AipNlp client = new AipNlp(appId, apiKey, secretKey);
        HashMap<String, Object> options = new HashMap<String, Object>();
        options.put("title", title);

        // 新闻摘要接口
        JSONObject res = client.newsSummary(src, length, options);
        String result = res.getString("summary");
        if (result.length() > length) result = result.substring(0, length) + "...";
        return result;
    }

hanlp

首页地址: https://www.hanlp.com/
需要注意: 这个工具提供的摘要算法, 需要指定一个数值类型参数, 意为希望获得几个句子[根据权重倒排]
开发过程:

pom.xml添加依赖:
        <dependency>
            <groupId>com.hankcs</groupId>
            <artifactId>hanlp</artifactId>
            <version>portable-1.7.8</version>
        </dependency>

application.properties:
nlp.nlpir.dataPath=E:/TEMP2/lib
digest.length = 200

java程序:

    @Value("${digest.length}")
    private int length;

    @ResponseBody
    @RequestMapping("/hanlp")
    public String hanlp(String title, String src) {
        List<String> strings = HanLP.extractSummary(src, 3); // 注意数值型参数
        if (strings == null || strings.size() <= 0) return null;
        String result = StringUtils.join(strings, "。");
        if (result.length() > length) result = result.substring(0, length) + "...";
        return result;
    }

nlpir

注意: 使用过程, 需要每个月更新一次license, 从这里下载license: https://github.com/NLPIR-team/NLPIR/tree/master/License/license%20for%20a%20month
每个月需要将下载的文件替换到lib/Data目录下.
github地址: https://github.com/NLPIR-team/Summary

java本地工程, 直接导入github的下载结果即可.
对于javaweb的部署过程要点:

  1. 将解压后的resource目录下的全部内容, 放到classes目录
  2. 将src下的两个类文件保存到自己的项目类目录中
  3. 将lib目录保存到文件系统
  4. 示例程序如下:
application.properties:
nlp.nlpir.dataPath=E:/TEMP2/lib
digest.length = 200

java代码:

    @Value("${digest.length}")
    private int length;

    @Value("${nlp.nlpir.dataPath}")
    private String nlpirDataPath;

    @ResponseBody
    @RequestMapping("/nlpir")
    public String nlpir(String title, String src) {
        Summary.init(nlpirDataPath);
        String result = Summary.singleDoc(src, 0, length, 0);
        if (result == null || result.length() <= 0) return null;
        if (result.length() > length) result = result.substring(0, length) + "...";
        return result;
    }

相关文章

网友评论

      本文标题:基于NLP的摘要生成工具(java)

      本文链接:https://www.haomeiwen.com/subject/bfjviktx.html