{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input/embeddings/GoogleNews-vectors-negative300/\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import gensim\npath = \"../input/embeddings/GoogleNews-vectors-negative300/GoogleNews-vectors-negative300.bin\"\nembeddings = gensim.models.KeyedVectors.load_word2vec_format(path, binary =True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"093b213137d624d2206e80f34ef8b5f51d68a29a"},"cell_type":"code","source":"embeddings['amazon'] #for every word google has a vector representation","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e99ad60473d5f3e40b452668bac3b4efe29aaf48"},"cell_type":"code","source":"embeddings.most_similar('modi', topn=10) #shows terms related to hyundai","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"36fb7446358a8b5b555b48f3a322cff872c69094"},"cell_type":"code","source":"embeddings.doesnt_match(['rahul', 'modi', 'sonia', 'sachin']) #tells odd one out from list","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ef5daa70850656e2cc00319a5460d5627b771c67"},"cell_type":"code","source":"embeddings.doesnt_match(['football', 'cricket', 'basketball', 'swimming'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"32031f10a90f9863538179753171d2dad39bf464"},"cell_type":"code","source":"embeddings.most_similar(positive=['king', 'woman'], negative=['man'], topn=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"35a934cb2edf0af04b6a4d39391936fdeb8d152c"},"cell_type":"code","source":"## Word Similarity**¶","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e7f970169d1a16f286bde70b6bc3f668eb8d1e92"},"cell_type":"code","source":"url  = 'https://bit.ly/2G4zbHA'\namazon = pd.read_csv(url)\namazon.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4e7ba9e681813179e265e163ae052862ce0e92e0"},"cell_type":"code","source":"docs = amazon['reviewText'].fillna('').str.lower()\ndocs = docs.str.replace('[^a-z]', ' ')\ndocs.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ac3988607db388cbe58ee3e7f3c8e6a543d203c9"},"cell_type":"code","source":"import nltk\nstopwords = nltk.corpus.stopwords.words('english')\nstemmer = nltk.stem.PorterStemmer()\ndef clean_sentence(text):\n    words = text.split(' ')\n    words_clean = [stemmer.stem(word) for word in words if word not in  stopwords]\n    return ' '.join(words_clean)\ndocs_clean = docs.apply(clean_sentence)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b45469e01b8e5cd201446e6c6900eb98521efe85"},"cell_type":"code","source":"from sklearn.feature_extraction.text import CountVectorizer\nvectorizer = CountVectorizer(min_df = 5)\nvectorizer.fit(docs_clean)\ndf_dtm = pd.DataFrame(vectorizer.transform(docs_clean).toarray(),\n                     columns = vectorizer.get_feature_names())\ndf_dtm.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"19f44441d37a6a17e4deeeac3a0f7f7b72e2e1a9"},"cell_type":"code","source":"# Semantic Analysis","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"17b32d2111a4495cd19f6d0d7fb4575cb6bb622d"},"cell_type":"code","source":"from sklearn.metrics.pairwise import cosine_similarity\ncosine_similarity([df_dtm['kindl'], df_dtm['book']])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0d735c8e4d919c888315b0c1c73749c51409fdd3"},"cell_type":"code","source":"cosine_similarity([embeddings['kindle'], embeddings['book']])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"80df43f2c4d0e9fc2a1271a15701c307486038bb"},"cell_type":"code","source":"cos_matrix = pd.DataFrame(cosine_similarity(df_dtm.T), columns = df_dtm.columns, index = df_dtm.columns)\ncos_matrix","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"04bbfe729bb0877b8eebdca6d90f0f13e4bbb110"},"cell_type":"code","source":"cos_matrix['tablet'].sort_values(ascending=False).drop('tablet')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f3cf39a39ef6d8e73f99cbc738a7fc5f9856adf"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}