openNlp入门例子ngram
1.maven依赖
<dependency> <groupId>org.apache.opennlp</groupId> <artifactId>opennlp-tools</artifactId> <version>1.9.3</version> </dependency> <dependency> <groupId>org.apache.opennlp</groupId> <artifactId>opennlp-uima</artifactId> <version>1.9.3</version> </dependency>
2.java main函数
package net.highersoft.nlp; import opennlp.tools.ngram.NGramModel; import opennlp.tools.tokenize.WhitespaceTokenizer; import opennlp.tools.util.StringList; public class Test { public static void main(String[] args) { String sampleTxt="This is n-gram model "; StringList tokens=new StringList(WhitespaceTokenizer.INSTANCE.tokenize(sampleTxt)); System.out.println("tokens "+tokens); NGramModel ngram=new NGramModel(); ngram.add(tokens,3,4); System.out.println("Total ngrams:"+ngram.numberOfGrams()); for(StringList n:ngram) { System.out.println(ngram.getCount(n)+"-"+n); } } }
3.输出结果
tokens [This,is,n-gram,model] Total ngrams:3 1-[is,n-gram,model] 1-[This,is,n-gram] 1-[This,is,n-gram,model]
https://github.com/IsaacChanghau/Word2VecfJava
https://drive.google.com/file/d/0B7XkCwpI5KDYN1NUTT1SS21pQmM/edit?usp=sharing
相关阅读
评论:
↓ 广告开始-头部带绿为生活 ↓
↑ 广告结束-尾部支持多点击 ↑