English:Natural language processing and text classification - AI Year 8
Introduction

Can a short text be labelled Music or Sport without losing its meaning? Tokenise fictional sentences, inspect learned word counts and classify ten examples. Two deliberately uncertain cases will help you distinguish a topic, a sentiment and a justified request for context. No real learner messages or personal text records are used.
Choose the language task before choosing a label
Natural language processing applies computational methods to language. A text classifier assigns a label according to a defined task. Topic classification asks what a text is about; sentiment classification asks about an expressed attitude, under a declared scheme; an intent task asks what action a speaker is trying to achieve. These are different targets. A topic label Music does not tell you whether the writer likes the song, and an emotional label does not capture every fact or intention in a message.
Our original classroom task permits one topic, Music or Sport, when the supplied evidence is adequate. Otherwise the response is ASK for review. A token is a piece produced by a declared text-splitting rule. In this course's small program, it is a run of letters after Unicode normalisation and case folding. Punctuation and digits act as boundaries or are ignored. This is a supplied word-like tokenizer, not the tokenizer of a commercial large language model. Do not infer a vendor token count, price or context capacity from it.
Consider the exact text That was sick. A discussion of illness can make it a negative health comment. A reaction to a striking performance can make sick an expression of praise. Without enough context, neither interpretation is guaranteed. Slang or dialect is not inherently defective language; a model may lack the appropriate examples, context or task definition. Ask for clarification rather than treating a speaker's language variety as evidence about ability or character.
Sarcasm provides another challenge. Great, another cancelled match may communicate frustration even though great often appears in praise. It may also be sincere in some context. Tone, the surrounding conversation and the user's purpose can matter. A system that notices match might correctly suggest Sport while missing the attitude entirely. Success on the topic task would not validate a sentiment judgement.
Try before checking: compare That song was wonderful with That song was awful. Hint: separate topic from attitude. Partial step: song is a topic clue in both. After attempting, check that both may be about Music while their expressed evaluations differ. Our topic-only program below returns the same topic for both. It has not trained a sentiment model and does not promise to infer sarcasm or a person's feelings.
CodeAI introduces chatbots and large language models. Use it as wider language-technology context, not as a description of our count-based classifier. The program below generates no new prose, calls no chatbot and does not need an online learner account. All required explanations, examples and tests are supplied independently of video playback.
From training words to a transparent topic score
A bag-of-words representation counts tokens while discarding their order. This can be useful for a limited topic task, but it loses information about phrases, negation and who did what to whom. Our original model learns token counts separately for two topics from eight fictional training records. The feature representation and scoring rule are supplied designs; the counts come from the labelled training data.
| Training record | Supplied token sequence | Topic |
|---|---|---|
| M1 | guitar song melody | Music |
| M2 | piano rhythm concert | Music |
| M3 | song concert band | Music |
| M4 | drums band music | Music |
| S1 | football goal match | Sport |
| S2 | team coach training | Sport |
| S3 | goal team stadium | Sport |
| S4 | coach match sport | Sport |
For Music, song, concert and band each occur twice. Guitar, melody, piano, rhythm, drums and music each occur once. For Sport, goal, match, team and coach each occur twice; football, training, stadium and sport each occur once. The training phrases intentionally make inspection easy. They are not a representative sample of everyday English or every school subject.
For a new text, count its tokens and sum query count multiplied by the topic's training count for each token. Unseen tokens add zero. Choose the unique topic with the highest positive score. If all scores are zero, return ASK with reason no_known_evidence. If the highest score ties, return ASK with reason score_tie. These rules are declared for this exercise. The resulting numbers are overlap scores, not probabilities, calibrated confidence or a Naive Bayes calculation.
For The band played a song, the known Music contributions are band two and song two, giving four. The Sport score is zero, so choose Music. For A coach planned training, Sport receives coach two and training one, giving three, while Music receives zero. Repeated query tokens count repeatedly: song song gives Music four. Reordering team song to song team changes neither count nor score; each topic receives two and the model defers. It has lost word order, rather than discovered that the sentences mean the same thing in every context.
Attempt a trace: score Our team heard a song. Hint: ignore words absent from the training vocabulary only for this declared rule. Partial step: team contributes two to Sport and song two to Music. Check after attempting: the score tie returns ASK. A different multi-label task might allow both topics, but our declared single-topic output requires review rather than silently choosing the first label.
The following complete bounded standard-library program was actually executed. It stores learned counts, query tokens, scores, predictions and reasons. It accepts fictional strings of at most one thousand characters, requires distinct training identifiers and evidence for both topics, and rejects malformed training input. It does not execute learner text or access messages, accounts or a generative model.
"""Original bounded token-count topic model on fictional short sentences."""
from collections import Counter
import json
import re
import unicodedata
def tokens(text):
if type(text) is not str or len(text) > 1000:
raise ValueError("Use a fictional text of at most one thousand characters")
normal = unicodedata.normalize("NFC", text).casefold()
return re.findall(r"[^\W\d_]+", normal)
def fit(rows):
if type(rows) not in [list, tuple] or not 1 <= len(rows) <= 1000:
raise ValueError("Supply a bounded nonempty labelled training collection")
model = dict(Music=Counter(), Sport=Counter())
identities = set()
for row in rows:
if type(row) not in [list, tuple] or len(row) != 3:
raise ValueError("Supply identity text and topic")
identity, text, label = row
if type(identity) is not str or not identity or identity in identities:
raise ValueError("Use distinct nonempty training identities")
if label not in model:
raise ValueError("Use a declared topic label")
words = tokens(text)
if not words:
raise ValueError("Training text needs letter tokens")
identities.add(identity)
model[label].update(words)
if not all(model.values()):
raise ValueError("Both topics need training evidence")
return model
def classify(text, model):
words = Counter(tokens(text))
scores = dict((label, sum(n * counts.get(word, 0) for word, n in words.items()))
for label, counts in model.items())
best = max(scores.values())
winners = [label for label, value in scores.items() if value == best]
if best == 0:
prediction, reason = "ASK", "no_known_evidence"
elif len(winners) != 1:
prediction, reason = "ASK", "score_tie"
else:
prediction, reason = winners[0], "unique_score_winner"
return dict(tokens=dict(words), scores=scores, prediction=prediction, reason=reason)
def demonstrate():
training = [("M1", "guitar song melody", "Music"),
("M2", "piano rhythm concert", "Music"),
("M3", "song concert band", "Music"),
("M4", "drums band music", "Music"),
("S1", "football goal match", "Sport"),
("S2", "team coach training", "Sport"),
("S3", "goal team stadium", "Sport"),
("S4", "coach match sport", "Sport")]
model = fit(training)
tests = [("Q1", "I practised piano rhythm.", "Music"),
("Q2", "The guitar carried melody.", "Music"),
("Q3", "The band played a song.", "Music"),
("Q4", "The concert featured drums.", "Music"),
("Q5", "The team scored a goal.", "Sport"),
("Q6", "A coach planned training.", "Sport"),
("Q7", "A football match filled the stadium.", "Sport"),
("Q8", "Our goal helped the team.", "Sport"),
("Q9", "That was sick.", "ASK"),
("Q10", "Our team heard a song.", "ASK")]
outputs = [dict(id=i, text=text, reference_action=ref, result=classify(text, model))
for i, text, ref in tests]
stresses = [dict(text=text, result=classify(text, model))
for text in ["team song", "song team", "song song", "", "123 !!!",
"That song was wonderful.", "That song was awful."]]
return dict(training=training, learned_counts=model, tests=outputs, stresses=stresses,
target="single topic or documented deferral", sentiment_model_trained=False,
local_generative_model_called=False, real_learner_messages_used=False)
if __name__ == "__main__":
print(json.dumps(demonstrate(), ensure_ascii=False, indent=2))
Ten examples, two uncertainties and a responsible evaluation
Try all ten examples before looking at the checked scores. The first eight have a supplied dominant topic. The last two call for a documented deferral under this single-topic teaching task. Your learning goal is to justify at least eight topic decisions and explain the two uncertain cases, not to pretend that every text has one context-free meaning. The reference actions below were authored for this exercise and are not measurements of any learner's achievement.
| Case | Fictional text | Music score | Sport score | Model response |
|---|---|---|---|---|
| Q1 | I practised piano rhythm. | 2 | 0 | Music |
| Q2 | The guitar carried melody. | 2 | 0 | Music |
| Q3 | The band played a song. | 4 | 0 | Music |
| Q4 | The concert featured drums. | 3 | 0 | Music |
| Q5 | The team scored a goal. | 0 | 4 | Sport |
| Q6 | A coach planned training. | 0 | 3 | Sport |
| Q7 | A football match filled the stadium. | 0 | 4 | Sport |
| Q8 | Our goal helped the team. | 0 | 4 | Sport |
| Q9 | That was sick. | 0 | 0 | ASK |
| Q10 | Our team heard a song. | 2 | 2 | ASK |
The classifier gives eight category labels, all matching the eight clear-topic references, and two deferrals. Coverage is eight of ten; correctness among those answered cases is eight of eight. Do not rename this ten correctly classified topics: ASK is a review action, not another topic. Q9 has no known vocabulary evidence and a context-sensitive expression. Q10 contains equally weighted clues to both topics. These are distinct reasons for deferral. The programme's numerical reasons do not prove a complete linguistic diagnosis.
A constant-Music baseline matches four of the eight clear-topic cases and answers all ten. It also forces a single topic on both review cases. That comparison helps expose the value and limits of our declared model, without establishing reliability on general messages. The fixtures are tiny, balanced across the eight clear examples and deliberately constructed from known clues. If these examples guide a redesign, they become development evidence; use fresh independent texts and justified references for a new final performance claim.
Investigate boundaries before trusting a score. Empty text or digits and punctuation alone produce no letter tokens and return ASK. Capitalising SONG or adding punctuation should preserve its declared topic score. The program normalises composed and decomposed forms of an accented word before counting, but that engineering choice does not make it a general multilingual classifier. It has no stemming rule: training and trained are different tokens. The apostrophe in don't splits the run into don and t under the supplied tokenizer. Explain that representation limit instead of claiming full grammatical understanding.
CodeAI discusses training data and bias. Connect this to missing vocabulary, contexts and language varieties. A low or zero score may reveal a model limitation rather than a poor writer. Do not use this demonstration to grade pupils, infer wellbeing, detect a protected characteristic or make an automated safeguarding judgement. A human review route needs an owner and a way to seek context, not simply the word ASK.
Transfer workshop: imagine labelling fictional library requests as a topic suggestion for books or sports equipment. First hint: define whether more than one label is allowed. Partial step: a request mentioning both a ball and a music book may genuinely need two topics. After your attempt, check that your plan declares labels, representation, data provenance, a simple baseline, separate fitting and evaluation, ambiguous-case handling and rights. Use invented sentences, not private pupil messages. More context-sensitive features or models require fresh tests; adding adjacent word pairs alone does not solve every ambiguity or sarcasm case.
Interactive Tasks
Quiz: Test Your Knowledge
What does this model predict when it has enough evidence? (A declared topic label) (!A complete explanation of every meaning) (!A measured personal emotion) (!A writers ability)
Why is That was sick uncertain without context? (It may express praise or refer to illness) (!It always expresses illness) (!It always expresses praise) (!It cannot be meaningful English)
What information does the declared bag of words discard? (Relative token order) (!Every token count) (!All training topic labels) (!The existence of text)
Why does The band played a song receive Music score four? (Band contributes two and song contributes two) (!There are exactly four letters) (!Every sentence automatically scores four) (!The model read a private music account)
What is the Sport score for A coach planned training? (Three) (!Zero) (!Two) (!Five)
Why does Our team heard a song return ASK? (Its highest topic scores tie at two each) (!Its text is missing) (!It is definitely about only Music) (!Its words have been executed as code)
Which summary of the ten examples is accurate? (Eight topic labels and two review actions) (!Ten correctly classified topic labels) (!No answered examples) (!Two labels prove a population guarantee)
Does the same Music label imply the same sentiment for wonderful and awful song comments? (No because topic and sentiment are different targets) (!Yes because song determines every attitude) (!Yes because both texts are identical) (!No because Music cannot be a topic)
What does an unseen token contribute under the supplied rule? (Zero to both topic scores) (!A guaranteed negative sentiment) (!An automatic Music vote) (!A newly invented training label)
What should happen after using evaluation examples to redesign the model? (Obtain fresh independent evaluation examples) (!Call the reused cases untouched forever) (!Use private pupil messages without permission) (!Assume all dialects are now covered)
Memory Game
| Topic | The subject category a text is assigned under a task |
| Sentiment | An attitude label under a declared scheme |
| Token | A piece produced by the specified splitting rule |
| Vocabulary | The tokens recorded from the fitting examples |
| Context | Surrounding information that can change an interpretation |
| Deferral | A documented decision to seek review rather than force a label |
Drag and Drop
| State whether one or several subject labels are allowed | Target definition |
| Convert fictional text to declared letter tokens | Representation |
| Count labelled examples without evaluation leakage | Fitting |
| Compare known query contributions to topic totals | Prediction |
| Request more context when evidence is absent or tied | Review route |
Crossword Puzzle
| TOPIC | What subject category is the model asked to assign? |
| TOKEN | What piece comes from a specified splitting rule? |
| CONTEXT | What surrounding information can change an interpretation? |
| SENTIMENT | What kind of label concerns expressed attitude? |
| VOCABULARY | What collection records the fitting tokens? |
| TRAINING | What labelled evidence sets the model counts? |
LearningApps
Optional extra practice. Choose an appropriate activity with your teacher. If the external collection is unavailable, use the native activities and independent tasks in this course.
Cloze Text
Open Tasks
Easy
- Separate topic and sentiment: Explain the difference between wonderful-song and awful-song comments. Hint: identify what is discussed before the attitude. Partial step: both contain song. Give a different sentiment interpretation without claiming a complete reading of the writer.
- Tokenise two fictional strings: Apply the supplied normalisation and letter-run rule to SONG! and team-song. Hint: punctuation is a boundary. Compare your expected tokens with actual output only after attempting.
- Count the training evidence: Rebuild Music counts for song concert and band and Sport counts for team and coach. Hint: inspect all four records for the relevant class. Preserve repeated occurrences rather than silently reducing every count to one.
- Classify the clear examples: Assign topics to Q1 through Q8 with a short clue-based explanation. Hint: list known terms first. Your goal is at least eight justified decisions; the supplied references do not represent a measured learner score.
Standard
- Trace a topic score: Calculate both topic scores for Q3 and Q6. Hint: multiply each query occurrence by its training-topic count. Partial step: band contributes two to Music. Compare with the worked score only after calculation.
- Explain two deferrals: Explain the distinct numerical and language issues in Q9 and Q10. Hint: no evidence is different from a tied score. Discuss what context or multi-label task might be needed without forcing one universal interpretation.
- Test text boundaries: Predict results for blank text punctuation digits capitalization and an unknown word before optionally running the code. Hint: inspect the exact tokenizer. Retain expected and actual output and distinguish a valid deferral from rejected malformed input.
- Report the denominators: Summarise the ten-case coverage answered-case correctness and constant-Music comparison. Hint: do not count ASK as another subject topic. State why the designed fixture does not estimate general message performance.
Advanced
- Challenge a meaning claim: Critique a claim that a topic label captures tone sarcasm intent and all meaning. Hint: use the sick and cancelled-match examples. Offer alternative interpretations and state what evidence would be needed rather than inventing a speakers feelings.
- Investigate a representation limit: Use team song versus song team and training versus trained to explain lost order and absent stemming. Hint: distinguish count equality from semantic equivalence. Propose a changed representation and new tests without claiming it solves every ambiguity.
- Transfer to fictional library requests: Define a new single-label or multi-label request task using invented sentences. Hint: declare how mixed requests are handled first. Include provenance fitting evaluation baseline context review and a rights check without real learner messages.
- Review language-variety fairness: Write an accessible report on missing context and vocabulary across varieties of English. Hint: model coverage is not a measure of a persons intelligence. Propose diverse permissioned or synthetic tests and a human response without sensitive profiling.
Learning Check
- Calculate two predictions: Give Q3 and Q6 topic contributions and outcomes.
- Diagnose the context example: Explain two possible readings of That was sick and the actual model reason for ASK.
- Explain mixed evidence: Why does Q10 defer and what alternative task might be appropriate?
- Evaluate without inflating a score: State the ten-case results and baseline comparison.
- Demonstrate lost information: What do the reordered and opposite-attitude examples reveal?
- Transfer with a safe scope: What belongs in a new request-classification plan?
Worked checks and assessment guidance
- Q3 has band and song with Music training counts two each giving Music four and Sport zero. Q6 has coach two and training one giving Sport three and Music zero. These are supplied-rule overlap scores not probabilities or a complete meaning analysis.
- An illness context can support a negative health comment while a performance context can support praise. Without context neither is guaranteed. Our model has no known token overlap and returns ASK for no_known_evidence; it has not inferred sentiment or diagnosed the speaker.
- Team contributes two to Sport and song contributes two to Music so the highest scores tie. The declared single-topic rule returns ASK. A deliberately multi-label task could allow both topics but that is a changed target requiring a suitable evaluation.
- Eight clear-topic labels match the eight supplied clear references and two cases defer. Coverage is eight of ten and correctness among answered cases eight of eight. That is not ten classified topics. Constant Music matches four of eight clear topics and also forces single labels on the two review cases. The authored fixture is too small and constructed to establish general reliability.
- Team song and song team produce the same token counts and tied scores because position is discarded. Wonderful and awful song comments both produce Music because only song is known. Topic success therefore does not establish sentiment understanding or complete semantic equivalence. Unknown words and absent stemming add further limits.
- Define labels permitted representations and whether mixed topics are allowed. Use fictional or properly permissioned text separate fitting from selection and fresh final evaluation compare a baseline and document missing-context review. Do not infer personal ability wellbeing or protected characteristics from this toy score. A new representation or model requires new evidence.
Learning evidence
Retain the eight labelled training records, learned token counts, traces of two scores and at least eight justified clear-topic decisions. Explain Q9 and Q10 as distinct uncertainties and keep a record of context alternatives, coverage and baseline denominators. Include tokenisation boundaries, order and stemming limitations and an original request-task transfer plan. Preserve actual Python outputs if executed; the demonstration does not certify a general language model or practical decision-making competence.
OER and Wikipedia
scikit-learn: Text feature extraction is primary technical context for numerical token representations, count features and information lost by a bag of words. Our original scoring rule is a transparent count-overlap construction, not scikit-learn output, a probabilistic classifier or a large language model. Relevant source sections inspected 15 November 2026. DfE Computing, Key Stage 3 supplies digital representation context; this AI route is editorial enrichment rather than a separate statutory subject.
International books at Kent Library.jpg, Roc0ast3r, CC0. Original photograph unchanged; it is not our training corpus or an endorsement by the library.
https://www.youtube.com/watch?v=X-AWdfSFCHQ — CodeAI: How Chatbots and Large Language Models Work. https://www.youtube.com/watch?v=x2mRoFNm22g — CodeAI: AI: Training Data & Bias. Actual metadata, playability, embedding and preview bytes verified; previews visually inspected. Original recordings embedded unchanged without transcript copying, endorsement, redistribution or relicensing implied. These wider-context recordings do not describe the small fitted count model exactly; all examples and required learning activities work independently of playback.
The training fragments, ten test sentences, review actions and stress examples are original fictional materials. The complete bounded standard-library program was actually executed. It learns topic token counts and generates no prose. No real learner messages, personal inference, local generative LLM, separately billed API or external chatbot request was used.
Linked learning areas
aiMOOC Projects
Schulfach+


aiMOOCs


aiMOOC Projekte


MOOCwiki · Deutsch
Nach dem Lernen ist vor dem Lernen
Entdecke direkt den nächsten Lernkurs. Weitere Inhalte erscheinen, wenn Du weiter nach unten scrollst.
Zur MOOCwiki-HauptseiteMediathek
Mediathek wird aus dem Wiki geladen ...
Keine passenden Inhalte gefunden. Bitte ändere Suche oder Filter.
NEWSLernweltNOAH fragen