How Neam reads a Khmer article

Real news text is full of invisible characters, and articles are longer than the model can read at once. Neam has to cut the text into words, read it in pieces and still point every answer at the exact characters in the original. This page follows an invented, news-style article through those steps.

  1. Hidden charactersRemove them for the model only, and keep every word's original position.
  2. Overlapping windowsRead long articles in overlapping windows, and take each word from the window where it has the most context.
  3. Back to the textTurn word labels into character spans that match the original exactly.
Step 1

Invisible characters, visible problems

In plain words: Khmer has no spaces between words, so many websites insert invisible "zero-width spaces" to help browsers wrap lines. They don't follow any rule, and the model never saw them in training. Neam removes them from what the model reads, but remembers where every word sits in the original text, so its answers still point to the right place.

The sentence below comes from an invented news-style article written for this page. We added zero-width spaces the way many Khmer news sites do: of them in characters, shown as dashed boxes. Click a word to see where it maps back to in the original.

Technical detail: why "lossless"

Neam records the position of every visible character, removes U+200B, U+200C and U+200D, and passes the clean text to the word segmenter. Each word the segmenter returns is located in the clean text and mapped back to its original start and end. Word 6 in this example, សុវត្ថិភាព, has a hidden character inside it; its span (33–44) still covers exactly the right characters in the original.

The mapping also checks the segmenter: if it ever changed, dropped or invented a non-space character, Neam raises an error instead of returning shifted positions. Coeng and other combining marks are never removed.

positions = [i for i, c in enumerate(text) if c not in HIDDEN]
clean = normalize(text)                      # drops only U+200B/C/D
for piece in segmenter(clean):
    start = clean.find(piece, cursor)
    if start < 0 or clean[cursor:start].strip():
        raise ValueError('Segmentation changed or skipped non-whitespace text.')
    end = start + len(piece)
    words.append(Word(piece, positions[start], positions[end - 1] + 1))
Step 2

Reading a long article in overlapping windows

In plain words: the model can only read 128 pieces at a time, and a news article is usually longer. If you cut it into back-to-back chunks, a word near a cut sees text on one side only, like reading a sentence that has been torn in half. Neam reads overlapping windows instead, and takes each word's answer from the window where that word is most central.

Below is an invented news-style article written for this page, about a road-safety campaign in Kampong Cham. The people and the event are fictional. It has words and pieces; the windows, piece counts and tags come from running it through the released Neam model. Each bar is a window. Drag along the article strip, or use the slider, to pick a word. For every window that contains the word, the cards show how much context it has on each side; the window with the larger weaker side wins.

MethodWords with fewer than 16 pieces of context on their weaker side
Back-to-back chunks
Neam's overlapping windows

Coloured ticks under the strip are the names Neam found: people, organizations, places. The article edges count as full context, because there is nothing more to read.

What it changed

Overall, little: validation F1 moved from 76.28 to 76.50. But organization F1 rose by 4.6 to 6.4 points in all four comparisons, and it recovered a country name that had been missed at the end of a long article. Organization names are long, so they are the most likely to be cut in half. We kept the windows because they remove a systematic blind spot and have no settings to tune.

Where the rule comes from

The "most context on the weaker side" rule is the one Google's original BERT code uses for question answering over long documents (_check_is_max_context in BERT's SQuAD script). Neam applies it to named entity recognition, builds windows from whole words, advances them by about half a window, and right-aligns the last one so it stays full.

Technical detail: the window rule

For word j in window W = [a, b), let LW(j) and RW(j) be the pieces to its left and right inside W, counted as infinite at a document edge. Each word is read from exactly one window:

W*(j) = argmax over W ∋ j of min( LW(j), RW(j) )

Windows hold at most 128 pieces including the two special tokens, so 126 for words. In this article that gives the six windows shown above; the last two overlap heavily because the last one is right-aligned.

Step 3

Back to the exact characters

In plain words: the model labels words, but people need the exact characters: where a name starts and ends in their text. Two bugs in this last step made correct answers look wrong. Finding them was worth more than many rounds of training.

The scorer was dropping punctuation

Validation F1 54.90 → 69.03, same model weights.

Turning word labels back into spans dropped punctuation inside names, such as the dots in an initialism ("U.S.A." came back as "U.S.A"), and mishandled prefixes. Under exact matching, those correct predictions counted as errors. Fixing it before any retraining raised the baseline by 14 points.

Names that crossed a line break

Held-out F1 86.70 → 86.98, same model (release 0.2.1).

Articles are imported line by line, but a name could still be joined across a line break, gluing the end of one paragraph to the start of the next. Since 0.2.1, a name never spans a line break, and the article importer trims menu lines at the edges of Khmer pages.

The lesson

Check the path from model output to score before training anything new. Every Neam model is scored through exactly the same code the app uses (segmentation, windows, decoding, reconstruction and display rules), so a fix here applies to every model and every number.

Credits

Built on the work of

Offset-preserving segmentation, reconstruction fixesNeamHidden-character handling, the position mapping and its checks, the span fixes, and using overlapping windows for Khmer NER.
khmer-nltk · Phan Viet HoangLibraryThe word segmenter Neam calls (VietHoang1512/khmer-nltk), written with Prof. Huong Le Thanh as advisor.
Khmer word segmentation with CRFs · Vichet Chea et al. (2015)Khmer NLPResearch cited by khmer-nltk. The first author was at NIPTICT in Phnom Penh (KhNLP 2015 paper).
Khmer text segmentation articles · Phylypo TumKhmer NLPArticles on CRF and dictionary-based Khmer word segmentation, also cited by khmer-nltk (Medium).
Maximum-context window rule · Devlin et al. (Google BERT)MethodThe "most context on the weaker side" rule, from BERT's SQuAD code for long documents.
Kampuchea Thmey, Fresh News, Khmer PressKhmer publishersThe news articles Neam was trained and tested on. None of their text appears on this page; the examples here are invented. Neam's repository stores only positions, not article text.