markov state queries need the context to be unique

This commit is contained in:
Brian S. Stephan 2023-02-19 19:36:16 -06:00
parent d516c1b08e
commit cfeddfdc4e
Signed by: bss
GPG Key ID: 3DE06D3180895FCB
1 changed files with 6 additions and 4 deletions

View File

@ -16,7 +16,7 @@ class Command(BaseCommand):
self.stdout.write(self.style.NOTICE(f"scanning context {context}..."))
# get starting states that look like they came over the bridge
bridge_states = context.states.filter(k1=MarkovState._start1, k2=MarkovState._start2,
v__regex=r'<.*>')
v__regex=r'<[A-Za-z0-9_]+>', context=context)
self._chain_remover(context, bridge_states)
# get states that look like mentions
for target in context.markovtarget_set.all():
@ -27,7 +27,7 @@ class Command(BaseCommand):
all_nicks = target.channel.server.nickname
mention_regex = r'^(({nicks})[:,]|@({nicks}))$'.format(nicks=all_nicks)
mention_states = context.states.filter(k1=MarkovState._start1, k2=MarkovState._start2,
v__regex=mention_regex)
v__regex=mention_regex, context=context)
self._chain_remover(context, mention_states)
def _chain_remover(self, context, start_states):
@ -50,7 +50,8 @@ class Command(BaseCommand):
self.stdout.write(self.style.NOTICE(f" upserting state based on {leaf_state}"))
# get/update state without the nick from the bridge
try:
updated_leaf = MarkovState.objects.get(k1=second_state.k1, k2=leaf_state.k2, v=leaf_state.v)
updated_leaf = MarkovState.objects.get(k1=second_state.k1, k2=leaf_state.k2, v=leaf_state.v,
context=context)
updated_leaf.count += leaf_state.count
updated_leaf.save()
self.stdout.write(self.style.SUCCESS(f" updated count for {updated_leaf}"))
@ -68,7 +69,8 @@ class Command(BaseCommand):
# take care of the new middle state
self.stdout.write(self.style.NOTICE(f" upserting state based on {second_state}"))
try:
updated_second = MarkovState.objects.get(k1=start_state.k1, k2=start_state.k2, v=second_state.v)
updated_second = MarkovState.objects.get(k1=start_state.k1, k2=start_state.k2, v=second_state.v,
context=context)
updated_second.count += second_state.count
updated_second.save()
self.stdout.write(self.style.SUCCESS(f" updated count for {updated_second}"))