英文ZPar统计分词/解析器的包装器
项目描述
简介
python-zpar 是围绕ZPar解析器的一个Python包装器。ZPar是在Yue Zhang在牛津大学时编写的。根据其主页上的描述: ZPar是一个统计自然语言解析器,执行包括词性标注、词性标注和解析在内的句法分析任务。ZPar支持多种语言和多种语法形式。ZPar主要针对中文和英语进行了大量开发,同时也为其他语言提供了通用支持。ZPar速度快,使用标准宾州大学(华尔街日报)数据每秒处理超过50个句子。
我编写了python-zpar,因为我需要一个快速高效的解析器来处理我的NLP工作,这些工作主要在Python中进行,而不是C++。我希望能够直接从Python中使用这个解析器,而无需创建大量文件并通过子进程运行它们。python-zpar不仅提供了一个简单的Python包装器,还提供了一个XML-RPC ZPar服务器,使得批量处理大文件变得更容易。
python-zpar使用ctypes,这是Python附带的一个非常酷的外部函数库,允许直接调用C DLL或共享库中的函数。
安装
目前,python-zpar仅适用于64位Linux和OS X系统。这两个平台是我每天使用的。我愿意随着时间的推移尝试让python-zpar在其他平台上运行。欢迎提交拉取请求!
为了让python-zpar正常运行,它需要可以直接调用的C函数。由于ZPar中唯一对外暴露的入口点是命令行客户端,因此我需要编写一个共享库,该库基于ZPar功能构建函数,并以ctypes能够理解的方式暴露它们。
因此,为了从头开始构建python-zpar,我们需要下载ZPar源代码,添加新的功能,并编译共享库。当您使用pip安装时,所有这些操作都会自动完成。
pip install python-zpar
重要:在OS X上,安装时只能使用通过macports或homebrew安装的gcc。zpar源代码不能使用clang编译。如果您在克隆存储库或使用pip安装包后遇到编译代码的问题,您可以尝试显式覆盖C++编译器。
CXX=<path to c++ compiler> make -e
或
CXX=<path to c++ compiler> pip install python-zpar
如果您想了解共享库模块中的C函数的样子,请参阅src/zpar.lib.cpp。
用法
要使用python-zpar,您需要ZPar的英语模型。您可以从ZPar发布页面这里下载。有三个模型:一个词性标注器、一个依存句法分析器和一个依存关系分析器。以下示例中使用的模型位于当前目录的english-models目录中。
以下是一个使用python-zpar的示例:
from six import print_
from zpar import ZPar
# use the zpar wrapper as a context manager
with ZPar('english-models') as z:
# get the parser and the dependency parser models
tagger = z.get_tagger()
depparser = z.get_depparser()
# tag a sentence
tagged_sent = tagger.tag_sentence("I am going to the market.")
print_(tagged_sent)
# tag an already tokenized sentence
tagged_sent = tagger.tag_sentence("Do n't you want to come with me to the market ?", tokenize=False)
print_(tagged_sent)
# get the dependency parse of an already tagged sentence
dep_parsed_sent = depparser.dep_parse_tagged_sentence("I/PRP am/VBP going/VBG to/TO the/DT market/NN ./.")
print_(dep_parsed_sent)
# get the dependency parse of an already tokenized sentence
dep_parsed_sent = depparser.dep_parse_sentence("Do n't you want to come with me to the market ?", tokenize=False)
print_(dep_parsed_sent)
上述代码示例会产生以下输出
I/PRP am/VBP going/VBG to/TO the/DT market/NN ./.
Do/VBP n't/RB you/PRP want/VBP to/TO come/VB with/IN me/PRP to/TO the/DT market/NN ?/.
I PRP 1 SUB
am VBP -1 ROOT
going VBG 1 VC
to TO 2 VMOD
the DT 5 NMOD
market NN 3 PMOD
. . 1 P
Do VBP -1 ROOT
n't RB 0 VMOD
you PRP 0 SUB
want VBP 0 VMOD
to TO 5 VMOD
come VB 3 VMOD
with IN 5 VMOD
me PRP 6 PMOD
to TO 5 VMOD
the DT 10 NMOD
market NN 8 PMOD
? . 0 P
包含注释的详细用法请参阅包含的文件examples/zpar_example.py。运行python zpar_example.py -h以查看所有可用选项的列表。
ZPar服务器
该软件包还提供了一个ZPar服务器的python XML-RPC实现,这使得通过只加载模型一次(通过ctypes接口)并允许客户端连接和请求分析,更容易处理多个句子和文件。实现包含在安装软件包时安装的可执行文件zpar_server中。服务器非常灵活,仅加载所需的模型。以下是一个只加载标注器和依存关系分析器模型的启动服务器的示例
$> zpar_server --modeldir english-models --models tagger parser depparser
INFO:Initializing server ...
Loading tagger from english-models/tagger
Loading model... done.
Loading constituency parser from english-models/conparser
Loading scores... done. (65.9334s)
Loading dependency parser from english-models/depparser
Loading scores... done. (14.9623s)
INFO:Registering introspection ...
INFO:Starting server on port 8859...
运行zpar_server -h以查看所有选项的列表。
一旦服务器启动,您就可以使用客户端连接到它。示例客户端包含在文件examples/zpar_client.py中,可以按照以下方式运行(请注意,如果您在运行服务器时指定了自定义的主机和端口,您需要在这里指定相同的参数)
$> cd examples
$> python zpar_client.py
INFO:Attempting connection to http://localhost:8859
INFO:Tagging "Don't you want to come with me to the market?"
INFO:Output: Do/VBP n't/RB you/PRP want/VBP to/TO come/VB with/IN me/PRP to/TO the/DT market/NN ?/.
INFO:Tagging "Do n't you want to come to the market with me ?"
INFO:Output: Do/VBP n't/RB you/PRP want/VBP to/TO come/VB to/TO the/DT market/NN with/IN me/PRP ?/.
INFO:Parsing "Don't you want to come with me to the market?"
INFO:Output: (SQ (VBP Do) (RB n't) (NP (PRP you)) (VP (VBP want) (S (VP (TO to) (VP (VB come) (PP (IN with) (NP (PRP me))) (PP (TO to) (NP (DT the) (NN market))))))) (. ?))
INFO:Dep Parsing "Do n't you want to come to the market with me ?"
INFO:Output: Do VBP -1 ROOT
n't RB 0 VMOD
you PRP 0 SUB
want VBP 0 VMOD
to TO 5 VMOD
come VB 3 VMOD
to TO 5 VMOD
the DT 8 NMOD
market NN 6 PMOD
with IN 5 VMOD
me PRP 9 PMOD
? . 0 P
INFO:Tagging file /Users/nmadnani/work/python-zpar/examples/test.txt into test.tag
INFO:Parsing file /Users/nmadnani/work/python-zpar/examples/test_tokenized.txt into test.parse
请注意,python-zpar以及所有示例脚本都应与Python 2.7和Python 3.4兼容。我已在Linux和Mac上测试了python-zpar,但未在Windows上测试。
Node.js版本
如果您想在node.js应用程序中使用ZPar,请查看我的其他项目node-zpar。
许可证
尽管python-zpar受MIT许可协议的约束——这意味着您可以随意使用包装代码——但ZPar本身受GPL v3许可协议约束。
待办事项
改进python和C两端的错误处理。
暴露更多功能,例如中文分词、解析等。
也许可以考虑使用CFFI而不是ctypes。