Spaces:
Build error
Build error
File size: 12,775 Bytes
c5d2283 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# !pip install pandas \n",
"# !pip install tdqm\n",
"# !pip install pandarallel"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import re\n",
"import string\n",
"import math\n",
"import json\n",
"import pandas as pd\n",
"from tqdm.auto import tqdm\n",
"tqdm.pandas()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"_WORD_SPLIT = re.compile(\"([.,!?\\\"/':;)(])\")\n",
"_DIGIT_RE = re.compile(br\"\\d\")\n",
"STOP_WORDS = \"\\\" \\' [ ] . , ! : ; ?\".split(\" \")\n",
"\n",
"def basic_tokenizer(sentence):\n",
" words = []\n",
" for space_separated_fragment in sentence.strip().split():\n",
" words.extend(_WORD_SPLIT.split(space_separated_fragment))\n",
" return [w.lower() for w in words if w != '' and w != ' ' and w not in string.punctuation]\n",
"\n",
"def remove_appending_title(text,title):\n",
" return text.replace(f\"{title}\\n\\n{title}\",f\"{title} \")\n",
"\n",
"def create_sliding_window(text, size=256, overlap=32):\n",
" actual_size = size - overlap\n",
" windows = []\n",
" n_windows = math.ceil(len(text)/actual_size)\n",
" for i in range(n_windows):\n",
" windows.append(\" \".join(text[i*actual_size:i*actual_size + size]))\n",
" return windows"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "16dcb3eb371e4084be66ee507040fe18",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"0it [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"all_titles = []\n",
"all_texts = []\n",
"all_bm25_texts = []\n",
"with open(\"../data/raw/wikipedia_20220620_cleaned/wikipedia_20220620_cleaned.jsonl\", encoding=\"utf-8\") as f:\n",
" for i,line in tqdm(enumerate(f)):\n",
" x = json.loads(line)\n",
" text = remove_appending_title(x[\"text\"],x[\"title\"])\n",
" text = text.split(\" \")\n",
" sliding_windows = create_sliding_window(text)\n",
" all_texts.extend(sliding_windows)\n",
" # all_bm25_texts.extend(bm25_windows)\n",
" all_titles.extend([x['title'],]*len(sliding_windows))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>title</th>\n",
" <th>text</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Trang Chính</td>\n",
" <td>Trang Chính\\n\\n<templatestyles src=\"Wiki2021/s...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Internet Society</td>\n",
" <td>Internet Society hay ISOC là một tổ chức quốc...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Tiếng Việt</td>\n",
" <td>Tiếng Việt , cũng gọi là tiếng Việt Nam hay Vi...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Tiếng Việt</td>\n",
" <td>hệ thống thanh điệu phát triển cao hơn, hệ thố...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Tiếng Việt</td>\n",
" <td>tiếp xúc Hán – Việt thành 2 giai đoạn chính: \\...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1944401</th>\n",
" <td>VNOI</td>\n",
" <td>chuyên viên của các tập đoàn công nghệ lớn như...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1944402</th>\n",
" <td>Vòng hoa</td>\n",
" <td>Vòng hoa (; IPA: ) là một loại hoa, lá, quả, ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1944403</th>\n",
" <td>Vòng hoa</td>\n",
" <td>vật trong thần thoại La Mã và Hy Lạp được miêu...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1944404</th>\n",
" <td>Vòng hoa</td>\n",
" <td>một vai trò nổi bật, tương tự như ở Anh.\\n\\n==...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1944405</th>\n",
" <td>Vòng hoa</td>\n",
" <td>trên giá đỡ vòng hoa thắp sáng những ngọn nến ...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1944406 rows × 2 columns</p>\n",
"</div>"
],
"text/plain": [
" title text\n",
"0 Trang Chính Trang Chính\\n\\n<templatestyles src=\"Wiki2021/s...\n",
"1 Internet Society Internet Society hay ISOC là một tổ chức quốc...\n",
"2 Tiếng Việt Tiếng Việt , cũng gọi là tiếng Việt Nam hay Vi...\n",
"3 Tiếng Việt hệ thống thanh điệu phát triển cao hơn, hệ thố...\n",
"4 Tiếng Việt tiếp xúc Hán – Việt thành 2 giai đoạn chính: \\...\n",
"... ... ...\n",
"1944401 VNOI chuyên viên của các tập đoàn công nghệ lớn như...\n",
"1944402 Vòng hoa Vòng hoa (; IPA: ) là một loại hoa, lá, quả, ...\n",
"1944403 Vòng hoa vật trong thần thoại La Mã và Hy Lạp được miêu...\n",
"1944404 Vòng hoa một vai trò nổi bật, tương tự như ở Anh.\\n\\n==...\n",
"1944405 Vòng hoa trên giá đỡ vòng hoa thắp sáng những ngọn nến ...\n",
"\n",
"[1944406 rows x 2 columns]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.DataFrame()\n",
"df[\"title\"] = all_titles\n",
"df[\"text\"] = all_texts\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pandarallel import pandarallel\n",
"\n",
"pandarallel.initialize(progress_bar=True, use_memory_fs=False, nb_workers=6) \n",
"\n",
"def apply_tokenizer(df, num_chunks):\n",
" chunk_sr = pd.Series()\n",
" i = 0\n",
" end = df.shape[0]\n",
" chunk_size = math.floor(end/num_chunks)\n",
" while i != end:\n",
" if end - i < chunk_size:\n",
" chunk_size = end - i\n",
"# print((i,i+chunk_size))\n",
" temp_df = df[i:i+chunk_size].copy()\n",
" chunk_sr = pd.concat([chunk_sr, temp_df[\"text\"].parallel_apply(lambda x: \" \".join(basic_tokenizer(x)))], ignore_index=True)\n",
" i+=chunk_size\n",
" return chunk_sr\n",
"\n",
"df[\"bm25_text\"] = apply_tokenizer(df, 5)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>title</th>\n",
" <th>text</th>\n",
" <th>bm25_text</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Trang Chính</td>\n",
" <td>Trang Chính\\n\\n<templatestyles src=\"Wiki2021/s...</td>\n",
" <td>trang chính <templatestyles src= wiki2021 styl...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Internet Society</td>\n",
" <td>Internet Society hay ISOC là một tổ chức quốc...</td>\n",
" <td>internet society hay isoc là một tổ chức quốc ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Tiếng Việt</td>\n",
" <td>Tiếng Việt , cũng gọi là tiếng Việt Nam hay Vi...</td>\n",
" <td>tiếng việt cũng gọi là tiếng việt nam hay việt...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Tiếng Việt</td>\n",
" <td>hệ thống thanh điệu phát triển cao hơn, hệ thố...</td>\n",
" <td>hệ thống thanh điệu phát triển cao hơn hệ thốn...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Tiếng Việt</td>\n",
" <td>tiếp xúc Hán – Việt thành 2 giai đoạn chính: \\...</td>\n",
" <td>tiếp xúc hán – việt thành 2 giai đoạn chính bu...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" title text \\\n",
"0 Trang Chính Trang Chính\\n\\n<templatestyles src=\"Wiki2021/s... \n",
"1 Internet Society Internet Society hay ISOC là một tổ chức quốc... \n",
"2 Tiếng Việt Tiếng Việt , cũng gọi là tiếng Việt Nam hay Vi... \n",
"3 Tiếng Việt hệ thống thanh điệu phát triển cao hơn, hệ thố... \n",
"4 Tiếng Việt tiếp xúc Hán – Việt thành 2 giai đoạn chính: \\... \n",
"\n",
" bm25_text \n",
"0 trang chính <templatestyles src= wiki2021 styl... \n",
"1 internet society hay isoc là một tổ chức quốc ... \n",
"2 tiếng việt cũng gọi là tiếng việt nam hay việt... \n",
"3 hệ thống thanh điệu phát triển cao hơn hệ thốn... \n",
"4 tiếp xúc hán – việt thành 2 giai đoạn chính bu... "
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1944406, 3)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.shape"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"df.to_csv(\"../data/processed/wikipedia_20220620_cleaned_v2.csv\",index=False)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|