{ "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": [ "
| \n", " | title\n", " | text\n", " | 
|---|---|---|
| 0\n", " | Trang Chính\n", " | Trang Chính\\n\\n<templatestyles src=\"Wiki2021/s...\n", " | 
| 1\n", " | Internet Society\n", " | Internet Society hay ISOC là một tổ chức quốc...\n", " | 
| 2\n", " | Tiếng Việt\n", " | Tiếng Việt , cũng gọi là tiếng Việt Nam hay Vi...\n", " | 
| 3\n", " | Tiếng Việt\n", " | hệ thống thanh điệu phát triển cao hơn, hệ thố...\n", " | 
| 4\n", " | Tiếng Việt\n", " | tiếp xúc Hán – Việt thành 2 giai đoạn chính: \\...\n", " | 
| ...\n", " | ...\n", " | ...\n", " | 
| 1944401\n", " | VNOI\n", " | chuyên viên của các tập đoàn công nghệ lớn như...\n", " | 
| 1944402\n", " | Vòng hoa\n", " | Vòng hoa (; IPA: ) là một loại hoa, lá, quả, ...\n", " | 
| 1944403\n", " | Vòng hoa\n", " | vật trong thần thoại La Mã và Hy Lạp được miêu...\n", " | 
| 1944404\n", " | Vòng hoa\n", " | một vai trò nổi bật, tương tự như ở Anh.\\n\\n==...\n", " | 
| 1944405\n", " | Vòng hoa\n", " | trên giá đỡ vòng hoa thắp sáng những ngọn nến ...\n", " | 
1944406 rows × 2 columns
\n", "| \n", " | title\n", " | text\n", " | bm25_text\n", " | 
|---|---|---|---|
| 0\n", " | Trang Chính\n", " | Trang Chính\\n\\n<templatestyles src=\"Wiki2021/s...\n", " | trang chính <templatestyles src= wiki2021 styl...\n", " | 
| 1\n", " | Internet Society\n", " | Internet Society hay ISOC là một tổ chức quốc...\n", " | internet society hay isoc là một tổ chức quốc ...\n", " | 
| 2\n", " | Tiếng Việt\n", " | Tiếng Việt , cũng gọi là tiếng Việt Nam hay Vi...\n", " | tiếng việt cũng gọi là tiếng việt nam hay việt...\n", " | 
| 3\n", " | Tiếng Việt\n", " | hệ thống thanh điệu phát triển cao hơn, hệ thố...\n", " | hệ thống thanh điệu phát triển cao hơn hệ thốn...\n", " | 
| 4\n", " | Tiếng Việt\n", " | tiếp xúc Hán – Việt thành 2 giai đoạn chính: \\...\n", " | tiếp xúc hán – việt thành 2 giai đoạn chính bu...\n", " |