当前位置: 首页>>代码示例>>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;未经允许,请勿转载。