Commit 97bb348f by Will Roberts

tgrep: tgrep_nodes and tgrep_positions also support generators

parent 28ec6788
...@@ -120,6 +120,7 @@ except ImportError: ...@@ -120,6 +120,7 @@ except ImportError:
print('Warning: nltk.tgrep will not work without the `pyparsing` package') print('Warning: nltk.tgrep will not work without the `pyparsing` package')
print('installed.') print('installed.')
import re import re
import types
class TgrepException(Exception): class TgrepException(Exception):
'''Tgrep exception type.''' '''Tgrep exception type.'''
...@@ -894,7 +895,8 @@ def tgrep_positions(tree, tgrep_string, search_leaves = True): ...@@ -894,7 +895,8 @@ def tgrep_positions(tree, tgrep_string, search_leaves = True):
# compile tgrep_string if needed # compile tgrep_string if needed
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)
if not _istree(tree) and isinstance(tree, (list, tuple)): if not _istree(tree) and isinstance(tree,
(list, tuple, types.GeneratorType)):
return [tgrep_positions(t, tgrep_string, search_leaves) for t in tree] return [tgrep_positions(t, tgrep_string, search_leaves) for t in tree]
else: else:
try: try:
...@@ -922,7 +924,8 @@ def tgrep_nodes(tree, tgrep_string, search_leaves = True): ...@@ -922,7 +924,8 @@ def tgrep_nodes(tree, tgrep_string, search_leaves = True):
# compile tgrep_string if needed # compile tgrep_string if needed
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)
if not _istree(tree) and isinstance(tree, (list, tuple)): if not _istree(tree) and isinstance(tree,
(list, tuple, types.GeneratorType)):
return [tgrep_nodes(t, tgrep_string, search_leaves) for t in tree] return [tgrep_nodes(t, tgrep_string, search_leaves) for t in tree]
else: else:
return [tree[position] for position in return [tree[position] for position in
......
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