Commit d8a20205 by Will Roberts

tgrep: tgrep_positions and tgrep_nodes can take lists of trees

parent 2c4d7627
...@@ -884,31 +884,49 @@ def tgrep_positions(tree, tgrep_string, search_leaves = True): ...@@ -884,31 +884,49 @@ def tgrep_positions(tree, tgrep_string, search_leaves = True):
Return all tree positions in the given tree which match the given Return all tree positions in the given tree which match the given
`tgrep_string`. `tgrep_string`.
If `search_leaves` is False, the method will not return any Arguments:
results in leaf positions. - `tree`: a NLTK tree (usually a ParentedTree), or a list of trees
''' - `tgrep_string`: a tgrep search query, either as a string value,
try: or compiled into a lambda function with `tgrep_compile`
if search_leaves: - `search_leaves`: Boolean flag; if this is False, the method will
search_positions = tree.treepositions() not return any positions which are leaf nodes of the given tree(s)
else: '''
search_positions = treepositions_no_leaves(tree) # compile tgrep_string if needed
except AttributeError:
return []
if isinstance(tgrep_string, (binary_type, text_type)): if isinstance(tgrep_string, (binary_type, text_type)):
tgrep_string = tgrep_compile(tgrep_string) tgrep_string = tgrep_compile(tgrep_string)
return [position for position in search_positions if isinstance(tree, (list, tuple)):
if tgrep_string(tree[position])] return [tgrep_positions(t, tgrep_string, search_leaves) for t in tree]
else:
try:
if search_leaves:
search_positions = tree.treepositions()
else:
search_positions = treepositions_no_leaves(tree)
except AttributeError:
return []
return [position for position in search_positions
if tgrep_string(tree[position])]
def tgrep_nodes(tree, tgrep_string, search_leaves = True): def tgrep_nodes(tree, tgrep_string, search_leaves = True):
''' '''
Return all tree nodes in the given tree which match the given Return all tree nodes in the given tree which match the given
`tgrep_ string`. `tgrep_ string`.
If `search_leaves` is False, the method will not return any Arguments:
results in leaf positions. - `tree`: a NLTK tree (usually a ParentedTree), or a list of trees
- `tgrep_string`: a tgrep search query, either as a string value,
or compiled into a lambda function with `tgrep_compile`
- `search_leaves`: Boolean flag; if this is False, the method will
not return any leaf nodes of the given tree(s)
''' '''
return [tree[position] for position in tgrep_positions(tree, tgrep_string, # compile tgrep_string if needed
search_leaves)] if isinstance(tgrep_string, (binary_type, text_type)):
tgrep_string = tgrep_compile(tgrep_string)
if isinstance(tree, (list, tuple)):
return [tgrep_nodes(t, tgrep_string, search_leaves) for t in tree]
else:
return [tree[position] for position in
tgrep_positions(tree, tgrep_string, search_leaves)]
# run module doctests # run module doctests
if __name__ == "__main__": if __name__ == "__main__":
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment