Train and inspect a contextual POS tagger
Retain original source tokens, evaluate Universal POS labels and confusion counts, and reproduce saved training continuation.
// Synthetic English POS annotation. Each source row is one explicit sequence.
// Run from a fresh writable directory: the checkpoint is created exclusively.
import aner.nlp
let fitting_source = nlp.corpus(
List("Alice visits Paris .", "Bob visits Rome .", "Clara works quietly .",
"The red train arrives .", "A blue bus arrives .", "The dog runs quickly .",
"A cat sleeps .", "Birds fly .", "They visit Paris .", "We work quietly .",
"Three red buses arrive .", "Alice and Bob work .", "She has a book .", "She is ready ."),
List("pos-train-01", "pos-train-02", "pos-train-03", "pos-train-04",
"pos-train-05", "pos-train-06", "pos-train-07", "pos-train-08",
"pos-train-09", "pos-train-10", "pos-train-11", "pos-train-12",
"pos-train-13", "pos-train-14"), role: "training")
let fitting_tokens = nlp.tokenize(fitting_source)
let training = nlp.annotate(fitting_tokens,
List(List("PROPN", "VERB", "PROPN", "PUNCT"),
List("PROPN", "VERB", "PROPN", "PUNCT"),
List("PROPN", "VERB", "ADV", "PUNCT"),
List("DET", "ADJ", "NOUN", "VERB", "PUNCT"),
List("DET", "ADJ", "NOUN", "VERB", "PUNCT"),
List("DET", "NOUN", "VERB", "ADV", "PUNCT"),
List("DET", "NOUN", "VERB", "PUNCT"),
List("NOUN", "VERB", "PUNCT"),
List("PRON", "VERB", "PROPN", "PUNCT"),
List("PRON", "VERB", "ADV", "PUNCT"),
List("NUM", "ADJ", "NOUN", "VERB", "PUNCT"),
List("PROPN", "CCONJ", "PROPN", "VERB", "PUNCT"),
List("PRON", "VERB", "DET", "NOUN", "PUNCT"),
List("PRON", "AUX", "ADJ", "PUNCT")), task: "pos")
let validation_source = nlp.corpus(
List("Bob works quietly .", "The blue train arrives .", "They visit Rome .", "Alice is ready ."),
List("pos-validation-1", "pos-validation-2", "pos-validation-3", "pos-validation-4"),
role: "validation")
let validation_tokens = nlp.tokenize(validation_source)
let validation = nlp.annotate(validation_tokens,
List(List("PROPN", "VERB", "ADV", "PUNCT"),
List("DET", "ADJ", "NOUN", "VERB", "PUNCT"),
List("PRON", "VERB", "PROPN", "PUNCT"),
List("PROPN", "AUX", "ADJ", "PUNCT")), task: "pos")
let complete = nlp.fit_tagger(training, epochs: 8, seed: 731,
shuffle: true, normalization: "nfc", casefold: true)
let report = complete.evaluate_tagger(validation)
let prediction = complete.tag(validation_tokens)
print("POS validation token accuracy on four synthetic sequences")
print(report.accuracy())
print("Token confusion counts for error analysis")
print(report.confusion())
print("Source tokens and predicted Universal POS labels")
print(validation_tokens.token_texts(0))
print(prediction.tags(0))
print("Fitted labels, cumulative epochs, examples seen and updates")
print(complete.labels())
print(complete.epochs())
print(complete.examples_seen())
print(complete.updates())
print("Canonical configuration and beginning of sequence weights")
print(complete.configuration())
print(complete.tagger_weights("@BOS"))
let first = nlp.fit_tagger(training, epochs: 3, seed: 731,
shuffle: true, normalization: "nfc", casefold: true)
first.save("pos-stage.anertagger")
let continued = nlp.load_tagger("pos-stage.anertagger").resume_tagger(training, epochs: 5)
print("Checkpoint continuation reproduces complete model and prediction identities")
print(continued.fingerprint() == complete.fingerprint())
print(continued.tag(validation_tokens).fingerprint() == prediction.fingerprint())
// Select hyperparameters on validation; reserve independent final testing data.
Make it your experiment.
Use Aner 0.0.5 or newer. Save this program as examples/nlp_pos.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.
aner check examples/nlp_pos.aner
aner run examples/nlp_pos.anerThe aner command must be on your PATH. Follow the installation guide if your terminal cannot find it.
Requires the native Aner 0.0.5 or newer with aner.nlp and VS Code 0.1.48 candidate integration. Earlier runtimes may predate these APIs. All documents are synthetic and included; no data download, service or credential is required. Run from a fresh writable directory because the example exclusively creates a new native artifact.
Bundled datasets retain their own source attribution and license notices. Example outcomes are not comparative benchmarks or evidence of clinical validity.
Dataset sources & attribution