{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-04T23:42:03.489691Z","iopub.execute_input":"2023-02-04T23:42:03.490206Z","iopub.status.idle":"2023-02-04T23:42:03.502115Z","shell.execute_reply.started":"2023-02-04T23:42:03.490166Z","shell.execute_reply":"2023-02-04T23:42:03.500212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/goodreads-books-reviews-290312/goodreads_train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-02-04T23:42:06.253742Z","iopub.execute_input":"2023-02-04T23:42:06.254267Z","iopub.status.idle":"2023-02-04T23:42:34.733901Z","shell.execute_reply.started":"2023-02-04T23:42:06.254215Z","shell.execute_reply":"2023-02-04T23:42:34.732637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nplt.style.use('ggplot')\nimport nltk\nprint(df.shape)","metadata":{"execution":{"iopub.status.busy":"2023-02-04T23:47:04.805949Z","iopub.execute_input":"2023-02-04T23:47:04.807692Z","iopub.status.idle":"2023-02-04T23:47:04.815652Z","shell.execute_reply.started":"2023-02-04T23:47:04.807627Z","shell.execute_reply":"2023-02-04T23:47:04.814181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2023-02-04T23:51:57.590031Z","iopub.execute_input":"2023-02-04T23:51:57.590494Z","iopub.status.idle":"2023-02-04T23:51:58.060565Z","shell.execute_reply.started":"2023-02-04T23:51:57.590458Z","shell.execute_reply":"2023-02-04T23:51:58.059287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = df[['review_id','rating','review_text']]\ndf1","metadata":{"execution":{"iopub.status.busy":"2023-02-04T23:55:59.110104Z","iopub.execute_input":"2023-02-04T23:55:59.110574Z","iopub.status.idle":"2023-02-04T23:55:59.191872Z","shell.execute_reply.started":"2023-02-04T23:55:59.110536Z","shell.execute_reply":"2023-02-04T23:55:59.190593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ax = df['rating'].value_counts().sort_index().plot(kind = 'bar', title = 'Count of ratings',\n                                                   figsize = (10,5))\nax.set_xlabel('Rating')\nax.set_ylabel('Count')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T00:01:43.276956Z","iopub.execute_input":"2023-02-05T00:01:43.277391Z","iopub.status.idle":"2023-02-05T00:01:43.514089Z","shell.execute_reply.started":"2023-02-05T00:01:43.277356Z","shell.execute_reply":"2023-02-05T00:01:43.512718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from nltk.sentiment import SentimentIntensityAnalyzer\nfrom tqdm.notebook import tqdm\n\nsia = SentimentIntensityAnalyzer()\ntrial1 = df1['review_text'][0]\nprint('Rating: ',df1['rating'][0])\nprint('Polarity scores:',sia.polarity_scores(trial1))\nprint(t1)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T00:23:56.680043Z","iopub.execute_input":"2023-02-05T00:23:56.681423Z","iopub.status.idle":"2023-02-05T00:23:56.707088Z","shell.execute_reply.started":"2023-02-05T00:23:56.681358Z","shell.execute_reply":"2023-02-05T00:23:56.705464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = {}\nfor i, row in tqdm(df1.head(10000).iterrows(), total=len(df1.head(10000))):\n    text = row['review_text']\n    res[i] = sia.polarity_scores(text)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T01:07:03.797962Z","iopub.execute_input":"2023-02-05T01:07:03.798420Z","iopub.status.idle":"2023-02-05T01:07:35.501503Z","shell.execute_reply.started":"2023-02-05T01:07:03.798375Z","shell.execute_reply":"2023-02-05T01:07:35.500268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vaders = pd.DataFrame(res).T\n\ncdf = pd.concat([df1.head(10000),vaders], axis=1)\ncdf['rev_len'] = df['review_text'].apply(len)\ncdf.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T01:07:50.100326Z","iopub.execute_input":"2023-02-05T01:07:50.100737Z","iopub.status.idle":"2023-02-05T01:07:50.807469Z","shell.execute_reply.started":"2023-02-05T01:07:50.100705Z","shell.execute_reply":"2023-02-05T01:07:50.806230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ax = sns.barplot(data = cdf, x = 'rating', y = 'compound')","metadata":{"execution":{"iopub.status.busy":"2023-02-05T01:07:54.333904Z","iopub.execute_input":"2023-02-05T01:07:54.335062Z","iopub.status.idle":"2023-02-05T01:07:54.787857Z","shell.execute_reply.started":"2023-02-05T01:07:54.335002Z","shell.execute_reply":"2023-02-05T01:07:54.786489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.naive_bayes import GaussianNB\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\n\ntrain_df, test_df = train_test_split(cdf, test_size=0.2, random_state=0)\nX_train = train_df[['neg','neu','pos','rev_len']]\ny_train = train_df[\"rating\"]\nX_test = test_df[['neg','neu','pos','rev_len']]\ny_test = test_df[\"rating\"]\n\nclf = GaussianNB()\nclf.fit(X_train, y_train)\ny_pred = clf.predict(X_test)\n\naccuracy = np.mean(y_pred == y_test)\nprint(\"Accuracy:\", accuracy)\n","metadata":{"execution":{"iopub.status.busy":"2023-02-05T01:08:39.904164Z","iopub.execute_input":"2023-02-05T01:08:39.904580Z","iopub.status.idle":"2023-02-05T01:08:39.933894Z","shell.execute_reply.started":"2023-02-05T01:08:39.904548Z","shell.execute_reply":"2023-02-05T01:08:39.932188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import pipeline \nsent_pipeline = pipeline(\"sentiment-analysis\")","metadata":{"execution":{"iopub.status.busy":"2023-02-05T01:15:49.720157Z","iopub.execute_input":"2023-02-05T01:15:49.720586Z","iopub.status.idle":"2023-02-05T01:15:55.038850Z","shell.execute_reply.started":"2023-02-05T01:15:49.720554Z","shell.execute_reply":"2023-02-05T01:15:55.037371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sent_pipeline(\"\"\"This is a special book. It started slow for about the first third, then in the middle third it started to get interesting, then the last third blew my mind. This is what I love about good science fiction - it pushes your thinking about where things can go. \n It is a 2015 Hugo winner, and translated from its original Chinese, which made it interesting in just a different way from most things I've read. For instance the intermixing of Chinese revolutionary history - how they kept accusing people of being \"reactionaries\", etc. \n It is a book about science, and aliens. The science described in the book is impressive - its a book grounded in physics and pretty accurate as far as I could tell. (view spoiler)[Though when it got to folding protons into 8 dimensions I think he was just making stuff up - interesting to think about though. \n But what would happen if our SETI stations received a message - if we found someone was out there - and the person monitoring and answering the signal on our side was disillusioned? That part of the book was a bit dark - I would like to think human reaction to discovering alien civilization that is hostile would be more like Enders Game where we would band together. \n I did like how the book unveiled the Trisolaran culture through the game. It was a smart way to build empathy with them and also understand what they've gone through across so many centuries. And who know a 3 body problem was an unsolvable math problem? But I still don't get who made the game - maybe that will come in the next book. \n I loved this quote: \n \"In the long history of scientific progress, how many protons have been smashed apart in accelerators by physicists? How many neutrons and electrons? Probably no fewer than a hundred million. Every collision was probably the end of the civilizations and intelligences in a microcosmos. In fact, even in nature, the destruction of universes must be happening at every second--for example, through the decay of neutrons. Also, a high-energy cosmic ray entering the atmosphere may destroy thousands of such miniature universes....\"\"\")","metadata":{"execution":{"iopub.status.busy":"2023-02-05T01:16:18.667203Z","iopub.execute_input":"2023-02-05T01:16:18.667671Z","iopub.status.idle":"2023-02-05T01:16:19.085233Z","shell.execute_reply.started":"2023-02-05T01:16:18.667637Z","shell.execute_reply":"2023-02-05T01:16:19.084021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res_t = {}\nfor i, row in tqdm(cdf.iterrows(), total=len(cdf)):\n    try:\n        text = row['review_text']\n        cdf.loc[i]['trans'] = sent_pipeline(text)[0]['score']\n    except:\n        cdf.drop(i)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T01:25:35.733793Z","iopub.execute_input":"2023-02-05T01:25:35.734370Z","iopub.status.idle":"2023-02-05T01:48:24.732758Z","shell.execute_reply.started":"2023-02-05T01:25:35.734329Z","shell.execute_reply":"2023-02-05T01:48:24.731548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-02-05T01:57:57.757102Z","iopub.execute_input":"2023-02-05T01:57:57.757600Z","iopub.status.idle":"2023-02-05T01:57:57.913517Z","shell.execute_reply.started":"2023-02-05T01:57:57.757565Z","shell.execute_reply":"2023-02-05T01:57:57.911635Z"},"trusted":true},"execution_count":null,"outputs":[]}]}