自然语言处理(NLP)Machine Learning & Recommendation & NLP & DL

【Java】文本聚类

2020-04-24  本文已影响0人  Hao_38b9

【Java】文本聚类

前言:

由于接到一个任务,大概是对回复内容的质量进行评估(有点类似于情感分类),由于这种分类,没有定性的指标,只能认为规定 好——坏 之间的几个梯度指标,但由于有些回复的内容过长,人工打标签的时候不方便,我就想着使用 主题抽取模型抽取长文本转化为短文本,再利用 文本聚类 的方法,把相似的文本存放到一起

准备工作

由于需要处理表格数据,这里我用到了java的 poi 框架

安装:

由于我是使用mavan构建的项目,所以直接添加依赖就可以了

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.11</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.11</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>3.11</version>
</dependency>

文本聚类和主题抽取

我直接采用hanlp这个框架中的模型

安装和使用教程参考: https://github.com/hankcs/HanLP/blob/1.x/README.md

项目流程

项目流程.png

具体实现

package com.NLP.test;


import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.mining.cluster.ClusterAnalyzer;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Set;

public class TextCluster {
    public static void main(String[] args) throws IOException {
        //聚类器分析器
        ClusterAnalyzer<String> analyzer = new ClusterAnalyzer<String>();
        //读入数据
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream("附件4.xlsx"));
        XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
        
        //
        int count = xssfSheet.getLastRowNum();
        for(int i = 0;i<=count;i++){
            XSSFRow xssfRow = xssfSheet.getRow(i);
            XSSFCell xssfCell = xssfRow.createCell(7,5);
            XSSFCell cateCell = xssfRow.createCell(8);
            cateCell.setCellValue(-1);
            if(i!=0){
                String text = xssfRow.getCell(5).getStringCellValue();
                String  id = String.valueOf(xssfRow.getCell(0).getNumericCellValue());
                if(text.length()<50){
                }else {
                    List<String> results = HanLP.extractSummary(text, 2);
                    String tempTest="";
                    for(String item:results){
                        tempTest+=item+",";
                    }
                    text = tempTest;
                }
                xssfCell.setCellValue(text);
                analyzer.addDocument(id,text);
            }else {
                xssfCell.setCellValue("shortText");
            }
        }
        System.out.println(analyzer.repeatedBisection(1.0));
        int categorynum = 0;
        for(Set<String> item:analyzer.repeatedBisection(1.0)){
            for(String uid:item){
                for(int i = 1;i<=count;i++){
                    XSSFRow xssfRow = xssfSheet.getRow(i);
                    String  id = String.valueOf(xssfRow.getCell(0).getNumericCellValue());
                    if(id.equals(uid)){
                        xssfRow.getCell(8).setCellValue(categorynum);
                    }
                }
            }
            categorynum++;
        }
        //生成数据
        xssfWorkbook.write(new FileOutputStream("附件4_转换.xlsx"));
    }
}

上一篇 下一篇

猜你喜欢

热点阅读