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

جواب «Step 4: Ten Test Questions» چالش 4 از جلسه‌ی 44

سؤال

دستیار کامل است و لیست TESTS ده سؤال با کلمه‌ی کلیدی جواب درست دارد (۲ تا «I don't know»). run_tests(): هر سؤال را با answer بپرس؛ اگر کلمه‌ی کلیدی در جواب بود right را زیاد کن و چاپ کن ok سؤال (اگر کلیدی I don't know بود، dont_know را هم زیاد کن)؛ وگرنه چاپ کن MISS سؤال -> جواب. آخر چاپ کن score: 7 / 10 = 70 % (با right * 100 // len(TESTS)) و correct I don't know: 2 / 2. مدل ساختگی فقط جمله‌ی اول تکه را می‌گوید، پس سه سؤال MISS می‌شوند. کدام‌ها؟ چرا؟ با مدل واقعی (رله‌ی روشن) دوباره اجرا کن و نمره را مقایسه کن.

راهنما

for question, expected in TESTS: و if expected in reply:. چاپ: print("ok ", question) (دو فاصله بعد از ok؛ print یکی دیگر می‌گذارد).

یک جواب درست

HANDOUT = """The Robo Garden club meets every Tuesday after school.
The club has 12 members and one teacher.

The garden has 4 tomato beds and 2 herb beds.
Each tomato bed needs 3 liters of water every day.

The robot Sprout waters the beds at 7 in the morning.
Sprout runs on a small solar panel.

Ripe tomatoes are picked on Fridays.
Herbs are picked whenever the cook asks for them."""

STOP = {"the", "a", "an", "is", "of", "in", "on", "and", "what", "how", "do", "does", "it", "at", "to", "many"}

def words(text):
    clean = ""
    for ch in text.lower():
        if ch in ".,?!":
            clean += " "
        else:
            clean += ch
    result = set()
    for w in clean.split():
        if w not in STOP:
            result.add(w)
    return result

def split_chunks(text):
    chunks = []
    for part in text.split("\n\n"):
        part = part.strip()
        if part != "":
            chunks.append(part)
    return chunks

def score(question, chunk):
    return len(words(question) & words(chunk))

CHUNKS = split_chunks(HANDOUT)

def best_chunk(question):
    best = None
    best_score = 0
    for i in range(len(CHUNKS)):
        s = score(question, CHUNKS[i])
        if s > best_score:
            best = i
            best_score = s
    return best

def calc(expr):
    parts = expr.split()
    if len(parts) != 3:
        return "error"
    a, op, b = parts
    if not (a.lstrip("-").isdigit() and b.lstrip("-").isdigit()):
        return "error"
    a = int(a)
    b = int(b)
    if op == "+":
        return a + b
    if op == "-":
        return a - b
    if op == "*":
        return a * b
    if op == "/":
        if b == 0:
            return "error"
        return a / b
    return "error"

def parse_tool(reply):
    reply = reply.strip()
    if not reply.startswith("TOOL "):
        return None
    parts = reply.split(" ", 2)
    if len(parts) < 3:
        return None
    return (parts[1], parts[2])

from ai import ask

SYSTEM = ("You are a study helper for the Robo Garden club. Be short and kind. "
          + "If the question needs maths, reply only: TOOL calc <a> <op> <b>")
NOT_FOUND = "I don't know. The handout does not say."

def answer(question):
    i = best_chunk(question)
    if i is None:
        return NOT_FOUND
    prompt = ("Use only this handout part. If the answer is not in it, say I don't know.\n"
              + "HANDOUT PART: " + CHUNKS[i] + "\n"
              + "QUESTION: " + question)
    reply = ask(prompt, system=SYSTEM)
    call = parse_tool(reply)
    if call is not None and call[0] == "calc":
        return str(calc(call[1])) + " (calculator) [part " + str(i + 1) + "]"
    return reply + " [part " + str(i + 1) + "]"

TESTS = [
    ("When does the club meet?", "Tuesday"),
    ("How many members does the club have?", "12"),
    ("How many tomato beds are there?", "4"),
    ("How much water does a tomato bed need?", "3 liters"),
    ("How much water do the tomato beds need in a week?", "84"),
    ("Who waters the beds?", "Sprout"),
    ("What does Sprout run on?", "solar"),
    ("When are ripe tomatoes picked?", "Fridays"),
    ("What is the capital of France?", "I don't know"),
    ("What colour is the teacher's car?", "I don't know"),
]

def run_tests():
    right = 0
    dont_know = 0
    for question, expected in TESTS:
        reply = answer(question)
        if expected in reply:
            right += 1
            print("ok  ", question)
            if expected == "I don't know":
                dont_know += 1
        else:
            print("MISS", question, "->", reply)
    print("score:", right, "/", len(TESTS), "=", right * 100 // len(TESTS), "%")
    print("correct I don't know:", dont_know, "/ 2")


# ---- test with a pretend model (do not change) ----
def fake_ask(prompt, system=None, history=None):
    if "HANDOUT PART: " not in prompt:
        return "???"
    part = prompt.split("HANDOUT PART: ")[1]
    if "week" in prompt and "TOOL" in (system or ""):
        return "TOOL calc 12 * 7"
    return part.split(".")[0] + "."
ask = fake_ask

run_tests()

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