本文整理匯總了Python中nltk.compat.imap方法的典型用法代碼示例。如果您正苦於以下問題:Python compat.imap方法的具體用法?Python compat.imap怎麽用?Python compat.imap使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類nltk.compat
的用法示例。
在下文中一共展示了compat.imap方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test
# 需要導入模塊: from nltk import compat [as 別名]
# 或者: from nltk.compat import imap [as 別名]
def test(self, test_sequence, verbose=False, **kwargs):
"""
Tests the HiddenMarkovModelTagger instance.
:param test_sequence: a sequence of labeled test instances
:type test_sequence: list(list)
:param verbose: boolean flag indicating whether training should be
verbose or include printed output
:type verbose: bool
"""
def words(sent):
return [word for (word, tag) in sent]
def tags(sent):
return [tag for (word, tag) in sent]
def flatten(seq):
return list(itertools.chain(*seq))
test_sequence = self._transform(test_sequence)
predicted_sequence = list(imap(self._tag, imap(words, test_sequence)))
if verbose:
for test_sent, predicted_sent in izip(test_sequence, predicted_sequence):
print('Test:',
' '.join('%s/%s' % (token, tag)
for (token, tag) in test_sent))
print()
print('Untagged:',
' '.join("%s" % token for (token, tag) in test_sent))
print()
print('HMM-tagged:',
' '.join('%s/%s' % (token, tag)
for (token, tag) in predicted_sent))
print()
print('Entropy:',
self.entropy([(token, None) for
(token, tag) in predicted_sent]))
print()
print('-' * 60)
test_tags = flatten(imap(tags, test_sequence))
predicted_tags = flatten(imap(tags, predicted_sequence))
acc = accuracy(test_tags, predicted_tags)
count = sum(len(sent) for sent in test_sequence)
print('accuracy over %d tokens: %.2f' % (count, acc * 100))