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

Method create_many

chatterbot/storage/sql_storage.py:318–371  ·  view source on GitHub ↗

Creates multiple statement entries.

(self, statements)

Source from the content-addressed store, hash-verified

316 return statement_object
317
318 def create_many(self, statements):
319 """
320 Creates multiple statement entries.
321 """
322 Statement = self.get_model('statement')
323 Tag = self.get_model('tag')
324
325 session = self.Session()
326
327 create_statements = []
328 create_tags = {}
329
330 # Check if any statements already have a search text
331 have_search_text = any(statement.search_text for statement in statements)
332
333 # Generate search text values in bulk
334 if not have_search_text:
335 if self.raise_on_missing_search_text:
336 raise Exception('generate bulk_search_text values')
337
338 for statement in statements:
339
340 statement_data = statement.serialize()
341 tag_data = statement_data.pop('tags', [])
342
343 statement_model_object = Statement(**statement_data)
344
345 new_tags = set(tag_data) - set(create_tags.keys())
346
347 if new_tags:
348 existing_tags = session.query(Tag).filter(
349 Tag.name.in_(new_tags)
350 )
351
352 for existing_tag in existing_tags:
353 create_tags[existing_tag.name] = existing_tag
354
355 for tag_name in tag_data:
356 if tag_name in create_tags:
357 tag = create_tags[tag_name]
358 else:
359 # Create the tag if it does not exist
360 tag = Tag(name=tag_name)
361
362 create_tags[tag_name] = tag
363
364 statement_model_object.tags.append(tag)
365 create_statements.append(statement_model_object)
366
367 try:
368 session.add_all(create_statements)
369 session.commit()
370 finally:
371 session.close()
372
373 def update(self, statement):
374 """

Callers

nothing calls this directly

Calls 6

StatementClass · 0.90
TagClass · 0.90
get_modelMethod · 0.80
serializeMethod · 0.80
filterMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected