{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "dc83b044", "metadata": { "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19", "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5", "execution": { "iopub.execute_input": "2025-07-31T07:30:53.259401Z", "iopub.status.busy": "2025-07-31T07:30:53.259098Z", "iopub.status.idle": "2025-07-31T07:30:56.207017Z", "shell.execute_reply": "2025-07-31T07:30:56.206404Z" }, "papermill": { "duration": 2.954748, "end_time": "2025-07-31T07:30:56.208452", "exception": false, "start_time": "2025-07-31T07:30:53.253704", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "import os, re, warnings\n", "\n", "warnings.filterwarnings('ignore')" ] }, { "cell_type": "code", "execution_count": 2, "id": "3fef7e91", "metadata": { "execution": { "iopub.execute_input": "2025-07-31T07:30:56.217100Z", "iopub.status.busy": "2025-07-31T07:30:56.216792Z", "iopub.status.idle": "2025-07-31T07:30:56.326729Z", "shell.execute_reply": "2025-07-31T07:30:56.325825Z" }, "papermill": { "duration": 0.115872, "end_time": "2025-07-31T07:30:56.328285", "exception": false, "start_time": "2025-07-31T07:30:56.212413", "status": "completed" }, "tags": [] }, "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", "
Id_NumberPatient QuestionDistorted partDominant DistortionSecondary Distortion (Optional)
04500Hello, I have a beautiful,smart,outgoing and a...The voice are always fimilar (someone she know...PersonalizationNaN
14501Since I was about 16 years old I’ve had these ...I feel trapped inside my disgusting self and l...LabelingEmotional Reasoning
24502So I’ve been dating on and off this guy for a...NaNNo DistortionNaN
34503My parents got divorced in 2004. My mother has...NaNNo DistortionNaN
44504I don’t really know how to explain the situati...I refused to go because I didn’t know if it wa...Fortune-tellingEmotional Reasoning
\n", "
" ], "text/plain": [ " Id_Number Patient Question \\\n", "0 4500 Hello, I have a beautiful,smart,outgoing and a... \n", "1 4501 Since I was about 16 years old I’ve had these ... \n", "2 4502 So I’ve been dating on and off this guy for a... \n", "3 4503 My parents got divorced in 2004. My mother has... \n", "4 4504 I don’t really know how to explain the situati... \n", "\n", " Distorted part Dominant Distortion \\\n", "0 The voice are always fimilar (someone she know... Personalization \n", "1 I feel trapped inside my disgusting self and l... Labeling \n", "2 NaN No Distortion \n", "3 NaN No Distortion \n", "4 I refused to go because I didn’t know if it wa... Fortune-telling \n", "\n", " Secondary Distortion (Optional) \n", "0 NaN \n", "1 Emotional Reasoning \n", "2 NaN \n", "3 NaN \n", "4 Emotional Reasoning " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv('/kaggle/input/dis-full-speech/distortion.csv')\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 3, "id": "feae1753", "metadata": { "execution": { "iopub.execute_input": "2025-07-31T07:30:56.338782Z", "iopub.status.busy": "2025-07-31T07:30:56.338552Z", "iopub.status.idle": "2025-07-31T07:30:56.353997Z", "shell.execute_reply": "2025-07-31T07:30:56.353278Z" }, "papermill": { "duration": 0.02184, "end_time": "2025-07-31T07:30:56.355138", "exception": false, "start_time": "2025-07-31T07:30:56.333298", "status": "completed" }, "tags": [] }, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Id_NumberPatient QuestionDistorted partDominant DistortionSecondary Distortion (Optional)
04500Hello, I have a beautiful,smart,outgoing and a...The voice are always fimilar (someone she know...PersonalizationNaN
14501Since I was about 16 years old I’ve had these ...I feel trapped inside my disgusting self and l...LabelingEmotional Reasoning
44504I don’t really know how to explain the situati...I refused to go because I didn’t know if it wa...Fortune-tellingEmotional Reasoning
94510I have been with my fiancé for two years now....I felt like the response was totally irrationa...MagnificationNaN
104511My husband and I have been married for over a ...I thought that he displayed traits of honor, l...LabelingNaN
..................
25192553I feel overprotective about my mother because ...Since then, whenever my mother is out alone, I...All-or-nothing thinkingOvergeneralization
25222557From Lebanon: I am dealing with a big problem!...My family hate him but they didn’t met him at ...MagnificationNaN
25232559From the U.S.: I am a junior in high school, a...However, I am not happy, at the least only hal...LabelingAll-or-nothing thinking
25262563I am 21 female and have not had any friends fo...Now I am at university my peers around me all ...OvergeneralizationNaN
25272564From the U.S.: My brother is 19 years old and ...He claims he’s severely depressed and has outb...Mental filterMind Reading
\n", "

1597 rows × 5 columns

\n", "
" ], "text/plain": [ " Id_Number Patient Question \\\n", "0 4500 Hello, I have a beautiful,smart,outgoing and a... \n", "1 4501 Since I was about 16 years old I’ve had these ... \n", "4 4504 I don’t really know how to explain the situati... \n", "9 4510 I have been with my fiancé for two years now.... \n", "10 4511 My husband and I have been married for over a ... \n", "... ... ... \n", "2519 2553 I feel overprotective about my mother because ... \n", "2522 2557 From Lebanon: I am dealing with a big problem!... \n", "2523 2559 From the U.S.: I am a junior in high school, a... \n", "2526 2563 I am 21 female and have not had any friends fo... \n", "2527 2564 From the U.S.: My brother is 19 years old and ... \n", "\n", " Distorted part \\\n", "0 The voice are always fimilar (someone she know... \n", "1 I feel trapped inside my disgusting self and l... \n", "4 I refused to go because I didn’t know if it wa... \n", "9 I felt like the response was totally irrationa... \n", "10 I thought that he displayed traits of honor, l... \n", "... ... \n", "2519 Since then, whenever my mother is out alone, I... \n", "2522 My family hate him but they didn’t met him at ... \n", "2523 However, I am not happy, at the least only hal... \n", "2526 Now I am at university my peers around me all ... \n", "2527 He claims he’s severely depressed and has outb... \n", "\n", " Dominant Distortion Secondary Distortion (Optional) \n", "0 Personalization NaN \n", "1 Labeling Emotional Reasoning \n", "4 Fortune-telling Emotional Reasoning \n", "9 Magnification NaN \n", "10 Labeling NaN \n", "... ... ... \n", "2519 All-or-nothing thinking Overgeneralization \n", "2522 Magnification NaN \n", "2523 Labeling All-or-nothing thinking \n", "2526 Overgeneralization NaN \n", "2527 Mental filter Mind Reading \n", "\n", "[1597 rows x 5 columns]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = df[df['Dominant Distortion'] != 'No Distortion']\n", "df" ] }, { "cell_type": "code", "execution_count": 4, "id": "e14bfea0", "metadata": { "execution": { "iopub.execute_input": "2025-07-31T07:30:56.364603Z", "iopub.status.busy": "2025-07-31T07:30:56.364387Z", "iopub.status.idle": "2025-07-31T07:30:56.374436Z", "shell.execute_reply": "2025-07-31T07:30:56.373550Z" }, "papermill": { "duration": 0.016193, "end_time": "2025-07-31T07:30:56.375717", "exception": false, "start_time": "2025-07-31T07:30:56.359524", "status": "completed" }, "tags": [] }, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Id_NumberPatient QuestionDistorted partDominant DistortionSecondary Distortion (Optional)
04500Hello, I have a beautiful,smart,outgoing and a...The voice are always fimilar (someone she know...PersonalizationNaN
14501Since I was about 16 years old I’ve had these ...I feel trapped inside my disgusting self and l...LabelingEmotional Reasoning
44504I don’t really know how to explain the situati...I refused to go because I didn’t know if it wa...Fortune-tellingEmotional Reasoning
94510I have been with my fiancé for two years now....I felt like the response was totally irrationa...MagnificationNaN
104511My husband and I have been married for over a ...I thought that he displayed traits of honor, l...LabelingNaN
..................
25192553I feel overprotective about my mother because ...Since then, whenever my mother is out alone, I...All-or-nothing thinkingOvergeneralization
25222557From Lebanon: I am dealing with a big problem!...My family hate him but they didn’t met him at ...MagnificationNaN
25232559From the U.S.: I am a junior in high school, a...However, I am not happy, at the least only hal...LabelingAll-or-nothing thinking
25262563I am 21 female and have not had any friends fo...Now I am at university my peers around me all ...OvergeneralizationNaN
25272564From the U.S.: My brother is 19 years old and ...He claims he’s severely depressed and has outb...Mental filterMind Reading
\n", "

1597 rows × 5 columns

\n", "
" ], "text/plain": [ " Id_Number Patient Question \\\n", "0 4500 Hello, I have a beautiful,smart,outgoing and a... \n", "1 4501 Since I was about 16 years old I’ve had these ... \n", "4 4504 I don’t really know how to explain the situati... \n", "9 4510 I have been with my fiancé for two years now.... \n", "10 4511 My husband and I have been married for over a ... \n", "... ... ... \n", "2519 2553 I feel overprotective about my mother because ... \n", "2522 2557 From Lebanon: I am dealing with a big problem!... \n", "2523 2559 From the U.S.: I am a junior in high school, a... \n", "2526 2563 I am 21 female and have not had any friends fo... \n", "2527 2564 From the U.S.: My brother is 19 years old and ... \n", "\n", " Distorted part \\\n", "0 The voice are always fimilar (someone she know... \n", "1 I feel trapped inside my disgusting self and l... \n", "4 I refused to go because I didn’t know if it wa... \n", "9 I felt like the response was totally irrationa... \n", "10 I thought that he displayed traits of honor, l... \n", "... ... \n", "2519 Since then, whenever my mother is out alone, I... \n", "2522 My family hate him but they didn’t met him at ... \n", "2523 However, I am not happy, at the least only hal... \n", "2526 Now I am at university my peers around me all ... \n", "2527 He claims he’s severely depressed and has outb... \n", "\n", " Dominant Distortion Secondary Distortion (Optional) \n", "0 Personalization NaN \n", "1 Labeling Emotional Reasoning \n", "4 Fortune-telling Emotional Reasoning \n", "9 Magnification NaN \n", "10 Labeling NaN \n", "... ... ... \n", "2519 All-or-nothing thinking Overgeneralization \n", "2522 Magnification NaN \n", "2523 Labeling All-or-nothing thinking \n", "2526 Overgeneralization NaN \n", "2527 Mental filter Mind Reading \n", "\n", "[1597 rows x 5 columns]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df" ] }, { "cell_type": "code", "execution_count": 5, "id": "89f5af91", "metadata": { "execution": { "iopub.execute_input": "2025-07-31T07:30:56.385670Z", "iopub.status.busy": "2025-07-31T07:30:56.385416Z", "iopub.status.idle": "2025-07-31T07:30:57.404255Z", "shell.execute_reply": "2025-07-31T07:30:57.403257Z" }, "papermill": { "duration": 1.025376, "end_time": "2025-07-31T07:30:57.405599", "exception": false, "start_time": "2025-07-31T07:30:56.380223", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "i luv programmng u r gr8 i cant believe its 123 times better\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "[nltk_data] Downloading package punkt to /usr/share/nltk_data...\n", "[nltk_data] Package punkt is already up-to-date!\n", "[nltk_data] Downloading package stopwords to /usr/share/nltk_data...\n", "[nltk_data] Package stopwords is already up-to-date!\n", "[nltk_data] Downloading package wordnet to /usr/share/nltk_data...\n", "[nltk_data] Package wordnet is already up-to-date!\n" ] } ], "source": [ "# !pip install spellchecker\n", "import re\n", "import string\n", "import nltk\n", "from nltk.tokenize import word_tokenize\n", "from nltk.corpus import stopwords\n", "from nltk.stem import PorterStemmer, WordNetLemmatizer\n", "# from spellchecker import SpellChecker\n", "\n", "# Tải các tài nguyên cần thiết\n", "nltk.download(\"punkt\")\n", "nltk.download(\"stopwords\")\n", "nltk.download(\"wordnet\")\n", "\n", "# Khởi tạo các công cụ\n", "stemmer = PorterStemmer()\n", "lemmatizer = WordNetLemmatizer()\n", "# spell = SpellChecker()\n", "stop_words = set(stopwords.words(\"english\"))\n", "\n", "def preprocess_text(text, use_stemming=False, use_lemmatization=True, correct_spelling=False):\n", " text = text.lower()\n", " \n", " text = text.translate(str.maketrans(\"\", \"\", string.punctuation))\n", " \n", " # tokens = word_tokenize(text)\n", " \n", " # tokens = [word for word in tokens if word not in stop_words]\n", " \n", " # if use_stemming:\n", " # tokens = [stemmer.stem(word) for word in tokens]\n", " # elif use_lemmatization:\n", " # tokens = [lemmatizer.lemmatize(word) for word in tokens]\n", " \n", " # 7. Xóa khoảng trắng dư thừa (không cần vì token đã tách sẵn)\n", " \n", " # 8. Chuyển đổi số thành \"NUM\"\n", " # tokens = [\"NUM\" if word.isdigit() else word for word in tokens]\n", " \n", " # 9. Xử lý lỗi chính tả\n", " # if correct_spelling:\n", " # tokens = [spell.correction(word) if word not in spell else word for word in tokens]\n", " \n", " return text#\" \".join(text)\n", "\n", "# Ví dụ sử dụng\n", "text = \"I luv programmng! U r gr8. I can't believe it's 123 times better!\"\n", "processed_text = preprocess_text(text)\n", "print(processed_text)" ] }, { "cell_type": "code", "execution_count": 6, "id": "495f293b", "metadata": { "execution": { "iopub.execute_input": "2025-07-31T07:30:57.414915Z", "iopub.status.busy": "2025-07-31T07:30:57.414710Z", "iopub.status.idle": "2025-07-31T07:30:57.444182Z", "shell.execute_reply": "2025-07-31T07:30:57.443553Z" }, "papermill": { "duration": 0.035306, "end_time": "2025-07-31T07:30:57.445277", "exception": false, "start_time": "2025-07-31T07:30:57.409971", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "df['cleaned'] = df['Distorted part'].map(lambda text: preprocess_text(text))" ] }, { "cell_type": "code", "execution_count": 7, "id": "469d9017", "metadata": { "execution": { "iopub.execute_input": "2025-07-31T07:30:57.454280Z", "iopub.status.busy": "2025-07-31T07:30:57.454049Z", "iopub.status.idle": "2025-07-31T07:30:57.463547Z", "shell.execute_reply": "2025-07-31T07:30:57.462800Z" }, "papermill": { "duration": 0.015149, "end_time": "2025-07-31T07:30:57.464573", "exception": false, "start_time": "2025-07-31T07:30:57.449424", "status": "completed" }, "tags": [] }, "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", " \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", "
Id_NumberPatient QuestionDistorted partDominant DistortionSecondary Distortion (Optional)cleaned
04500Hello, I have a beautiful,smart,outgoing and a...The voice are always fimilar (someone she know...PersonalizationNaNthe voice are always fimilar someone she knows...
14501Since I was about 16 years old I’ve had these ...I feel trapped inside my disgusting self and l...LabelingEmotional Reasoningi feel trapped inside my disgusting self and l...
44504I don’t really know how to explain the situati...I refused to go because I didn’t know if it wa...Fortune-tellingEmotional Reasoningi refused to go because i didn’t know if it wa...
94510I have been with my fiancé for two years now....I felt like the response was totally irrationa...MagnificationNaNi felt like the response was totally irrationa...
104511My husband and I have been married for over a ...I thought that he displayed traits of honor, l...LabelingNaNi thought that he displayed traits of honor lo...
.....................
25192553I feel overprotective about my mother because ...Since then, whenever my mother is out alone, I...All-or-nothing thinkingOvergeneralizationsince then whenever my mother is out alone i b...
25222557From Lebanon: I am dealing with a big problem!...My family hate him but they didn’t met him at ...MagnificationNaNmy family hate him but they didn’t met him at ...
25232559From the U.S.: I am a junior in high school, a...However, I am not happy, at the least only hal...LabelingAll-or-nothing thinkinghowever i am not happy at the least only half ...
25262563I am 21 female and have not had any friends fo...Now I am at university my peers around me all ...OvergeneralizationNaNnow i am at university my peers around me all ...
25272564From the U.S.: My brother is 19 years old and ...He claims he’s severely depressed and has outb...Mental filterMind Readinghe claims he’s severely depressed and has outb...
\n", "

1597 rows × 6 columns

\n", "
" ], "text/plain": [ " Id_Number Patient Question \\\n", "0 4500 Hello, I have a beautiful,smart,outgoing and a... \n", "1 4501 Since I was about 16 years old I’ve had these ... \n", "4 4504 I don’t really know how to explain the situati... \n", "9 4510 I have been with my fiancé for two years now.... \n", "10 4511 My husband and I have been married for over a ... \n", "... ... ... \n", "2519 2553 I feel overprotective about my mother because ... \n", "2522 2557 From Lebanon: I am dealing with a big problem!... \n", "2523 2559 From the U.S.: I am a junior in high school, a... \n", "2526 2563 I am 21 female and have not had any friends fo... \n", "2527 2564 From the U.S.: My brother is 19 years old and ... \n", "\n", " Distorted part \\\n", "0 The voice are always fimilar (someone she know... \n", "1 I feel trapped inside my disgusting self and l... \n", "4 I refused to go because I didn’t know if it wa... \n", "9 I felt like the response was totally irrationa... \n", "10 I thought that he displayed traits of honor, l... \n", "... ... \n", "2519 Since then, whenever my mother is out alone, I... \n", "2522 My family hate him but they didn’t met him at ... \n", "2523 However, I am not happy, at the least only hal... \n", "2526 Now I am at university my peers around me all ... \n", "2527 He claims he’s severely depressed and has outb... \n", "\n", " Dominant Distortion Secondary Distortion (Optional) \\\n", "0 Personalization NaN \n", "1 Labeling Emotional Reasoning \n", "4 Fortune-telling Emotional Reasoning \n", "9 Magnification NaN \n", "10 Labeling NaN \n", "... ... ... \n", "2519 All-or-nothing thinking Overgeneralization \n", "2522 Magnification NaN \n", "2523 Labeling All-or-nothing thinking \n", "2526 Overgeneralization NaN \n", "2527 Mental filter Mind Reading \n", "\n", " cleaned \n", "0 the voice are always fimilar someone she knows... \n", "1 i feel trapped inside my disgusting self and l... \n", "4 i refused to go because i didn’t know if it wa... \n", "9 i felt like the response was totally irrationa... \n", "10 i thought that he displayed traits of honor lo... \n", "... ... \n", "2519 since then whenever my mother is out alone i b... \n", "2522 my family hate him but they didn’t met him at ... \n", "2523 however i am not happy at the least only half ... \n", "2526 now i am at university my peers around me all ... \n", "2527 he claims he’s severely depressed and has outb... \n", "\n", "[1597 rows x 6 columns]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df" ] }, { "cell_type": "code", "execution_count": 8, "id": "c00caea9", "metadata": { "execution": { "iopub.execute_input": "2025-07-31T07:30:57.473744Z", "iopub.status.busy": "2025-07-31T07:30:57.473559Z", "iopub.status.idle": "2025-07-31T07:30:57.478796Z", "shell.execute_reply": "2025-07-31T07:30:57.478100Z" }, "papermill": { "duration": 0.010926, "end_time": "2025-07-31T07:30:57.479770", "exception": false, "start_time": "2025-07-31T07:30:57.468844", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'All-or-nothing thinking': 0, 'Emotional Reasoning': 1, 'Fortune-telling': 2, 'Labeling': 3, 'Magnification': 4, 'Mental filter': 5, 'Mind Reading': 6, 'Overgeneralization': 7, 'Personalization': 8, 'Should statements': 9}\n" ] } ], "source": [ "from sklearn.preprocessing import LabelEncoder\n", "\n", "label_encoder = LabelEncoder()\n", "df['labelencode'] = label_encoder.fit_transform(df['Dominant Distortion'])\n", "\n", "label_mapping = dict(zip(label_encoder.classes_, label_encoder.transform(label_encoder.classes_)))\n", "print(label_mapping)" ] }, { "cell_type": "code", "execution_count": 9, "id": "de6f3e92", "metadata": { "execution": { "iopub.execute_input": "2025-07-31T07:30:57.488928Z", "iopub.status.busy": "2025-07-31T07:30:57.488730Z", "iopub.status.idle": "2025-07-31T07:30:57.493429Z", "shell.execute_reply": "2025-07-31T07:30:57.492815Z" }, "papermill": { "duration": 0.010412, "end_time": "2025-07-31T07:30:57.494441", "exception": false, "start_time": "2025-07-31T07:30:57.484029", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", "\n", "X_train, X_test, y_train, y_test = train_test_split(df['cleaned'].values,\n", " df['labelencode'].values,\n", " test_size=0.2, random_state=42)" ] }, { "cell_type": "code", "execution_count": 10, "id": "c6925af3", "metadata": { "execution": { "iopub.execute_input": "2025-07-31T07:30:57.503620Z", "iopub.status.busy": "2025-07-31T07:30:57.503407Z", "iopub.status.idle": "2025-07-31T07:31:22.008709Z", "shell.execute_reply": "2025-07-31T07:31:22.008043Z" }, "papermill": { "duration": 24.5114, "end_time": "2025-07-31T07:31:22.010032", "exception": false, "start_time": "2025-07-31T07:30:57.498632", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2025-07-31 07:31:11.326196: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:477] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n", "WARNING: All log messages before absl::InitializeLog() is called are written to STDERR\n", "E0000 00:00:1753947071.514409 19 cuda_dnn.cc:8310] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n", "E0000 00:00:1753947071.569002 19 cuda_blas.cc:1418] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n" ] } ], "source": [ "import torch\n", "from torch.optim import AdamW \n", "from torch.utils.data import Dataset, DataLoader\n", "from transformers import RobertaTokenizer, RobertaForSequenceClassification\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.metrics import accuracy_score\n", "from transformers import AutoTokenizer, AutoModel" ] }, { "cell_type": "code", "execution_count": 11, "id": "dcc4e584", "metadata": { "execution": { "iopub.execute_input": "2025-07-31T07:31:22.020632Z", "iopub.status.busy": "2025-07-31T07:31:22.020134Z", "iopub.status.idle": "2025-07-31T07:31:22.023685Z", "shell.execute_reply": "2025-07-31T07:31:22.022994Z" }, "papermill": { "duration": 0.009946, "end_time": "2025-07-31T07:31:22.024751", "exception": false, "start_time": "2025-07-31T07:31:22.014805", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "# from transformers import AutoTokenizer\n", "\n", "# tokenizer = AutoTokenizer.from_pretrained('roberta-large')\n", "\n", "\n", "# df[\"body_len\"] = df[\"cleaned\"].apply(lambda x: len(tokenizer.encode(x, truncation=False)))\n", "\n", "# avg_summary_len = df[\"body_len\"].mean()\n", "\n", "# print(f\"Độ dài token trung bình của summary: {avg_summary_len:.2f}\")\n" ] }, { "cell_type": "code", "execution_count": 12, "id": "eac3c141", "metadata": { "execution": { "iopub.execute_input": "2025-07-31T07:31:22.034353Z", "iopub.status.busy": "2025-07-31T07:31:22.034101Z", "iopub.status.idle": "2025-07-31T07:31:22.037644Z", "shell.execute_reply": "2025-07-31T07:31:22.037025Z" }, "papermill": { "duration": 0.009443, "end_time": "2025-07-31T07:31:22.038713", "exception": false, "start_time": "2025-07-31T07:31:22.029270", "status": "completed" }, "tags": [] }, "outputs": [], "source": [ "# from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score\n", "# def calculate_results(y_true, y_pred, y_pred_proba=None):\n", "# \"\"\"\n", "# Tính toán các chỉ số đánh giá, bao gồm accuracy, precision, recall, f1 và roc-auc.\n", "# Nếu `y_pred_proba` được cung cấp, tính thêm ROC-AUC cho từng nhãn trong bài toán đa nhãn.\n", "# \"\"\"\n", "# results = {\n", "# \"accuracy\": accuracy_score(y_true, y_pred) * 100,\n", "# \"precision\": precision_score(y_true, y_pred, average='weighted'),\n", "# \"recall\": recall_score(y_true, y_pred, average='weighted'),\n", "# \"f1\": f1_score(y_true, y_pred, average='weighted')\n", "# }\n", " \n", "# # if y_pred_proba is not None:\n", "# # # Chuyển đổi nhãn thành dạng nhị phân\n", "# # y_true_bin = label_binarize(y_true, classes=np.unique(y_true))\n", " \n", "# # # Tính ROC-AUC cho từng nhãn (macro-average)\n", "# # roc_auc_macro = []\n", "# # for i in range(y_true_bin.shape[1]):\n", "# # roc_auc = roc_auc_score(y_true_bin[:, i], y_pred_proba[:, i])\n", "# # roc_auc_macro.append(roc_auc)\n", " \n", "# # # Tính ROC-AUC macro trung bình\n", "# # results[\"roc_auc_macro\"] = np.mean(roc_auc_macro)\n", " \n", "# # # Tính ROC-AUC micro-average\n", "# # roc_auc_micro = roc_auc_score(y_true_bin, y_pred_proba, average=\"micro\")\n", "# # results[\"roc_auc_micro\"] = roc_auc_micro\n", " \n", "# return results" ] }, { "cell_type": "code", "execution_count": 13, "id": "a734a14b", "metadata": { "execution": { "iopub.execute_input": "2025-07-31T07:31:22.047692Z", "iopub.status.busy": "2025-07-31T07:31:22.047504Z", "iopub.status.idle": "2025-07-31T07:31:23.970642Z", "shell.execute_reply": "2025-07-31T07:31:23.969612Z" }, "papermill": { "duration": 1.929215, "end_time": "2025-07-31T07:31:23.972033", "exception": false, "start_time": "2025-07-31T07:31:22.042818", "status": "completed" }, "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "596be61ac3f34a568ab29218295be87d", "version_major": 2, "version_minor": 0 }, "text/plain": [ "tokenizer_config.json: 0%| | 0.00/25.0 [00:00