本文整理匯總了Python中nltk.compat.string_types方法的典型用法代碼示例。如果您正苦於以下問題:Python compat.string_types方法的具體用法?Python compat.string_types怎麽用?Python compat.string_types使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類nltk.compat
的用法示例。
在下文中一共展示了compat.string_types方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: productions
# 需要導入模塊: from nltk import compat [as 別名]
# 或者: from nltk.compat import string_types [as 別名]
def productions(self):
"""
Generate the productions that correspond to the non-terminal nodes of the tree.
For each subtree of the form (P: C1 C2 ... Cn) this produces a production of the
form P -> C1 C2 ... Cn.
>>> t = Tree.fromstring("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))")
>>> t.productions()
[S -> NP VP, NP -> D N, D -> 'the', N -> 'dog', VP -> V NP, V -> 'chased',
NP -> D N, D -> 'the', N -> 'cat']
:rtype: list(Production)
"""
if not isinstance(self._label, string_types):
raise TypeError('Productions can only be generated from trees having node labels that are strings')
prods = [Production(Nonterminal(self._label), _child_names(self))]
for child in self:
if isinstance(child, Tree):
prods += child.productions()
return prods
示例2: call_tadm
# 需要導入模塊: from nltk import compat [as 別名]
# 或者: from nltk.compat import string_types [as 別名]
def call_tadm(args):
"""
Call the ``tadm`` binary with the given arguments.
"""
if isinstance(args, compat.string_types):
raise TypeError('args should be a list of strings')
if _tadm_bin is None:
config_tadm()
# Call tadm via a subprocess
cmd = [_tadm_bin] + args
p = subprocess.Popen(cmd, stdout=sys.stdout)
(stdout, stderr) = p.communicate()
# Check the return code.
if p.returncode != 0:
print()
print(stderr)
raise OSError('tadm command failed!')
示例3: call_megam
# 需要導入模塊: from nltk import compat [as 別名]
# 或者: from nltk.compat import string_types [as 別名]
def call_megam(args):
"""
Call the ``megam`` binary with the given arguments.
"""
if isinstance(args, compat.string_types):
raise TypeError('args should be a list of strings')
if _megam_bin is None:
config_megam()
# Call megam via a subprocess
cmd = [_megam_bin] + args
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
(stdout, stderr) = p.communicate()
# Check the return code.
if p.returncode != 0:
print()
print(stderr)
raise OSError('megam command failed!')
if isinstance(stdout, compat.string_types):
return stdout
else:
return stdout.decode('utf-8')
示例4: _markproduction
# 需要導入模塊: from nltk import compat [as 別名]
# 或者: from nltk.compat import string_types [as 別名]
def _markproduction(self, prod, tree=None):
if tree is None: tree = self._tree
for i in range(len(tree.subtrees())-len(prod.rhs())):
if tree['color', i] == 'white':
self._markproduction
for j, node in enumerate(prod.rhs()):
widget = tree.subtrees()[i+j]
if (isinstance(node, Nonterminal) and
isinstance(widget, TreeSegmentWidget) and
node.symbol == widget.label().text()):
pass # matching nonterminal
elif (isinstance(node, compat.string_types) and
isinstance(widget, TextWidget) and
node == widget.text()):
pass # matching nonterminal
else: break
else:
# Everything matched!
print('MATCH AT', i)
#//////////////////////////////////////////////////
# Grammar
#//////////////////////////////////////////////////
示例5: __init__
# 需要導入模塊: from nltk import compat [as 別名]
# 或者: from nltk.compat import string_types [as 別名]
def __init__(self, regexp, repl, descr):
"""
Construct a new RegexpChunkRule.
:type regexp: regexp or str
:param regexp: The regular expression for this ``RegexpChunkRule``.
When this rule is applied to a ``ChunkString``, any
substring that matches ``regexp`` will be replaced using
the replacement string ``repl``. Note that this must be a
normal regular expression, not a tag pattern.
:type repl: str
:param repl: The replacement expression for this ``RegexpChunkRule``.
When this rule is applied to a ``ChunkString``, any substring
that matches ``regexp`` will be replaced using ``repl``.
:type descr: str
:param descr: A short description of the purpose and/or effect
of this rule.
"""
if isinstance(regexp, string_types):
regexp = re.compile(regexp)
self._repl = repl
self._descr = descr
self._regexp = regexp
示例6: _getfileids
# 需要導入模塊: from nltk import compat [as 別名]
# 或者: from nltk.compat import string_types [as 別名]
def _getfileids(self, documents, subcorpus):
"""
Helper that selects the appropriate fileids for a given set of
documents from a given subcorpus (pos or psd).
"""
if documents is None:
documents = self._documents
else:
if isinstance(documents, compat.string_types):
documents = [documents]
for document in documents:
if document not in self._documents:
if document[-4:] in ('.pos', '.psd'):
raise ValueError(
'Expected a document identifier, not a file '
'identifier. (Use corpus.documents() to get '
'a list of document identifiers.')
else:
raise ValueError('Document identifier %s not found'
% document)
return ['%s.%s' % (d, subcorpus) for d in documents]
# Delegate to one of our two sub-readers:
示例7: _resolve
# 需要導入模塊: from nltk import compat [as 別名]
# 或者: from nltk.compat import string_types [as 別名]
def _resolve(self, fileids, categories, textids=None):
tmp = None
if fileids is not None:
if not tmp:
tmp = fileids, None
else:
raise ValueError('Specify only fileids, categories or textids')
if categories is not None:
if not tmp:
tmp = self.fileids(categories), None
else:
raise ValueError('Specify only fileids, categories or textids')
if textids is not None:
if not tmp:
if isinstance(textids, compat.string_types): textids = [textids]
files = sum((self._t2f[t] for t in textids), [])
tdict = dict()
for f in files:
tdict[f] = (set(self._f2t[f]) & set(textids))
tmp = files, tdict
else:
raise ValueError('Specify only fileids, categories or textids')
return None, None
示例8: words
# 需要導入模塊: from nltk import compat [as 別名]
# 或者: from nltk.compat import string_types [as 別名]
def words(self, fileids=None, categories=None, textids=None):
fileids, textids = self._resolve(fileids, categories, textids)
if fileids is None: fileids = self._fileids
elif isinstance(fileids, compat.string_types): fileids = [fileids]
if textids:
return concat([TEICorpusView(self.abspath(fileid),
False, False, False,
headLen=self.headLen,
textids=textids[fileid])
for fileid in fileids])
else:
return concat([TEICorpusView(self.abspath(fileid),
False, False, False,
headLen=self.headLen)
for fileid in fileids])
示例9: paras
# 需要導入模塊: from nltk import compat [as 別名]
# 或者: from nltk.compat import string_types [as 別名]
def paras(self, fileids=None, categories=None, textids=None):
fileids, textids = self._resolve(fileids, categories, textids)
if fileids is None: fileids = self._fileids
elif isinstance(fileids, compat.string_types): fileids = [fileids]
if textids:
return concat([TEICorpusView(self.abspath(fileid),
False, True, True,
headLen=self.headLen,
textids=textids[fileid])
for fileid in fileids])
else:
return concat([TEICorpusView(self.abspath(fileid),
False, True, True,
headLen=self.headLen)
for fileid in fileids])
示例10: tagged_words
# 需要導入模塊: from nltk import compat [as 別名]
# 或者: from nltk.compat import string_types [as 別名]
def tagged_words(self, fileids=None, categories=None, textids=None):
fileids, textids = self._resolve(fileids, categories, textids)
if fileids is None: fileids = self._fileids
elif isinstance(fileids, compat.string_types): fileids = [fileids]
if textids:
return concat([TEICorpusView(self.abspath(fileid),
True, False, False,
headLen=self.headLen,
textids=textids[fileid])
for fileid in fileids])
else:
return concat([TEICorpusView(self.abspath(fileid),
True, False, False,
headLen=self.headLen)
for fileid in fileids])
示例11: tagged_sents
# 需要導入模塊: from nltk import compat [as 別名]
# 或者: from nltk.compat import string_types [as 別名]
def tagged_sents(self, fileids=None, categories=None, textids=None):
fileids, textids = self._resolve(fileids, categories, textids)
if fileids is None: fileids = self._fileids
elif isinstance(fileids, compat.string_types): fileids = [fileids]
if textids:
return concat([TEICorpusView(self.abspath(fileid),
True, True, False,
headLen=self.headLen,
textids=textids[fileid])
for fileid in fileids])
else:
return concat([TEICorpusView(self.abspath(fileid),
True, True, False,
headLen=self.headLen)
for fileid in fileids])
示例12: tagged_paras
# 需要導入模塊: from nltk import compat [as 別名]
# 或者: from nltk.compat import string_types [as 別名]
def tagged_paras(self, fileids=None, categories=None, textids=None):
fileids, textids = self._resolve(fileids, categories, textids)
if fileids is None: fileids = self._fileids
elif isinstance(fileids, compat.string_types): fileids = [fileids]
if textids:
return concat([TEICorpusView(self.abspath(fileid),
True, True, True,
headLen=self.headLen,
textids=textids[fileid])
for fileid in fileids])
else:
return concat([TEICorpusView(self.abspath(fileid),
True, True, True,
headLen=self.headLen)
for fileid in fileids])
示例13: _pretty_any
# 需要導入模塊: from nltk import compat [as 別名]
# 或者: from nltk.compat import string_types [as 別名]
def _pretty_any(obj):
"""
Helper function for pretty-printing any AttrDict object.
:param obj: The obj to be printed.
:type obj: AttrDict
:return: A nicely formated string representation of the AttrDict object.
:rtype: str
"""
outstr = ""
for k in obj:
if isinstance(obj[k], string_types) and len(obj[k]) > 65:
outstr += "[{0}]\n".format(k)
outstr += "{0}".format(_pretty_longstring(obj[k], prefix=' '))
outstr += '\n'
else:
outstr += "[{0}] {1}\n".format(k, obj[k])
return outstr
示例14: __init__
# 需要導入模塊: from nltk import compat [as 別名]
# 或者: from nltk.compat import string_types [as 別名]
def __init__(self, root, fileids, columntypes,
chunk_types=None, root_label='S', pos_in_tree=False,
srl_includes_roleset=True, encoding='utf8',
tree_class=Tree, tagset=None):
for columntype in columntypes:
if columntype not in self.COLUMN_TYPES:
raise ValueError('Bad column type %r' % columntype)
if isinstance(chunk_types, compat.string_types):
chunk_types = [chunk_types]
self._chunk_types = chunk_types
self._colmap = dict((c,i) for (i,c) in enumerate(columntypes))
self._pos_in_tree = pos_in_tree
self._root_label = root_label # for chunks
self._srl_includes_roleset = srl_includes_roleset
self._tree_class = tree_class
CorpusReader.__init__(self, root, fileids, encoding)
self._tagset = tagset
#/////////////////////////////////////////////////////////////////
# Data Access Methods
#/////////////////////////////////////////////////////////////////
示例15: _tree2conll
# 需要導入模塊: from nltk import compat [as 別名]
# 或者: from nltk.compat import string_types [as 別名]
def _tree2conll(self, tree, wordnum, words, pos, synt):
assert isinstance(tree, Tree)
if len(tree) == 1 and isinstance(tree[0], compat.string_types):
pos[wordnum] = tree.label()
assert words[wordnum] == tree[0]
return wordnum+1
elif len(tree) == 1 and isinstance(tree[0], tuple):
assert len(tree[0]) == 2
pos[wordnum], pos[wordnum] = tree[0]
return wordnum+1
else:
synt[wordnum] = '(%s%s' % (tree.label(), synt[wordnum])
for child in tree:
wordnum = self._tree2conll(child, wordnum, words,
pos, synt)
synt[wordnum-1] += ')'
return wordnum