<s> """<STR_LIT>""" <EOL> import os <EOL> import shutil <EOL> import hashlib <EOL> from sys import platform <EOL> import gensim <EOL> def prepend_line ( infile , outfile , line ) : <EOL> """<STR_LIT>""" <EOL> with open ( infile , '<STR_LIT:r>' ) as old : <EOL> with open ( outfile , '<STR_LIT:w>' ) as new : <EOL> new . write ( str ( line ) + "<STR_LIT:\n>" ) <EOL> shutil . copyfileobj ( old , new ) <EOL> def prepend_slow ( infile , outfile , line ) : <EOL> """<STR_LIT>""" <EOL> with open ( infile , '<STR_LIT:r>' ) as fin : <EOL> with open ( outfile , '<STR_LIT:w>' ) as fout : <EOL> fout . write ( line + "<STR_LIT:\n>" ) <EOL> for line in fin : <EOL> fout . write ( line ) <EOL> def checksum ( filename ) : <EOL> """<STR_LIT>""" <EOL> BLOCKSIZE = <NUM_LIT> <EOL> hasher = hashlib . md5 ( ) <EOL> with open ( filename , '<STR_LIT:rb>' ) as afile : <EOL> buf = afile . read ( BLOCKSIZE ) <EOL> while len ( buf ) > <NUM_LIT:0> : <EOL> hasher . update ( buf ) <EOL> buf = afile . read ( BLOCKSIZE ) <EOL> return hasher . hexdigest ( ) <EOL> pretrain_num_lines = { "<STR_LIT>" : <NUM_LIT> , "<STR_LIT>" : <NUM_LIT> } <EOL> pretrain_checksum = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> def check_num_lines_in_glove ( filename , check_checksum = False ) : <EOL> if check_checksum : <EOL> assert checksum ( filename ) == pretrain_checksum [ filename ] <EOL> if filename . startswith ( '<STR_LIT>' ) : <EOL> return <NUM_LIT> <EOL> elif filename . startswith ( '<STR_LIT>' ) : <EOL> return <NUM_LIT> <EOL> else : <EOL> return pretrain_num_lines [ filename ] <EOL> glove_file = "<STR_LIT>" <EOL> _ , tokens , dimensions , _ = glove_file . split ( '<STR_LIT:.>' ) <EOL> num_lines = check_num_lines_in_glove ( glove_file ) <EOL> dims = int ( dimensions [ : - <NUM_LIT:1> ] ) <EOL> gensim_file = '<STR_LIT>' <EOL> gensim_first_line = "<STR_LIT>" . format ( num_lines , dims ) <EOL> if platform == "<STR_LIT>" or platform == "<STR_LIT>" : <EOL> prepend_line ( glove_file , gensim_file , gensim_first_line ) <EOL> else : <EOL> prepend_slow ( glove_file , gensim_file , gensim_first_line ) <EOL> model = gensim . models . Word2Vec . load_word2vec_format ( gensim_file , binary = False ) <EOL> print model . most_similar ( positive = [ '<STR_LIT>' ] , topn = <NUM_LIT:10> ) <EOL> print model . similarity ( '<STR_LIT>' , '<STR_LIT>' ) </s>
