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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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7fca8158",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n"
     ]
    }
   ],
   "source": [
    "print(\"hello\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "b24cb367",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.naive_bayes import MultinomialNB\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.model_selection import train_test_split\n",
    "import joblib\n",
    "from utils import mask_pii"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f51ac2e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"dataset.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bc084a0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>email</th>\n",
       "      <th>type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Subject: Unvorhergesehener Absturz der Datenan...</td>\n",
       "      <td>Incident</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Subject: Customer Support Inquiry\\n\\nSeeking i...</td>\n",
       "      <td>Request</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Subject: Data Analytics for Investment\\n\\nI am...</td>\n",
       "      <td>Request</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Subject: Krankenhaus-Dienstleistung-Problem\\n\\...</td>\n",
       "      <td>Incident</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Subject: Security\\n\\nDear Customer Support, I ...</td>\n",
       "      <td>Request</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23995</th>\n",
       "      <td>Subject: Problem mit der HP DeskJet 3755 WLAN-...</td>\n",
       "      <td>Problem</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23996</th>\n",
       "      <td>Subject: Problemas com a impressora HP DeskJet...</td>\n",
       "      <td>Incident</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23997</th>\n",
       "      <td>Subject: Problema urgente con el envío\\n\\nEsti...</td>\n",
       "      <td>Incident</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23998</th>\n",
       "      <td>Subject: \\n\\nCher Service Client, nous rencont...</td>\n",
       "      <td>Incident</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23999</th>\n",
       "      <td>Subject: Overcharge on yearly Microsoft Office...</td>\n",
       "      <td>Problem</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>24000 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                   email      type\n",
       "0      Subject: Unvorhergesehener Absturz der Datenan...  Incident\n",
       "1      Subject: Customer Support Inquiry\\n\\nSeeking i...   Request\n",
       "2      Subject: Data Analytics for Investment\\n\\nI am...   Request\n",
       "3      Subject: Krankenhaus-Dienstleistung-Problem\\n\\...  Incident\n",
       "4      Subject: Security\\n\\nDear Customer Support, I ...   Request\n",
       "...                                                  ...       ...\n",
       "23995  Subject: Problem mit der HP DeskJet 3755 WLAN-...   Problem\n",
       "23996  Subject: Problemas com a impressora HP DeskJet...  Incident\n",
       "23997  Subject: Problema urgente con el envío\\n\\nEsti...  Incident\n",
       "23998  Subject: \\n\\nCher Service Client, nous rencont...  Incident\n",
       "23999  Subject: Overcharge on yearly Microsoft Office...   Problem\n",
       "\n",
       "[24000 rows x 2 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "930212ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Subject: Unvorhergesehener Absturz der Datenanalyse-Plattform\\n\\nDie Datenanalyse-Plattform brach unerwartet ab, da die Speicheroberfläche zu gering war My name is Sophia Rossi.. Ich habe versucht, Laravel 8 und meinen MacBook Pro neu zu starten, aber das Problem behält sich bei. Ich benötige Ihre Unterstützung, um diesen Fehler zu beheben. You can reach me at janesmith@company.com.'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['email'][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9a9a7167",
   "metadata": {},
   "outputs": [],
   "source": [
    "masked_texts = []\n",
    "for text in df['email']:\n",
    "    masked, _ = mask_pii(text)\n",
    "    masked_texts.append(masked)\n",
    "\n",
    "df['masked_body'] = masked_texts\n",
    "# This code take approx 4 to 7 min to run(that is mask every sensitive info in the dataset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7cf0fd9d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>type</th>\n",
       "      <th>masked_body</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Incident</td>\n",
       "      <td>Subject: Unvorhergesehener Absturz der Datenan...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Request</td>\n",
       "      <td>Subject: Customer Support Inquiry\\n\\nSeeking i...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Request</td>\n",
       "      <td>Subject: Data Analytics for Investment\\n\\nI am...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Incident</td>\n",
       "      <td>Subject: Krankenhaus-Dienstleistung-Problem\\n\\...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Request</td>\n",
       "      <td>Subject: Security\\n\\nDear Customer Support, I ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23995</th>\n",
       "      <td>Problem</td>\n",
       "      <td>Subject: Problem mit der HP DeskJet 3755 WLAN-...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23996</th>\n",
       "      <td>Incident</td>\n",
       "      <td>Subject: Problemas com a impressora HP DeskJet...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23997</th>\n",
       "      <td>Incident</td>\n",
       "      <td>Subject: Problema urgente con el envío\\n\\nEsti...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23998</th>\n",
       "      <td>Incident</td>\n",
       "      <td>Subject: \\n\\nCher Service Client, nous rencont...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23999</th>\n",
       "      <td>Problem</td>\n",
       "      <td>Subject: [full_name] on yearly Microsoft Offic...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>24000 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           type                                        masked_body\n",
       "0      Incident  Subject: Unvorhergesehener Absturz der Datenan...\n",
       "1       Request  Subject: Customer Support Inquiry\\n\\nSeeking i...\n",
       "2       Request  Subject: Data Analytics for Investment\\n\\nI am...\n",
       "3      Incident  Subject: Krankenhaus-Dienstleistung-Problem\\n\\...\n",
       "4       Request  Subject: Security\\n\\nDear Customer Support, I ...\n",
       "...         ...                                                ...\n",
       "23995   Problem  Subject: Problem mit der HP DeskJet 3755 WLAN-...\n",
       "23996  Incident  Subject: Problemas com a impressora HP DeskJet...\n",
       "23997  Incident  Subject: Problema urgente con el envío\\n\\nEsti...\n",
       "23998  Incident  Subject: \\n\\nCher Service Client, nous rencont...\n",
       "23999   Problem  Subject: [full_name] on yearly Microsoft Offic...\n",
       "\n",
       "[24000 rows x 2 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.drop(columns=['email'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "66e94d2a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Subject: Unvorhergesehener Absturz der Datenanalyse-Plattform\\n\\nDie Datenanalyse-Plattform brach unerwartet ab, da die Speicheroberfläche zu gering war My name is [full_name].. Ich habe versucht, Laravel 8 und meinen M[full_name]neu zu starten, aber das Problem behält sich bei. Ich benötige I[full_name] um diesen Fehler z[full_name]ben. You can reach me at [email].'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['masked_body'][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "02f957e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    df['masked_body'], df['type'], test_size=0.2, random_state=42\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "1dc9d917",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pipeline = Pipeline([\n",
    "#     ('tfidf', TfidfVectorizer(max_features=5000, ngram_range=(1,2))),\n",
    "#     ('clf', LogisticRegression(max_iter=1000)),\n",
    "# ])\n",
    "pipeline = Pipeline([\n",
    "    ('tfidf', TfidfVectorizer(max_features=5000, ngram_range=(1,2))),\n",
    "    ('clf', MultinomialNB()),\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "9c811f7a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training accuracy: 0.7411979166666667\n",
      "Validation accuracy: 0.7235416666666666\n"
     ]
    }
   ],
   "source": [
    "pipeline.fit(X_train, y_train)\n",
    "print(\"Training accuracy:\", pipeline.score(X_train, y_train))\n",
    "print(\"Validation accuracy:\", pipeline.score(X_test, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "af539dd5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['model.joblib']"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "joblib.dump(pipeline, 'model.joblib')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "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.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}