// 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.
