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

جواب «The Top k» چالش 6 از جلسه‌ی 71

سؤال

گاهی یک تکه کافی نیست. تابع top_k(question, chunks, k) را بنویس که k تکه‌ی برتر را به شکل لیست (score, index) برگرداند: مرتب از بیشترین امتیاز؛ در امتیاز برابر، شماره‌ی کوچک‌تر اول؛ تکه‌های با امتیاز 0 حذف شوند. خروجی:
Why does rain fall from clouds?
chunk 2: 0.20
chunk 1: 0.10
Where does the water in clouds come from?
chunk 1: 0.09
chunk 3: 0.08

راهنما

لیست (score, index) بساز و فقط امتیازهای بزرگ‌تر از صفر را نگه دار. مرتب کردن: sorted(pairs, key=lambda p: (-p[0], p[1]))، بعد [:k].

یک جواب درست

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"}


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)


CHUNKS = [
    "The sun heats water in seas and lakes. The warm water turns into vapour and rises. This is called evaporation.",
    "High in the sky the vapour cools and forms clouds. This is called condensation.",
    "When the drops in a cloud get heavy, they fall as rain or snow. This is called precipitation.",
    "Plants take in water through their roots and lose it through their leaves.",
]


def top_k(question, chunks, k):
    q = tokens(question)
    pairs = []
    for i, chunk in enumerate(chunks):
        s = jaccard(q, tokens(chunk))
        if s > 0:
            pairs.append((s, i))
    pairs = sorted(pairs, key=lambda p: (-p[0], p[1]))
    return pairs[:k]


for q in ["Why does rain fall from clouds?", "Where does the water in clouds come from?"]:
    print(q)
    for score, index in top_k(q, CHUNKS, 2):
        print(f"  chunk {index}: {score:.2f}")

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