java parser .java_Stanford Parser使用之 Eclipse+java调用

时间:2024-10-02 19:37:03

==================================================================================

官网-》Download

Stanford Parser version

3.2.0-》

==================================================================================

解压缩:-》stanford-parser-full

部分如下

-》解压缩-》stanford-parser文件夹

-》解压缩stanford-parser-3.2.-》stanford-parser-3.2.0-models文件夹-》将下面的

edu\stanford\nlp\models\lexparser中的拷贝到工程的source文件夹下。

==================================================================================

使用:

首先要将文件加载到lib文件夹中。右键build

path->add to build

path. 只加载即可。

其中参考1中将

错的!!!

也加入到Referenced Libraries中,在本地报错,不要加入。

另外在初始化要写绝对路径,也和参考的不一样!

更新:拷贝到工程的source文件夹下即可。

工程的层次结构图:

stanfordpatser工程名

src代码

stanfordparser包

lib

source

==================================================================================

本地.java中调用的代码

//LexicalizedParser lp =

("...\\stanford-parser-

//full\\");//本地中为绝对路径

//相对路径即可

LexicalizedParser lp =

("source/");

String subsen = "One beer later and I'm

walking down the street smoking a cig with them";

PTBTokenizer ptb =

(new StringReader(subsen));

List words = ();

((words));

=================

结果:

(ROOT (S (NP (NP (CD One) (NN beer) (RB

later)) (CC and) (NP (PRP I))) (VP (VBP 'm) (VP (VBG walking) (PRT

(RP down)) (NP (NP (DT the) (NN street)) (VP (VBG smoking) (NP (DT

a) (NN cig)) (PP (IN with) (NP (PRP them)))))))))

==================================================================================

自带的样例

public static void main(String[] args) {

//LexicalizedParser lp =

("D:\\my

download\\Parser\\Stanford

//Parser\\stanford-parser-full\\");

//相对路径即可

LexicalizedParser lp =

("source/"); demoAPI(lp);

}

public static void demoAPI(LexicalizedParser lp)

{

// This option shows parsing a list

of correctly tokenized

words第一块 String[] sent = { "This", "is", "an", "easy", "sentence", "."

};

List

rawWords = (sent);

Tree parse =

(rawWords);

();

();

// This option shows loading and

using an explicit

tokenizer第二块 String sent2 = "This is another sentence.";

TokenizerFactory tokenizerFactory =

(new CoreLabelTokenFactory(), "");

List

rawWords2 =

(new

StringReader(sent2)).tokenize();

parse =

(rawWords2);

TreebankLanguagePack tlp = new PennTreebankLanguagePack();

GrammaticalStructureFactory gsf =

();

GrammaticalStructure gs = (parse);

List tdl =

();

(tdl);

//for(TypedDependency tdl1:tdl){

// (tdl1); //例如输出完整的:nsubj(sentence-4, This-1)

// (()); //例如输出支配地位的:sentence-4

// (()); //例如输出从属地位的:This-1

// (());//例如输出关系:nsubj

// }

();

输出第三块

TreePrint tp = new

TreePrint("penn,typedDependenciesCollapsed"); (parse);

}

输出结果:

Loading parser from serialized file D:\my

download\Parser\Stanford

Parser\stanford-parser-full\ ... done [1.6

sec].

Loading parser from

serialized file source/ ... done [1.4

sec].(ROOT

(S

(NP (DT

This))

(VP (VBZ

is)

(NP (DT an) (JJ easy) (NN sentence)))

(.

.)))

[nsubj(sentence-4, This-1),

cop(sentence-4, is-2), det(sentence-4, another-3), root(ROOT-0,

sentence-4)]

(ROOT

(S

(NP (DT

This))

(VP (VBZ

is)

(NP (DT another) (NN sentence)))

(.

.)))

nsubj(sentence-4, This-1)

cop(sentence-4, is-2)

det(sentence-4, another-3)

root(ROOT-0, sentence-4)