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
d8a20205
Commit
d8a20205
authored
Apr 27, 2015
by
Will Roberts
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tgrep: tgrep_positions and tgrep_nodes can take lists of trees
parent
2c4d7627
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
16 deletions
+34
-16
nltk/tgrep.py
+34
-16
No files found.
nltk/tgrep.py
View file @
d8a20205
...
...
@@ -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
`tgrep_string`.
If `search_leaves` is False, the method will not return any
results in leaf positions.
'''
try
:
if
search_leaves
:
search_positions
=
tree
.
treepositions
()
else
:
search_positions
=
treepositions_no_leaves
(
tree
)
except
AttributeError
:
return
[]
Arguments:
- `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 positions which are leaf nodes of the given tree(s)
'''
# compile tgrep_string if needed
if
isinstance
(
tgrep_string
,
(
binary_type
,
text_type
)):
tgrep_string
=
tgrep_compile
(
tgrep_string
)
return
[
position
for
position
in
search_positions
if
tgrep_string
(
tree
[
position
])]
if
isinstance
(
tree
,
(
list
,
tuple
)):
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
):
'''
Return all tree nodes in the given tree which match the given
`tgrep_ string`.
If `search_leaves` is False, the method will not return any
results in leaf positions.
Arguments:
- `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
,
search_leaves
)]
# compile tgrep_string if needed
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
if
__name__
==
"__main__"
:
...
...
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