Add name suggestion engine with API endpoint and tests
Expand services/naming.py with suggest_names() that picks random words
from a category while excluding nicknames already used in the run. Add
GET /runs/{run_id}/name-suggestions?count=10 endpoint that reads the
run's naming_scheme and returns filtered suggestions. Includes 12 unit
tests covering selection, exclusion, exhaustion, and cross-category
independence.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import json
|
||||
import random
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
|
||||
@@ -16,3 +17,29 @@ def _load_dictionary() -> dict[str, list[str]]:
|
||||
def get_naming_categories() -> list[str]:
|
||||
"""Return sorted list of available naming category names."""
|
||||
return sorted(_load_dictionary().keys())
|
||||
|
||||
|
||||
def get_words_for_category(category: str) -> list[str]:
|
||||
"""Return the word list for a category, or empty list if not found."""
|
||||
return _load_dictionary().get(category, [])
|
||||
|
||||
|
||||
def suggest_names(
|
||||
category: str,
|
||||
used_names: set[str],
|
||||
count: int = 10,
|
||||
) -> list[str]:
|
||||
"""Pick random name suggestions from a category, excluding used names.
|
||||
|
||||
Returns up to `count` names. If the category is nearly exhausted,
|
||||
returns fewer.
|
||||
"""
|
||||
words = get_words_for_category(category)
|
||||
if not words:
|
||||
return []
|
||||
|
||||
available = [w for w in words if w not in used_names]
|
||||
if not available:
|
||||
return []
|
||||
|
||||
return random.sample(available, min(count, len(available)))
|
||||
|
||||
Reference in New Issue
Block a user