Class: Collavre::TypoCorrector
- Inherits:
-
Object
- Object
- Collavre::TypoCorrector
- Defined in:
- app/services/collavre/typo_corrector.rb
Overview
Server-side typo correction. Sends the text the user is typing to an LLM and returns a validated *structured edit list* — never a rewritten string. Each edit is suggestion, reason, confidence. Validation rejects anything that is not a small, in-place spelling fix (style rewrites, hallucinated spans, edits inside code/URLs/mentions), so the UI can trust every returned edit.
Constant Summary collapse
- AGENT_EMAIL =
"typo-corrector@collavre.local"- MAX_ORIGINAL_LENGTH =
Reject “corrections” that are really rewrites: an edit is kept only when the original is short and the suggestion is within this edit distance of it.
40- MIN_EDIT_DISTANCE_CAP =
2- EDIT_DISTANCE_RATIO =
0.4- FALLBACK_SYSTEM_PROMPT =
<<~PROMPT.freeze You are a typo correction engine. Fix only spelling mistakes and obvious typos, never style, grammar or meaning, in any language. `original` must be an exact substring of the input. Return ONLY JSON: {"edits":[{"original":"x","suggestion":"y","reason":"spelling","confidence":0.0}]} PROMPT
Instance Method Summary collapse
-
#correct(text) ⇒ Object
Returns an Array of Hashes with string keys: “original”, “suggestion”, “reason”, “confidence”.
-
#initialize(client: nil) ⇒ TypoCorrector
constructor
A new instance of TypoCorrector.
Constructor Details
#initialize(client: nil) ⇒ TypoCorrector
Returns a new instance of TypoCorrector.
23 24 25 |
# File 'app/services/collavre/typo_corrector.rb', line 23 def initialize(client: nil) @client = client end |
Instance Method Details
#correct(text) ⇒ Object
Returns an Array of Hashes with string keys: “original”, “suggestion”, “reason”, “confidence”. Empty array on blank input or LLM/parse failure.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'app/services/collavre/typo_corrector.rb', line 29 def correct(text) text = text.to_s return [] if text.strip.blank? response = client.chat([ { role: :user, parts: [ { text: "Text:\n#{text}" } ] } ]) edits = parse_response(response) validate(edits, text) end |