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


Python Markov.score_for_line方法代碼示例

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


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

示例1: FullTextMarkovEvaluator

# 需要導入模塊: from markov import Markov [as 別名]
# 或者: from markov.Markov import score_for_line [as 別名]
class FullTextMarkovEvaluator(HaikuEvaluator):
    """
    Evaluate the full text of a haiku using Markov chains, determine how it 
    """
    def __init__(self, weight=1, prefix=None):
        self.prefix = prefix or getattr(settings, "MARKOV_DATA_PREFIX", "goodlines")
        self.data = Markov(prefix=prefix, **getattr(settings, 'REDIS', {}))
        super(FullTextMarkovEvaluator, self).__init__(weight=weight)
    
    def evaluate(self, haiku):
        haiku_text = []
        for line in haiku.get_lines():
            haiku_text += line.split()
        score = self.data.score_for_line(haiku_text)
        return score
開發者ID:wieden-kennedy,項目名稱:django-haikus,代碼行數:17,代碼來源:evaluators.py

示例2: TestMarkovClass

# 需要導入模塊: from markov import Markov [as 別名]
# 或者: from markov.Markov import score_for_line [as 別名]
class TestMarkovClass(unittest.TestCase):
    """
    Test that the Markov wrapper class behaves as expected
    """
    def setUp(self):
        self.markov = Markov(prefix="testclass",db=11)

    def test_add_line_to_index(self):
        line =  ['i','ate','a','peach']
        line1 = ['i','ate','one','peach']
        line2 = ['i','ate','a', 'sandwich']

        self.markov.add_line_to_index(line)
        self.markov.add_line_to_index(line1)
        self.markov.add_line_to_index(line2)
        self.assertEqual(self.markov.client.zscore("testclass:i:ate", "a"), 2.0)
        self.assertEqual(self.markov.client.zscore("testclass:ate:a", "peach"), 1.0)
                
    def test_score_for_line(self):
        self.test_add_line_to_index()
        line = ['i','ate','a','peach']
        self.assertEqual(self.markov.score_for_line(line), 100)
       
        
    def test_generate(self):
        self.test_add_line_to_index()
        generated = self.markov.generate(max_words=3)
        assert len(generated) >= 2
        assert len(generated) <= 3
        generated = self.markov.generate(seed=['ate','one'], max_words=3)
        assert 'peach' in generated 
        assert 'sandwich' not in generated
        
    def tearDown(self):
        """
        clean up our redis keys
        """
        keys = self.markov.client.keys(self.markov.prefix+"*")
        for key in keys:
            self.markov.client.delete(key)
開發者ID:gthomas,項目名稱:python-markov,代碼行數:42,代碼來源:tests.py

示例3: TestMarkovClass

# 需要導入模塊: from markov import Markov [as 別名]
# 或者: from markov.Markov import score_for_line [as 別名]
class TestMarkovClass(unittest.TestCase):
    """
    Test that the Markov wrapper class behaves as expected
    """
    def setUp(self):
        self.markov = Markov(prefix="testclass", db=11)

    def test_add_line_to_index(self):
        line =  ['i','ate','a','peach']
        line1 = ['i','ate','one','peach']
        line2 = ['i','ate','a', 'sandwich']

        self.markov.add_line_to_index(line)
        self.markov.add_line_to_index(line1)
        self.markov.add_line_to_index(line2)
        self.assertEqual(self.markov.client.zscore("testclass:i:ate", "a"), 2.0)
        self.assertEqual(self.markov.client.zscore("testclass:ate:a", "peach"), 1.0)
                
    def test_score_for_line(self):
        self.test_add_line_to_index()
        line = ['i','ate','a','peach']
        self.assertEqual(self.markov.score_for_line(line), 100)
       
        
    def test_generate(self):
        self.test_add_line_to_index()
        generated = self.markov.generate(max_words=3)
        assert len(generated) >= 2
        assert len(generated) <= 3
        generated = self.markov.generate(seed=['ate','one'], max_words=3)
        assert 'peach' in generated 
        assert 'sandwich' not in generated

    def test_flush(self):
        m1 = Markov(prefix="one", db=5)
        m2 = Markov(prefix="two", db=5)

        line =  ['i','ate','a','peach']
        line1 = ['i','ate','one','peach']
        line2 = ['i','ate','a', 'sandwich']

        m1.add_line_to_index(line)
        m1.add_line_to_index(line1)
        m1.add_line_to_index(line2)

        important_line =  ['we', 'all', 'have', 'phones']

        m2.add_line_to_index(important_line)

        r = redis.Redis(db=5)
        assert len(r.keys("one:*")) == 6
        assert len(r.keys("two:*")) == 3

        m1.flush(prefix="one")

        assert len(r.keys("one:*")) == 0
        assert len(r.keys("two:*")) == 3

        m2.flush(prefix="two")

        assert len(r.keys("one:*")) == 0
        assert len(r.keys("two:*")) == 0        
        
    def tearDown(self):
        """
        clean up our redis keys
        """
        keys = self.markov.client.keys(self.markov.prefix+"*")
        for key in keys:
            self.markov.client.delete(key)
開發者ID:vfulco,項目名稱:python-markov,代碼行數:72,代碼來源:tests.py


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