本文整理汇总了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))