Preparing a Corpus
A good training corpus is essential for model accuracy. This guide explains how to prepare one using Universal Dependencies (UD) Treebanks.
Data Source: UD Treebanks
Litsea uses UD Treebanks as the data source for both word segmentation and POS tagging. UD Treebanks provide high-quality, manually annotated data in CoNLL-U format for many languages.
Available Treebanks
| Language | Treebank | Repository |
|---|---|---|
| Japanese | UD Japanese-GSD | UD_Japanese-GSD |
| Chinese | UD Chinese-GSD | UD_Chinese-GSD |
| Korean | UD Korean-GSD | UD_Korean-GSD |
| English | UD English-EWT | UD_English-EWT |
Step 1: Download a UD Treebank
Use scripts/download_udtreebank.sh to download a UD Treebank. It prints the path to the training CoNLL-U file to stdout:
conllu_file=$(bash scripts/download_udtreebank.sh -l ja -o /tmp)
Supported languages: ja (Japanese, default), ko (Korean), zh (Chinese), en (English). Without -l, the LANG_CODE environment variable, when set, replaces the ja default. Use -o to specify the output directory (default: current directory).
Corpus for Word Segmentation
For word segmentation (AdaBoost), the corpus must be a plain text file with:
- One sentence per line
- Words separated by spaces
太郎 は 走っ た 。
Litsea は コンパクト な 単語 分割 ソフトウェア です 。
A byte order mark at the start of the file, which some editors write when saving as “UTF-8 with BOM”, is ignored, in this format and in the TSV and POS formats below (issue #281).
Convert CoNLL-U to Word Segmentation Corpus
Use scripts/corpus_udtreebank.sh to convert a CoNLL-U file to corpus format:
conllu_file=$(bash scripts/download_udtreebank.sh -l ja -o /tmp)
bash scripts/corpus_udtreebank.sh "$conllu_file" corpus.txt
This converts the CoNLL-U data into space-separated words (one sentence per line).
Space-Preserving TSV Corpus (Korean, English)
The space-separated format above discards the original spacing of the
sentence: the words are later concatenated without spaces for training. For
Korean and English this loses the strongest boundary signal (Korean’s
inter-eojeol space, English’s inter-word space), so use the -s flag
instead, which emits a tab-separated corpus in which every original
space (reconstructed from the treebank’s SpaceAfter annotations) is kept
as its own token:
conllu_file=$(bash scripts/download_udtreebank.sh -l ko -o /tmp)
bash scripts/corpus_udtreebank.sh -s "$conllu_file" ko_corpus.tsv
conllu_file=$(bash scripts/download_udtreebank.sh -l en -o /tmp)
bash scripts/corpus_udtreebank.sh -s "$conllu_file" en_corpus.tsv
For English specifically, -s also handles multiword tokens (contractions
like don't, represented in CoNLL-U as a range line covering two word
lines): the range’s member words are joined with no space token between
them, and the range’s own SpaceAfter annotation is applied after the
last member word. See
English for
the details.
Extract features from a TSV corpus with litsea extract --format tsv. The
-s combines with -p to emit a space-preserving TSV of word/POS
tokens — the two-stage POS training corpus for space-delimited languages
(issue #198), consumed by litsea extract --pos --format tsv.
Corpus for POS Tagging
For POS tagging, each word must be annotated with its POS tag. POS tags come from the two-stage model: stage 1 is a binary boundary classifier that labels each character position B (a word starts here) or O (continuation), and stage 2 tags each resulting word with one of the 17 UPOS tags. The earlier joint model, which predicted one of 18 B-<UPOS> / O labels per character, was removed; loading such a model file as a POS model fails with an error that asks you to retrain with litsea train --pos.
POS Corpus Format
Each line represents one sentence, with words annotated as word/POS pairs separated by spaces:
これ/PRON は/ADP テスト/NOUN です/AUX 。/PUNCT
Litsea/PROPN は/ADP 単語/NOUN 分割/NOUN ソフトウェア/NOUN です/AUX 。/PUNCT
The POS tags follow the Universal POS (UPOS) tagset with 17 categories: ADJ, ADP, ADV, AUX, CCONJ, DET, INTJ, NOUN, NUM, PART, PRON, PROPN, PUNCT, SCONJ, SYM, VERB, X.
Convert CoNLL-U to POS Corpus
Use scripts/corpus_udtreebank.sh with the -p flag to produce a POS corpus:
conllu_file=$(bash scripts/download_udtreebank.sh -l ja -o /tmp)
bash scripts/corpus_udtreebank.sh -p "$conllu_file" pos_corpus.txt
Multi-word tokens and empty nodes are automatically handled during conversion.
Automated Corpus Preparation
Litsea includes helper scripts in the scripts/ directory that automate the UD Treebank download and conversion:
scripts/download_udtreebank.sh– Downloads a UD Treebank and prints the path to the training CoNLL-U filescripts/corpus_udtreebank.sh– Converts a CoNLL-U file to Litsea corpus format
# Download UD Treebank and get CoNLL-U file path
conllu_file=$(bash scripts/download_udtreebank.sh -l ja -o /tmp)
# Generate word segmentation corpus
bash scripts/corpus_udtreebank.sh "$conllu_file" corpus.txt
# Generate POS corpus
bash scripts/corpus_udtreebank.sh -p "$conllu_file" pos_corpus.txt
Supported languages for download_udtreebank.sh: ja (Japanese, default), ko (Korean), zh (Chinese), en (English).
Corpus from Wikipedia Dump
For larger-scale training, you can build a corpus from a full Wikipedia dump using scripts/corpus_wikidump.sh. This extracts plain text with wicket, filters for actual sentences, and tokenizes with lindera.
Usage
Download a dump with scripts/download_wikidump.sh (-l lang: ja, ko or zh, default ja, or LANG_CODE when set; -o output_dir, default the current directory). It saves <lang>wiki-latest-pages-articles.xml.bz2 from dumps.wikimedia.org, skips the download if that file already exists, and prints the file’s path:
dump_file=$(bash scripts/download_wikidump.sh -l ja -o /tmp)
bash scripts/corpus_wikidump.sh "$dump_file" corpus_ja.txt
Or pass a dump file you already have:
# Japanese (default)
bash scripts/corpus_wikidump.sh jawiki-latest-pages-articles.xml.bz2 corpus_ja.txt
# Korean
bash scripts/corpus_wikidump.sh -l ko kowiki-latest-pages-articles.xml.bz2 corpus_ko.txt
# Chinese
bash scripts/corpus_wikidump.sh -l zh zhwiki-latest-pages-articles.xml.bz2 corpus_zh.txt
Options
| Option | Description | Default |
|---|---|---|
-l lang | Language code: ja, ko, zh | ja |
-n max_lines | Maximum sentence lines to process (0 = unlimited) | 100000 |
The script needs wicket, shuf and bash 4 or later, and a lindera on PATH that embeds the dictionary of the language (the table below): a plain cargo install lindera-cli has none, so install it with --features embed-unidic for ja, embed-ko-dic for ko or embed-cc-cedict for zh. It concatenates wicket’s output files in random order (shuf) before the filters and the -n limit, so a limited corpus is a random sample of the dump and differs from run to run.
Sentence Filtering
The script applies two filters to keep only well-formed sentences:
- Sentence-ending punctuation – Lines must end with
。,.,!, or?. This excludes section headers (e.g., “参考文献”), list items, and metadata. - Minimum length – Lines must be at least 20 characters, as measured by
awk’slength: characters with a multibyte-aware awk (such as gawk under a UTF-8 locale), but bytes with an awk that has no multibyte support, where 20 bytes is about 7 CJK characters. This excludes short fragments and isolated labels.
Tokenizer Dictionaries
| Language | Dictionary | Token Filter |
|---|---|---|
Japanese (ja) | embedded://unidic | japanese_compound_word (numeral compound) |
Korean (ko) | embedded://ko-dic | None |
Chinese (zh) | embedded://cc-cedict | None |
Corpus Size Guidelines
The recommended corpus size depends on your use case:
| Size (sentence lines) | Use Case |
|---|---|
| ~10,000 | Minimum for prototyping and smoke tests |
| 50,000 – 100,000 | Practical range for model training |
| 100,000 – 500,000 | High-quality, robust models |
| Unlimited | Use full dump for maximum accuracy |
The default max_lines=100000 in corpus_wikidump.sh targets the practical-to-high-quality range.
Corpus Quality Tips
- Diversity – Include text from various domains (news, literature, web, etc.)
- Size – See Corpus Size Guidelines above for recommended sizes
- Consistency – Ensure consistent tokenization throughout the corpus
- Deduplication – Remove duplicate sentences to avoid bias
- Cleaning – Remove HTML tags, special formatting, and non-text content