Files
chinese-xinhua/scripts/clean.ipynb
2026-10-02 22:47:01 +08:00

115 lines
2.4 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Clean Data (Duplicate Removal)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import json"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open(\"/home/pwxcoo/projects/chinese-xinhua/archived/idiom.json\") as fp:\n",
" idioms = json.load(fp)\n",
"\n",
"len(idioms)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"idioms[:5]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"unique_idioms = set(idiom[\"word\"] for idiom in idioms)\n",
"unique_idioms"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"len(unique_idioms)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"check = dict()\n",
"for idiom in idioms:\n",
" if idiom[\"word\"] in check and str(idiom) > str(check[idiom[\"word\"]]):\n",
"# print(idiom)\n",
"# print(check[idiom[\"word\"]])\n",
" check[idiom[\"word\"]] = idiom\n",
" else:\n",
" check[idiom[\"word\"]] = idiom\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open(\"/home/pwxcoo/projects/chinese-xinhua/data/idiom-clean.json\", \"w+\", encoding=\"utf-8\") as fp:\n",
" json.dump(list(check.values()), fp, ensure_ascii=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}