最近在研究一段时间的NLP。。试用Stanford Parser初体验。。
先说下本人对语义这一块看法,欢迎打脸。。个人感觉国内很多此类方向的研究,方向和技术层面确实很先进,但真正不能实际应
用到项目中。很多只是个噱头罢了。上一次研究的Ontology的语义检索。。就当学习吧。
Stanford Parser 最新版本下载地址:http://nlp.stanford.edu/software/lex-parser.shtml。Stanford Parser通过对主语、谓语和宾语 的分析去切分语句。
解压运行lexparser-gui.bat ,这是一个可执行的可视化界面窗口。点击Load Parser选择模型文件,Stanford Parser 有两个重要的
jar包,stanford-parser,stanford-parser-3.3.0-models。其中stanford-parser-3.3.0-models 里面有其模型文件,解压该jar包。选择
xinhuaFactoredSegmenting.ser.gz这个新华网的模型文件。在程序中加载模型文件需要一段时间。输入分析的语句,效果图如下:
把stanford-parser,stanford-parser-3.3.0-models 这两jar包引入项目。实现代码如下:
package test;
import java.util.Collection;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TypedDependency;
import edu.stanford.nlp.trees.international.pennchinese.ChineseGrammaticalStructure;
/**
* LexicalizedParser.loadModel(modelpath); 加载模型比较消耗时间,可以把这一步以加载到服务中启动
* @author 胡慧超
*
*/
public class test1 {
public static void main(String[] args) {
String modelpath="edu/stanford/nlp/models/lexparser/xinhuaFactoredSegmenting.ser.gz";
String str="清华大学生 说正在研究生命起源。";
LexicalizedParser lp = LexicalizedParser.loadModel(modelpath);
Tree t = lp.parse(str);
// TokenizerFactory<CoreLabel> tokenizerFactory =PTBTokenizer.factory(new CoreLabelTokenFactory(), "");
// List<CoreLabel> rawWords2 =tokenizerFactory.getTokenizer(new StringReader(str)).tokenize();
// Tree t1 = lp.apply(rawWords2);
// ChineseTreebankLanguagePack tlp = new ChineseTreebankLanguagePack();
// GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
ChineseGrammaticalStructure gs = new ChineseGrammaticalStructure(t);
// List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
Collection<TypedDependency> tdl = gs.typedDependenciesCollapsed();
System.out.println(tdl.toString());
String s="";
for(int i = 0;i < tdl.size();i ++)
{
TypedDependency td = (TypedDependency)tdl.toArray()[i];
String age = td.dep().nodeString();
s+=age+"/";
}
System.out.println(s);
}
}