{ "cells": [ { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import os; os.chdir('..')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# pip install apache-beam==2.43.0\n", "# pip install --no-deps multiprocess==0.70.14" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "ename": "ImportError", "evalue": "cannot import name 'get_context' from 'multiprocess' (/home/ubuntu/SentenceStructureComparision/venv/lib/python3.10/site-packages/multiprocess/__init__.py)", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m/home/ubuntu/SentenceStructureComparision/wiki_gpt/process_wiki_data.ipynb Cell 3\u001b[0m line \u001b[0;36m1\n\u001b[0;32m----> 1\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39mdatasets\u001b[39;00m \u001b[39mimport\u001b[39;00m load_dataset\n\u001b[1;32m 2\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39mtqdm\u001b[39;00m \u001b[39mimport\u001b[39;00m tqdm\n\u001b[1;32m 3\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mpandas\u001b[39;00m \u001b[39mas\u001b[39;00m \u001b[39mpd\u001b[39;00m\n", "File \u001b[0;32m~/SentenceStructureComparision/venv/lib/python3.10/site-packages/datasets/__init__.py:22\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[39m# flake8: noqa\u001b[39;00m\n\u001b[1;32m 2\u001b[0m \u001b[39m# Copyright 2020 The HuggingFace Datasets Authors and the TensorFlow Datasets Authors.\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[39m#\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[39m# pylint: enable=line-too-long\u001b[39;00m\n\u001b[1;32m 18\u001b[0m \u001b[39m# pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position\u001b[39;00m\n\u001b[1;32m 20\u001b[0m __version__ \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39m2.14.5\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m---> 22\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39marrow_dataset\u001b[39;00m \u001b[39mimport\u001b[39;00m Dataset\n\u001b[1;32m 23\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39marrow_reader\u001b[39;00m \u001b[39mimport\u001b[39;00m ReadInstruction\n\u001b[1;32m 24\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39mbuilder\u001b[39;00m \u001b[39mimport\u001b[39;00m ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder\n", "File \u001b[0;32m~/SentenceStructureComparision/venv/lib/python3.10/site-packages/datasets/arrow_dataset.py:67\u001b[0m\n\u001b[1;32m 65\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m \u001b[39mimport\u001b[39;00m config\n\u001b[1;32m 66\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39marrow_reader\u001b[39;00m \u001b[39mimport\u001b[39;00m ArrowReader\n\u001b[0;32m---> 67\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39marrow_writer\u001b[39;00m \u001b[39mimport\u001b[39;00m ArrowWriter, OptimizedTypedSequence\n\u001b[1;32m 68\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39mdata_files\u001b[39;00m \u001b[39mimport\u001b[39;00m sanitize_patterns\n\u001b[1;32m 69\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39mdownload\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mdownload_config\u001b[39;00m \u001b[39mimport\u001b[39;00m DownloadConfig\n", "File \u001b[0;32m~/SentenceStructureComparision/venv/lib/python3.10/site-packages/datasets/arrow_writer.py:27\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mpyarrow\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mparquet\u001b[39;00m \u001b[39mas\u001b[39;00m \u001b[39mpq\u001b[39;00m\n\u001b[1;32m 26\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m \u001b[39mimport\u001b[39;00m config\n\u001b[0;32m---> 27\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39mfeatures\u001b[39;00m \u001b[39mimport\u001b[39;00m Features, Image, Value\n\u001b[1;32m 28\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39mfeatures\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mfeatures\u001b[39;00m \u001b[39mimport\u001b[39;00m (\n\u001b[1;32m 29\u001b[0m FeatureType,\n\u001b[1;32m 30\u001b[0m _ArrayXDExtensionType,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 36\u001b[0m to_pyarrow_listarray,\n\u001b[1;32m 37\u001b[0m )\n\u001b[1;32m 38\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39mfilesystems\u001b[39;00m \u001b[39mimport\u001b[39;00m is_remote_filesystem\n", "File \u001b[0;32m~/SentenceStructureComparision/venv/lib/python3.10/site-packages/datasets/features/__init__.py:17\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[39m# flake8: noqa\u001b[39;00m\n\u001b[1;32m 3\u001b[0m __all__ \u001b[39m=\u001b[39m [\n\u001b[1;32m 4\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mAudio\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[1;32m 5\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mArray2D\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mTranslationVariableLanguages\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[1;32m 16\u001b[0m ]\n\u001b[0;32m---> 17\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39maudio\u001b[39;00m \u001b[39mimport\u001b[39;00m Audio\n\u001b[1;32m 18\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39mfeatures\u001b[39;00m \u001b[39mimport\u001b[39;00m Array2D, Array3D, Array4D, Array5D, ClassLabel, Features, Sequence, Value\n\u001b[1;32m 19\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39mimage\u001b[39;00m \u001b[39mimport\u001b[39;00m Image\n", "File \u001b[0;32m~/SentenceStructureComparision/venv/lib/python3.10/site-packages/datasets/features/audio.py:11\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39m.\u001b[39;00m \u001b[39mimport\u001b[39;00m config\n\u001b[1;32m 10\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mdownload\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mdownload_config\u001b[39;00m \u001b[39mimport\u001b[39;00m DownloadConfig\n\u001b[0;32m---> 11\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mdownload\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mstreaming_download_manager\u001b[39;00m \u001b[39mimport\u001b[39;00m xopen, xsplitext\n\u001b[1;32m 12\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mtable\u001b[39;00m \u001b[39mimport\u001b[39;00m array_cast\n\u001b[1;32m 13\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mutils\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mpy_utils\u001b[39;00m \u001b[39mimport\u001b[39;00m no_op_if_value_is_null, string_to_dict\n", "File \u001b[0;32m~/SentenceStructureComparision/venv/lib/python3.10/site-packages/datasets/download/__init__.py:9\u001b[0m\n\u001b[1;32m 1\u001b[0m __all__ \u001b[39m=\u001b[39m [\n\u001b[1;32m 2\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mDownloadConfig\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[1;32m 3\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mDownloadManager\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[1;32m 4\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mDownloadMode\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[1;32m 5\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mStreamingDownloadManager\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[1;32m 6\u001b[0m ]\n\u001b[1;32m 8\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39mdownload_config\u001b[39;00m \u001b[39mimport\u001b[39;00m DownloadConfig\n\u001b[0;32m----> 9\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39mdownload_manager\u001b[39;00m \u001b[39mimport\u001b[39;00m DownloadManager, DownloadMode\n\u001b[1;32m 10\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39mstreaming_download_manager\u001b[39;00m \u001b[39mimport\u001b[39;00m StreamingDownloadManager\n", "File \u001b[0;32m~/SentenceStructureComparision/venv/lib/python3.10/site-packages/datasets/download/download_manager.py:35\u001b[0m\n\u001b[1;32m 33\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mutils\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39minfo_utils\u001b[39;00m \u001b[39mimport\u001b[39;00m get_size_checksum_dict\n\u001b[1;32m 34\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mutils\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mlogging\u001b[39;00m \u001b[39mimport\u001b[39;00m get_logger, is_progress_bar_enabled, tqdm\n\u001b[0;32m---> 35\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mutils\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mpy_utils\u001b[39;00m \u001b[39mimport\u001b[39;00m NestedDataStructure, map_nested, size_str\n\u001b[1;32m 36\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39mdownload_config\u001b[39;00m \u001b[39mimport\u001b[39;00m DownloadConfig\n\u001b[1;32m 39\u001b[0m logger \u001b[39m=\u001b[39m get_logger(\u001b[39m__name__\u001b[39m)\n", "File \u001b[0;32m~/SentenceStructureComparision/venv/lib/python3.10/site-packages/datasets/utils/py_utils.py:41\u001b[0m\n\u001b[1;32m 39\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mdill\u001b[39;00m\n\u001b[1;32m 40\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mmultiprocess\u001b[39;00m\n\u001b[0;32m---> 41\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mmultiprocess\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mpool\u001b[39;00m\n\u001b[1;32m 42\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mnumpy\u001b[39;00m \u001b[39mas\u001b[39;00m \u001b[39mnp\u001b[39;00m\n\u001b[1;32m 43\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39mpackaging\u001b[39;00m \u001b[39mimport\u001b[39;00m version\n", "File \u001b[0;32m~/SentenceStructureComparision/venv/lib/python3.10/site-packages/multiprocess/pool.py:29\u001b[0m\n\u001b[1;32m 26\u001b[0m \u001b[39m# If threading is available then ThreadPool should be provided. Therefore\u001b[39;00m\n\u001b[1;32m 27\u001b[0m \u001b[39m# we avoid top-level imports which are liable to fail on some systems.\u001b[39;00m\n\u001b[1;32m 28\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m \u001b[39mimport\u001b[39;00m util\n\u001b[0;32m---> 29\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m \u001b[39mimport\u001b[39;00m get_context, \u001b[39mTimeoutError\u001b[39;00m\n\u001b[1;32m 30\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39mconnection\u001b[39;00m \u001b[39mimport\u001b[39;00m wait\n\u001b[1;32m 32\u001b[0m \u001b[39m#\u001b[39;00m\n\u001b[1;32m 33\u001b[0m \u001b[39m# Constants representing the state of a pool\u001b[39;00m\n\u001b[1;32m 34\u001b[0m \u001b[39m#\u001b[39;00m\n", "\u001b[0;31mImportError\u001b[0m: cannot import name 'get_context' from 'multiprocess' (/home/ubuntu/SentenceStructureComparision/venv/lib/python3.10/site-packages/multiprocess/__init__.py)" ] } ], "source": [ "from datasets import load_dataset\n", "from tqdm import tqdm\n", "import pandas as pd\n", "import pyarrow as pa\n", "import pyarrow.compute as pc\n", "import re\n", "import pyarrow.dataset as ds" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Downloading: 100%|██████████| 15.3k/15.3k [00:00<00:00, 19.5MB/s]\n", "Downloading: 3%|▎ | 703M/20.3G [00:11<05:19, 61.2MB/s] " ] } ], "source": [ "dataset = load_dataset(\"wikipedia\", \"20220301.en\", split=\"train\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "data_df = dataset.to_pandas()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def keep_intro(text):\n", " return re.sub(r'\\(\\W*\\)', '', ' '.join(text.split('\\n\\n')[:2]))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "data_df['intro'] = data_df.apply(lambda row: keep_intro(row['text']), axis=1)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "data_df[['id', 'title', 'url', 'intro']].to_csv('wiki_intro.csv', index=False)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "data_df = pd.read_csv('wiki_intro.csv')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6458670" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(data_df)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | id | \n", "title | \n", "url | \n", "intro | \n", "
---|---|---|---|---|
0 | \n", "12 | \n", "Anarchism | \n", "https://en.wikipedia.org/wiki/Anarchism | \n", "Anarchism is a political philosophy and moveme... | \n", "
1 | \n", "25 | \n", "Autism | \n", "https://en.wikipedia.org/wiki/Autism | \n", "Autism is a neurodevelopmental disorder charac... | \n", "
2 | \n", "39 | \n", "Albedo | \n", "https://en.wikipedia.org/wiki/Albedo | \n", "Albedo is the measure of the diffuse reflecti... | \n", "
3 | \n", "290 | \n", "A | \n", "https://en.wikipedia.org/wiki/A | \n", "A, or a, is the first letter and the first vow... | \n", "
4 | \n", "303 | \n", "Alabama | \n", "https://en.wikipedia.org/wiki/Alabama | \n", "Alabama is a state in the Southeastern region... | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
6458665 | \n", "70201819 | \n", "Bianca Fernandez | \n", "https://en.wikipedia.org/wiki/Bianca%20Fernandez | \n", "Bianca Jolie Fernandez (born 24 February 2004)... | \n", "
6458666 | \n", "70201882 | \n", "Condons and Clangibbon | \n", "https://en.wikipedia.org/wiki/Condons%20and%20... | \n", "Condons and Clangibbon is a barony in County ... | \n", "
6458667 | \n", "70201886 | \n", "2022 Chattanooga Red Wolves SC season | \n", "https://en.wikipedia.org/wiki/2022%20Chattanoo... | \n", "The 2022 Chattanooga Red Wolves SC season will... | \n", "
6458668 | \n", "70201947 | \n", "Nkiko Prosper | \n", "https://en.wikipedia.org/wiki/Nkiko%20Prosper | \n", "Turatsinze Nkiko Prosper (born 1985) professio... | \n", "
6458669 | \n", "70201959 | \n", "Michael O'Donnell (Missouri politician) | \n", "https://en.wikipedia.org/wiki/Michael%20O%27Do... | \n", "Michael A. O'Donnell (born June 17, 1968) is a... | \n", "
6458670 rows × 4 columns
\n", "