fix: pre-filter TCGdex results by card number before candidate cap - #317
Open
lamiskin wants to merge 1 commit into
Open
fix: pre-filter TCGdex results by card number before candidate cap#317lamiskin wants to merge 1 commit into
lamiskin wants to merge 1 commit into
Conversation
The scanner caps each TCGdex name search at the first 8 results (recognize.py:249). TCGdex returns those results sorted ascending by localId, so the cap keeps only the lowest-numbered printings of a Pokemon and discards everything else. Number ranking (Git-Romer#96, step 3) then sorts a list the target card has already been removed from, so it can only ever succeed for cards that happen to be numbered low enough to survive the slice. Float printings whose number matches the recognized number to the front of the result list before the cap is applied. When no number is recognized, or nothing matches, the original order is returned unchanged. Verified against live TCGdex data and live scans: card # results in top 8 before after Snorlax (me03-063) 63 64 no yes Gengar (me03-050) 50 58 no yes Rare Candy (sv1-191) 191 23 no yes Exeggcute (base2-52) 52 39 no yes Also restores the "unique number match skips visual verification" row of the Git-Romer#96 spec table: previously number_match_count was almost always 0 for high-numbered cards, so number_match_clear never became true and every scan paid a second Gemini call. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The scanner rarely returns the correct card as match #1 for anything but low-numbered printings. On my install it never matched correctly until I traced it.
backend/api/recognize.py:249caps each TCGdex name search at the first 8 results:TCGdex returns search results sorted ascending by
localId, so this keeps only the 8 lowest-numbered printings of that Pokémon and discards the rest. The recognized card number is then used at:317only to sort the survivors — never to select from the full result set.Example, Snorlax (64 EN results, target #63):
The correct card (
me03-063) exists in TCGdex and is thrown away before ranking runs.Why this contradicts #96
PR #96 ("smarter card scanner — number ranking + visual verification") describes:
That works only when the target survives the Step 2 cap. Its spec table also says:
In practice
number_match_countwas almost always0for high-numbered cards, sonumber_match_clearnever became true and every scan paid a second Gemini call — with the second call being shown five wrong candidates and correctly answering0. The PR notes "minimal extra cost" for step 4 on the assumption it's the exception; currently it's the default.Fix
Float printings matching the recognized number to the front of the result list before the cap. When no number is recognized, or nothing matches, the original order is returned unchanged — so the worst case is identical to current behaviour. No extra API calls.
Verification
Candidate list before/after, against live TCGdex data:
me03-063me03-050sv1-191base2-52me03-014sv1-005Live scans through
/api/cards/recognizeon the patched build — all five English cards returned the exact correct printing at position 1:New log line, and verification correctly skipped where the match is unique:
Scope / known limitations
This is a candidate-selection fix, not a matching-quality fix:
No. 039(the National Pokédex number — Jungle-era JP cards carry no collector number), whichre.match(r"(\d+)", ...)cannot parse since it's anchored at string start. That card still doesn't match.No. 039as a card number would floatSV2a-039— an unrelated Scarlet & Violet card — to position 1, turning a no-match into a confidently wrong match. That needs separate discussion.set_hintis extracted by the prompt and never read by any code path. Using it would be more decisive than number matching alone (it would have resolved theme03-063vsA2a-063tie with no second Gemini call), but that's a design change and out of scope here.