{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Import Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport spacy\nimport re\nimport nltk\nfrom nltk.corpus import stopwords\nfrom nltk.tokenize import word_tokenize\nfrom string import punctuation\nfrom nltk.stem import WordNetLemmatizer\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.feature_extraction.text import TfidfVectorizer\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.svm import LinearSVC\nfrom sklearn.naive_bayes import MultinomialNB\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.metrics import confusion_matrix, classification_report","metadata":{"execution":{"iopub.status.busy":"2023-05-11T15:35:32.153449Z","iopub.execute_input":"2023-05-11T15:35:32.153974Z","iopub.status.idle":"2023-05-11T15:35:48.163140Z","shell.execute_reply.started":"2023-05-11T15:35:32.153866Z","shell.execute_reply":"2023-05-11T15:35:48.161602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Reading the training data**","metadata":{}},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/goodreads-books-reviews-290312/goodreads_train.csv')\nprint('train set: {0}'.format(df_train.shape))\ndf_train.head()","metadata":{"id":"Z7X0yzMzvyV8","outputId":"581fc7ac-f985-45ce-c6b1-2d50c9aae102","execution":{"iopub.status.busy":"2023-05-11T15:35:48.165844Z","iopub.execute_input":"2023-05-11T15:35:48.166890Z","iopub.status.idle":"2023-05-11T15:36:13.205628Z","shell.execute_reply.started":"2023-05-11T15:35:48.166834Z","shell.execute_reply":"2023-05-11T15:36:13.204277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Information about the data\ndf_train.info()","metadata":{"execution":{"iopub.status.busy":"2023-05-11T15:36:13.207222Z","iopub.execute_input":"2023-05-11T15:36:13.207654Z","iopub.status.idle":"2023-05-11T15:36:13.623131Z","shell.execute_reply.started":"2023-05-11T15:36:13.207617Z","shell.execute_reply":"2023-05-11T15:36:13.621727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Display the number of each rating**","metadata":{}},{"cell_type":"code","source":"sns.countplot(x=df_train['rating']);","metadata":{"execution":{"iopub.status.busy":"2023-05-11T15:36:13.626294Z","iopub.execute_input":"2023-05-11T15:36:13.626727Z","iopub.status.idle":"2023-05-11T15:36:13.962713Z","shell.execute_reply.started":"2023-05-11T15:36:13.626689Z","shell.execute_reply":"2023-05-11T15:36:13.961302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Tetx preprocessing","metadata":{}},{"cell_type":"code","source":"# Modify the form of some words\nreplace_list = {r\"'m\": ' am',\n                r\"'re\": ' are',\n                r\"let’s\": 'let us',\n                r\"'s\":  ' is',\n                r\"'ve\": ' have',\n                r\"can't\": 'can not',\n                r\"cannot\": 'can not',\n                r\"shan’t\": 'shall not',\n                r\"n't\": ' not',\n                r\"'d\": ' would',\n                r\"'ll\": ' will',\n                r\"'scuse\": 'excuse',\n                ',': ' ,',\n                '.': ' .',\n                '!': ' !',\n                '?': ' ?',\n                '\\s+': ' '}\ndef clean_text(text):\n    text = text.lower()\n    for s in replace_list:\n        text = text.replace(s, replace_list[s])\n    text = ' '.join(text.split())\n    return text","metadata":{"id":"5_PwKParv71N","execution":{"iopub.status.busy":"2023-05-11T15:36:13.964396Z","iopub.execute_input":"2023-05-11T15:36:13.964947Z","iopub.status.idle":"2023-05-11T15:36:13.975157Z","shell.execute_reply.started":"2023-05-11T15:36:13.964896Z","shell.execute_reply":"2023-05-11T15:36:13.973305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Apply the clean_text function\ndf_train['review_text'] = df_train['review_text'].apply(lambda p: clean_text(p))\ny = df_train['rating']","metadata":{"id":"jDnOY4aOv9dU","execution":{"iopub.status.busy":"2023-05-11T15:36:13.977252Z","iopub.execute_input":"2023-05-11T15:36:13.977827Z","iopub.status.idle":"2023-05-11T15:36:56.921230Z","shell.execute_reply.started":"2023-05-11T15:36:13.977774Z","shell.execute_reply":"2023-05-11T15:36:56.919754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nlp = spacy.load('en_core_web_sm')\n\n# We will delete 'not' from stopwords because in sometimes it affects on meaning\nnlp.Defaults.stop_words.remove('not')\nnlp.vocab['not'].is_stop = False","metadata":{"execution":{"iopub.status.busy":"2023-05-11T15:36:56.922580Z","iopub.execute_input":"2023-05-11T15:36:56.922965Z","iopub.status.idle":"2023-05-11T15:36:58.078287Z","shell.execute_reply.started":"2023-05-11T15:36:56.922931Z","shell.execute_reply":"2023-05-11T15:36:58.076727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stop_words = set(stopwords.words('english'))\npunctuation = list(punctuation)\n\nlemmatizer = WordNetLemmatizer()","metadata":{"execution":{"iopub.status.busy":"2023-05-11T15:36:58.080334Z","iopub.execute_input":"2023-05-11T15:36:58.080857Z","iopub.status.idle":"2023-05-11T15:36:58.094584Z","shell.execute_reply.started":"2023-05-11T15:36:58.080807Z","shell.execute_reply":"2023-05-11T15:36:58.093167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Cleaning our text data**","metadata":{}},{"cell_type":"code","source":"def remove_stop_words(document):\n    # Remove all the special characters\n    document = re.sub(r'\\W', ' ', str(document))\n\n    # remove all single characters\n    document = re.sub(r'\\s+[a-zA-Z]\\s+', ' ', document)\n\n    # Remove single characters from the start\n    document = re.sub(r'\\^[a-zA-Z]\\s+', ' ', document) \n\n    # Remove single digit \n    document = re.sub(r'\\s*\\d\\s+', '', document)\n\n    # Tokenizer, Stemmer and removing stop_words\n    document = word_tokenize(document)\n        \n    document = [lemmatizer.lemmatize(word, pos='v') for word in document if word not in stop_words and word not in punctuation]\n\n    document = ' '.join(document)\n        \n    return document","metadata":{"execution":{"iopub.status.busy":"2023-05-11T15:36:58.096213Z","iopub.execute_input":"2023-05-11T15:36:58.096641Z","iopub.status.idle":"2023-05-11T15:36:58.105096Z","shell.execute_reply.started":"2023-05-11T15:36:58.096595Z","shell.execute_reply":"2023-05-11T15:36:58.103658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Apply the remove_stop_words function\ndf_train['review_text'] = df_train['review_text'].apply(lambda x: remove_stop_words(x))","metadata":{"execution":{"iopub.status.busy":"2023-05-11T15:36:58.110764Z","iopub.execute_input":"2023-05-11T15:36:58.111220Z","iopub.status.idle":"2023-05-11T15:59:11.258095Z","shell.execute_reply.started":"2023-05-11T15:36:58.111183Z","shell.execute_reply":"2023-05-11T15:59:11.256284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Example on our data cleaned\ndf_train['review_text'][818021]","metadata":{"execution":{"iopub.status.busy":"2023-05-11T15:59:11.260164Z","iopub.execute_input":"2023-05-11T15:59:11.260616Z","iopub.status.idle":"2023-05-11T15:59:11.269921Z","shell.execute_reply.started":"2023-05-11T15:59:11.260579Z","shell.execute_reply":"2023-05-11T15:59:11.268377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Our data is not balancing and when we create ML model the percentage is not accurate, so we will use resample from sklearn to do balancing for our data**","metadata":{}},{"cell_type":"code","source":"from sklearn.utils import resample\n#majority = max(df_train['rating'].value_counts())\nmajority = 1000000\ndf_0 = df_train[(df_train['rating']==0)] \ndf_1 = df_train[(df_train['rating']==1)] \ndf_2 = df_train[(df_train['rating']==2)] \ndf_3 = df_train[(df_train['rating']==3)]\ndf_4 = df_train[(df_train['rating']==4)]\ndf_5 = df_train[(df_train['rating']==5)] \n\ndf_0_upsampled = resample(df_0, replace=True, n_samples= majority, random_state=44) \ndf_1_upsampled = resample(df_1, replace=True, n_samples= majority, random_state=44) \ndf_2_upsampled = resample(df_2, replace=True, n_samples= majority, random_state=44) \ndf_3_upsampled = resample(df_3, replace=True, n_samples= majority, random_state=44)\ndf_4_upsampled = resample(df_4, replace=True, n_samples= majority, random_state=44)\ndf_5_upsampled = resample(df_5, replace=True, n_samples= majority, random_state=44) \n\ndf_upsampled = pd.concat([df_0_upsampled, df_1_upsampled, df_2_upsampled, df_4_upsampled, df_3_upsampled, df_5_upsampled])","metadata":{"execution":{"iopub.status.busy":"2023-05-11T15:59:11.272278Z","iopub.execute_input":"2023-05-11T15:59:11.273237Z","iopub.status.idle":"2023-05-11T15:59:16.478916Z","shell.execute_reply.started":"2023-05-11T15:59:11.273191Z","shell.execute_reply":"2023-05-11T15:59:16.477580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_upsampled.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-11T15:59:16.480547Z","iopub.execute_input":"2023-05-11T15:59:16.480984Z","iopub.status.idle":"2023-05-11T15:59:16.500522Z","shell.execute_reply.started":"2023-05-11T15:59:16.480944Z","shell.execute_reply":"2023-05-11T15:59:16.499110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# delete the not important variables\ndel [df_0_upsampled, df_1_upsampled, df_2_upsampled, df_4_upsampled, df_3_upsampled, df_5_upsampled]","metadata":{"execution":{"iopub.status.busy":"2023-05-11T15:59:16.502092Z","iopub.execute_input":"2023-05-11T15:59:16.502707Z","iopub.status.idle":"2023-05-11T15:59:17.039611Z","shell.execute_reply.started":"2023-05-11T15:59:16.502673Z","shell.execute_reply":"2023-05-11T15:59:17.038370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display the count of each type of rating after balancing\nsns.countplot(x=df_upsampled['rating']);","metadata":{"execution":{"iopub.status.busy":"2023-05-11T15:59:17.041501Z","iopub.execute_input":"2023-05-11T15:59:17.041888Z","iopub.status.idle":"2023-05-11T15:59:17.817421Z","shell.execute_reply.started":"2023-05-11T15:59:17.041854Z","shell.execute_reply":"2023-05-11T15:59:17.816125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Using TfidfVectorizer to convert our text data to matrix of TF-IDF features","metadata":{}},{"cell_type":"code","source":"tfidfconverter = TfidfVectorizer()\nX_ = tfidfconverter.fit_transform(df_upsampled['review_text'])","metadata":{"execution":{"iopub.status.busy":"2023-05-11T15:59:17.821688Z","iopub.execute_input":"2023-05-11T15:59:17.822113Z","iopub.status.idle":"2023-05-11T16:07:49.564255Z","shell.execute_reply.started":"2023-05-11T15:59:17.822063Z","shell.execute_reply":"2023-05-11T16:07:49.562196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Splitting our data to training and testing","metadata":{}},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X_, df_upsampled['rating'], test_size=0.01, random_state=1000)","metadata":{"execution":{"iopub.status.busy":"2023-05-11T16:07:49.566423Z","iopub.execute_input":"2023-05-11T16:07:49.566952Z","iopub.status.idle":"2023-05-11T16:07:52.782265Z","shell.execute_reply.started":"2023-05-11T16:07:49.566903Z","shell.execute_reply":"2023-05-11T16:07:52.781029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape, y_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-11T16:07:52.787077Z","iopub.execute_input":"2023-05-11T16:07:52.787522Z","iopub.status.idle":"2023-05-11T16:07:52.796437Z","shell.execute_reply.started":"2023-05-11T16:07:52.787486Z","shell.execute_reply":"2023-05-11T16:07:52.794975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Machine Learning Models","metadata":{}},{"cell_type":"markdown","source":"**LogisticRegression model**","metadata":{}},{"cell_type":"code","source":"LogisticRegressionModel = LogisticRegression(C=1, penalty='l2', solver='sag', multi_class='multinomial', random_state=150, max_iter=500)\nLogisticRegressionModel.fit(X_train, y_train)\n\n#Calculating Details\nprint('LogisticRegressionModel Train Score is : ' , LogisticRegressionModel.score(X_train, y_train))\nprint('LogisticRegressionModel Test Score is : ' , LogisticRegressionModel.score(X_test, y_test))","metadata":{"execution":{"iopub.status.busy":"2023-05-11T16:07:52.798009Z","iopub.execute_input":"2023-05-11T16:07:52.798487Z","iopub.status.idle":"2023-05-11T16:18:53.746809Z","shell.execute_reply.started":"2023-05-11T16:07:52.798449Z","shell.execute_reply":"2023-05-11T16:18:53.745350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = cross_val_score(LogisticRegressionModel, X_test, y_test, cv=5)\nprint('Cross-Validation Accuracy Scores', scores)","metadata":{"execution":{"iopub.status.busy":"2023-05-11T16:18:53.748396Z","iopub.execute_input":"2023-05-11T16:18:53.748772Z","iopub.status.idle":"2023-05-11T16:19:19.110854Z","shell.execute_reply.started":"2023-05-11T16:18:53.748738Z","shell.execute_reply":"2023-05-11T16:19:19.109435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = LogisticRegressionModel.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-11T16:19:19.112175Z","iopub.execute_input":"2023-05-11T16:19:19.112514Z","iopub.status.idle":"2023-05-11T16:19:19.161092Z","shell.execute_reply.started":"2023-05-11T16:19:19.112486Z","shell.execute_reply":"2023-05-11T16:19:19.159927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CM = confusion_matrix(y_test, y_pred)\nprint('Confusion Matrix is : \\n', CM)\n\n# drawing confusion matrix\nsns.heatmap(CM, center = True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-11T16:19:19.162848Z","iopub.execute_input":"2023-05-11T16:19:19.163231Z","iopub.status.idle":"2023-05-11T16:19:19.457571Z","shell.execute_reply.started":"2023-05-11T16:19:19.163195Z","shell.execute_reply":"2023-05-11T16:19:19.456145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ClassificationReport = classification_report(y_test,y_pred)\nprint(ClassificationReport)","metadata":{"execution":{"iopub.status.busy":"2023-05-11T16:19:19.459402Z","iopub.execute_input":"2023-05-11T16:19:19.459796Z","iopub.status.idle":"2023-05-11T16:19:19.589599Z","shell.execute_reply.started":"2023-05-11T16:19:19.459762Z","shell.execute_reply":"2023-05-11T16:19:19.588117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**LinearSVC Model**","metadata":{}},{"cell_type":"code","source":"lsvc = LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,intercept_scaling=1, loss='squared_hinge', max_iter=800,\n          multi_class='ovr', penalty='l2', random_state=150, tol=0.0001, verbose=0)\nlsvc.fit(X_train, y_train)\n\n#Calculating Details\nprint('LinearSVC Model Train Score is : ' , lsvc.score(X_train, y_train))\nprint('LinearSVC Model Test Score is : ' , lsvc.score(X_test, y_test))","metadata":{"execution":{"iopub.status.busy":"2023-05-11T16:19:19.592902Z","iopub.execute_input":"2023-05-11T16:19:19.593453Z","iopub.status.idle":"2023-05-11T16:48:56.211932Z","shell.execute_reply.started":"2023-05-11T16:19:19.593414Z","shell.execute_reply":"2023-05-11T16:48:56.210610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = cross_val_score(lsvc, X_test, y_test, cv=5)\nprint('Cross-Validation Accuracy Scores', scores)","metadata":{"execution":{"iopub.status.busy":"2023-05-11T16:48:56.213531Z","iopub.execute_input":"2023-05-11T16:48:56.213908Z","iopub.status.idle":"2023-05-11T16:49:30.460381Z","shell.execute_reply.started":"2023-05-11T16:48:56.213875Z","shell.execute_reply":"2023-05-11T16:49:30.459350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_svc = lsvc.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-11T16:49:30.461991Z","iopub.execute_input":"2023-05-11T16:49:30.462425Z","iopub.status.idle":"2023-05-11T16:49:30.505534Z","shell.execute_reply.started":"2023-05-11T16:49:30.462388Z","shell.execute_reply":"2023-05-11T16:49:30.504248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CM_SVC = confusion_matrix(y_test, y_pred_svc)\nprint('Confusion Matrix is : \\n', CM_SVC)\n\n# drawing confusion matrix\nsns.heatmap(CM_SVC, center = True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-11T16:49:30.507252Z","iopub.execute_input":"2023-05-11T16:49:30.507628Z","iopub.status.idle":"2023-05-11T16:49:30.787297Z","shell.execute_reply.started":"2023-05-11T16:49:30.507597Z","shell.execute_reply":"2023-05-11T16:49:30.786038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ClassificationReport = classification_report(y_test,y_pred_svc)\nprint(ClassificationReport)","metadata":{"execution":{"iopub.status.busy":"2023-05-11T16:49:30.795446Z","iopub.execute_input":"2023-05-11T16:49:30.796447Z","iopub.status.idle":"2023-05-11T16:49:30.921092Z","shell.execute_reply.started":"2023-05-11T16:49:30.796405Z","shell.execute_reply":"2023-05-11T16:49:30.919857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**MultinomialNB model**","metadata":{}},{"cell_type":"code","source":"MultinomialNBModel = MultinomialNB(alpha=0.0001)\nMultinomialNBModel.fit(X_train, y_train)\n\n#Calculating Details\nprint('MultinomialNBModel Train Score is : ' , MultinomialNBModel.score(X_train, y_train))\nprint('MultinomialNBModel Test Score is : ' , MultinomialNBModel.score(X_test, y_test))","metadata":{"execution":{"iopub.status.busy":"2023-05-11T16:49:30.922591Z","iopub.execute_input":"2023-05-11T16:49:30.922975Z","iopub.status.idle":"2023-05-11T16:49:40.307873Z","shell.execute_reply.started":"2023-05-11T16:49:30.922943Z","shell.execute_reply":"2023-05-11T16:49:40.306714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = cross_val_score(MultinomialNBModel, X_test, y_test, cv=3)\nprint('Cross-Validation Accuracy Scores', scores)","metadata":{"execution":{"iopub.status.busy":"2023-05-11T16:49:40.309491Z","iopub.execute_input":"2023-05-11T16:49:40.309934Z","iopub.status.idle":"2023-05-11T16:49:40.693921Z","shell.execute_reply.started":"2023-05-11T16:49:40.309895Z","shell.execute_reply":"2023-05-11T16:49:40.692326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_mnb = MultinomialNBModel.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-11T16:49:40.695551Z","iopub.execute_input":"2023-05-11T16:49:40.696683Z","iopub.status.idle":"2023-05-11T16:49:40.746102Z","shell.execute_reply.started":"2023-05-11T16:49:40.696634Z","shell.execute_reply":"2023-05-11T16:49:40.745014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CM_MNB = confusion_matrix(y_test, y_pred_mnb)\nprint('Confusion Matrix is : \\n', CM_MNB)\n\n# drawing confusion matrix\nsns.heatmap(CM_MNB, center = True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-11T16:49:40.747527Z","iopub.execute_input":"2023-05-11T16:49:40.747962Z","iopub.status.idle":"2023-05-11T16:49:42.373274Z","shell.execute_reply.started":"2023-05-11T16:49:40.747932Z","shell.execute_reply":"2023-05-11T16:49:42.371967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ClassificationReport_MNB = classification_report(y_test,y_pred_mnb)\nprint(ClassificationReport_MNB)","metadata":{"execution":{"iopub.status.busy":"2023-05-11T16:49:42.374653Z","iopub.execute_input":"2023-05-11T16:49:42.374996Z","iopub.status.idle":"2023-05-11T16:49:42.500740Z","shell.execute_reply.started":"2023-05-11T16:49:42.374965Z","shell.execute_reply":"2023-05-11T16:49:42.499686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Sentiment Analysis with vader","metadata":{}},{"cell_type":"code","source":"from nltk.sentiment.vader import SentimentIntensityAnalyzer\nsentiments = SentimentIntensityAnalyzer()","metadata":{"execution":{"iopub.status.busy":"2023-05-11T16:49:42.502282Z","iopub.execute_input":"2023-05-11T16:49:42.502892Z","iopub.status.idle":"2023-05-11T16:49:42.535659Z","shell.execute_reply.started":"2023-05-11T16:49:42.502855Z","shell.execute_reply":"2023-05-11T16:49:42.534065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ratings = df_train[\"rating\"].value_counts()\nnumbers = ratings.index\nquantity = ratings.values\n\ncustom_colors = [\"skyblue\", \"yellowgreen\", 'tomato', \"blue\", \"red\"]\nplt.figure(figsize=(5, 5))\nplt.pie(quantity, labels=numbers, colors=custom_colors)\ncentral_circle = plt.Circle((0, 0), 0.5, color='white')\nfig = plt.gcf()\nfig.gca().add_artist(central_circle)\nplt.rc('font', size=12)\nplt.title(\"Amazon Alexa Reviews\", fontsize=20)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-11T16:49:42.537334Z","iopub.execute_input":"2023-05-11T16:49:42.537733Z","iopub.status.idle":"2023-05-11T16:49:42.694158Z","shell.execute_reply.started":"2023-05-11T16:49:42.537697Z","shell.execute_reply":"2023-05-11T16:49:42.692880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sentiments = SentimentIntensityAnalyzer()\ndf_train[\"Positive\"] = [sentiments.polarity_scores(i)[\"pos\"] for i in df_train[\"review_text\"]]\ndf_train[\"Negative\"] = [sentiments.polarity_scores(i)[\"neg\"] for i in df_train[\"review_text\"]]\ndf_train[\"Neutral\"] = [sentiments.polarity_scores(i)[\"neu\"] for i in df_train[\"review_text\"]]\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-11T16:49:42.696295Z","iopub.execute_input":"2023-05-11T16:49:42.696906Z","iopub.status.idle":"2023-05-11T18:00:16.505137Z","shell.execute_reply.started":"2023-05-11T16:49:42.696859Z","shell.execute_reply":"2023-05-11T18:00:16.502654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = sum(df_train[\"Positive\"])\ny = sum(df_train[\"Negative\"])\nz = sum(df_train[\"Neutral\"])\n\ndef sentiment_score(a, b, c):\n    if (a>b) and (a>c):\n        print(\"Positive 😊 \")\n    elif (b>a) and (b>c):\n        print(\"Negative 😠 \")\n    else:\n        print(\"Neutral 🙂 \")\nsentiment_score(x, y, z)","metadata":{"execution":{"iopub.status.busy":"2023-05-11T18:00:16.508218Z","iopub.execute_input":"2023-05-11T18:00:16.508844Z","iopub.status.idle":"2023-05-11T18:00:16.743173Z","shell.execute_reply.started":"2023-05-11T18:00:16.508801Z","shell.execute_reply":"2023-05-11T18:00:16.742204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Positive: \", x)\nprint(\"Negative: \", y)\nprint(\"Neutral: \", z)","metadata":{"execution":{"iopub.status.busy":"2023-05-11T18:00:16.744294Z","iopub.execute_input":"2023-05-11T18:00:16.744644Z","iopub.status.idle":"2023-05-11T18:00:16.751264Z","shell.execute_reply.started":"2023-05-11T18:00:16.744613Z","shell.execute_reply":"2023-05-11T18:00:16.750051Z"},"trusted":true},"execution_count":null,"outputs":[]}]}