Example #1
0
    def update(self, statement):
        """
        Modifies an entry in the database.
        Creates an entry if one does not exist.
        """
        Statement = self.get_model('statement')
        Response = self.get_model('response')
        Tag = self.get_model('tag')

        if statement:
            session = self.Session()

            query = session.query(Statement).filter_by(text=statement.text)
            record = query.first()

            # Create a new statement entry if one does not already exist
            if not record:
                record = Statement(text=statement.text)

            record.extra_data = dict(statement.extra_data)

            for _tag in statement.tags:
                tag = session.query(Tag).filter_by(name=_tag).first()

                if not tag:
                    # Create the record
                    tag = Tag(name=_tag)

                record.tags.append(tag)

            # Get or create the response records as needed
            for response in statement.in_response_to:
                _response = session.query(Response).filter_by(
                    text=response.text,
                    statement_text=statement.text
                ).first()

                if _response:
                    _response.occurrence += 1
                else:
                    # Create the record
                    _response = Response(
                        text=response.text,
                        statement_text=statement.text,
                        occurrence=response.occurrence
                    )

                record.in_response_to.append(_response)

            session.add(record)

            self._session_finish(session)
Example #2
0
    def update(self, statement):
        """
        Modifies an entry in the database.
        Creates an entry if one does not exist.
        """
        Statement = self.get_model('statement')
        Response = self.get_model('response')
        Tag = self.get_model('tag')

        if statement:
            session = self.Session()

            query = session.query(Statement).filter_by(text=statement.text)
            record = query.first()

            # Create a new statement entry if one does not already exist
            if not record:
                record = Statement(text=statement.text)

            record.extra_data = dict(statement.extra_data)

            for _tag in statement.tags:
                tag = session.query(Tag).filter_by(name=_tag).first()

                if not tag:
                    # Create the record
                    tag = Tag(name=_tag)

                record.tags.append(tag)

            # Get or create the response records as needed
            for response in statement.in_response_to:
                _response = session.query(Response).filter_by(
                    text=response.text,
                    statement_text=statement.text
                ).first()

                if _response:
                    _response.occurrence += 1
                else:
                    # Create the record
                    _response = Response(
                        text=response.text,
                        statement_text=statement.text,
                        occurrence=response.occurrence
                    )

                record.in_response_to.append(_response)

            session.add(record)

            self._session_finish(session)
Example #3
0
    def update(self, statement):
        """
        Modifies an entry in the database.
        Creates an entry if one does not exist.
        """
        Statement = self.get_model('statement')
        Tag = self.get_model('tag')

        if statement is not None:
            session = self.Session()
            record = None

            if hasattr(statement, 'id') and statement.id is not None:
                record = session.query(Statement).get(statement.id)
            else:
                record = session.query(Statement).filter(
                    Statement.text == statement.text,
                    Statement.conversation == statement.conversation,
                ).first()

                # Create a new statement entry if one does not already exist
                if not record:
                    record = Statement(text=statement.text,
                                       conversation=statement.conversation)

            # Update the response value
            record.in_response_to = statement.in_response_to

            record.created_at = statement.created_at
            record.extra_data = dict(statement.extra_data)

            if statement.extra_data is None:
                statement.extra_data = {}

            for _tag in statement.tags:
                tag = session.query(Tag).filter_by(name=_tag).first()

                if not tag:
                    # Create the record
                    tag = Tag(name=_tag)

                record.tags.append(tag)

            session.add(record)

            self._session_finish(session)
Example #4
0
    def update(self, statement):
        """
        Modifies an entry in the database.
        Creates an entry if one does not exist.
        """
        from chatterbot.ext.sqlalchemy_app.models import Statement, Response

        if statement:
            session = self.Session()
            query = self.__statement_filter(session, **{"text": statement.text})
            record = query.first()

            # Create a new statement entry if one does not already exist
            if not record:
                record = Statement(text=statement.text)

            record.extra_data = dict(statement.extra_data)

            if statement.in_response_to:
                # Get or create the response records as needed
                for response in statement.in_response_to:
                    _response = session.query(Response).filter_by(
                        text=response.text,
                        statement_text=statement.text
                    ).first()

                    if _response:
                        _response.occurrence += 1
                    else:
                        # Create the record
                        _response = Response(
                            text=response.text,
                            statement_text=statement.text,
                            occurrence=response.occurrence
                        )

                    record.in_response_to.append(_response)

            session.add(record)

            self._session_finish(session)
Example #5
0
    def update(self, statement):
        """
        Modifies an entry in the database.
        Creates an entry if one does not exist.
        """
        from chatterbot.ext.sqlalchemy_app.models import Statement, Response

        if statement:
            session = self.Session()
            query = self.__statement_filter(session,
                                            **{"text": statement.text})
            record = query.first()

            # Create a new statement entry if one does not already exist
            if not record:
                record = Statement(text=statement.text)

            record.extra_data = dict(statement.extra_data)

            if statement.in_response_to:
                # Get or create the response records as needed
                for response in statement.in_response_to:
                    _response = session.query(Response).filter_by(
                        text=response.text,
                        statement_text=statement.text).first()

                    if _response:
                        _response.occurrence += 1
                    else:
                        # Create the record
                        _response = Response(text=response.text,
                                             statement_text=statement.text,
                                             occurrence=response.occurrence)

                    record.in_response_to.append(_response)

            session.add(record)

            self._session_finish(session)