Stanford jar 檔的安裝方式請參閱:
http://xken831.pixnet.net/blog/post/448840217-%5BTextMining%5DStanford%20parser%20%E4%BD%BF%E7%94%A8%E6%96%B9%E5%BC%8F
安裝好之後,請使用下列的 Sample code,就可以產出下列的結果:
import java.util.List;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.ling.Sentence;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
public class stanfordParserUtility {
public static void main(String args[]){
//model路徑
String parserModel = "edu/stanford/nlp/models/lexparser/chinesePCFG.ser.gz";
//讀取model
LexicalizedParser lp = LexicalizedParser.loadModel(parserModel);
demoAPI(lp);
}
public static void demoAPI(LexicalizedParser lp) {
String[] sent = { "我", "喜歡", "吃", "蘋果", "。" };
List rawWords = Sentence.toCoreLabelList(sent);
Tree parse = lp.apply(rawWords);
TreebankLanguagePack tlp = lp.treebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
List tdl = gs.typedDependenciesCCprocessed();
System.out.println(tdl);
System.out.println();
}
}
Dependency tree 主要是以動詞為主的樹狀結構,所以可以清楚看到 root(ROOT-0, 吃-3) 是這棵樹的第一個節點。
括弧中是節點與節點,而括弧的前方則是代表節點與節點的關係。
全站熱搜