{"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":"Problem statement\n\nIf you have two sentences, there are three ways they could be related: one could entail the other, one could contradict the other, or they could be unrelated. Natural Language Inferencing (NLI) is a popular NLP problem that involves determining how pairs of sentences (consisting of a premise and a hypothesis) are related.\n\nYour task is to create an NLI model that assigns labels of 0, 1, or 2 (corresponding to entailment, neutral, and contradiction) to pairs of premises and hypotheses. To make things more interesting, the train and test set include text in fifteen different languages! ","metadata":{}},{"cell_type":"markdown","source":"Import libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:05:21.454963Z","iopub.execute_input":"2022-07-05T14:05:21.455341Z","iopub.status.idle":"2022-07-05T14:05:21.460488Z","shell.execute_reply.started":"2022-07-05T14:05:21.455310Z","shell.execute_reply":"2022-07-05T14:05:21.459565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Load datasets","metadata":{}},{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:05:21.462217Z","iopub.execute_input":"2022-07-05T14:05:21.462719Z","iopub.status.idle":"2022-07-05T14:05:21.475855Z","shell.execute_reply.started":"2022-07-05T14:05:21.462687Z","shell.execute_reply":"2022-07-05T14:05:21.474647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Read datasets","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/contradictory-my-dear-watson/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/contradictory-my-dear-watson/test.csv\")\nsubmission = pd.read_csv(\"/kaggle/input/contradictory-my-dear-watson/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:05:21.478298Z","iopub.execute_input":"2022-07-05T14:05:21.479170Z","iopub.status.idle":"2022-07-05T14:05:21.589470Z","shell.execute_reply.started":"2022-07-05T14:05:21.479121Z","shell.execute_reply":"2022-07-05T14:05:21.588430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:05:21.590735Z","iopub.execute_input":"2022-07-05T14:05:21.591053Z","iopub.status.idle":"2022-07-05T14:05:21.610248Z","shell.execute_reply.started":"2022-07-05T14:05:21.591024Z","shell.execute_reply":"2022-07-05T14:05:21.609168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:05:21.613034Z","iopub.execute_input":"2022-07-05T14:05:21.614039Z","iopub.status.idle":"2022-07-05T14:05:21.633919Z","shell.execute_reply.started":"2022-07-05T14:05:21.613998Z","shell.execute_reply":"2022-07-05T14:05:21.632696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:05:21.636575Z","iopub.execute_input":"2022-07-05T14:05:21.637300Z","iopub.status.idle":"2022-07-05T14:05:21.658196Z","shell.execute_reply.started":"2022-07-05T14:05:21.637255Z","shell.execute_reply":"2022-07-05T14:05:21.657034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:05:21.659580Z","iopub.execute_input":"2022-07-05T14:05:21.660006Z","iopub.status.idle":"2022-07-05T14:05:21.672068Z","shell.execute_reply.started":"2022-07-05T14:05:21.659972Z","shell.execute_reply":"2022-07-05T14:05:21.671219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Define target","metadata":{}},{"cell_type":"code","source":"target = train['label']","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:05:21.673546Z","iopub.execute_input":"2022-07-05T14:05:21.674345Z","iopub.status.idle":"2022-07-05T14:05:21.678064Z","shell.execute_reply.started":"2022-07-05T14:05:21.674314Z","shell.execute_reply":"2022-07-05T14:05:21.677313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Analyse target","metadata":{}},{"cell_type":"code","source":"sns.displot(target)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:05:21.679311Z","iopub.execute_input":"2022-07-05T14:05:21.679845Z","iopub.status.idle":"2022-07-05T14:05:21.959186Z","shell.execute_reply.started":"2022-07-05T14:05:21.679813Z","shell.execute_reply":"2022-07-05T14:05:21.958053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.boxplot(target)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:05:21.963081Z","iopub.execute_input":"2022-07-05T14:05:21.963484Z","iopub.status.idle":"2022-07-05T14:05:22.130374Z","shell.execute_reply.started":"2022-07-05T14:05:21.963448Z","shell.execute_reply":"2022-07-05T14:05:22.129226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:05:22.133250Z","iopub.execute_input":"2022-07-05T14:05:22.133867Z","iopub.status.idle":"2022-07-05T14:05:22.151140Z","shell.execute_reply.started":"2022-07-05T14:05:22.133833Z","shell.execute_reply":"2022-07-05T14:05:22.149795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"TfIdf Vectorizer","metadata":{}},{"cell_type":"code","source":"from sklearn.feature_extraction.text import TfidfVectorizer\nfrom sklearn.metrics.pairwise import cosine_similarity\n\ntfidf= TfidfVectorizer(stop_words='english', lowercase=False)\n\ncos_sim = []\n\nfor r in range(len(test)):\n    doc1 = test['premise'].loc[r]\n    doc2 = test['hypothesis'].loc[r]\n    \n    docs = [doc1, doc2]\n    \n    tfidf_vector = tfidf.fit_transform(docs)\n    \n    similarities = cosine_similarity(tfidf_vector)\n\n    cos_sim.append(similarities)\n    \ncos_sim = np.array(cos_sim)\n\ncos_sim_sub = cos_sim[:, 0, 1]\nprint(cos_sim_sub)\ncos_sim_sub.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:05:22.152636Z","iopub.execute_input":"2022-07-05T14:05:22.153458Z","iopub.status.idle":"2022-07-05T14:05:31.706794Z","shell.execute_reply.started":"2022-07-05T14:05:22.153421Z","shell.execute_reply":"2022-07-05T14:05:31.705613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Number submissions","metadata":{}},{"cell_type":"code","source":"prediction = []\n\nfor p in range(len(cos_sim_sub)):\n    if cos_sim_sub[p] <= 0.1:\n        pred = 2\n    elif cos_sim_sub[p] > 0.1 and cos_sim_sub[p] <= 0.3 :\n        pred = 1\n    elif cos_sim_sub[p] > 0.3:\n        pred = 0\n    prediction.append(pred)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:05:31.708334Z","iopub.execute_input":"2022-07-05T14:05:31.709691Z","iopub.status.idle":"2022-07-05T14:05:31.723944Z","shell.execute_reply.started":"2022-07-05T14:05:31.709640Z","shell.execute_reply":"2022-07-05T14:05:31.722633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Prepare submission","metadata":{}},{"cell_type":"code","source":"submission['prediction'] = prediction\n\nsubmission.to_csv('submission.csv',index=False) # writing data to a CSV file\nsubmission = pd.read_csv(\"submission.csv\")\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-07-05T14:05:31.725668Z","iopub.execute_input":"2022-07-05T14:05:31.726325Z","iopub.status.idle":"2022-07-05T14:05:31.764792Z","shell.execute_reply.started":"2022-07-05T14:05:31.726287Z","shell.execute_reply":"2022-07-05T14:05:31.763595Z"},"trusted":true},"execution_count":null,"outputs":[]}]}