بتل کد Battle Codeدارد باز می‌شود…

جواب «Track A: The Bot's RAG Core» چالش 1 از جلسه‌ی 78

سؤال

مسیر الف. تابع answer(question) را بنویس: بهترین تکه را با jaccard پیدا کن؛ اگر بهترین امتیاز 0 بود Not in the notes.؛ وگرنه <chunk> [source: chunk <i>]. خروجی:
Condensation is when vapour cools and forms clouds. [source: chunk 1]
Precipitation is rain or snow falling from heavy clouds. [source: chunk 2]
Not in the notes.

راهنما

scores = [jaccard(tokens(question), tokens(c)) for c in CHUNKS]؛ best = max(scores)؛ if best == 0: return "Not in the notes."؛ i = scores.index(best).

یک جواب درست

STOP = {"the", "a", "an", "is", "are", "and", "or", "of", "in", "on", "to", "it", "what", "how", "why", "does", "do", "when"}
CHUNKS = [
    "Evaporation is when the sun heats water and it turns into vapour.",
    "Condensation is when vapour cools and forms clouds.",
    "Precipitation is rain or snow falling from heavy clouds.",
]


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 jaccard(a, b):
    a, b = set(a), set(b)
    if not a or not b:
        return 0
    return len(a & b) / len(a | b)


def answer(question):
    scores = [jaccard(tokens(question), tokens(c)) for c in CHUNKS]
    best = max(scores)
    if best == 0:
        return "Not in the notes."
    i = scores.index(best)
    return f"{CHUNKS[i]} [source: chunk {i}]"


for q in ["What is condensation?", "Why does rain fall from clouds?", "Who painted the Mona Lisa?"]:
    print(answer(q))

اول خودت امتحان کن؛ جواب‌های دیگر هم قبول است. برگرد و خودت حلش کن