在项目的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>