جواب «Step 3: Is the Answer Grounded?» چالش 3 از جلسهی 72
سؤال
این مدل گاهی از خودش اضافه میکند. تابع grounded(answer, context) را بنویس: True اگر همهی کلمههای تمیز (tokens) جواب در کلمههای زمینه باشند. اگر نبود، به جای جواب Not sure: the answer says more than the notes. Check chunk <i>. چاپ کن و کلمههای اضافه را به ترتیب الفبا. خروجی:Condensation is when the vapour cools high in the sky and forms clouds.Not sure: the answer says more than the notes. Check chunk 2. extra words: ['chocolate', 'fridays']
راهنما
extra = set(tokens(answer)) - set(tokens(context))؛ grounded یعنی len(extra) == 0. برای چاپ کلمههای اضافه: sorted(extra).
یک جواب درست
STOP = {"the", "a", "an", "is", "are", "and", "or", "of", "in", "on", "to", "into", "it", "this",
"that", "what", "how", "why", "does", "do", "as", "when", "they", "called", "its"}
CHUNKS = [
"Evaporation is when the sun heats water in seas and lakes and the warm water turns into vapour and rises.",
"Condensation is when the vapour cools high in the sky and forms clouds.",
"Precipitation is when the drops in a cloud get heavy and fall as rain or snow.",
]
def tokens(text):
words = []
for w in text.lower().split():
w = "".join(ch for ch in w if ch.isalpha())
if w and w not in STOP:
words.append(w)
return words
def fake_model(question, context):
# A pretend model that answers from the context... and sometimes adds a "fact".
if context.startswith("Precipitation"):
return context.rstrip(".") + ", and on Fridays as chocolate."
return context
def extra_words(answer, context):
return set(tokens(answer)) - set(tokens(context))
def grounded(answer, context):
return len(extra_words(answer, context)) == 0
for question, i in [("What is condensation?", 1), ("What is precipitation?", 2)]:
answer = fake_model(question, CHUNKS[i])
if grounded(answer, CHUNKS[i]):
print(answer)
else:
print(f"Not sure: the answer says more than the notes. Check chunk {i}.")
print(" extra words:", sorted(extra_words(answer, CHUNKS[i])))
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن