{ "cells": [ { "cell_type": "code", "execution_count": 7, "id": "cf6af8de-b9f8-4396-8496-9f1d28dd6156", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Done! Created EASC.csv and EASC.jsonl\n" ] } ], "source": [ "import os\n", "import re\n", "import json\n", "import pandas as pd\n", "\n", "ARTICLES_DIR = \"Articles\"\n", "MTURK_DIR = \"MTurk\"\n", "\n", "records_csv = []\n", "records_jsonl = []\n", "\n", "for folder in sorted(os.listdir(ARTICLES_DIR)):\n", " folder_path = os.path.join(ARTICLES_DIR, folder)\n", " if not os.path.isdir(folder_path):\n", " continue\n", "\n", " # Extract article_id from folder name \"Article001\"\n", " m = re.match(r\"Article(\\d+)\", folder)\n", " if not m:\n", " print(f\"Skipping folder (no article id): {folder}\")\n", " continue\n", "\n", " article_id = int(m.group(1))\n", "\n", " # Read article text + topic name (from file name)\n", " article_files = os.listdir(folder_path)\n", " if len(article_files) == 0:\n", " print(f\"No article file in {folder}\")\n", " continue\n", "\n", " article_file = article_files[0] # only one expected\n", " article_file_path = os.path.join(folder_path, article_file)\n", "\n", " # Extract topic name from filename \"Science and Tech (8).txt\"\n", " base = os.path.splitext(article_file)[0]\n", " match = re.match(r\"(.+?)\\s*\\(\\d+\\)\", base)\n", " topic_name = match.group(1).strip() if match else \"Unknown\"\n", "\n", " # Read article text (strict UTF-8)\n", " with open(article_file_path, \"r\", encoding=\"utf-8\", errors=\"replace\") as f:\n", " article_text = f.read().strip()\n", "\n", " # Read MTurk summaries for the same article\n", " summaries_dir = os.path.join(MTURK_DIR, folder)\n", " if not os.path.isdir(summaries_dir):\n", " print(f\"No MTurk summaries for {folder}\")\n", " continue\n", "\n", " summary_files = sorted(os.listdir(summaries_dir))\n", " summaries = []\n", "\n", " for sfile in summary_files:\n", " s_path = os.path.join(summaries_dir, sfile)\n", " with open(s_path, \"r\", encoding=\"utf-8\", errors=\"replace\") as f:\n", " summaries.append(f.read().strip())\n", "\n", " # Ensure exactly 5 summaries (MTurk provides 5)\n", " while len(summaries) < 5:\n", " summaries.append(\"\")\n", "\n", " # Trim excess if more than 5 (rare)\n", " summaries = summaries[:5]\n", "\n", " # --- CSV ROW ---\n", " records_csv.append({\n", " \"article_id\": article_id,\n", " \"topic_name\": topic_name,\n", " \"article_text\": article_text,\n", " \"summary_A\": summaries[0],\n", " \"summary_B\": summaries[1],\n", " \"summary_C\": summaries[2],\n", " \"summary_D\": summaries[3],\n", " \"summary_E\": summaries[4]\n", " })\n", "\n", " # --- JSONL ROW ---\n", " records_jsonl.append({\n", " \"article_id\": article_id,\n", " \"topic_name\": topic_name,\n", " \"article_text\": article_text,\n", " \"summaries\": summaries\n", " })\n", "\n", "# Save CSV (UTF-8)\n", "df = pd.DataFrame(records_csv)\n", "df.to_csv(\"EASC.csv\", index=False, encoding=\"utf-8\")\n", "\n", "# Save JSONL (UTF-8)\n", "with open(\"EASC.jsonl\", \"w\", encoding=\"utf-8\") as f:\n", " for row in records_jsonl:\n", " f.write(json.dumps(row, ensure_ascii=False) + \"\\n\")\n", "\n", "print(\"Done! Created EASC.csv and EASC.jsonl\")\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "599e5c36-b09f-4dc3-b79e-3e418c67f318", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from sklearn.model_selection import train_test_split\n", "\n", "df = pd.read_csv(\"EASC.csv\")\n", "\n", "train_df, temp_df = train_test_split(df, test_size=0.2, random_state=42)\n", "val_df, test_df = train_test_split(temp_df, test_size=0.5, random_state=42)\n", "\n", "train_df.to_csv(\"EASC_train.csv\", index=False)\n", "val_df.to_csv(\"EASC_val.csv\", index=False)\n", "test_df.to_csv(\"EASC_test.csv\", index=False)\n" ] } ], "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.13.9" } }, "nbformat": 4, "nbformat_minor": 5 }