Stanford parser:入门使用

链接:https://www.cnblogs.com/stGeekpower/p/3457746.html

一、stanford parser是什么?
stanford parser是stanford nlp小组提供的一系列工具之一,能够用来完成语法分析任务。支持英文、中文、德文、法文、阿拉伯文等多种语言。

可以从这里(http://nlp.stanford.edu/software/lex-parser.shtml#Download)下载编译好的jar包、源码、javadoc等等。

http://nlp.stanford.edu/software/parser-faq.shtml是FAQ,看一下FAQ基本就能明白很多东西。当然,你得懂英文是吧?哈哈。

二、stanford parser怎么用(针对中文)?
这里只说如何在java工程中调用相关功能。从上面的地址下载到压缩包后,解压缩,将下面两个jar包加入到java build path里即可:

stanford-parser.jar
stanford-parser-xxx-models.jar
在stanford-parser.jar!\edu.stanford.nlp.parser.lexparser.demo包下面有两个最简单的例子,是可以直接运行的。

不过例子给出的是英文的使用方法,我们处理的中文还是有些不一样的。不过,在一中提到的FAQ页面上,是有简单的如何处理中文的使用方法的(第24问),不过处理方式是直接用java命令来做。

FAQ中给出的处理命令如下:

$ java -server -mx500m edu.stanford.nlp.parser.lexparser.LexicalizedParser -encoding utf-8 /u/nlp/data/lexparser/chineseFactored.ser.gz chinese-onesent-utf8.txt
很明显,所有的东西都是从edu.stanford.nlp.parser.lexparser.LexicalizedParser这个类开始的。所以我们只要把这个类的main函数搞清楚,如何处理中文我们大概也就知道了。

在这之前,先看一下训练好的中文grammars:

The parser is supplied with 5 Chinese grammars (and, with access to suitable training data, you could train other versions). You can find them inside the supplied stanford-parser-YYYY-MM-DD-models.jar file (in the GUI, select this file and then navigate inside it; at the command line, use jar -tf to see its contents). All of these grammars are trained on data from the Penn Chinese Treebank, and you should consult their site for details of the syntactic representation of Chinese which they use. They are:

image

The PCFG parsers are smaller and faster. But the Factored parser is significantly better for Chinese, and we would generally recommend its use. The xinhua grammars are trained solely on Xinhua newspaper text from mainland China. We would recommend their use for parsing material from mainland China. The chinese grammars also include some training material from Hong Kong SAR and Taiwan. We’d recommend their use if parsing material from these areas or a mixture of text types. Note, though that all the training material uses simplified characters; traditional characters were converted to simplified characters (usually correctly). Four of the parsers assume input that has already been word segmented, while the fifth does word segmentation internal to the parser. This is discussed further below. The parser also comes with 3 Chinese example sentences, in files whose names all begin with chinese.

三、LexicalizedParser类main函数分析
先从javadoc了解下基本用法,main函数支持多个选项。可以用来从treebank data完成建立和序列化一个解析器,可以解析文件或者url页面内容中的句子。

主要就是训练生成解析器,用解析器解析句子两大功能。以下摘自main函数的javadoc:

Sample Usages:

Train a parser (saved to serializedGrammarFilename) from a directory of trees (trainFilesPath, with an optional fileRange, e.g., 0-1000):
java -mx1500m edu.stanford.nlp.parser.lexparser.LexicalizedParser [-v] -train trainFilesPath [fileRange] -saveToSerializedFile serializedGrammarFilename
Train a parser (not saved) from a directory of trees, and test it (reporting scores) on a directory of trees :
java -mx1500m edu.stanford.nlp.parser.lexparser.LexicalizedParser [-v] -train trainFilesPath [fileRange] -testTreebank testFilePath [fileRange]
Parse one or more files, given a serialized grammar and a list of files :
java -mx512m edu.stanford.nlp.parser.lexparser.LexicalizedParser [-v] serializedGrammarPath filename [filename] …
Test and report scores for a serialized grammar on trees in an output directory :
java -mx512m edu.stanford.nlp.parser.lexparser.LexicalizedParser [-v] -loadFromSerializedFile serializedGrammarPath -testTreebank testFilePath [fileRange]
如果 serializedGrammarPath 以.gz结尾, 那么grammar是以gzip格式来读写的。

如果serializedGrammarPath是一个URL, 以http://开始,则会从URL读取解析器。

fileRange参数 specifies a numeric value that must be included within a filename for it to be used in training or testing (this works well with most current treebanks). It can be specified like a range of pages to be printed, for instance as 200-2199 or 1-300,500-725,9000 or just as 1 (if all your trees are in a single file, just give a dummy argument such as 0 or 1).

解析器可以将语法分写成ca序列化的Java object文件,或者输出到文本文件,或者同时输出两种方式,用如下命令来使用:

java edu.stanford.nlp.parser.lexparser.LexicalizedParser [-v] -train trainFilesPath [fileRange] [-saveToSerializedFile grammarPath] [-saveToTextFile grammarPath]
如果没有提供要解析的文件,则一个默认的句子会被解析。

Parameters:在-v 同样的位置,可以有很多其他的选项,比较常用的如下(水平有限,有的英文就懒的翻译了):

-tLPP class 当使用除英文外的语言或者English Penn Treebank之外的Treebank时候需要指定TreebankLangParserParams,该选项必须出现在其他的与语言相关的选项之前。(即使是导入一个序列化grammar时候,也建议制定该选项;it is necessary if the language pack specifies a needed character encoding or you wish to specify language-specific options on the command line.)

-encoding charset 指定输入输出文件的编码类型。 当这个选项出现在-tLPP选项后时,会覆盖TreebankLangParserParams中设置的值。

-tokenized 输入是否已经完成分词(以空白符分割各词)。此选项存在则忽略分词处理,只使用whitespace进行分词。除非用-escaper指定特殊的escape,否则需要确保分词结果中的特殊符号符合所用的Treebank。(例如,如果用Penn English Treebank, 必须将”(” 转为 “-LRB-“, “3/4” 转为 “3\/4”, 等等.)

-escaper class 指定一个Function

猜你喜欢

转载自blog.csdn.net/WitsMakeMen/article/details/81413904