creegon's picture
Release 0.1.0 (same files as staging-0.1.0 @ 90a07ac4)
7b0b7bc verified
Raw History Blame Contribute Delete
1.13 kB
"""Quote routing and spelling-format preservation."""
import re
from .prompt_routing import (
normalize_single_quotes,
preprocess_and_route_text_prompt,
should_load_writing_lora,
)
QUOTE_PATTERN = re.compile(r'"([^"\n]+)"|β€œ([^”\n]+)”|γ€Œ([^」\n]+)」|γ€Ž([^』\n]+)』|'
r'(?<!\w)\x27([^\x27\n]+)\x27(?!\w)|β€˜([^’\n]+)’')
SPELLING_PATTERN = re.compile(r'\s*\(spelled: [^\n)]*\)')
def triggers(prompt, adapter):
if adapter != 'text':
raise ValueError('Only the writing adapter is available')
return should_load_writing_lora(prompt)
def spell_prompt(prompt, canonical_spelling):
"""Append spelling annotations without changing the original quote marks."""
def annotate(match):
quoted = match[0]
target = quoted[1:-1]
if SPELLING_PATTERN.match(prompt, match.end()):
return quoted
if quoted[0] in '"β€œ':
return canonical_spelling(quoted)
canonical = f'"{target}"'
return quoted + canonical_spelling(canonical)[len(canonical):]
return QUOTE_PATTERN.sub(annotate, prompt)