forked from babylonhealth/fastText_multilingual
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nils Hammerla
committed
Apr 21, 2017
1 parent
003a833
commit 6190716
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
BSD 3-Clause License | ||
|
||
Copyright (c) 2017, babylon health | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
* Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# | ||
# Copyright (c) 2017-present, babylon health | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
# | ||
|
||
import numpy as np | ||
|
||
|
||
class FastVector: | ||
""" | ||
Minimal wrapper for fastvector embeddings. | ||
``` | ||
Usage: | ||
$ model = FastVector(vector_file='/path/to/wiki.en.vec') | ||
$ 'apple' in model | ||
> TRUE | ||
$ model['apple'].shape | ||
> (300,) | ||
``` | ||
""" | ||
|
||
def __init__(self, vector_file='', transform=None): | ||
"""Read in word vectors in fasttext format""" | ||
self.word2id = {} | ||
print('reading word vectors from %s' % vector_file) | ||
with open(vector_file, 'r') as f: | ||
(self.n_words, self.n_dim) = \ | ||
(int(x) for x in f.readline().rstrip('\n').split(' ')) | ||
self.embed = np.zeros((self.n_words, self.n_dim)) | ||
for i, line in enumerate(f): | ||
elems = line.rstrip('\n').split(' ') | ||
self.word2id[elems[0]] = i | ||
self.embed[i] = elems[1:self.n_dim+1] | ||
|
||
if transform is not None: | ||
print('Applying transformation to embedding') | ||
self.apply_transform(transform) | ||
|
||
def apply_transform(self, transform): | ||
""" | ||
Apply the given transformation to the vector space | ||
Right-multiplies given transform with embeddings E: | ||
E = E * transform | ||
Transform can either be a string with a filename to a | ||
text file containing a ndarray (compat. with np.loadtxt) | ||
or a numpy ndarray. | ||
""" | ||
transmat = np.loadtxt(transform) if isinstance(transform, str) else transform | ||
self.embed = np.matmul(self.embed, transmat) | ||
|
||
@classmethod | ||
def cosine_similarity(cls, vec_a, vec_b): | ||
"""Compute cosine similarity between vec_a and vec_b""" | ||
return np.dot(vec_a, vec_b) / \ | ||
(np.linalg.norm(vec_a) * np.linalg.norm(vec_b)) | ||
|
||
def __contains__(self, key): | ||
return key in self.word2id | ||
|
||
def __getitem__(self, key): | ||
return self.embed[self.word2id[key]] |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.