linter fixes for markov library methods
This commit is contained in:
parent
8b3caabb57
commit
3d0be3c25a
@ -1,3 +1,4 @@
|
||||
"""Provide methods for manipulating markov chain processing."""
|
||||
import logging
|
||||
import random
|
||||
|
||||
@ -5,13 +6,11 @@ from django.db.models import Sum
|
||||
|
||||
from markov.models import MarkovContext, MarkovState, MarkovTarget
|
||||
|
||||
|
||||
log = logging.getLogger('markov.lib')
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def generate_line(context, topics=None, min_words=15, max_words=30, sentence_bias=2, max_tries=5):
|
||||
"""String multiple sentences together into a coherent sentence."""
|
||||
|
||||
"""Combine multiple sentences together into a coherent sentence."""
|
||||
tries = 0
|
||||
line = []
|
||||
min_words_per_sentence = min_words / sentence_bias
|
||||
@ -23,7 +22,7 @@ def generate_line(context, topics=None, min_words=15, max_words=30, sentence_bia
|
||||
else:
|
||||
if len(line) > 0:
|
||||
if line[-1][-1] not in [',', '.', '!', '?', ':']:
|
||||
line[-1] += random.choice(['?', '.', '!'])
|
||||
line[-1] += random.SystemRandom().choice(['?', '.', '!'])
|
||||
|
||||
tries += 1
|
||||
|
||||
@ -33,7 +32,6 @@ def generate_line(context, topics=None, min_words=15, max_words=30, sentence_bia
|
||||
|
||||
def generate_longish_sentence(context, topics=None, min_words=15, max_words=30, max_tries=100):
|
||||
"""Generate a Markov chain, but throw away the short ones unless we get desperate."""
|
||||
|
||||
sent = ""
|
||||
tries = 0
|
||||
while tries < max_tries:
|
||||
@ -52,20 +50,19 @@ def generate_longish_sentence(context, topics=None, min_words=15, max_words=30,
|
||||
|
||||
def generate_sentence(context, topics=None, min_words=15, max_words=30):
|
||||
"""Generate a Markov chain."""
|
||||
|
||||
words = []
|
||||
# if we have topics, try to work from it and work backwards
|
||||
if topics:
|
||||
topic_word = random.choice(topics)
|
||||
topic_word = random.SystemRandom().choice(topics)
|
||||
topics.remove(topic_word)
|
||||
log.debug("looking for topic '{0:s}'".format(topic_word))
|
||||
log.debug("looking for topic '%s'", topic_word)
|
||||
new_states = MarkovState.objects.filter(context=context, v=topic_word)
|
||||
|
||||
if len(new_states) > 0:
|
||||
log.debug("found '{0:s}', starting backwards".format(topic_word))
|
||||
log.debug("found '%s', starting backwards", topic_word)
|
||||
words.insert(0, topic_word)
|
||||
while len(words) <= max_words and words[0] != MarkovState._start2:
|
||||
log.debug("looking backwards for '{0:s}'".format(words[0]))
|
||||
log.debug("looking backwards for '%s'", words[0])
|
||||
new_states = MarkovState.objects.filter(context=context, v=words[0])
|
||||
# if we find a start, use it
|
||||
if MarkovState._start2 in new_states:
|
||||
@ -87,7 +84,7 @@ def generate_sentence(context, topics=None, min_words=15, max_words=30):
|
||||
|
||||
i = len(words)
|
||||
while words[-1] != MarkovState._stop:
|
||||
log.debug("looking for '{0:s}','{1:s}'".format(words[i-2], words[i-1]))
|
||||
log.debug("looking for '%s','%s'", words[i-2], words[i-1])
|
||||
new_states = MarkovState.objects.filter(context=context, k1=words[i-2], k2=words[i-1])
|
||||
log.debug("states retrieved")
|
||||
|
||||
@ -103,7 +100,7 @@ def generate_sentence(context, topics=None, min_words=15, max_words=30):
|
||||
words.append(MarkovState._stop)
|
||||
elif len(target_hits) > 0:
|
||||
# if there's a target word in the states, pick it
|
||||
target_hit = random.choice(target_hits)
|
||||
target_hit = random.SystemRandom().choice(target_hits)
|
||||
log.debug("found a topic hit %s, using it", target_hit)
|
||||
topics.remove(target_hit)
|
||||
words.append(target_hit)
|
||||
@ -129,7 +126,6 @@ def generate_sentence(context, topics=None, min_words=15, max_words=30):
|
||||
|
||||
def get_or_create_target_context(target_name):
|
||||
"""Return the context for a provided nick/channel, creating missing ones."""
|
||||
|
||||
target_name = target_name.lower()
|
||||
|
||||
# find the stuff, or create it
|
||||
@ -156,7 +152,6 @@ def get_or_create_target_context(target_name):
|
||||
|
||||
def get_word_out_of_states(states, backwards=False):
|
||||
"""Pick one random word out of the given states."""
|
||||
|
||||
# work around possible broken data, where a k1,k2 should have a value but doesn't
|
||||
if len(states) == 0:
|
||||
states = MarkovState.objects.filter(v=MarkovState._stop)
|
||||
@ -168,9 +163,9 @@ def get_word_out_of_states(states, backwards=False):
|
||||
# this being None probably means there's no data for this context
|
||||
raise ValueError("no markov states to generate from")
|
||||
|
||||
hit = random.randint(0, count_sum)
|
||||
hit = random.SystemRandom().randint(0, count_sum)
|
||||
|
||||
log.debug("sum: {0:d} hit: {1:d}".format(count_sum, hit))
|
||||
log.debug("sum: %s hit: %s", count_sum, hit)
|
||||
|
||||
states_itr = states.iterator()
|
||||
for state in states_itr:
|
||||
@ -183,13 +178,12 @@ def get_word_out_of_states(states, backwards=False):
|
||||
|
||||
break
|
||||
|
||||
log.debug("found '{0:s}'".format(new_word))
|
||||
log.debug("found '%s'", new_word)
|
||||
return new_word
|
||||
|
||||
|
||||
def learn_line(line, context):
|
||||
"""Create a bunch of MarkovStates for a given line of text."""
|
||||
|
||||
log.debug("learning %s...", line[:40])
|
||||
|
||||
words = line.split()
|
||||
@ -200,7 +194,7 @@ def learn_line(line, context):
|
||||
return
|
||||
|
||||
for i, word in enumerate(words):
|
||||
log.debug("'{0:s}','{1:s}' -> '{2:s}'".format(words[i], words[i+1], words[i+2]))
|
||||
log.debug("'%s','%s' -> '%s'", words[i], words[i+1], words[i+2])
|
||||
state, created = MarkovState.objects.get_or_create(context=context,
|
||||
k1=words[i],
|
||||
k2=words[i+1],
|
||||
|
Loading…
Reference in New Issue
Block a user