0. 比赛介绍
本项目来自 Kaggle 的 NER 比赛:比赛链接
此 pipeline 及 code 参考自
- https://www.kaggle.com/tungmphung/coleridge-matching-bert-ner?select=kaggle_run_ner.py
- https://www.kaggle.com/tungmphung/pytorch-bert-for-named-entity-recognition
1. Bert NER Finetune
数据准备
首先需要将数据转换成 NER 的 json 格式。
原始数据
train.csv
0007f880-0a9b-492d-9a58-76eb0b0e0bd7.json
(某篇文章)
由于train.csv
中 Id 有重复,首先通过 group
将相同的并入一行:
train = train.groupby('Id').agg({
'pub_title': 'first',
'dataset_title': '|'.join,
'dataset_label': '|'.join,
'cleaned_label': '|'.join
}).reset_index()
print(f'No. grouped training rows: {len(train)}')
No. grouped training rows: 14316
数据转换
直接上代码:
cnt_pos, cnt_neg = 0, 0 # number of sentences that contain/not contain labels
ner_data = []
pbar = tqdm(total