forked from explosion/sense2vec
-
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.
Merge pull request explosion#11 from elyase/master
Add gensim to sense2vec format convert script
- Loading branch information
Showing
2 changed files
with
34 additions
and
1 deletion.
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,30 @@ | ||
from sense2vec.vectors import VectorMap | ||
from gensim.models import Word2Vec | ||
import plac | ||
|
||
@plac.annotations( | ||
gensim_model_path=("Location of gensim's .bin file"), | ||
out_dir=("Location of output directory"), | ||
min_count=("Min count", "option", "m", int), | ||
) | ||
def main(gensim_model_path, out_dir, min_count=None): | ||
"""Convert a gensim.models.Word2Vec file to VectorMap format""" | ||
|
||
gensim_model = Word2Vec.load(gensim_model_path) | ||
vector_map = VectorMap(128) | ||
|
||
if min_count is None: | ||
min_count = gensim_model.min_count | ||
|
||
for string in gensim_model.vocab: | ||
vocab = gensim_model.vocab[string] | ||
freq, idx = vocab.count, vocab.index | ||
if freq < min_count: | ||
continue | ||
vector = gensim_model.syn0[idx] | ||
vector_map.borrow(string, freq, vector) | ||
|
||
vector_map.save(out_dir) | ||
|
||
if __name__ == '__main__': | ||
plac.call(main) |
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