🐍 python polinators

python

fukurou

the supreme coder
ADMIN
Python:
# Simple pronoun resolver using history
import re

PRONOUNS = re.compile(r'\b(it|they|them|that|this|its|their)\b', re.I)

def last_entity(history: list[dict]) -> str | None:
    """Walk backwards through history, return last user noun phrase."""
    noun = re.compile(r'\b(a|an|the)\s+([A-Za-z]+(?: [A-Za-z]+)?)')
    for msg in reversed(history):
        if msg["role"] == "user":
            m = noun.search(msg["content"])
            if m:
                return m.group(2)
    return None

def resolve_pronouns(text: str, history: list[dict]) -> str:
    entity = last_entity(history)
    if not entity:
        return text
    return PRONOUNS.sub(entity, text)

# --- demo ---
history = [
    {"role": "user",      "content": "I'm thinking about a golden retriever."},
    {"role": "assistant", "content": "Great choice!"},
]
new_msg = "How much does it cost?"
resolved = resolve_pronouns(new_msg, history)
print(resolved)
 
Top