本文整理汇总了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
示例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)
示例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)