import re
DOCUMENTS = [
{
"source": "Returns policy · §1",
"text": "Unused items may be returned within 30 days of delivery with proof of purchase.",
"keywords": {"return", "returns", "refund"},
},
{
"source": "Shipping FAQ · §2",
"text": "Standard shipping takes 3–5 business days after dispatch.",
"keywords": {"shipping", "delivery", "arrive"},
},
]
def answer(question):
tokens = set(re.findall(r"[a-z]+", question.lower()))
for document in DOCUMENTS:
if tokens & document["keywords"]:
return {"answer": document["text"], "source": document["source"]}
return {
"answer": "I could not find this in the provided documents. Please contact support.",
"source": None,
}
if __name__ == "__main__":
questions = [
"Can I return an unused item?",
"How long does shipping take?",
"Do you offer a lifetime warranty?",
]
for question in questions:
result = answer(question)
print("Question:", question)
print("Answer:", result["answer"])
print("Source:", result["source"] or "No supporting source")
print()
assert answer(questions[2])["source"] is None