{ "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" ] }, { "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": [], "source": [ "pre_steps = [\n", " \"normalize_unicode\",\n", " \"normalize_acronyms\",\n", " \"normalize_bullet_points\",\n", " \"normalize_hyphenated_words\",\n", " \"normalize_quotation_marks\",\n", " \"normalize_useless_spaces\",\n", " \"normalize_repeating_words\",\n", " \"normalize_repeating_chars\",\n", " \"normalize_whitespaces\",\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": 4, "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": 5, "metadata": {}, "outputs": [], "source": [ "pipe = PreprocessingPipeline(\n", " language=\"English\",\n", " lemmatization_step=\"Spacy lemmatizer (no stopwords)\", # \"Disable lemmatizer\",\n", " pre_steps=pre_steps,\n", " post_steps=post_steps,\n", ")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"I think it's time John Rambo move on with his life and try to put Vietnam behind him. This series is getting old and Rambo is no longer a solider but a cold blooded killer. Ever time he turns up on the screen someone dies. Vietnam was not a fun place to be and frankly I am tired of Hollywood making it seem like it was. This is not the worst of the films concerning Vietnam, that honor goes to John Waynes Green Berets. In any case John Rambo carrying around a 50 cal Machine Gun taking on what seems to be half of the Viet Cong army plus a good many Russians is an insult to watch. What is worse is Rambos cheesy speech at the end.Please!! Oh yeah I heard they are making another one.\"" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pipe.pre(df.text[0])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'think time John Rambo life try Vietnam . series get old Rambo long solider cold blooded killer . time turn screen die . Vietnam fun place frankly tired Hollywood make like . bad film concern Vietnam , honor go John Waynes Green Berets . case John Rambo carry 50 cal Machine Gun take half Viet Cong army plus good Russians insult watch . bad Rambos cheesy speech end . ! ! oh yeah hear make .'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pipe.lemma(pipe.nlp(pipe.pre(df.text[0])))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'think time john rambo life try vietnam series get old rambo long solider cold blooded killer time turn screen die vietnam fun place frankly tired hollywood make like bad film concern vietnam honor go john waynes green berets case john rambo carry cal machine gun take half viet cong army plus good russians insult watch bad rambos cheesy speech end oh yeah hear make'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pipe.post(pipe.lemma(pipe.nlp(pipe.pre(df.text[0]))))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "odf = pipe.vaex_process(df, \"text\")" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
labeltextprocessed_text
00I think it's time John Rambo move on with his ...think time john rambo life try vietnam series ...
11I've just watch 2 films of Pang brothers, The ...watch film pang brother eye watch eye kind dis...
21Jewel Thief is *THE* crime thriller of Bollywo...jewel thief crime thriller bollywood direct bi...
30This so called remake is terrible. I went to s...call remake terrible go tonight day anticipati...
41When Northfork debuted at the Cannes Film Fest...northfork debut cannes film festival people li...
............
49950The title tells it all -- Ed Gein, the butcher...title tell ed gein butcher plainfield it zappy...
49960This film makes about as much sense as an 'Ozz...film make sense ozzie harriet father know best...
49970\"Sex and the City\" has some great things going...sex city great thing go problem saddle number ...
49980Please...if anybody gets the chance to read th...please if anybody get chance read watch movie ...
49990...a film comes along that manages to be absol...a film come manage absolutely terrible open ti...
\n", "

5000 rows × 3 columns

\n", "
" ], "text/plain": [ " label text \\\n", "0 0 I think it's time John Rambo move on with his ... \n", "1 1 I've just watch 2 films of Pang brothers, The ... \n", "2 1 Jewel Thief is *THE* crime thriller of Bollywo... \n", "3 0 This so called remake is terrible. I went to s... \n", "4 1 When Northfork debuted at the Cannes Film Fest... \n", "... ... ... \n", "4995 0 The title tells it all -- Ed Gein, the butcher... \n", "4996 0 This film makes about as much sense as an 'Ozz... \n", "4997 0 \"Sex and the City\" has some great things going... \n", "4998 0 Please...if anybody gets the chance to read th... \n", "4999 0 ...a film comes along that manages to be absol... \n", "\n", " processed_text \n", "0 think time john rambo life try vietnam series ... \n", "1 watch film pang brother eye watch eye kind dis... \n", "2 jewel thief crime thriller bollywood direct bi... \n", "3 call remake terrible go tonight day anticipati... \n", "4 northfork debut cannes film festival people li... \n", "... ... \n", "4995 title tell ed gein butcher plainfield it zappy... \n", "4996 film make sense ozzie harriet father know best... \n", "4997 sex city great thing go problem saddle number ... \n", "4998 please if anybody get chance read watch movie ... \n", "4999 a film come manage absolutely terrible open ti... \n", "\n", "[5000 rows x 3 columns]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "odf" ] }, { "cell_type": "code", "execution_count": 15, "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": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 14, 2, 3, 4, 21, 23, 22, 5, 24]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "default_pre_steps_idx" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 17, 15, 19, 23, 22, 21, 24]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "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": 14, "metadata": {}, "outputs": [], "source": [ "import re" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "_re_non_words = re.compile(\"[^A-Za-z]+\")" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Mimmo '" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "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 }