HanLP测试代码

在项目的src/main/resources下新建一个文本文件 testdata.txt

 

测试代码

package package03;

import com.hankcs.hanlp.seg.common.Term;
import com.hankcs.hanlp.tokenizer.NLPTokenizer;

import java.io.*;
import java.util.List;

public class Main {
    private String text;

    public static void main(String[] args) {
        String fileName = Main.class.getResource("/testdata.txt").getFile();
        File file = new File(fileName);
        BufferedReader br;
        StringBuffer sb = new StringBuffer();
        try {
            br = new BufferedReader(new FileReader(file));
            while (br.ready()) {
                sb.append(br.readLine().concat("\n"));
            }
            br.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        final String text = sb.toString();
        List<Term> terms = NLPTokenizer.segment(text);
        System.out.println(terms);
    }
}

  

 

<!-- hanlp -->
<dependency>
<groupId>com.hankcs</groupId>
<artifactId>hanlp</artifactId>
<version>portable-1.7.8</version>
</dependency>
上一篇:NLP入门学习中关于分词库HanLP导入使用教程


下一篇:HanLp句法分析和依存句法可视化、保存到json文件中