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d
 
 
 
 
 
b3ecaa7
dbb343d
 
 
 
 
 
 
 
 
 
b3ecaa7
dbb343d
b3ecaa7
dbb343d
 
 
 
 
 
b3ecaa7
dbb343d
b3ecaa7
dbb343d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57b9989
 
b3ecaa7
57b9989
 
 
 
 
 
 
 
b3ecaa7
57b9989
 
 
 
 
 
 
 
b3ecaa7
57b9989
b3ecaa7
57b9989
 
 
 
dbb343d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import sys\n",
    "sys.path.append(\"..\")\n",
    "from src.preprocessing import PreprocessingPipeline\n",
    "import pandas as pd\n",
    "import vaex"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----\n",
    "### Test vaex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"../data/test_en.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<thead>\n",
       "<tr><th>#                                </th><th>label  </th><th>text                                               </th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td><i style='opacity: 0.6'>0</i>    </td><td>0      </td><td>&quot;I think it&#x27;s time John Rambo move on with his l...</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>1</i>    </td><td>1      </td><td>&quot;I&#x27;ve just watch 2 films of Pang brothers, The E...</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>2</i>    </td><td>1      </td><td>&#x27;Jewel Thief is *THE* crime thriller of Bollywoo...</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>3</i>    </td><td>0      </td><td>&#x27;This so called remake is terrible. I went to se...</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>4</i>    </td><td>1      </td><td>&#x27;When Northfork debuted at the Cannes Film Festi...</td></tr>\n",
       "<tr><td>...                              </td><td>...    </td><td>...                                                </td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>4,995</i></td><td>0      </td><td>&#x27;The title tells it all -- Ed Gein, the butcher ...</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>4,996</i></td><td>0      </td><td>&quot;This film makes about as much sense as an &#x27;Ozzi...</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>4,997</i></td><td>0      </td><td>&#x27;&quot;Sex and the City&quot; has some great things going ...</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>4,998</i></td><td>0      </td><td>&#x27;Please...if anybody gets the chance to read thi...</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>4,999</i></td><td>0      </td><td>&#x27;...a film comes along that manages to be absolu...</td></tr>\n",
       "</tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "#      label    text\n",
       "0      0        \"I think it's time John Rambo move on with his l...\n",
       "1      1        \"I've just watch 2 films of Pang brothers, The E...\n",
       "2      1        'Jewel Thief is *THE* crime thriller of Bollywoo...\n",
       "3      0        'This so called remake is terrible. I went to se...\n",
       "4      1        'When Northfork debuted at the Cannes Film Festi...\n",
       "...    ...      ...\n",
       "4,995  0        'The title tells it all -- Ed Gein, the butcher ...\n",
       "4,996  0        \"This film makes about as much sense as an 'Ozzi...\n",
       "4,997  0        '\"Sex and the City\" has some great things going ...\n",
       "4,998  0        'Please...if anybody gets the chance to read thi...\n",
       "4,999  0        '...a film comes along that manages to be absolu..."
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vaex.from_pandas(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_small = df.iloc[:1000]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<thead>\n",
       "<tr><th>#                              </th><th>label  </th><th>text                                               </th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td><i style='opacity: 0.6'>0</i>  </td><td>0      </td><td>&quot;I think it&#x27;s time John Rambo move on with his l...</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>1</i>  </td><td>1      </td><td>&quot;I&#x27;ve just watch 2 films of Pang brothers, The E...</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>2</i>  </td><td>1      </td><td>&#x27;Jewel Thief is *THE* crime thriller of Bollywoo...</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>3</i>  </td><td>0      </td><td>&#x27;This so called remake is terrible. I went to se...</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>4</i>  </td><td>1      </td><td>&#x27;When Northfork debuted at the Cannes Film Festi...</td></tr>\n",
       "<tr><td>...                            </td><td>...    </td><td>...                                                </td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>995</i></td><td>1      </td><td>&quot;It&#x27;s a funny business, reviewing movies. These ...</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>996</i></td><td>1      </td><td>&#x27;Right from the start you see that &quot;Anchors Awei...</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>997</i></td><td>0      </td><td>&#x27;I saw this movie in NEW York city. I was waitin...</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>998</i></td><td>0      </td><td>&#x27;Firstly, this is NOT an adaptation of a Stephen...</td></tr>\n",
       "<tr><td><i style='opacity: 0.6'>999</i></td><td>1      </td><td>&quot;Barbra Streisand&#x27;s debut television special is ...</td></tr>\n",
       "</tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "#    label    text\n",
       "0    0        \"I think it's time John Rambo move on with his l...\n",
       "1    1        \"I've just watch 2 films of Pang brothers, The E...\n",
       "2    1        'Jewel Thief is *THE* crime thriller of Bollywoo...\n",
       "3    0        'This so called remake is terrible. I went to se...\n",
       "4    1        'When Northfork debuted at the Cannes Film Festi...\n",
       "...  ...      ...\n",
       "995  1        \"It's a funny business, reviewing movies. These ...\n",
       "996  1        'Right from the start you see that \"Anchors Awei...\n",
       "997  0        'I saw this movie in NEW York city. I was waitin...\n",
       "998  0        'Firstly, this is NOT an adaptation of a Stephen...\n",
       "999  1        \"Barbra Streisand's debut television special is ..."
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vaex.from_pandas(df_small)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# df = pd.read_csv(\"../data/test_en.csv\")\n",
    "df = pd.read_excel(\"../data/test_chinese.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "pre_steps = [\n",
    "    \"normalize_unicode\",\n",
    "    \"normalize_acronyms\",\n",
    "    \"normalize_bullet_points\",\n",
    "    \"normalize_hyphenated_words\",\n",
    "    \"normalize_quotation_marks\",\n",
    "    \"normalize_whitespaces\",\n",
    "    \"normalize_repeating_words\",\n",
    "    \"normalize_repeating_chars\",\n",
    "    \"normalize_useless_spaces\",\n",
    "    # \"replace_currency_symbols\",\n",
    "    # \"replace_emails\",\n",
    "    # \"replace_emojis\",\n",
    "    # \"replace_hashtags\",\n",
    "    # \"replace_numbers\",\n",
    "    # \"replace_phone_numbers\",\n",
    "    # \"replace_urls\",\n",
    "    # \"replace_user_handles\",\n",
    "    # \"remove_accents\",\n",
    "    # \"remove_brackets\",\n",
    "    # \"remove_html_tags\",\n",
    "    # \"remove_non_words\",\n",
    "    # \"remove_punctuation\",\n",
    "    # \"lowercase\",\n",
    "    \"strip\",\n",
    "]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "post_steps = [\n",
    "    \"lowercase\",\n",
    "    # \"replace_currency_symbols\",\n",
    "    # \"replace_urls\",\n",
    "    # \"replace_emails\",\n",
    "    # \"replace_user_handles\",\n",
    "    # \"replace_hashtags\",\n",
    "    # \"replace_emojis\",\n",
    "    # \"replace_phone_numbers\",\n",
    "    # \"replace_numbers\",\n",
    "    # \"remove_html_tags\",\n",
    "    # \"remove_accents\",\n",
    "    # \"remove_brackets\",\n",
    "    \"remove_non_words\",\n",
    "    # \"remove_numbers\",\n",
    "    # \"remove_punctuation\",\n",
    "    \"normalize_repeating_words\",\n",
    "    \"normalize_repeating_chars\",\n",
    "    \"normalize_useless_spaces\",\n",
    "    \"strip\",\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "pipe = PreprocessingPipeline(\n",
    "    language=\"Chinese\",\n",
    "    lemmatization_step=\"Spacy lemmatizer (keep stopwords)\", # \"Disable lemmatizer\",\n",
    "    pre_steps=pre_steps,\n",
    "    post_steps=post_steps,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'全金属 指纹识别 垃圾 买手机 不行 指纹识别 不好 太慢 好多 失败 电池 哥哥 一部 华为 mate7 手机 旅游 丢掉 我哥 算是 二手 二手手机 用个 两天 毛 手机 只能 大半天 玩 手机游戏 最多 看个 新闻 微信 不行 急 手机 买手机 谈谈 通话 想 问 一句 手机 通话 保证 畅通 手机 意义 一部 MP4 区别 第一次 通话 五分钟 声音 说 女朋友 手机 朋友 父母 打电话 情况 毛呢 所有人 手机   利用 全金属 吸引 眼球 做工 体验 不好 电池 耐用 通话 易 无声 加油 拿出 诚意'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.text[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'全金属 指纹识别 垃圾 买手机 不行 指纹识别 不好 太慢 好多 失败 电池 哥哥 一部 华为 mate7 手机 旅游 丢掉 我哥 算是 二手 二手手机 用个 两天 毛 手机 只能 大半天 玩 手机游戏 最多 看个 新闻 微信 不行 急 手机 买手机 谈谈 通话 想 问 一句 手机 通话 保证 畅通 手机 意义 一部 MP4 区别 第一次 通话 五分钟 声音 说 女朋友 手机 朋友 父母 打电话 情况 毛呢 所有人 手机 利用 全金属 吸引 眼球 做工 体验 不好 电池 耐用 通话 易 无声 加油 拿出 诚意'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pipe.pre(df.text[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'全金属 指纹识别 垃圾 买手机 不行 指纹识别 不好 太慢 好多 失败 电池 哥哥 一部 华为 mate7 手机 旅游 丢掉 我哥 算是 二手 二手手机 用个 两天 毛 手机 只能 大半天 玩 手机游戏 最多 看个 新闻 微信 不行 急 手机 买手机 谈谈 通话 想 问 一句 手机 通话 保证 畅通 手机 意义 一部 MP4 区别 第一次 通话 五分钟 声音 说 女朋友 手机 朋友 父母 打电话 情况 毛呢 所有人 手机 利用 全金属 吸引 眼球 做工 体验 不好 电池 耐用 通话 易 无声 加油 拿出 诚意'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pipe.lemma(pipe.nlp(pipe.pre(df.text[0])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'全金属 指纹识别 垃圾 买手机 不行 指纹识别 不好 太慢 好多 失败 电池 哥哥 一部 华为 mate 手机 旅游 丢掉 我哥 算是 二手 二手手机 用个 两天 毛 手机 只能 大半天 玩 手机游戏 最多 看个 新闻 微信 不行 急 手机 买手机 谈谈 通话 想 问 一句 手机 通话 保证 畅通 手机 意义 一部 mp 区别 第一次 通话 五分钟 声音 说 女朋友 手机 朋友 父母 打电话 情况 毛呢 所有人 手机 利用 全金属 吸引 眼球 做工 体验 不好 电池 耐用 通话 易 无声 加油 拿出 诚意'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pipe.post(pipe.lemma(pipe.nlp(pipe.pre(df.text[0]))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Compose(<function strip at 0x7ff4894750e0>, <function normalize_useless_spaces at 0x7ff48946eef0>, <function normalize_repeating_chars at 0x7ff48946ef80>, <function normalize_repeating_words at 0x7ff4871a7170>, <function punctuation at 0x7ff48946e4d0>, <function remove_numbers at 0x7ff4894754d0>, <function lowercase at 0x7ff489475050>)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pipe.post"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "odf = pipe.vaex_process(df, \"text\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "odf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "steps = list(PreprocessingPipeline.pipeline_components().keys())\n",
    "default_pre_steps_idx = [steps.index(i) for i in pre_steps]\n",
    "default_post_steps_idx = [steps.index(i) for i in post_steps]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "default_pre_steps_idx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "default_post_steps_idx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sorted(list(PreprocessingPipeline.pipeline_components().keys()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list(PreprocessingPipeline.lemmatization_component().keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "_re_non_words = re.compile(\"[^A-Za-z]+\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "_re_non_words.sub(\" \", \"Mimmo23\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "aa7efd0b3ada76bb0689aa8ed0b61d7de788847e3d11d2d142fc5800c765982f"
  },
  "kernelspec": {
   "display_name": "Python 3.7.11 64-bit ('wordify': conda)",
   "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.7.11"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}