當前位置: 首頁>>代碼示例>>Python>>正文


Python Markov.make_ngram_freqs_dict方法代碼示例

本文整理匯總了Python中markov.Markov.make_ngram_freqs_dict方法的典型用法代碼示例。如果您正苦於以下問題:Python Markov.make_ngram_freqs_dict方法的具體用法?Python Markov.make_ngram_freqs_dict怎麽用?Python Markov.make_ngram_freqs_dict使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在markov.Markov的用法示例。


在下文中一共展示了Markov.make_ngram_freqs_dict方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: Test_Markov

# 需要導入模塊: from markov import Markov [as 別名]
# 或者: from markov.Markov import make_ngram_freqs_dict [as 別名]
class Test_Markov(unittest.TestCase):

    def setUp(self):
        self.test_text = "I have bought this bonnet.\r\nI am Mr. Darcy.\r\nI am.\r\n"
        with open("in.txt",'w') as f:
            f.write(self.test_text)
        self.test_outfile = "out.txt"
        ospl.process_data("in.txt", self.test_outfile, True)
        self.test_markov = Markov(self.test_outfile)
    
    def test_unigrams(self):
        self.assertEqual(type(self.test_markov.unigrams), defaultdict)
        self.assertEqual(round(sum(self.test_markov.unigrams.values()), 5), 1.0)
        self.assertEqual(self.test_markov.unigrams["**Beginning**"], 0.15)
    
    def test_bigrams(self):
        self.assertEqual(type(self.test_markov.bigrams), defaultdict)
        self.assertEqual(round(sum(self.test_markov.bigrams["I"].values()), 5), 1.0)
        self.assertEqual(self.test_markov.bigrams["**Beginning**"]["I"], 1.0)
        self.assertEqual(self.test_markov.bigrams["I"]["have"], 1/3.)
    
    def test_bigrams_counter(self):
        bigram_freqs = self.test_markov.make_ngram_freqs_dict(2)
        self.assertEqual(type(bigram_freqs), defaultdict)
        self.assertEqual(bigram_freqs["I"]["am"], 2)
        self.assertEqual(bigram_freqs["."]["**End**"], 3)
        
    def test_trigrams(self):
        self.assertEqual(type(self.test_markov.trigrams), defaultdict)
        self.assertEqual(round(sum(self.test_markov.trigrams[("I","am")].values()), 5), 1.0)
        self.assertEqual(self.test_markov.trigrams[("**Beginning**","I")]["am"], 2/3.)
        self.assertEqual(self.test_markov.trigrams[("I","have")]["rocketships"], 1e-5)

    def test_trigrams_counter(self):
        trigram_freqs = self.test_markov.make_ngram_freqs_dict(3)
        self.assertEqual(type(trigram_freqs), defaultdict)
        self.assertEqual(trigram_freqs[("Darcy",".")]["**End**"], 1)
        self.assertEqual(trigram_freqs[("**Beginning**","I")]["am"], 2)
        
    def test_make_sentence(self):
        self.test_markov.make_ngram_sentence(n=2)
        self.test_markov.make_ngram_sentence(n=3)
        self.assertRaises(AssertionError, self.test_markov.make_ngram_sentence, n=5)
        
    def test_score_sentence(self):
        """ make up nonsense sentence, make sure it scores worse than Markov sentence. possibly also make up word with same words as corpus but in a completly bogus order, that should also score lower"""
        nonsense = "Vestiges of cetaceous gentility."
        nonsense_score = self.test_markov.score_sentence(nonsense)
        real_trigram_sent = self.test_markov.make_ngram_sentence()
        real_trigram_score = self.test_markov.score_sentence(real_trigram_sent)
        weird_order = "Darcy . I bonnet am bought have am"
        weird_order_score = self.test_markov.score_sentence(weird_order)
        self.assertGreater(nonsense_score, real_trigram_score)
        self.assertGreater(weird_order_score, real_trigram_score)
        
    def tearDown(self):
        os.remove("in.txt")
        os.remove("out.txt")
開發者ID:astrosilverio,項目名稱:PandPandWhales,代碼行數:60,代碼來源:test_ngrams.py


注:本文中的markov.Markov.make_ngram_freqs_dict方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。