3_selection.ipynb 4.26 KB
Newer Older
Philipp Koncar's avatar
Philipp Koncar committed
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
172
173
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "import glob"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Configuration\n",
    "\n",
    "*input_dir:* The path to the directory containg annoted seed words. Please make sure to use a '/' (slash) in the end. For example: `path/to/annotated/seed_words/`.\n",
    "\n",
    "*output_dir:* The path to the directory where you want to save selected seed words. Please make sure to use a '/' (slash) in the end. For example: `path/to/output/`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "input_dir = \"results/annotated/\"\n",
    "output_dir = \"results/selected/\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Directory Setup (Optional)\n",
    "Creates directories according to the configuration if not already created manually."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if not os.path.exists(input_dir):\n",
    "    os.makedirs(input_dir)\n",
    "if not os.path.exists(output_dir):\n",
    "    os.makedirs(output_dir)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Seed Word Selection"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load annoteted seed words"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "annotation_file_names = glob.glob(\"{}*.csv\".format(input_dir))\n",
    "print(\"found {} annotations\".format(len(annotation_file_names)))\n",
    "annotations = []\n",
    "for annotation_file_name in annotation_file_names:\n",
    "    annotations.append(pd.read_csv(annotation_file_name, index_col=\"word\"))\n",
    "print(\"loaded {} annotations\".format(len(annotations)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Select seed words\n",
    "This is based on a majority vote."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "annotations_df = pd.concat(annotations, axis=1).fillna(\"neutral\")\n",
    "pos_words = []\n",
    "neg_words = []\n",
    "neu_words = []\n",
    "for w, row in annotations_df.mode(axis=1).iterrows():\n",
    "    row = row.dropna()\n",
    "    if len(row) > 1:\n",
    "        continue\n",
    "    if row[0] == \"positive\":\n",
    "        pos_words.append(w)\n",
    "    elif row[0] == \"negative\":\n",
    "        neg_words.append(w)\n",
    "    elif row[0] == \"neutral\":\n",
    "        neu_words.append(w)\n",
    "print(\"number of positive:\", len(pos_words))\n",
    "print(\"number of negative:\", len(neg_words))\n",
    "print(\"number of neutral:\", len(neu_words))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Save selected seed words"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"{}positive.txt\".format(output_dir), mode=\"wt\", encoding=\"utf-8\") as pos_file:\n",
    "    pos_file.write(\"\\n\".join(pos_words))\n",
    "with open(\"{}negative.txt\".format(output_dir), mode=\"wt\", encoding=\"utf-8\") as neg_file:\n",
    "    neg_file.write(\"\\n\".join(neg_words))\n",
    "with open(\"{}neutral.txt\".format(output_dir), mode=\"wt\", encoding=\"utf-8\") as neu_file:\n",
    "    neu_file.write(\"\\n\".join(neu_words))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}