本文整理匯總了Python中nltk.compat.binary_type方法的典型用法代碼示例。如果您正苦於以下問題:Python compat.binary_type方法的具體用法?Python compat.binary_type怎麽用?Python compat.binary_type使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類nltk.compat
的用法示例。
在下文中一共展示了compat.binary_type方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: tgrep_positions
# 需要導入模塊: from nltk import compat [as 別名]
# 或者: from nltk.compat import binary_type [as 別名]
def tgrep_positions(pattern, trees, search_leaves=True):
"""
Return the tree positions in the trees which match the given pattern.
:param pattern: a tgrep search pattern
:type pattern: str or output of tgrep_compile()
:param trees: a sequence of NLTK trees (usually ParentedTrees)
:type trees: iter(ParentedTree) or iter(Tree)
:param search_leaves: whether ot return matching leaf nodes
:type search_leaves: bool
:rtype: iter(tree positions)
"""
if isinstance(pattern, (binary_type, text_type)):
pattern = tgrep_compile(pattern)
for tree in trees:
try:
if search_leaves:
positions = tree.treepositions()
else:
positions = treepositions_no_leaves(tree)
yield [position for position in positions
if pattern(tree[position])]
except AttributeError:
yield []
示例2: tgrep_nodes
# 需要導入模塊: from nltk import compat [as 別名]
# 或者: from nltk.compat import binary_type [as 別名]
def tgrep_nodes(pattern, trees, search_leaves=True):
"""
Return the tree nodes in the trees which match the given pattern.
:param pattern: a tgrep search pattern
:type pattern: str or output of tgrep_compile()
:param trees: a sequence of NLTK trees (usually ParentedTrees)
:type trees: iter(ParentedTree) or iter(Tree)
:param search_leaves: whether ot return matching leaf nodes
:type search_leaves: bool
:rtype: iter(tree nodes)
"""
if isinstance(pattern, (binary_type, text_type)):
pattern = tgrep_compile(pattern)
for tree in trees:
try:
if search_leaves:
positions = tree.treepositions()
else:
positions = treepositions_no_leaves(tree)
yield [tree[position] for position in positions
if pattern(tree[position])]
except AttributeError:
yield []
示例3: tgrep_tokenize
# 需要導入模塊: from nltk import compat [as 別名]
# 或者: from nltk.compat import binary_type [as 別名]
def tgrep_tokenize(tgrep_string):
'''
Tokenizes a TGrep search string into separate tokens.
'''
parser = _build_tgrep_parser(False)
if isinstance(tgrep_string, binary_type):
tgrep_string = tgrep_string.decode()
return list(parser.parseString(tgrep_string))
示例4: tgrep_compile
# 需要導入模塊: from nltk import compat [as 別名]
# 或者: from nltk.compat import binary_type [as 別名]
def tgrep_compile(tgrep_string):
'''
Parses (and tokenizes, if necessary) a TGrep search string into a
lambda function.
'''
parser = _build_tgrep_parser(True)
if isinstance(tgrep_string, binary_type):
tgrep_string = tgrep_string.decode()
return list(parser.parseString(tgrep_string, parseAll=True))[0]
示例5: tgrep_nodes
# 需要導入模塊: from nltk import compat [as 別名]
# 或者: from nltk.compat import binary_type [as 別名]
def tgrep_nodes(pattern, trees, search_leaves=True):
"""
Return the tree nodes in the trees which match the given pattern.
:param pattern: a tgrep search pattern
:type pattern: str or output of tgrep_compile()
:param trees: a sequence of NLTK trees (usually ParentedTrees)
:type trees: iter(ParentedTree) or iter(Tree)
:param search_leaves: whether ot return matching leaf nodes
:type search_leaves: bool
:rtype: iter(tree nodes)
"""
if isinstance(pattern, (binary_type, text_type)):
pattern = tgrep_compile(pattern)
for tree in trees:
try:
if search_leaves:
positions = tree.treepositions()
else:
positions = treepositions_no_leaves(tree)
yield [tree[position] for position in positions
if pattern(tree[position])]
except AttributeError:
yield []
# run module doctests