بتل کد 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

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):
    return []


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

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 71: RAG کوچک (۱): تکه‌ی درست را پیدا کن

بیشتر بخوان: تابع در پایتون: آموزش def، پارامتر و return با مثال