本文整理汇总了Python中hmm.HMM.classify方法的典型用法代码示例。如果您正苦于以下问题:Python HMM.classify方法的具体用法?Python HMM.classify怎么用?Python HMM.classify使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hmm.HMM
的用法示例。
在下文中一共展示了HMM.classify方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TagHMM
# 需要导入模块: from hmm import HMM [as 别名]
# 或者: from hmm.HMM import classify [as 别名]
class TagHMM(TestCase):
"""Train and test an HMM POS tagger."""
def setUp(self):
self.train, self.test = self.split_sents()
self.hmm = HMM()
self.hmm.train(self.train)
def split_sents(self, train=0.95, total=3500,
document_class=TaggedSentence):
sents = tagged_corpus.tagged_sents()[:total]
total = len(sents) if total is None else total
i = int(round(train * total))
j = i + int(round(total - train * total))
return (map(document_class, sents[0:i]),
map(document_class, sents[i:j]))
def accuracy(self, test_sents, verbose=sys.stderr):
"""Compute accuracy of the HMM tagger on the given sentences."""
total = correct = 0
for sent in test_sents:
tags = self.hmm.classify(sent)
total += len(tags)
for guess, tag in zip(tags, sent.label):
correct += (guess == tag)
if verbose:
print >> verbose, "%.2d%% " % (100 * correct / total),
return correct / total
@skip("too slow")
def test_tag_train(self):
"""Tag the training data"""
self.assertGreater(self.accuracy(self.train), 0.85)
def test_tag(self):
"""Tag the test data"""
self.assertGreater(self.accuracy(self.test), 0.85)