Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nltk
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
nltk
Commits
97bb348f
Commit
97bb348f
authored
Apr 27, 2015
by
Will Roberts
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tgrep: tgrep_nodes and tgrep_positions also support generators
parent
28ec6788
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
5 additions
and
2 deletions
+5
-2
nltk/tgrep.py
+5
-2
No files found.
nltk/tgrep.py
View file @
97bb348f
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment