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

1944406 rows × 2 columns

\n", "
" ], "text/plain": [ " title text\n", "0 Trang Chính Trang Chính\\n\\n\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
titletextbm25_text
0Trang ChínhTrang Chính\\n\\n<templatestyles src=\"Wiki2021/s...trang chính <templatestyles src= wiki2021 styl...
1Internet SocietyInternet Society hay ISOC là một tổ chức quốc...internet society hay isoc là một tổ chức quốc ...
2Tiếng ViệtTiếng Việt , cũng gọi là tiếng Việt Nam hay Vi...tiếng việt cũng gọi là tiếng việt nam hay việt...
3Tiếng Việthệ thống thanh điệu phát triển cao hơn, hệ thố...hệ thống thanh điệu phát triển cao hơn hệ thốn...
4Tiếng Việttiếp xúc Hán – Việt thành 2 giai đoạn chính: \\...tiếp xúc hán – việt thành 2 giai đoạn chính bu...
\n", "" ], "text/plain": [ " title text \\\n", "0 Trang Chính Trang Chính\\n\\n