Fixes of saving algorithm
This commit is contained in:
parent
31076224f8
commit
5331582570
|
@ -9,7 +9,7 @@ class CreateChatsTable(Migration):
|
|||
"""
|
||||
with self.schema.create('chats') as table:
|
||||
table.increments('id')
|
||||
table.big_integer('telegram_id').unsigned()
|
||||
table.integer('telegram_id').unique()
|
||||
table.string('chat_type')
|
||||
table.integer('random_chance').default(5)
|
||||
table.timestamps()
|
||||
|
|
|
@ -9,12 +9,13 @@ class CreatePairsTable(Migration):
|
|||
"""
|
||||
with self.schema.create('pairs') as table:
|
||||
table.increments('id')
|
||||
table.integer('chat_id').unsigned()
|
||||
table.foreign('chat_id').references('id').on('chats')
|
||||
table.integer('first_id').unsigned().nullable()
|
||||
#table.foreign('first_id').references('id').on('replies').nullable()
|
||||
table.integer('second_id').unsigned().nullable()
|
||||
#table.foreign('second_id').references('id').on('replies').nullable()
|
||||
table.integer('chat_id')
|
||||
table.integer('first_id').nullable()
|
||||
table.integer('second_id').nullable()
|
||||
# table.unique(
|
||||
# ['chat_id', 'first_id', 'second_id'],
|
||||
# name='unique_pairs_idx'
|
||||
# )
|
||||
table.timestamps()
|
||||
|
||||
def down(self):
|
||||
|
|
|
@ -9,12 +9,9 @@ class CreateRepliesTable(Migration):
|
|||
"""
|
||||
with self.schema.create('replies') as table:
|
||||
table.increments('id')
|
||||
table.integer('pair_id').unsigned()
|
||||
table.foreign('pair_id').references('id').on('pairs')
|
||||
table.integer('word_id').unsigned().nullable()
|
||||
# table.foreign('word_id').references('id').on('words').nullable()
|
||||
table.integer('pair_id')
|
||||
table.integer('word_id').nullable()
|
||||
table.integer('count').default(1)
|
||||
table.timestamps()
|
||||
|
||||
def down(self):
|
||||
"""
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
import random
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from orator.orm import Model
|
||||
from orator.orm import belongs_to
|
||||
from orator.orm import has_many
|
||||
|
||||
from src.utils import *
|
||||
|
||||
import src.domain.chat
|
||||
import src.domain.reply
|
||||
import src.domain.word
|
||||
|
@ -54,18 +55,28 @@ class Pair(Model):
|
|||
words = [None]
|
||||
for word in message.words:
|
||||
words.append(word)
|
||||
if word[-1:] in Pair.end_sentence:
|
||||
if word[-1] in Pair.end_sentence:
|
||||
words.append(None)
|
||||
if words[-1:] is not None:
|
||||
if words[-1] is not None:
|
||||
words.append(None)
|
||||
|
||||
while any(word for word in words):
|
||||
trigram = words[:3]
|
||||
words.pop(0)
|
||||
trigram_word_ids = list(map(lambda x: None if x is None else src.domain.word.Word.where('word', word).first().id, trigram))
|
||||
pair = Pair.first_or_create(chat_id=message.chat.id,
|
||||
first_id=trigram_word_ids[0],
|
||||
second_id=trigram_word_ids[1])
|
||||
trigram_word_ids = list(map(
|
||||
lambda x: None if x is None else src.domain.word.Word.where('word', x).first().id,
|
||||
trigram
|
||||
))
|
||||
|
||||
pair = Pair.where('chat_id', message.chat.id)\
|
||||
.where('first_id', trigram_word_ids[0])\
|
||||
.where('second_id', trigram_word_ids[1])\
|
||||
.first()
|
||||
if pair is None:
|
||||
pair = Pair.create(chat_id=message.chat.id,
|
||||
first_id=trigram_word_ids[0],
|
||||
second_id=trigram_word_ids[1])
|
||||
|
||||
last_trigram_id = trigram_word_ids[2] if len(trigram_word_ids) == 3 else None
|
||||
reply = pair.replies().where('word_id', last_trigram_id).first()
|
||||
|
||||
|
@ -92,17 +103,27 @@ class Pair(Model):
|
|||
|
||||
reply = random.choice(replies.all())
|
||||
first_word = pair.second.id
|
||||
second_words = getattr(reply.word, 'id')
|
||||
if second_words is not None:
|
||||
second_words = [second_words]
|
||||
|
||||
# TODO. WARNING! Do not try to fix, it's magic, i have no clue why
|
||||
try:
|
||||
second_words = [reply.word.id]
|
||||
except AttributeError:
|
||||
second_words = None
|
||||
|
||||
if len(sentences) == 0:
|
||||
sentences.append(Pair.capitalize(pair.second.word) + " ")
|
||||
sentences.append(capitalize(pair.second.word) + " ")
|
||||
word_ids.remove(pair.second.id)
|
||||
|
||||
reply_word = getattr(reply.word, 'word', '')
|
||||
if reply_word not in sentences and Pair.capitalize(reply_word) not in sentences:
|
||||
# TODO. WARNING! Do not try to fix, it's magic, i have no clue why
|
||||
try:
|
||||
reply_word = reply.word.word
|
||||
except AttributeError:
|
||||
reply_word = None
|
||||
|
||||
if reply_word is not None:
|
||||
sentences.append(reply_word)
|
||||
else:
|
||||
break
|
||||
|
||||
sentence = ' '.join(sentences).strip()
|
||||
if sentence[-1:] not in Pair.end_sentence:
|
||||
|
@ -110,10 +131,6 @@ class Pair(Model):
|
|||
|
||||
return sentence
|
||||
|
||||
@staticmethod
|
||||
def capitalize(string):
|
||||
return string[:1].upper() + string[1:]
|
||||
|
||||
@staticmethod
|
||||
def __get_pair(chat_id, first_id, second_ids):
|
||||
pairs = Pair.with_('replies')\
|
||||
|
@ -125,4 +142,4 @@ class Pair(Model):
|
|||
.get()\
|
||||
.all()
|
||||
|
||||
return random.choice(pairs) if len(pairs) != 0 else None
|
||||
return random_element(pairs)
|
||||
|
|
|
@ -8,6 +8,7 @@ import src.domain.word
|
|||
|
||||
class Reply(Model):
|
||||
__guarded__ = ['id']
|
||||
__timestamps__ = False
|
||||
|
||||
@belongs_to_many
|
||||
def pairs(self):
|
||||
|
|
|
@ -2,6 +2,7 @@ from orator.orm import Model
|
|||
from orator.orm import has_many
|
||||
|
||||
import src.domain.chat
|
||||
from collections import OrderedDict
|
||||
|
||||
|
||||
class Word(Model):
|
||||
|
@ -15,9 +16,8 @@ class Word(Model):
|
|||
@staticmethod
|
||||
def learn(words):
|
||||
existing_words = Word.where_in('word', words).get().pluck('word').all()
|
||||
|
||||
new_words = {word for word in words}.difference(existing_words)
|
||||
# TODO. Слова должны быть уникальные И ТАКЖЕ ОБЯЗАТЕЛЬНО в оригинальном порядке
|
||||
new_words = [word for word in OrderedDict.fromkeys(words).keys() if word not in existing_words]
|
||||
|
||||
if len(new_words):
|
||||
for word in new_words:
|
||||
Word.create(word=word)
|
||||
for word in new_words:
|
||||
Word.create(word=word)
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
import random
|
||||
|
||||
|
||||
def capitalize(string):
|
||||
return string[:1].upper() + string[1:]
|
||||
|
||||
|
||||
def random_element(xlist):
|
||||
return random.choice(xlist) if len(xlist) > 0 else None
|
Loading…
Reference in New Issue