我正在尝试创建自定义块标记并从它们中提取关系。下面是带我到级联块树的代码。
grammar = r"""
NPH: {<DT|JJ|NN.*>+} # Chunk sequences of DT, JJ, NN
PPH: {<IN><NP>} # Chunk prepositions followed by NP
VPH: {<VB.*><NP|PP|CLAUSE>+$} # Chunk verbs and their arguments
CLAUSE: {<NP><VP>} # Chunk NP, VP
"""
cp = nltk.RegexpParser(grammar)
sentence = [("Mary", "NN"), ("saw", "VBD"), ("the", "DT"), ("cat", "NN"),
("sit", "VB"), ("on", "IN"), ("the", "DT"), ("mat", "NN")]
chunked = cp.parse(sentence)输出-
(S (NPH Mary/NN) saw/VBD (NPH /DT cat/NN) sit/VB on/IN (NPH the/DT mat/NN))
现在,我正在尝试使用nltk.sem.extract_rels函数提取NPH标记值与文本之间的关系,但它似乎只适用于使用ne_chunk函数生成的命名实体。
IN = re.compile(r'.*\bon\b')
for rel in nltk.sem.extract_rels('NPH', 'NPH', chunked,corpus='ieer',pattern = IN):
print(nltk.sem.rtuple(rel))这会产生以下错误-
ValueError:主题类型的值未被识别: NPH
是否有一种简单的方法只使用块标记来创建关系,因为我真的不想重新训练NER模型来检测我的块标记作为各自的命名实体
谢谢!
发布于 2018-07-20 09:15:16
extract_rels (文档)检查参数subjclass和objclass是否为已知的NE标记,因此出现了NPH错误。extract_rels函数(下面的示例)。
导入nltk re语法=r“”NPH:{+} # DT,JJ,NPH:{} #块介词和NP VPH:{+$} #块动词及其参数从句:{}块NP,VP“cp =nltk.RegexpParser(语法)句子= ("Mary"," NN "),(”看见“,"VBD"),("the”),“DT”、("cat“、"NN")、("sit”、"VB")、“on”、“IN”、("the“、"DT")、("mat”、"NN") =cp.parse(句子)IN=re.compile(r.*\bon\b‘) def extract_rels(子类、objclass、chunked、模式):#填充,因为这个函数检查正确的上下文对=nltk.sem.relextract.tree2semi_rel(块)+[] reldicts =nltk.sem.relextract.semi_rel2reldict(对) relfilter = lambda:(x' subjclass‘== subjclass和pattern.match(x’pattern.match‘)和x'objclass’== objclass)返回列表(筛选器,(extract_rels中e的reldicts)( 'NPH',‘NPH’,分块,pattern=IN):打印(nltk.sem.rtuple(E))
输出:
NPH:“/DT cat/NN”sit/VB on/IN NPH:'the/DT mat/NN‘https://stackoverflow.com/questions/51390568
复制相似问题