Chinese Word Segmentation
How to compile
Suppose that ZPar has been downloaded to the directory zpar
. To make the segmentor system, type make segmentor
. This will create a directory zpar/dist/segmentor
, in which there are two files: train
and segmentor
. The file train
is used to train a segmentation model,and the file segmentor
is used to segment new texts using a trained segmentation model.
Format of inputs and outputs
The input files to the segmentor are formatted as a sequence of Chinese characters. An example input is:
ZPar可以分析中文和英文
The output files contain space-separated words:
ZPar 可以 分析 中文 和 英文
The output format is also the format of training files for the train
executable.
Both input and output files must be enemd in utf8. Here is a script that transfers files that are enemd in gb to the utf8 encoding.
How to train a model
To train a model, use
zpar/dist/segmentor/train <train-file> <model-file> <number of iterations>
For example, using the example train file, you can train a model by
zpar/dist/segmentor/train train.txt model 1
After training is completed, a new file model
will be created in the current directory, which can be used to segment raw Chinese sentences. The above command performs training with one iteration
(see How to tune the performance of a system
) using the training file.
How to segment new texts
To apply an existing model to segment new texts, use
zpar/dist/segmentor/segmentor <model> [<input-file>] [<output-file>]
where the input file and output file are optional. If the output file is not specified, segmented texts will be printed to the console. If the input file is not specified, raw texts will be read from the console. For example, using the model we just trained, we can segment an example input by
zpar/dist/segmentor/segmentor model input.txt output.txt
The output file contains automatically segmented texts.
Outputs and evaluation
Automatically segmented texts contain errors. In order to evaluate the quality of the outputs, we can manually specify the segmentation of a sample, and compare the outputs with the correct sample.
A manually specified segmentation of the input file is given in this example reference file. Here is a Python script that performs automatic evaluation.
Using the above output.txt
and reference.txt
, we can evaluate the accuracies by typing
python evaluate.py output.txt reference.txt
You can find the precision, recall, and f-score here.
How to tune the performance of a system
The performance of the system after one training iteration may not be optimal. You can try training a model for another few iterations, after each you compare the performance. You can choose the model that gives the highest f-score on your test data. We conventionally call this test file the development test data, because you develop a segmentation model using this. Here is a shell script that automatically trains the segmentor for 30 iterations, and after the ith iteration, stores the model file to model.i. You can compare the f-score of all 30 iterations and choose model.k, which gives the best f-score, as the final model. In this file, there is a variable called segmentor
. You need to set this variable to the relative directory of zpar/dist/segmentor
.
Source code
The source code for the segmentor can be found at
zpar/src/chinese/segmentor/implementation/SEGMENTOR_IMPL
where SEGMENTOR_IMPL is a macro defined in Makefile
, and specifies a specific implementation for the segmentor.
Reference
- Yue Zhang and Stephen Clark. 2007. Chinese Segmentation Using a Word-based Perceptron Algorithm. In Proc. of ACL. pages 840-847.