File size: 5,857 Bytes
0758d29 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | {
"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
}
|