本文整理汇总了Python中tree.Tree.is_null方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.is_null方法的具体用法?Python Tree.is_null怎么用?Python Tree.is_null使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tree.Tree
的用法示例。
在下文中一共展示了Tree.is_null方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Sentence
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import is_null [as 别名]
class Sentence(object):
__slots__ = ('leaves', 'id', 'tree', 'true_connectives', 'checked_connectives', 'depTree', 'words', 'word_ids',
'begin_offset', 'end_offset', 'clauses')
def __init__(self, sent_id, parse_tree, dep_tree, words):
self.leaves = []
self.id = sent_id
self.tree = Tree(parse_tree, sent_id)
self.get_leaves()
self.words = words
self.begin_offset = words[0][1]['CharacterOffsetBegin']
self.end_offset = words[-1][1]['CharacterOffsetEnd']
self.word_ids = []
self.true_connectives = []
self.checked_connectives = []
self.stem_leaf()
self.depTree = DepTree(self, dep_tree)
self.clauses = []
self.break_clauses()
def get_leaves(self):
if not self.tree.is_null():
self.leaves = self.tree.root.get_leaves()
def stem_leaf(self):
for leaf in self.leaves:
leaf.stem = stem(leaf.value)
def check_connectives(self):
length = len(self.leaves)
for a in Conn_intra:
conns = a.split('..')
checked = [None, None]
i1 = i2 = length
for i in range(length):
if self.leaves[i].lowercased == conns[0]:
checked[0] = self.leaves[i]
i1 = i+1
break
for i in range(i1, length):
if self.leaves[i].lowercased == conns[1]:
checked[1] = self.leaves[i]
i2 = i
break
if checked[0] is not None and checked[1] is not None:
for ch in checked:
ch.is_conn = True
self.checked_connectives.append(checked)
if a == 'if..then':
for i in range(i1, i2):
if self.leaves[i].lowercased == 'if':
self.leaves[i].is_conn = True
self.checked_connectives.append([self.leaves[i], checked[1]])
for a in Conn_group:
conns = a.split()
len_conns = len(conns)
for i in range(length-len_conns+1):
ok = True
checked = []
for j in range(len_conns):
if conns[j] != self.leaves[i+j].lowercased or self.leaves[i+j].is_conn:
ok = False
break
else:
checked.append(self.leaves[i+j])
if ok:
for ch in checked:
ch.is_conn = True
self.checked_connectives.append(checked)
return self.checked_connectives
def get_syntactic_features(self, self_node, parent_node, left_sib_node, right_sib_node):
curr = self_node
self_to_root = curr.value
self_to_root2 = curr.value
while curr != self.tree.root:
prev = curr
curr = curr.parent_node
self_to_root += '_>_'+curr.value
if prev.value != curr.value:
self_to_root2 += '_>_'+curr.value
return [self_to_root, self_to_root2]
def get_production_rules(self, conn_leaves, rules={}):
if len(conn_leaves) == 1:
curr = conn_leaves[0].parent_node
else:
tree = conn_leaves[0].goto_tree()
curr = tree.find_least_common_ancestor(conn_leaves, with_leaves = True)
curr.get_production_rules(rules, -1)
def get_connective_categories(self, conn_leaves):
self_cat = parent_cat = left_cat = right_cat = 'NONE'
tree = conn_leaves[0].goto_tree()
curr = tree.find_highest_common_ancestor(conn_leaves)
parent = curr.parent_node
#.........这里部分代码省略.........
示例2: Sentence
# 需要导入模块: from tree import Tree [as 别名]
# 或者: from tree.Tree import is_null [as 别名]
class Sentence(object):
__slots__ = ('leaves', 'id', 'tree', 'true_connectives', 'checked_connectives', 'depTree', 'words', 'word_ids',
'begin_offset', 'end_offset', 'clauses')
def __init__(self, sent_id, parse_tree, dep_tree, words):
self.leaves = []
self.id = sent_id
self.tree = Tree(parse_tree, sent_id)
self.get_leaves()
self.words = words
self.begin_offset = words[0][1]['CharacterOffsetBegin']
self.end_offset = words[-1][1]['CharacterOffsetEnd']
self.word_ids = []
self.true_connectives = []
self.checked_connectives = []
self.depTree = DepTree(self, dep_tree)
self.clauses = []
self.break_clauses()
def get_leaves(self):
if not self.tree.is_null():
self.leaves = self.tree.root.get_leaves()
def _find_intra_conn(self, conn_parts, n, k, offset, checked, alls):
if n == k:
if len(checked) == n:
alls.append(checked)
else:
length = len(self.leaves)
conns = conn_parts[k].split()
len_conns = len(conns)
prev_checked = copy.copy(checked)
for i in range(offset, length - len_conns + 1):
ok = True
curr = []
for j in range(len_conns):
if conns[j] != self.leaves[i+j].value:
ok = False
break
else:
curr.append(self.leaves[i+j])
if ok and len(curr) != 0:
checked.append(curr)
self._find_intra_conn(conn_parts, n, k+1, i+len_conns, checked, alls)
checked = prev_checked
def check_intra_connectives(self):
for a in CONN_INTRA:
conn_parts = a.split('..')
alls = []
check = []
self._find_intra_conn(conn_parts, len(conn_parts), 0, 0, check, alls)
if len(alls) > 0:
for can in alls:
print ' '.join(n.value for n in can), '||', a
def check_connectives(self):
length = len(self.leaves)
for a in CONN_INTRA:
conn_parts = a.split('..')
alls = []
check = []
self._find_intra_conn(conn_parts, len(conn_parts), 0, 0, check, alls)
if len(alls) > 0:
for can in alls:
t = []
for c in can:
t += c
self.checked_connectives.append(t)
for a in CONN_GROUP:
conns = a.split()
len_conns = len(conns)
for i in range(length-len_conns+1):
ok = True
checked = []
for j in range(len_conns):
if conns[j] != self.leaves[i+j].value:
ok = False
break
else:
checked.append(self.leaves[i+j])
if ok:
self.checked_connectives.append(checked)
return self.checked_connectives
def get_syntactic_features(self, self_node, parent_node, left_sib_node, right_sib_node):
curr = self_node
self_to_root = curr.value
self_to_root2 = curr.value
while curr != self.tree.root:
prev = curr
curr = curr.parent_node
self_to_root += '_>_'+curr.value
if prev.value != curr.value:
self_to_root2 += '_>_'+curr.value
return [self_to_root, self_to_root2]
#.........这里部分代码省略.........