{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "2b1f5378", "metadata": {}, "outputs": [], "source": [ "import json\n", "import pandas as pd\n", "import itertools\n", "import os\n", "\n", "# Load data files\n", "annotator_df = pd.read_csv(\"./trac4_PER_train.csv\")\n", "df = pd.read_csv(\"./trac3_EMP_train.csv\", on_bad_lines='skip')\n", "article_df = pd.read_csv(\"./articles_adobe_AMT.csv\")\n", "\n", "# Mappings for structured user profile\n", "gender_map = {1: 'Male', 2: 'Female', 5: 'Other'}\n", "race_map = {1: 'White', 2: 'Hispanic / Latino', 3: 'Black / African American', 4: 'Native American / American Indian', 5: 'Asian / Pacific Islander', 6: 'Other'}\n", "education_map = {\n", " 1: 'a diploma less than a high school',\n", " 2: 'High school degree or diploma',\n", " 3: 'went to Technical / Vocational School',\n", " 4: 'went to college but did not get a degree',\n", " 5: 'Two year associate degree',\n", " 6: 'College or university degree',\n", " 7: 'Postgraduate / professional degree'\n", "}\n", "\n", "# Build annotator dicts\n", "annotator_text_dict = {}\n", "annotator_structured_dict = {}\n", "for idx, row in annotator_df.iterrows():\n", " persona = []\n", " structured = {}\n", " gender = gender_map.get(row['gender'], 'Unknown')\n", " race = race_map.get(row['race'], 'Unknown')\n", " age = row['age']\n", " education = education_map.get(row['education'], 'Unknown')\n", " income = row['income']\n", " personality = {\n", " \"openness\": row['personality_openess'],\n", " \"conscientiousness\": row['personality_conscientiousness'],\n", " \"extraversion\": row['personality_extraversion'],\n", " \"agreeableness\": row['personality_agreeableness'],\n", " \"stability\": row['personality_stability']\n", " }\n", " persona.append(f\"The person is {gender.lower()}.\")\n", " persona.append(f\"Racially, the person is {race.lower()}.\")\n", " persona.append(f\"The person is {age} years old.\")\n", " persona.append(f\"The person has a {education.lower()}.\")\n", " persona.append(f\"The person earns {income} dollar per year.\")\n", " persona.append(\n", " f\"According to the Big Five personality test, on a scale of 10, the person has scored {personality['openness']} in openness, {personality['conscientiousness']} in conscientiousness, {personality['extraversion']} in extraversion, {personality['agreeableness']} in agreeableness, and {personality['stability']} in stability.\"\n", " )\n", " persona = [p for p in persona if \"nan\" not in p]\n", " persona_text = \" \".join(persona)\n", " annotator_text_dict[row['person_id']] = persona_text\n", " structured = {\n", " \"gender\": gender,\n", " \"race\": race,\n", " \"age\": age,\n", " \"education\": education,\n", " \"income\": income,\n", " \"personality\": personality\n", " }\n", " annotator_structured_dict[row['person_id']] = structured\n", "\n", "# Prepare dataset\n", "dataset = []\n", "for idx, row in df.iterrows():\n", " person_id = row['person_id']\n", " article_id = row['article_id']\n", " # Get user profile\n", " user_profile_text = annotator_text_dict.get(person_id, \"\")\n", " user_profile_structured = annotator_structured_dict.get(person_id, {})\n", " # Get article\n", " article_row = article_df[article_df['article_id'] == article_id]\n", " if article_row.empty:\n", " continue\n", " article_text = article_row['text'].values[0]\n", " # Input prompt (as in ec.py, with_persona)\n", " prompt = (\n", " f\"{article_text}\"\n", " )\n", " # Output: the user's essay/response\n", " output = row['person_essay']\n", " # Compose item\n", " item = {\n", " \"user_id\": f\"ec_{person_id}\",\n", " \"profile_text\": user_profile_text,\n", " \"profile\": user_profile_structured,\n", " \"article\": article_text,\n", " # \"input\": prompt,\n", " \"essay\": output\n", " }\n", " dataset.append(item)\n", "\n", "# Save to JSON\n", "# with open(\"ec_dataset.json\", \"w\", encoding=\"utf-8\") as f:\n", "# json.dump(dataset, f, ensure_ascii=False, indent=2)\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "cb69aa41", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "974" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(dataset)" ] }, { "cell_type": "code", "execution_count": 3, "id": "13f8cca9", "metadata": {}, "outputs": [], "source": [ "with open('/home/zxtan/text-to-lora/data_p13n/EC/ec_dataset.jsonl', 'w') as f:\n", " for line in dataset:\n", " f.write(json.dumps(line) + '\\n')" ] }, { "cell_type": "code", "execution_count": null, "id": "46944ebd", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "pytorch", "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.11.11" } }, "nbformat": 4, "nbformat_minor": 5 }