我意识到完美地将主语名词短语和对象名词短语从句子中分离出来是一个开放的研究问题,这里不容易解释,但有一种聪明的方法可以做到这一点(假设我已经有一个POS标记的句子)大多数句子,或者至少相对简单的句子?我知道,简单地假设第一个名词短语是主题是一个非常好的近似,但在以介词短语开头的句子中(例如,"在整个清理过程中,通过流经过受惊的鹿."),这就失败了.理想情况下,我喜欢在这种情况下也能识别主题的东西.
作为参考,该例句给出了以下使用Stanford Parser的解析树:
[ROOT [S [PP [IN Across] [NP [NP [DT the] [NN clearing] ] [CC and] [NP [IN through] ] ] ] [NP [DT the] [NN stream] ] [VP [VBD ran] [NP [DT the] [ADJP [JJ frightened] ] [NNS deer] ] ] [. .] ] ]
我目前的策略如下:
主题:在树上做一个BFS,寻找第一个NP.
动词:在树上做一个BFS,寻找第一个VP.在这个子树上,做一个寻找VB的BFS(D | G | N | P | Z).
对象:在上面找到的VP子树上执行BFS,寻找NP.
这个策略在我的例子中产生以下结果:
SUBJECT: (NP (DT the) (NN stream) ) , VERB: (VBD ran) , OBJECT: (NP (DT the) (ADJP (JJ frightened) ) …
我没有很多面向对象Python的经验,想要重构一个简单的命令行工具.我当前的脚本只是导入了所需的库,有一些函数定义,使用全局变量(我知道这是不好的做法)并在一个.py文件中使用argparse,例如:
import argparse
dict = {}
#Some code to populate the dict, used both in checking the argument and later in the script
def check_value(value):
if not dict.has_key(value):
raise argparse.ArgumentTypeError("%s is invalid." % value)
return value
parser = argparse.ArgumentParser(…)
parser.add_argument('…', type=check_value, help='…')
args = parser.parse_args()
# Some other code that uses the dict
Run Code Online (Sandbox Code Playgroud)
同样,我处理一些参数解析的方式使用类似于"abc"的函数来修改我稍后在脚本中需要的字典.我怎么让这个不那么难看?或者使用简单命令行脚本可接受的全局变量?