Skip to content

Instantly share code, notes, and snippets.

@kanekomasahiro
Last active April 10, 2021 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanekomasahiro/3c75b219471ae45a3c97e6b31127985f to your computer and use it in GitHub Desktop.
Save kanekomasahiro/3c75b219471ae45a3c97e6b31127985f to your computer and use it in GitHub Desktop.
gensimのembeddingをloadする
import linecache
from gensim.models import KeyedVectors
def load_embedding_with_gensim(embedding_name):
'''
Load embeddings with gensim.
'''
if embedding_name.endswith('bin'):
binary = True
no_header = False
else:
binary = False
if linecache.getline(embedding_name, 1).split() == 2:
no_header = False
else:
no_header = True
embedding = KeyedVectors.load_word2vec_format(embedding_name, binary=binary, no_header=no_header)
return embedding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment