#NEWLINE# def load_wordvecs_from_binfile( word_vec_file , vocab = None ) : #NEWLINE# #INDENT# '\n    Load word vectors from bin file\n    :param word_vec_file: file path\n    :param vocab: vocabulary. If not none, only words from this vocab will be loaded\n    :return: dictionary of word to word_vector\n    ' #NEWLINE# with open ( word_vec_file , 'rb' ) as f : #NEWLINE# #INDENT# word_vecs = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# header = f . readline ( ) #NEWLINE# ( vocab_size , layer1_size ) = map ( int , header . split ( ) ) #NEWLINE# binary_len = ( np . dtype ( 'float32' ) . itemsize * layer1_size ) #NEWLINE# i = 0 #NEWLINE# for line in xrange ( i ) : #NEWLINE# #INDENT# word = [ ] #NEWLINE# while True : #NEWLINE# #INDENT# ch = f . read ( 1 ) #NEWLINE# if ( ch == ' ' ) : #NEWLINE# #INDENT# word = '' . join ( word ) #NEWLINE# break #NEWLINE# #UNINDENT# if ( ch != '\n' ) : #NEWLINE# #INDENT# word . append ( ch ) #NEWLINE# #UNINDENT# #UNINDENT# if ( ( vocab == None ) or ( word in vocab ) ) : #NEWLINE# #INDENT# word_vecs [ word ] = np . fromstring ( f . read ( binary_len ) , dtype = 'float32' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# f . read ( binary_len ) #NEWLINE# #UNINDENT# #UNINDENT# return word_vecs