{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport tqdm","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"!find .. -type f -exec du -ah {} +","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"15e52c819fa3c0600f9a1270c0603dc708e870f0","scrolled":true},"cell_type":"code","source":"!head -n3 ../input/embeddings/wiki-news-300d-1M/wiki-news-300d-1M.vec","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9025d100cdc0c7713111c312968c85856ff8cbf9"},"cell_type":"code","source":"def load_emb(fname):\n    embeddings = {}\n    with open(fname) as f:\n        for line in tqdm.tqdm(f, unit=' words'):\n            word, *coeffs = line.split(' ')\n            if len(coeffs) >= 100:\n                embeddings[word] = np.asarray(coeffs, dtype=np.float32)\n    return embeddings\n\ndef em_to_matrix(_em):\n    _W = np.stack(_em.values())\n    _vocab = {w: ind for ind, w in enumerate(_em.keys())}\n    return Embeddding(normalize(_W, axis=1), _vocab)\n    \ndef normalize(_W, axis=None):\n    W_norm = np.zeros(_W.shape)\n    d = np.sum(_W ** 2, axis=axis) ** (0.5)\n    W_norm = (_W.T / d).T\n    return W_norm  # each row is a unit vector\n    \ndef cosine_similarity(_W, _vec):\n    return np.dot(_W, _vec.T)  # expects everything already being unit\n\n\nclass EmbeddingItem:\n    \n    def __init__(self, vec, word_history=[]):\n        self.vec = vec\n        self.history = word_history\n        \n    def __add__(self, item):\n        return EmbeddingItem(normalize(self.vec + item.vec), self.history + item.history)\n    \n    def __sub__(self, item):\n        return EmbeddingItem(normalize(self.vec - item.vec), self.history + item.history)\n    \n    def __mul__(self, item):\n        return EmbeddingItem(normalize(self.vec.T * item.vec), self.history + item.history)\n    \n\nclass Embeddding:\n    \n    def __init__(self, _W, _vocab):\n        self.W = _W\n        self.vocab = _vocab\n        self.ivocab = {v: k for k, v in _vocab.items()}\n        \n    def __getitem__(self, word) -> EmbeddingItem:\n        return EmbeddingItem(np.copy(self.W[self.vocab[word], :]), word_history=[word])\n    \n    def n_closest(self, n, vec: EmbeddingItem):\n        distances = cosine_similarity(self.W, vec.vec)\n        for word in vec.history:\n            distances[self.vocab[word]] = -np.Inf\n        return [(self.ivocab[ind], distances[ind]) for ind in np.argsort(-distances)[:n]]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"56d73e37e3bd0184a98b0107b1377110b07fa890"},"cell_type":"code","source":"glove = '../input/embeddings/glove.840B.300d/glove.840B.300d.txt'\nfastext = '../input/embeddings/wiki-news-300d-1M/wiki-news-300d-1M.vec'\nem = load_emb(fastext)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"23b4e9ea421493b9b1dfa03325083814d8e33660"},"cell_type":"code","source":"emb = em_to_matrix(em)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"73fdeda239184e77997d794330faa6cfcb140004"},"cell_type":"code","source":"def show(_emb, _vec):\n    print('-'*30)\n    for word, dist in _emb.n_closest(3, _vec):\n        print('{:<20} {:.2f}'.format(word, dist))\n\nshow(emb, emb['berlin'] - emb['germany'] + emb['finland'])\nshow(emb, emb['mother'] - emb['woman'])\nshow(emb, emb['king'] - emb['man'])\nshow(emb, emb['man'] + emb['dress'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7d46ac1b36ff05729f8708cafce70be8ddeea8cf"},"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}