dr.botzo/markov/models.py
Brian Stephan 333424025b support markov targets with identical names on different servers
markov targets are queried and autogenerated based on chatter, but had a
legacy name which is no longer in use for this, preferring the foreign
keys to channel and consequently server. the name is really just
informative these days, but was still being used to find targets, and
thus was breaking when two servers had the same channel name in them.
this fixes that
2023-05-04 17:24:07 -05:00

65 lines
1.9 KiB
Python

"""Save brain pieces as markov chains for chaining."""
import logging
from django.db import models
from ircbot.models import IrcChannel
log = logging.getLogger(__name__)
class MarkovContext(models.Model):
"""Define contexts for Markov chains."""
name = models.CharField(max_length=200, unique=True)
def __str__(self):
"""Provide string representation."""
return "{0:s}".format(self.name)
class MarkovTarget(models.Model):
"""Define IRC targets that relate to a context, and can occasionally be talked to."""
name = models.CharField(max_length=200)
context = models.ForeignKey(MarkovContext, on_delete=models.CASCADE)
channel = models.ForeignKey(IrcChannel, on_delete=models.CASCADE)
chatter_chance = models.IntegerField(default=0)
def __str__(self):
"""Provide string representation."""
return "{0:s} -> {1:s}".format(str(self.channel), self.context.name)
class MarkovState(models.Model):
"""One element in a Markov chain, some text or something."""
_start1 = '__start1'
_start2 = '__start2'
_stop = '__stop'
k1 = models.CharField(max_length=128)
k2 = models.CharField(max_length=128)
v = models.CharField(max_length=128)
count = models.IntegerField(default=0)
context = models.ForeignKey(MarkovContext, on_delete=models.CASCADE, related_name='states')
class Meta:
"""Options for the model itself."""
index_together = [
['context', 'k1', 'k2'],
['context', 'v'],
]
permissions = {
('import_text_file', "Can import states from a text file"),
('teach_line', "Can teach lines"),
}
unique_together = ('context', 'k1', 'k2', 'v')
def __str__(self):
"""Provide string representation."""
return "{0:s},{1:s} -> {2:s} (count: {3:d})".format(self.k1, self.k2, self.v, self.count)