StanfordParser句法分析工具指南

一个简易的Stanford parser系统只需要包含四类文件,它们分别是:

①java包(最新版本为stanford-parser-3.5.2.jar)

②模板(英文:englishFactored.ser.gz/englishPCFG.ser.gz/wsjFactored.ser.gz/wsjPCFG.ser.gz;

         中文:chinesePCFG.ser.gz /chineseFactored.ser.gz/xinhuaFactored.ser.gz/xinhuaPCFG.ser.gz)、

③输入文件(一般为.txt后缀的分词文件)

④输出文件(一般为.parse后缀的成分句法树文件或是以.dep后缀的依存句法树文件)

1、英文句法分析:

举例:

java -mx1g -cp stanford-parser-3.5.2.jar edu.stanford.nlp.parser.lexparser.LexicalizedParser -maxLength 100 -outputFormat oneline -sentences newline -outputFormatOptions  removeTopBracket englishFactored.ser.gz input.txt > output.std_Factored_parse 

参数解释:  

①-mx1g:给java虚拟机分配的最大内存为1g(大小可自行设置)。 

②-cp :为了加载java包stanford-parser-3.5.2.jar  LexicalizedParser:parser类.  

③-maxLength:指定句子单词长度最大为100。 

④–outputFormat:指定输出句子的格式。

     outputFormat具体选项值如下:

      Oneline:成分句法分析输出文件的格式为每行一句的广义表形式的树结构。

      Penn:成分句法分析输出文件的格式为层次化树的形式。默认选项为penn。

      latexTree:格式类似于penn  

      Words:只给出分词格式。如:继续  播报  详细  的  新闻  内容。

      wordsAndTags :给出分词文本和标记。如:继续/VV  播报/VV  详细/VA  的/DEC  新闻/NN  内容/NN 。/PU  

      rootSymbolOnly:只给出ROOT结点 

      typedDependencies:给出依存句法分析结果。

          mmod(播报-2, 继续-1) 

          rcmod(内容-6, 详细-3) 

          cpm(详细-3, 的-4) 

          nn(内容-6, 新闻-5) 

         dobj(播报-2, 内容-6)

      typedDependenciesCollapsed:压缩格式的依存句法分析结果

      xmlTree:XML格式的依存句法树

      collocations:

      semanticGraph:

      conllStyleDependencies: CoNLL格式(每行一词,每词十项)

      conll2007:

           1 Radio _ NN NN _ 2 compound _ _

           2 programs _ NNS NNS _ 6 nsubj _ _
           3 and _ CC CC _ 2 cc _ _
    4 books _ NNS NNS _ 2 conj _ _
    5 have _ VBP VBP _ 6 aux _ _
    6 followed _ VBN VBN _ 0 root _ _
    7 the _ DT DT _ 10 det _ _
    8 daily _ JJ JJ _ 10 amod _ _
    9 television _ NN NN _ 10 compound _ _
    10 show _ NN NN _ 6 dobj _ _ 

