跳到正文
DEV Community· Craig George·· 3 小時前AI 評分46

不靠大語言模型生成自然流暢文字:以純 JavaScript 編寫的成績報告評語生成器

Generating Natural-Sounding Text Without an LLM: A Report Comment Generator in Plain JavaScript

AI 導讀

一名開發者以純 JavaScript 編寫成績報告評語生成器,不使用 AI 模型,並確保輸入資料不會離開瀏覽器。工具按科目、年級及成就水平組合短語,生成三種評語版本並顯示字數。可選的 AI 潤飾提示詞不含學生姓名;Toolbelt 的免費工具涵蓋 Foundation 至 Year 10,並在瀏覽器內運作。

正文

Teachers write hundreds of report card comments a term. Many reach for ChatGPT, but that usually means pasting student details into a third-party tool, which most school privacy policies forbid. So when I built a report comment generator, I set two rules: no AI model, and nothing typed leaves the browser.

It turned out you can get surprisingly natural output from a few hundred lines of plain JavaScript. Here's how it works.

The structure of a good comment

A useful report comment has four parts:

  1. An opening that matches the achievement level ("has achieved a sound standard in Mathematics").
  2. One or two specific strengths.
  3. One clear next step.
  4. Optionally, effort and a way home can help.

That structure is fixed. What varies is the wording, so the job is mostly about storing good phrases and fitting them together grammatically.

Phrase banks, keyed by subject and year band

Strengths and next steps live in banks keyed by subject and year band. English and maths have separate early, middle and secondary banks, because "blends letter sounds" and "integrates quotations into an essay" don't belong in the same list.

var BANK = {
  "maths-mid": {
    s: ["recall multiplication facts quickly and accurately",
        "measure and ^convert between metric units"],
    n: ["estimating answers first to check whether they are reasonable",
        "showing all working in multi-step problems"],
    h: "practising times tables for a few minutes each day"
  }
};

Strengths are stored as verb phrases in their base form. Next steps are stored as "-ing" phrases, so they slot straight into "A next step is …" or "should focus on …".

The caret trick for verb agreement

The same strength has to work in two grammatical shapes:

  • "Sam can measure and convert between metric units" (base form)
  • "Sam measures and converts between metric units" (third person)

Rather than storing both, I mark every verb after the first with a caret (^). The first word is always a verb, so a tiny conjugation function handles the rest:

function conj(v) {
  if (v === "have") return "has";
  if (/[^aeiou]y$/.test(v)) return v.slice(0, -1) + "ies";
  if (/(s|sh|ch|x|z|o)$/.test(v)) return v + "es";
  return v + "s";
}

function third(phrase) {
  var w = phrase.split(" ");
  w[0] = conj(w[0]);
  return w.map(function (x) {
    return x.charAt(0) === "^" ? conj(x.slice(1)) : x;
  }).join(" ");
}

function base(phrase) { return phrase.replace(/\^/g, ""); }

third("measure and ^convert") gives "measures and converts", and base() just strips the carets. Because I write every phrase in the bank myself, a handful of rules covers every verb that actually appears. There's no need for a full English conjugator.

Pronouns add one more wrinkle. With "they", the verb stays in its base form ("they measure"), so the code skips third() when the pronoun is plural.

Matching tone to achievement level

Each achievement level carries its own opening sentence, and a flag that changes how strengths are phrased. For students working towards or below the expected level, strengths come out as "can now, with support, …" instead of a flat statement. That keeps comments honest without sounding harsh, which is the hardest part of writing them by hand.

Three variants, so comments don't all sound the same

If thirty comments in a class open the same way, parents notice. So build() assembles three versions from the same inputs, with different openings and orderings: one leads with the achievement level, one leads with a highlight, and one leads with what the student can do. The teacher picks one and edits it. Each variant shows a word count, because schools often set limits.

Optional AI, without the student's name

Some teachers still want an AI to polish the wording, so the tool also builds a prompt from the same inputs. It never includes the name the teacher typed. It uses a [Name] placeholder, and adds rules like "Only use the information above. Do not invent achievements, behaviours or grades." The teacher can paste that into their school's approved AI tool, and no identifying details go with it.

What I'd take from this

For narrow, structured writing, templates plus a carefully written phrase bank beat a language model on privacy, cost and predictability. The output never makes up a fact, and it works offline. The trade-off is that someone has to write the phrases well. That's where the real effort goes, not the code.

You can try the result at Toolbelt's free report comment generator. It covers Foundation to Year 10, and everything runs in your browser.

來源:DEV Community · dev.to