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

جواب «Step 5: The Chat Page» چالش 5 از جلسه‌ی 44

سؤال

حالا همان دستیار به شکل یک صفحه‌ی وب چت برای گالری. جزوه، words، score، pretendModel (مدل ساختگی: جمله‌ی اول تکه؛ پیش‌نمایش به مدل واقعی راه ندارد) و say آماده‌اند. دو کار:
۱. bestChunk(question) مثل قدم ۱: اندیس تکه با بیشترین امتیاز، یا null اگر هیچ تکه‌ای امتیاز نگرفت.
۲. در onclick دکمه‌ی #ask: بعد از نشان دادن سؤال، const i = bestChunk(q)؛ اگر i === null بود say(NOT_FOUND, "bot")، وگرنه say(pretendModel(CHUNKS[i]) + " [part " + (i + 1) + "]", "bot").
بپرس «Who waters the beds?» و «What is the capital of France?».

راهنما

در جاوااسکریپت: let best = null; let bestScore = 0; for (let i = 0; i < CHUNKS.length; i++) { const s = score(question, CHUNKS[i]); if (s > bestScore) { best = i; bestScore = s; } }.

یک جواب درست

<h1>Robo Garden helper</h1>
<div id="chat"></div>
<form id="ask-form">
  <input id="q" type="text" placeholder="Ask about the handout..." autocomplete="off">
  <button id="ask" type="button">Ask</button>
</form>
<footer><p>AI: the answers. Me: finding the part and the page.</p></footer>

<style>
body { font-family: sans-serif; margin: 0; padding: 16px; background: #eef7ee; }
#chat { display: flex; flex-direction: column; gap: 8px; min-height: 120px; }
.you, .bot { padding: 8px 12px; border-radius: 12px; max-width: 80%; }
.you { background: #d6ecff; align-self: flex-end; }
.bot { background: white; align-self: flex-start; }
#ask-form { display: flex; gap: 8px; margin-top: 12px; }
#q { flex: 1; padding: 8px; font-size: 16px; }
</style>

<script>
const 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.`;
const STOP = ["the", "a", "an", "is", "are", "of", "on", "in", "to", "and", "it", "how", "what", "does", "do", "many", "much"];
const CHUNKS = HANDOUT.split("\n\n");
const NOT_FOUND = "I don't know. The handout does not say.";

function words(text) {
  return text.toLowerCase().split(/[^a-z0-9]+/).filter(w => w && !STOP.includes(w));
}

function score(question, chunk) {
  const qw = words(question);
  const cw = words(chunk);
  return qw.filter(w => cw.includes(w)).length;
}

function bestChunk(question) {
  let best = null;
  let bestScore = 0;
  for (let i = 0; i < CHUNKS.length; i++) {
    const s = score(question, CHUNKS[i]);
    if (s > bestScore) {
      best = i;
      bestScore = s;
    }
  }
  return best;
}

function pretendModel(chunk) {
  return chunk.split(".")[0] + ".";
}

function say(text, who) {
  const p = document.createElement("p");
  p.className = who;
  p.textContent = text;
  document.getElementById("chat").appendChild(p);
}

document.getElementById("ask").onclick = function () {
  const q = document.getElementById("q").value;
  say(q, "you");
  const i = bestChunk(q);
  if (i === null) {
    say(NOT_FOUND, "bot");
  } else {
    say(pretendModel(CHUNKS[i]) + " [part " + (i + 1) + "]", "bot");
  }
  document.getElementById("q").value = "";
};
</script>

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