MCPcopy
hub / github.com/gunthercox/ChatterBot / update

Method update

chatterbot/storage/sql_storage.py:373–426  ·  view source on GitHub ↗

Modifies an entry in the database. Creates an entry if one does not exist.

(self, statement)

Source from the content-addressed store, hash-verified

371 session.close()
372
373 def update(self, statement):
374 """
375 Modifies an entry in the database.
376 Creates an entry if one does not exist.
377 """
378 Statement = self.get_model('statement')
379 Tag = self.get_model('tag')
380
381 session = self.Session()
382 try:
383 record = None
384
385 if hasattr(statement, 'id') and statement.id is not None:
386 record = session.get(Statement, statement.id)
387 else:
388 record = session.query(Statement).filter(
389 Statement.text == statement.text,
390 Statement.conversation == statement.conversation,
391 ).first()
392
393 # Create a new statement entry if one does not already exist
394 if not record:
395 record = Statement(
396 text=statement.text,
397 conversation=statement.conversation,
398 persona=statement.persona
399 )
400
401 # Update the response value
402 record.in_response_to = statement.in_response_to
403
404 record.created_at = statement.created_at
405
406 if not statement.search_text:
407 if self.raise_on_missing_search_text:
408 raise Exception('update issued without search_text value')
409
410 if statement.in_response_to and not statement.search_in_response_to:
411 if self.raise_on_missing_search_text:
412 raise Exception('update issued without search_in_response_to value')
413
414 for tag_name in statement.get_tags():
415 tag = session.query(Tag).filter_by(name=tag_name).first()
416
417 if not tag:
418 # Create the record
419 tag = Tag(name=tag_name)
420
421 record.tags.append(tag)
422
423 session.add(record)
424 session.commit()
425 finally:
426 session.close()
427
428 def get_random(self):
429 """

Callers

nothing calls this directly

Calls 7

StatementClass · 0.90
TagClass · 0.90
get_modelMethod · 0.80
getMethod · 0.80
filterMethod · 0.45
get_tagsMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected