当前位置: 首页>>代码示例>>Python>>正文


Python Classifier.update_joint_apriori方法代码示例

本文整理汇总了Python中classifier.Classifier.update_joint_apriori方法的典型用法代码示例。如果您正苦于以下问题:Python Classifier.update_joint_apriori方法的具体用法?Python Classifier.update_joint_apriori怎么用?Python Classifier.update_joint_apriori使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在classifier.Classifier的用法示例。


在下文中一共展示了Classifier.update_joint_apriori方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestParser

# 需要导入模块: from classifier import Classifier [as 别名]
# 或者: from classifier.Classifier import update_joint_apriori [as 别名]
class TestParser(unittest.TestCase):

    def setUp(self):
        #Sentence is : 3 times, This sentence: 2 times - case insensitive
        self.text = "Line1: Sentence is! This sentence isn't no3.\n" \
                    "Line2 starts here. This sentence is 4, ok?\r\n" \
                    "There are two bigrams repeating above"
        self.sentencesWithoutStemming =  \
                    [ "line1 sentence is", "this sentence isnt no3",
                      "line2 starts here", "this sentence is 4 ok",
                      "there are two bigrams repeating above"]
        self.num_bigrams   = 14      #the unique ones
        self.num_sentences = 5
        #Classifier class, encapsulates the 'update_bigrams' method, so:
        self.classifier = Classifier(verbose=True)  
    
    def test_split_sentences(self):
        
        sentences = split_sentences(self.text)
        self.assertEqual(type(sentences).__name__, 'list')
        self.assertEqual(len(sentences), self.num_sentences)

    def test_remove_stemming(self):
        
        sentences = split_sentences(self.text)
        sentencesWithoutStemming = remove_stemming (sentences)
        #print sentencesWithoutStemming
        self.assertItemsEqual (sentencesWithoutStemming, 
                               self.sentencesWithoutStemming)

    def test_make_bigrams(self):
        
        sentences = split_sentences(self.text)
        sentencesWithoutStemming = remove_stemming (sentences)
        allBigrams = defaultdict(int)
        for s in sentencesWithoutStemming:
            self.assertIsInstance(s, str)
            newBigrams = make_bigrams(s)
            #print '\n\n'
            #for k,v in newBigrams.items():
                #print k,v
            self.assertIsInstance(newBigrams, defaultdict)
            merge_and_sum_bigrams(allBigrams, newBigrams)

        self.classifier.update_joint_apriori(allBigrams)
        #for k,v in self.classifier.apriori.items():
            #print k,v
        self.assertEqual (len(allBigrams), self.num_bigrams)
    
    def assertDictEqual(self, d1, d2, msg=None): # assertEqual uses for dicts
        for k,v1 in d1.iteritems():
            self.assertIn(k, d2, msg)
            v2 = d2[k]
            if(isinstance(v1, collections.Iterable) and
               not isinstance(v1, basestring)):
                self.assertItemsEqual(v1, v2, msg)
            else:
                self.assertEqual(v1, v2, msg)
        return True
    
    def test_save_load(self):
        sentences = split_sentences(self.text)
        sentencesWithoutStemming = remove_stemming (sentences)
        allBigrams = defaultdict(int)
        for s in sentencesWithoutStemming:
            newBigrams = make_bigrams(s)
            merge_and_sum_bigrams(allBigrams, newBigrams)

        self.classifier.update_joint_apriori(allBigrams)
        
        for k,v in self.classifier.apriori.items():
            print k,v
        
        self.classifier.save('testC')
        newClassifier = Classifier()
        newClassifier.load('testC')
        #self.assertDictEqual(self.classifier.apriori, newClassifier.apriori)
        print '\nCOMPARE\n'
        for k,v in self.classifier.apriori.items():
            print k,v
        for k,v in newClassifier.apriori.items():
            print k,v
        print '\nEND OF COMPARE\n'
    '''
开发者ID:chefarov,项目名称:predictNextWord,代码行数:86,代码来源:unittests.py


注:本文中的classifier.Classifier.update_joint_apriori方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。