<commit_before>import json
import random

from chatterbot.storage import StorageAdapter
from chatterbot.conversation import Response
from chatterbot.conversation import Statement

Base = None

try:
    from sqlalchemy.ext.declarative import declarative_base

    Base = declarative_base()

    class StatementTable(Base):
        from sqlalchemy import Column, Integer, String, PickleType
        from sqlalchemy.orm import relationship

        __tablename__ = 'StatementTable'

        def get_statement(self):
            stmt = Statement(self.text, **self.extra_data)
            for resp in self.in_response_to:
                stmt.add_response(resp.get_response())
            return stmt

        def get_statement_serialized(context):
            params = context.current_parameters
            del params['text_search']
            return json.dumps(params)

        id = Column(Integer)
        text = Column(String, primary_key=True)
        extra_data = Column(PickleType)
        # relationship:
        in_response_to = relationship("ResponseTable", back_populates="statement_table")
        text_search = Column(String, primary_key=True, default=get_statement_serialized)

    class ResponseTable(Base):
        from sqlalchemy import Column, Integer, String, ForeignKey
        from sqlalchemy.orm import relationship

        __tablename__ = 'ResponseTable'

        def get_reponse_serialized(context):
            params = context.current_parameters
            del params['text_search']
            return json.dumps(params)

        id = Column(Integer)
        text = Column(String, primary_key=True)
        occurrence = Column(Integer)
        statement_text = Column(String, ForeignKey('StatementTable.text'))

        statement_table = relationship("StatementTable", back_populates="in_response_to", cascade="all", uselist=False)
        text_search = Column(String, primary_key=True, default=get_reponse_serialized)

        def get_response(self):
            occ = {"occurrence": self.occurrence}
            return Response(text=self.text, **occ)

except ImportError:
    pass


def get_statement_table(statement):
    responses = list(map(get_response_table, statement.in_response_to))
    return StatementTable(text=statement.text, in_response_to=responses, extra_data=statement.extra_data)


def get_response_table(response):
    return ResponseTable(text=response.text, occurrence=response.occurrence)<commit_msg>Clean up sqlalchemy model code<commit_after>import json
import random

from chatterbot.storage import StorageAdapter
from chatterbot.conversation import Response
from chatterbot.conversation import Statement

Base = None

try:
    from sqlalchemy.ext.declarative import declarative_base

    Base = declarative_base()

    class StatementTable(Base):
        from sqlalchemy import Column, Integer, String, PickleType
        from sqlalchemy.orm import relationship

        __tablename__ = 'StatementTable'

        def get_statement(self):
            statement = Statement(self.text, **self.extra_data)
            for response in self.in_response_to:
                statement.add_response(response.get_response())
            return statement

        def get_statement_serialized(context):
            params = context.current_parameters
            del params['text_search']
            return json.dumps(params)

        id = Column(Integer)
        text = Column(String, primary_key=True)
        extra_data = Column(PickleType)

        in_response_to = relationship(
            'ResponseTable',
            back_populates='statement_table'
        )
        text_search = Column(
            String,
            primary_key=True,
            default=get_statement_serialized
        )

    class ResponseTable(Base):
        from sqlalchemy import Column, Integer, String, ForeignKey
        from sqlalchemy.orm import relationship

        __tablename__ = 'ResponseTable'

        def get_reponse_serialized(context):
            params = context.current_parameters
            del params['text_search']
            return json.dumps(params)

        id = Column(Integer)
        text = Column(String, primary_key=True)
        occurrence = Column(Integer)
        statement_text = Column(String, ForeignKey('StatementTable.text'))

        statement_table = relationship(
            'StatementTable',
            back_populates='in_response_to',
            cascade='all',
            uselist=False
        )
        text_search = Column(
            String,
            primary_key=True,
            default=get_reponse_serialized
        )

        def get_response(self):
            occ = {'occurrence': self.occurrence}
            return Response(text=self.text, **occ)

except ImportError:
    pass


def get_statement_table(statement):
    responses = list(map(get_response_table, statement.in_response_to))
    return StatementTable(text=statement.text, in_response_to=responses, extra_data=statement.extra_data)


def get_response_table(response):
    return ResponseTable(text=response.text, occurrence=response.occurrence)
{'fraction_non_alphanumeric': 0.07573082205218756, 'fraction_numerical': 0.0, 'mean_word_length': 2.9095092024539877, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4491595', 'n_tokens_mistral': 1376, 'n_tokens_neox': 1311, 'n_words': 290}