feat: collega il lessico semantico al filler

This commit is contained in:
2026-04-14 18:56:17 +02:00
parent 77c7e709b6
commit b172b9c04b
15 changed files with 2255563 additions and 9 deletions

View File

@@ -3,6 +3,7 @@ from __future__ import annotations
from dataclasses import dataclass
import json
from pathlib import Path
import random
import sys
import time
from typing import Dict, Iterable, List, Optional, Sequence, Set, Tuple
@@ -72,6 +73,7 @@ class CrosswordFiller:
*,
target_empty_ratio: float = TARGET_EMPTY_RATIO,
vocabulary_metadata: Optional[Dict[str, Dict[str, object]]] = None,
seed: Optional[int] = None,
) -> None:
self.state = state.copy()
self.initial_state = state.copy()
@@ -81,6 +83,8 @@ class CrosswordFiller:
self.vocabulary = self._normalize_vocabulary(vocabulary)
self.words_by_length = self._index_vocabulary(self.vocabulary)
self.vocabulary_metadata = vocabulary_metadata or {}
self.seed = seed
self.rng = random.Random(seed)
self.bounds = self._compute_bounds(self.state.grid)
self.total_cells = self._area(self.bounds)
self.target_empty_cells = max(0, int(round(self.total_cells * self.target_empty_ratio)))
@@ -181,6 +185,10 @@ class CrosswordFiller:
collected = list(unique.values())
collected.sort(key=self._slot_priority, reverse=True)
if len(collected) > 1:
top_slice = collected[: min(MAX_SLOT_CANDIDATES, len(collected))]
self.rng.shuffle(top_slice)
collected = top_slice + collected[min(MAX_SLOT_CANDIDATES, len(collected)) :]
return collected
def _slots_from_start(self, x: int, y: int, direction: str) -> Iterable[FillSlot]:
@@ -292,7 +300,7 @@ class CrosswordFiller:
return None
candidates.sort(key=lambda item: item.local_score, reverse=True)
return candidates[0]
return self.rng.choice(candidates[: min(3, len(candidates))])
def _word_quality(self, word: str) -> int:
metadata = self.vocabulary_metadata.get(word)