如何将listiterator对象转换为tree对象?

2024-06-16 12:28:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我想通过运行find来提取解析树_图案.py,这些匹配具有以下模式:

SIMPLE_PREDICATE = (ROOT, ((SENTENCE, (NP, VP, PERIOD)),))          
APPOSITION = (SENTENCE, ((NP, (NP, COMMA, NP, COMMA)), VP, PERIOD)) 

但以下输出显示:

Traceback (most recent call last):
 File "C:\Users\Anon\Desktop\ZAHID\Working Model 1\zahid\Practice\New folder\find_pattern.py", line 40, in <module>
appos = sent_extract.find_appositions(parse_trees)
 File "C:\Users\Anon\Desktop\ZAHID\Working Model 1\zahid\Practice\New folder\sent_extract.py", line 30, in find_appositions
s in search_for_matches(parse_tree, APPOSITION)]
 File "C:\Users\Anon\Desktop\ZAHID\Working Model 1\zahid\Practice\New folder\sent_extract.py", line 58, in search_for_matches
if is_match(parse_tree, pattern):
 File "C:\Users\Anon\Desktop\ZAHID\Working Model 1\zahid\Practice\New folder\sent_extract.py", line 45, in is_match
if tree.label() == parent and len(tree) == len(children): 
AttributeError: 'listiterator' object has no attribute 'label'

查找_图案.py你知道吗

import os
import parse_article
import stanford_parser
import sent_extract

article_filename = 'C:\Users\Anon\Desktop\ZAHID\Working Model 1\zahid\Practice\New folder\____.html'

sentences = parse_article.parse_html(article_filename)

user='' 
parser = stanford_parser.create_parser(user)
parse_trees = parser.raw_parse_sents(sentences)

appos = sent_extract.find_appositions(parse_trees)
print(appos)

已发送_提取.py你知道吗

from nltk.tree import Tree
from tags import *

SIMPLE_PREDICATE = (ROOT, ((SENTENCE, (NP, VP, PERIOD)),))          
APPOSITION = (SENTENCE, ((NP, (NP, COMMA, NP, COMMA)), VP, PERIOD)) 

def find_predicates(parse_trees):
  preds = []
  for parse_tree in parse_trees:
    if is_match(parse_tree, SIMPLE_PREDICATE):         
      preds.append(parse_tree[0])
  return preds

def find_appositions(parse_trees):
  appos = []
  for parse_tree in parse_trees:
    appos += [(s[0,0], s[0,2]) for
        s in search_for_matches(parse_tree, APPOSITION)]
  return appos


def is_match(tree, pattern):
  if not isinstance(pattern, tuple):
    return tree.label() == pattern

  else:
    parent = pattern[0]
    children = pattern[1]
    if tree.label() == parent and len(tree) == len(children): 
      for i in xrange(len(tree)):
        ith_child = tree[i]
        if not is_match(ith_child, children[i]):
          return False
      return True

def search_for_matches(parse_tree, pattern):
  matches = []
  if is_match(parse_tree, pattern):
    matches.append(parse_tree)
  for child in parse_tree:
    if isinstance(child, Tree):
      matches += search_for_matches(child, pattern)
  return matches

你知道吗标签.py你知道吗

ADJP = 'ADJP'
ADVP = 'ADVP'
NUMBER = 'CD'
DET = 'DT'
PREP = 'IN'
ADJ = 'JJ'
ADJ_COMP = 'JJR'
ADJ_SUP = 'JJS'
MODAL = 'MD'
NOUN = 'NN'
NOUN_PROPER = 'NNP'
NOUN_PL = 'NNS'
NP = 'NP'
POSS = 'POS'
PP = 'PP'
PRONOUN = 'PRP'
PRONOUN_POSS = 'PRP$'
ADVERB = 'RB'
ROOT = 'ROOT'
SENTENCE = 'S'
SBAR = 'SBAR'
WH_QUESTION = 'SBARQ'
BIN_QUESTION = 'SQ'
TO = 'TO'
VERB_INF = 'VB'
VERB_PAST = 'VBD'
VERB_PLURAL = 'VBP'
VERB_3SG = 'VBZ'
VP = 'VP'
WHNP = 'WHNP'
WHADJP = 'WHADJP'
WHADVP = 'WHADVP'
WDT = 'WDT'
WP_POSS = 'WP$'
COMMA = ','
PERIOD = '.'

Tags: inpytreeforifparsenpextract
1条回答
网友
1楼 · 发布于 2024-06-16 12:28:22

^{} method被设计用来处理多个句子。每个句子都被解析成一个树序列,但是您的代码假设每个句子只有一棵树。根据文件:

Return type: iter(iter(Tree))

所以你得到了无数的树。你知道吗

所以不用

for parse_tree in parse_trees:
    if is_match(parse_tree, SIMPLE_PREDICATE):         
        preds.append(parse_tree[0])

你必须使用

for sentence in parse_trees:
    for parse_tree in sentence:
        if is_match(parse_tree, SIMPLE_PREDICATE):         
            preds.append(parse_tree[0])

现在您正在传入实际的^{} instances,它们是类似于列表的对象,可以索引并具有长度。你知道吗

这同样适用于find_appositions()

def find_appositions(parse_trees):
    appos = []
    for sentence in parse_trees:
        for parse_tree in sentence:
            appos += [(s[0,0], s[0,2]) for
                      s in search_for_matches(parse_tree, APPOSITION)]
    return appos

相关问题 更多 >