⑤-escaper:字符的标准化(例如将英文的”(”改成”-LRB-”,默认情况即这样转换)。

   英文的escaper为 edu.stanford.nlp.process.PTBEscapingProcessor。

   中文为:edu.stanford.nlp.trees.international.pennchinese.ChineseEscaper。

   举例:  

   java -mx500m -cp stanford-parser.jar edu.stanford.nlp.parser.lexparser.LexicalizedParser -escaper          edu.stanford.nlp.trees.international.pennchinese.ChineseEscaper -sentences newline  chineseFactored.ser.gz chinese-onesent > chinese-onesent.stp 

⑥-sentences:指定句子之间的边界,一般为newline :输入文件的句子通过换行符分割。Parser得到的文本是每行一句,一句一句的进行分析。  

⑦-encoding:指定输入输出文件的字符集。(中文默认为GB18030)  

⑧-outputFormatOptions:进一步控制各种–outputFormat选项的输出行为(可以说是–outputFormat的附加选项)。

   当–outputFormat 为typedDependencies时,-outputFormatOptions可有如下选项(默认选项为collapsed   dependencyies):

   举例:

   basicDependencies:基本格式  
   treeDependencies:以树结构保存的压缩依存关系(去除依存图中一些边构成树)。                                      
   collapsedDependencies:压缩依存(不一定为树结构)  
       cc(makes-11, and-12)  
       conj(makes-11, distributes-13) 
   转化为:  
       Conj_and(makes-11, distributes-13)               
       CCPropagatedDependencies:带有连词依存传播的压缩依存。

 

⑨-writeOutputFiles:产生对应于输入文件的输出文件,输出文件名同输入文件,只是增加了”.stp”的后缀。-outputFilesExtension:指定输出文件扩展名,默认为”.stp”  

 ⑩-outputFilesDirectory :指定输出文件目录,默认为当前目录。

       在这一小节中,我们用到的parser类为parser.lexparser.LexicalizedParser,这个类既能生成基于短语结构的成分句法树(指定输出格式为penn或oneline),又可以生成基于依存结构的依存句法树(指定输出格式为typedDependencies)。  

       接下来,我们用到的类名为:trees.EnglishGrammaticalStructure。我们使用这个类将已经是成分句法树结构(penn Treebank-style trees)转化为依存句法树结构。这里的成分句法树来源,既可以是stanford parser生成的,又可以是其他种类的parser(如:berkeley parser、charniak parser)生成的。

2、依存句法分析

      举例:  

java -mx1g -cp stanford-parser.3.5.2.jar  edu.stanford.nlp.trees.EnglishGrammaticalStructure -treeFile input.tree -basic -collapsed -extraSep -keepPunct -parserFile englishPCFG.ser.gz >output.deptree 

 

选项解释(与LexicalizedParser相同的选项省略) 输出文件的树结构可以通过以下参数直接指定:

 -basic:basic dependencies 

 -conllx :basic dependencies printed out in CoNLL X (CoNLL 2006) format 

 -CCprocessed: collapsed dependencies with propagation of conjunctdependencies (not necessarily a tree structure) 

 -collapsedTree:collapsed dependencies that preserve a tree structure 

 -nonCollapsed: non-collapsed dependencies: basic dependencies as well as the extra ones which do  not preserve a tree structure 

 -collapsed:collapsed dependencies (not necessarily a tree structure)

其他参数: 

 -treeFile:指定输入文件,即成分句法树结构文件。 

 -extraSep:如果存在多种类型的输出格式,使用分隔符(------)将basicdependencies和其他形式的分隔开。 

 -keepPunct:默认不输出符号依存关系,可以通过该选项保留。

 

3、trees.EnglishGrammaticalStructure其他功能 

①trees.EnglishGrammaticalStructure还可以将CoNLL格式的依存关系转化为其他类型的依存关系。可以通过-conllxFile来指定输入文件。

②它还可以parse分词文档,不过对输入文件的要求更高。必须是每行一句。只能指定依存类型,不能指定其他选项。用-sentFile替代-treeFile指定输入文件,需要用-parserFile选项指定模板文件,使用-parseTree选项打印句法树。 

举例:

java -mx100m edu.stanford.nlp.trees.EnglishGrammaticalStructure -sentFile file.txt 

-collapsedTree -CCprocessed -parseTree –parserFile englishPCFG.ser.gz

 

4、中英文模板说明

Stanford parser不需要我们自己训练模板,它为我们提供了现成的模板。  英文模板: englishFactored.ser.gz/englishPCFG.ser.gz/wsjFactored.ser.gz/wsjPCFG.ser.gz englishPCFG.ser.gz仅仅包含一个未词汇化的概率上下文无关文法。englishFactored.ser.gz包含两种语法(a (simpler) PCFG parser and then an untyped dependency parser)。对英文来说,虽然上述两种模板的语法和分析方法都不同,但是两者的平均性能相似。所以,一般倾向于使用快一点的模板englishPCFG.ser.gz。对于其他语言(如中文),由于factored模板包含词汇化信息,factored类型模板的性能也明显的好于PCFG模板的性能。

 

5、未分词文本直接parse举例: java -mx500m -cp stanford-parser.jar edu.stanford.nlp.parser.lexparser.LexicalizedParser xinhuaFactoredSegmenting.ser.gz chinese-onesent-unseg.txt | & iconv -f gb18030 -t utf-8 

解释“| & iconv -f gb18030 -t utf-8”如下: 

Linux环境下,必须转换编码格式才可以显示parser的输出结果。此处使用管道将parser的结果送到unix字符集转换工具中。

 

 

参考:http://wenku.baidu.com/view/8d672929ed630b1c59eeb595.html

转载于:https://www.cnblogs.com/MeTeor-Lzz/p/5142101.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值