scanning¶
High-level collision detection pipeline combining the fallback encoder, similarity scoring, and variant generation.
phonemenal.scanning
¶
Scan candidate names against a set of known names for phonetic collisions.
This is the high-level scanning pipeline that combines the fallback phonetic encoder, similarity scoring, and variant generation into a complete workflow.
Two scan modes
- Forward: check candidates against known names (fast, daemon pipeline)
- Reverse: also generate variants of candidates and check if they exist in a provided lookup function (e.g. a registry check such as a PyPI HEAD request)
build_phonetic_index(names: list[str]) -> dict[str, list[str]]
¶
Build a mapping from phonetic key → list of names.
Used for fast exact-key lookups before falling back to pairwise scoring.
Source code in phonemenal/scanning.py
check_collision(candidate: str, known_names: list[str], phonetic_index: dict[str, list[str]], *, threshold: float = DEFAULT_THRESHOLD, use_composite: bool = False, composite_weights: tuple[float, float, float] = (1.0, 1.0, 1.0)) -> list[dict]
¶
Check a candidate name for phonetic collisions against known names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
candidate
|
str
|
Name to check. |
required |
known_names
|
list[str]
|
List of known/legitimate names. |
required |
phonetic_index
|
dict[str, list[str]]
|
Pre-built index from build_phonetic_index(). |
required |
threshold
|
float
|
Minimum similarity score to flag (0.0–1.0). |
DEFAULT_THRESHOLD
|
use_composite
|
bool
|
If True, use CMU-dict-backed composite scoring (PPC+PLD+LCS) instead of fallback key similarity. Slower but more accurate. |
False
|
composite_weights
|
tuple[float, float, float]
|
Weights for composite scoring (ppc, pld, lcs). |
(1.0, 1.0, 1.0)
|
Returns list of match dicts sorted by similarity descending
- candidate: input name
- matched_name: the known name
- similarity: 0.0–1.0 score
- candidate_key: phonetic key of candidate
- matched_key: phonetic key of match
- collision_type: "exact_phonetic" | "near_phonetic"
Source code in phonemenal/scanning.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
scan(candidates: list[str], known_names: list[str], *, threshold: float = DEFAULT_THRESHOLD, use_composite: bool = False, composite_weights: tuple[float, float, float] = (1.0, 1.0, 1.0)) -> list[dict]
¶
Scan candidate names for phonetic collisions with known names.
Forward scan only — checks each candidate against the known set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
candidates
|
list[str]
|
Names to check. |
required |
known_names
|
list[str]
|
Known/legitimate names to compare against. |
required |
threshold
|
float
|
Minimum similarity score to flag. |
DEFAULT_THRESHOLD
|
use_composite
|
bool
|
Use CMU-dict-backed composite scoring. |
False
|
composite_weights
|
tuple[float, float, float]
|
Weights for composite scoring. |
(1.0, 1.0, 1.0)
|
Returns list of all matches across all candidates.
Source code in phonemenal/scanning.py
scan_with_reverse(candidates: list[str], known_names: list[str], *, exists_fn: Optional[Callable[[str], bool]] = None, threshold: float = DEFAULT_THRESHOLD, use_composite: bool = False, composite_weights: tuple[float, float, float] = (1.0, 1.0, 1.0), include_morphological: bool = True) -> list[dict]
¶
Scan candidates with forward AND reverse checking.
Forward: candidate vs known names (same as scan()). Reverse: generate variants of each candidate, check if they exist via exists_fn, and score any that do against the candidate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
candidates
|
list[str]
|
Names to check. |
required |
known_names
|
list[str]
|
Known/legitimate names. |
required |
exists_fn
|
Optional[Callable[[str], bool]]
|
Callable that returns True if a name exists in an external system (e.g. a PyPI HEAD request). If None, reverse scanning is skipped. |
None
|
threshold
|
float
|
Minimum similarity score. |
DEFAULT_THRESHOLD
|
use_composite
|
bool
|
Use CMU-dict composite scoring. |
False
|
composite_weights
|
tuple[float, float, float]
|
Weights for composite scoring. |
(1.0, 1.0, 1.0)
|
include_morphological
|
bool
|
Include morphological variants in reverse scan. |
True
|
Returns all matches (forward + reverse).
Source code in phonemenal/scanning.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
format_matches(matches: list[dict]) -> str
¶
Format collision results for display.
Returns a human-readable string summarizing all matches.