MCPcopy
hub / github.com/redis/redis-py / _parse_spellcheck_resp3

Method _parse_spellcheck_resp3

redis/commands/search/commands.py:641–677  ·  view source on GitHub ↗

Parse RESP3 FT.SPELLCHECK response into unified format. RESP3 format: {"results": {"term": [{"suggestion": score}, ...], ...}} Unified format (matches RESP2 parsed output): {"term": [{"score": score_str, "suggestion": suggestion}, ...], ...}

(self, res, **kwargs)

Source from the content-addressed store, hash-verified

639 # ---- RESP3 spellcheck parser ----
640
641 def _parse_spellcheck_resp3(self, res, **kwargs):
642 """Parse RESP3 FT.SPELLCHECK response into unified format.
643
644 RESP3 format:
645 {"results": {"term": [{"suggestion": score}, ...], ...}}
646 Unified format (matches RESP2 parsed output):
647 {"term": [{"score": score_str, "suggestion": suggestion}, ...], ...}
648 """
649 if not isinstance(res, dict):
650 return self._parse_spellcheck(res, **kwargs)
651 # On RESP3 connections with decode_responses=False the server's map
652 # keys arrive as bytes, so normalise the structural ``results`` key
653 # to a string before lookup. Mirrors ``Result.from_resp3``.
654 res = {str_if_bytes(k): v for k, v in res.items()}
655 corrections = {}
656 results = res.get("results", {})
657 for term, suggestions in results.items():
658 if not suggestions:
659 continue
660 term_corrections = []
661 for suggestion_dict in suggestions:
662 for suggestion, score in suggestion_dict.items():
663 # Normalize score to match RESP2's string form: RESP3
664 # returns a float (e.g. ``0.0``) but RESP2 returns the
665 # string ``"0"``.
666 score_str = str(score)
667 if score_str.endswith(".0"):
668 score_str = score_str[:-2]
669 # Preserve ``suggestion`` as-is so it keeps the
670 # ``decode_responses`` shape RESP2 would produce
671 # (``str`` when decoded, ``bytes`` otherwise).
672 term_corrections.append(
673 {"score": score_str, "suggestion": suggestion}
674 )
675 if term_corrections:
676 corrections[term] = term_corrections
677 return corrections
678
679 # ---- RESP3 profile parsers ----
680

Calls 4

_parse_spellcheckMethod · 0.95
str_if_bytesFunction · 0.90
getMethod · 0.45
appendMethod · 0.45