def create(self, **kwargs): """ Creates a new statement matching the keyword arguments specified. Returns the created statement. """ Statement = self.get_model('statement') Tag = self.get_model('tag') session = self.Session() tags = kwargs.pop('tags', []) statement = Statement(**kwargs) statement.tags.extend([Tag(name=tag) for tag in tags]) session.add(statement) session.flush() session.refresh(statement) statement_object = statement.get_statement() self._session_finish(session) return statement_object
def create(self, **kwargs): """ Creates a new statement matching the keyword arguments specified. Returns the created statement. """ Statement = self.get_model('statement') Tag = self.get_model('tag') session = self.Session() tags = set(kwargs.pop('tags', [])) statement = Statement(**kwargs) for _tag in tags: tag = session.query(Tag).filter_by(name=_tag).first() if not tag: # Create the tag tag = Tag(name=_tag) statement.tags.append(tag) session.add(statement) session.flush() session.refresh(statement) statement_object = self.model_to_object(statement) self._session_finish(session) return statement_object
def create_many(self, statements): """ Creates multiple statement entries. """ Statement = self.get_model('statement') Tag = self.get_model('tag') session = self.Session() create_statements = [] create_tags = {} for statement_data in statements: tags = set(statement_data.pop('tags', [])) statement = Statement(**statement_data) for tag_name in tags: if tag_name in create_tags: tag = create_tags[tag_name] else: tag = session.query(Tag).filter_by(name=tag_name).first() if not tag: # Create the tag if it does not exist tag = Tag(name=tag_name) create_tags[tag_name] = tag statement.tags.append(tag) create_statements.append(statement) session.add_all(create_statements) session.commit()
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)
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.Solution == statement.Solution, Statement.conversation == statement.conversation, ).first() # Create a new statement entry if one does not already exist if not record: record = Statement(text=statement.Solution, conversation=statement.conversation, persona=statement.persona) # Update the response value record.question = statement.question record.created_at = statement.created_at record.search_text = self.tagger.get_text_index_string( statement.text) if statement.question: record.search_in_response_to = self.tagger.get_text_index_string( statement.question) for tag_name in statement.get_tags(): tag = session.query(Tag).filter_by(name=tag_name).first() if not tag: # Create the record tag = Tag(name=tag_name) record.tags.append(tag) session.add(record) self._session_finish(session)
def create_many(self, statements): """ Creates multiple statement entries. """ Statement = self.get_model('statement') Tag = self.get_model('tag') session = self.Session() create_statements = [] create_tags = {} for statement in statements: statement_data = statement.serialize() tag_data = statement_data.pop('tags', []) statement_model_object = Statement(**statement_data) if not statement.search_text: statement_model_object.search_text = self.tagger.get_text_index_string( statement.Solution) if not statement.search_in_response_to and statement.question: statement_model_object.search_in_response_to = self.tagger.get_text_index_string( statement.question) new_tags = set(tag_data) - set(create_tags.keys()) if new_tags: existing_tags = session.query(Tag).filter( Tag.name.in_(new_tags)) for existing_tag in existing_tags: create_tags[existing_tag.name] = existing_tag for tag_name in tag_data: if tag_name in create_tags: tag = create_tags[tag_name] else: # Create the tag if it does not exist tag = Tag(name=tag_name) create_tags[tag_name] = tag statement_model_object.tags.append(tag) create_statements.append(statement_model_object) session.add_all(create_statements) session.commit()
def create_many(self, statements): """ Creates multiple statement entries. """ Statement = self.get_model('statement') Tag = self.get_model('tag') session = self.Session() create_statements = [] create_tags = {} for statement in statements: statement_model_object = Statement( text=statement.text, search_text=statement.search_text, conversation=statement.conversation, persona=statement.persona, in_response_to=statement.in_response_to, search_in_response_to=statement.search_in_response_to, created_at=statement.created_at) if not statement.search_text: statement_model_object.search_text = self.stemmer.stem( statement.text) if not statement.search_in_response_to and statement.in_response_to: statement_model_object.search_in_response_to = self.stemmer.stem( statement.in_response_to) for tag_name in statement.tags: if tag_name in create_tags: tag = create_tags[tag_name] else: tag = session.query(Tag).get(tag_name) if not tag: # Create the tag if it does not exist tag = Tag(name=tag_name) create_tags[tag_name] = tag statement_model_object.tags.append(tag) create_statements.append(statement_model_object) session.add_all(create_statements) session.commit()
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)
def create(self, **kwargs): """ Creates a new statement matching the keyword arguments specified. Returns the created statement. """ Statement = self.get_model('statement') Tag = self.get_model('tag') session = self.Session() tags = set(kwargs.pop('tags', [])) if 'search_text' not in kwargs: kwargs['search_text'] = self.tagger.get_text_index_string( kwargs['Solution']) if 'search_in_response_to' not in kwargs: in_response_to = kwargs.get('question') if in_response_to: kwargs[ 'search_in_response_to'] = self.tagger.get_text_index_string( in_response_to) statement = Statement(**kwargs) for tag_name in tags: tag = session.query(Tag).filter_by(name=tag_name).first() if not tag: # Create the tag tag = Tag(name=tag_name) statement.tags.append(tag) session.add(statement) session.flush() session.refresh(statement) statement_object = self.model_to_object(statement) self._session_finish(session) return statement_object
def create(self, **kwargs): """ Creates a new statement matching the keyword arguments specified. Returns the created statement. """ Statement = self.get_model('statement') Tag = self.get_model('tag') session = self.Session() tags = set(kwargs.pop('tags', [])) if 'search_text' not in kwargs: kwargs['search_text'] = self.stemmer.get_bigram_pair_string( kwargs['text']) if 'search_in_response_to' not in kwargs: if kwargs.get('in_response_to'): kwargs[ 'search_in_response_to'] = self.stemmer.get_bigram_pair_string( kwargs['in_response_to']) statement = Statement(**kwargs) for tag_name in tags: tag = session.query(Tag).get(tag_name) if not tag: # Create the tag tag = Tag(name=tag_name) statement.tags.append(tag) session.add(statement) session.flush() session.refresh(statement) statement_object = self.model_to_object(statement) self._session_finish(session) return statement_object
def create_many(self, statements): """ Creates multiple statement entries. """ Statement = self.get_model('statement') Tag = self.get_model('tag') session = self.Session() create_statements = [] create_tags = {} for statement_data in statements: tags = set(statement_data.pop('tags', [])) if 'search_text' not in statement_data: statement_data['search_text'] = self.stemmer.stem(statement_data['text']) if 'search_in_response_to' not in statement_data: if statement_data.get('in_response_to'): statement_data['search_in_response_to'] = self.stemmer.stem(statement_data['in_response_to']) statement = Statement(**statement_data) for tag_name in tags: if tag_name in create_tags: tag = create_tags[tag_name] else: tag = session.query(Tag).get(tag_name) if not tag: # Create the tag if it does not exist tag = Tag(name=tag_name) create_tags[tag_name] = tag statement.tags.append(tag) create_statements.append(statement) session.add_all(create_statements) session.commit()
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)