This repository contains a Chinese text-classification workflow that studies robustness to homophone noise.
It includes:
data/splits/*.csv, models/lm_jieba_3gram_20k.klm).Many scripts assume this layout:
data/
splits/
train.csv
dev.csv
test.csv
models/
lm_jieba_3gram_20k.klm
resources/
chinese_homophone_char.txt
chinese_homophone_word.txt
review (default in most scripts)label (default in training/evaluation scripts)For generated N-best files (from pipeline/run_jieba.py or pipeline/run_pkuseg.py), columns typically include:
id, origcand_1 ... cand_kw_1 ... w_kscore_1 ... score_kscripts/inject_noise.py: injects homophone noise into text data.pipeline/normalize_top10.py: homophone normalization with candidate generation, KenLM scoring, beam search, and top-N output.pipeline/run_jieba.py, pipeline/run_pkuseg.py: build N-best candidate CSV files.train/train_baseline.py: baseline training entry point.train/train_contrastive.py: supervised-contrastive training entry point.train/infer_with_nbest.py: N-best inference and evaluation (including entropy/dynamic gating options).scripts/run_matrix_experiment.sh, scripts/run_fewshot_experiment.sh: matrix/few-shot style experiment runners with caching/resume logic.config/tunable_params_Version3.yaml: suggested tunable parameters and ablation combinations.Based on model.yml and in.py imports, the project expects:
torch, transformers, datasets, accelerate, sentencepiece)pandas, numpy, scikit-learn, scipy)pypinyin, jieba, pkuseg, kenlm)PyYAML, tqdm, regex, fsspec)Note: there is no
requirements.txtin this repository. Usemodel.ymlas the primary environment specification.
git clone https://github.com/lyt-ai-acm/models.git
cd models
model.ymlconda env create -f model.yml
conda activate <your-env-name>
model.yml includes a Windows-style prefix and mirror channels; adjust/remove them for your local setup as needed.
python in.py
data/splits/.review and label columns exist (or pass custom --text_col / --label_col where supported).resources/.models/lm_jieba_3gram_20k.klm).PYTHONPATH=. python scripts/inject_noise.py \
--input_csv data/splits/train.csv \
--output_csv data/splits/train_noisy.csv \
--char_homo resources/chinese_homophone_char.txt \
--text_col review \
--noise_ratio 0.15 \
--seed 42
noise_ratio is the per-character probability of replacing a character with a randomly selected homophone candidate.
PYTHONPATH=. python pipeline/run_jieba.py \
--input_csv data/splits/test.csv \
--output_csv outputs/test_top10.csv \
--kenlm_path models/lm_jieba_3gram_20k.klm \
--word_homo resources/chinese_homophone_word.txt \
--char_homo resources/chinese_homophone_char.txt
PYTHONPATH=. python train/train_baseline.py \
--backbone roberta_wwm_ext \
--train_csv data/splits/train.csv \
--dev_csv data/splits/dev.csv \
--test_csv data/splits/test.csv \
--output_dir outputs/baseline \
--model_name hfl/chinese-roberta-wwm-ext \
--epochs 3 --batch_size 32 --lr 2e-5 --fp16
PYTHONPATH=. python train/train_contrastive.py \
--backbone roberta_wwm_ext \
--train_csv data/splits/train.csv \
--dev_csv data/splits/dev.csv \
--test_csv data/splits/test.csv \
--output_dir outputs/ours \
--model_name hfl/chinese-roberta-wwm-ext \
--epochs 3 --batch_size 32 --lr 2e-5 --fp16 --scl_weight 0.15
PYTHONPATH=. python train/infer_with_nbest.py \
--model_dir outputs/ours/best_model \
--input_csv outputs/test_top10.csv \
--truth_csv data/splits/test.csv \
--out_json outputs/ours/metrics_e4.json \
--alpha 2.0 --fallback_orig
bash scripts/run_matrix_experiment.sh
bash scripts/run_fewshot_experiment.sh
The implemented workflow in this repository is:
See config/tunable_params_Version3.yaml for:
m, beam_size, topn, alpha, beta, lamb, delta, tau),top_k, alpha, fallback_orig, thresholds),42 is used by default in major scripts.outputs/ (for example outputs/matrix_experiment/...).metrics_e4.json).The original test dataset used in this repository is a Chinese text dataset, not an English text dataset. In the default setting, the text data are Chinese user-generated reviews or posts, for example Chinese Weibo sentiment data. All preprocessing, homophone noise injection, candidate generation, normalization, and evaluation are designed for Chinese text.
The default label space is a three-class sentiment classification:
The text column review means "user review", "comment", or "post content". It does not mean "censorship" or "examination" (shencha in pinyin). The label column label means the sentiment class. If the original data are binary (0/1), a separate neutral dataset can be merged and assigned label 1 (neutral) to form the three-class setting.
For N-best files, orig is the original Chinese sentence, which may contain homophone noise (tongyinzi in pinyin). cand_1 ... cand_k are Chinese correction candidates. w_1 ... w_k are their weights or confidences. score_1 ... score_k are normalization or language-model scores. These fields are not English text fields. They should be interpreted as Chinese text and Chinese-oriented correction scores.
neg, neu, and pos are abbreviations for negative, neutral, and positive. Hanzi in pinyin means Chinese characters. Zhongwen in pinyin means Chinese language or Chinese text.
The primary dataset used in this repository is weibo_senti_100k, a publicly available Chinese sentiment analysis dataset derived from Sina Weibo. It contains over 100,000 manually annotated Weibo posts, with approximately 59,993 positive and 59,995 negative samples. The dataset covers diverse social media content, including daily user expressions, trending topic discussions, and short texts containing emoticons and internet slang.
In this repository, the dataset is used as a Chinese text classification corpus. The text column review means “user review”, “comment”, or “post content”; it does not mean “censorship” or “examination” (shencha in pinyin). The label column label contains the sentiment class. For the three-class setting, a separate neutral dataset can be merged and assigned label 1 (neutral).
Source / Download Link:
datasets/weibo_senti_100kSuggested Citation Format:
@misc{weibo_senti_100k,
title = {weibo\_senti\_100k: Chinese Sentiment Analysis Dataset},
howpublished = {\url{https://github.com/SophonPlus/ChineseNlpCorpus}},
note = {Chinese sentiment analysis dataset from Sina Weibo}
}
If a separate neutral dataset is used to extend the binary setting to three classes, its source, license, and citation should follow the original dataset provider. The neutral data are assigned label 1 (neutral) in this repository.
No explicit repository license file was detected at the time of writing.
Please add a LICENSE file if you want to define redistribution and usage terms.
Practical contribution workflow:
This repository contains a Chinese text-classification workflow that studies robustness to homophone noise.
It includes:
data/splits/*.csv, models/lm_jieba_3gram_20k.klm).Many scripts assume this layout:
data/
splits/
train.csv
dev.csv
test.csv
models/
lm_jieba_3gram_20k.klm
resources/
chinese_homophone_char.txt
chinese_homophone_word.txt
review (default in most scripts)label (default in training/evaluation scripts)For generated N-best files (from pipeline/run_jieba.py or pipeline/run_pkuseg.py), columns typically include:
id, origcand_1 ... cand_kw_1 ... w_kscore_1 ... score_kscripts/inject_noise.py: injects homophone noise into text data.pipeline/normalize_top10.py: homophone normalization with candidate generation, KenLM scoring, beam search, and top-N output.pipeline/run_jieba.py, pipeline/run_pkuseg.py: build N-best candidate CSV files.train/train_baseline.py: baseline training entry point.train/train_contrastive.py: supervised-contrastive training entry point.train/infer_with_nbest.py: N-best inference and evaluation (including entropy/dynamic gating options).scripts/run_matrix_experiment.sh, scripts/run_fewshot_experiment.sh: matrix/few-shot style experiment runners with caching/resume logic.config/tunable_params_Version3.yaml: suggested tunable parameters and ablation combinations.Based on model.yml and in.py imports, the project expects:
torch, transformers, datasets, accelerate, sentencepiece)pandas, numpy, scikit-learn, scipy)pypinyin, jieba, pkuseg, kenlm)PyYAML, tqdm, regex, fsspec)Note: there is no
requirements.txtin this repository. Usemodel.ymlas the primary environment specification.
git clone https://github.com/lyt-ai-acm/models.git
cd models
model.ymlconda env create -f model.yml
conda activate <your-env-name>
model.yml includes a Windows-style prefix and mirror channels; adjust/remove them for your local setup as needed.
python in.py
data/splits/.review and label columns exist (or pass custom --text_col / --label_col where supported).resources/.models/lm_jieba_3gram_20k.klm).PYTHONPATH=. python scripts/inject_noise.py \
--input_csv data/splits/train.csv \
--output_csv data/splits/train_noisy.csv \
--char_homo resources/chinese_homophone_char.txt \
--text_col review \
--noise_ratio 0.15 \
--seed 42
noise_ratio is the per-character probability of replacing a character with a randomly selected homophone candidate.
PYTHONPATH=. python pipeline/run_jieba.py \
--input_csv data/splits/test.csv \
--output_csv outputs/test_top10.csv \
--kenlm_path models/lm_jieba_3gram_20k.klm \
--word_homo resources/chinese_homophone_word.txt \
--char_homo resources/chinese_homophone_char.txt
PYTHONPATH=. python train/train_baseline.py \
--backbone roberta_wwm_ext \
--train_csv data/splits/train.csv \
--dev_csv data/splits/dev.csv \
--test_csv data/splits/test.csv \
--output_dir outputs/baseline \
--model_name hfl/chinese-roberta-wwm-ext \
--epochs 3 --batch_size 32 --lr 2e-5 --fp16
PYTHONPATH=. python train/train_contrastive.py \
--backbone roberta_wwm_ext \
--train_csv data/splits/train.csv \
--dev_csv data/splits/dev.csv \
--test_csv data/splits/test.csv \
--output_dir outputs/ours \
--model_name hfl/chinese-roberta-wwm-ext \
--epochs 3 --batch_size 32 --lr 2e-5 --fp16 --scl_weight 0.15
PYTHONPATH=. python train/infer_with_nbest.py \
--model_dir outputs/ours/best_model \
--input_csv outputs/test_top10.csv \
--truth_csv data/splits/test.csv \
--out_json outputs/ours/metrics_e4.json \
--alpha 2.0 --fallback_orig
bash scripts/run_matrix_experiment.sh
bash scripts/run_fewshot_experiment.sh
The implemented workflow in this repository is:
See config/tunable_params_Version3.yaml for:
m, beam_size, topn, alpha, beta, lamb, delta, tau),top_k, alpha, fallback_orig, thresholds),42 is used by default in major scripts.outputs/ (for example outputs/matrix_experiment/...).metrics_e4.json).The original test dataset used in this repository is a Chinese text dataset, not an English text dataset. In the default setting, the text data are Chinese user-generated reviews or posts, for example Chinese Weibo sentiment data. All preprocessing, homophone noise injection, candidate generation, normalization, and evaluation are designed for Chinese text.
The default label space is a three-class sentiment classification:
The text column review means "user review", "comment", or "post content". It does not mean "censorship" or "examination" (shencha in pinyin). The label column label means the sentiment class. If the original data are binary (0/1), a separate neutral dataset can be merged and assigned label 1 (neutral) to form the three-class setting.
For N-best files, orig is the original Chinese sentence, which may contain homophone noise (tongyinzi in pinyin). cand_1 ... cand_k are Chinese correction candidates. w_1 ... w_k are their weights or confidences. score_1 ... score_k are normalization or language-model scores. These fields are not English text fields. They should be interpreted as Chinese text and Chinese-oriented correction scores.
neg, neu, and pos are abbreviations for negative, neutral, and positive. Hanzi in pinyin means Chinese characters. Zhongwen in pinyin means Chinese language or Chinese text.
The primary dataset used in this repository is weibo_senti_100k, a publicly available Chinese sentiment analysis dataset derived from Sina Weibo. It contains over 100,000 manually annotated Weibo posts, with approximately 59,993 positive and 59,995 negative samples. The dataset covers diverse social media content, including daily user expressions, trending topic discussions, and short texts containing emoticons and internet slang.
In this repository, the dataset is used as a Chinese text classification corpus. The text column review means “user review”, “comment”, or “post content”; it does not mean “censorship” or “examination” (shencha in pinyin). The label column label contains the sentiment class. For the three-class setting, a separate neutral dataset can be merged and assigned label 1 (neutral).
Source / Download Link:
datasets/weibo_senti_100kSuggested Citation Format:
@misc{weibo_senti_100k,
title = {weibo\_senti\_100k: Chinese Sentiment Analysis Dataset},
howpublished = {\url{https://github.com/SophonPlus/ChineseNlpCorpus}},
note = {Chinese sentiment analysis dataset from Sina Weibo}
}
If a separate neutral dataset is used to extend the binary setting to three classes, its source, license, and citation should follow the original dataset provider. The neutral data are assigned label 1 (neutral) in this repository.
No explicit repository license file was detected at the time of writing.
Please add a LICENSE file if you want to define redistribution and usage terms.
Practical contribution workflow: