<s> import numpy as np <EOL> import sys <EOL> import logging <EOL> from gensim . models import Word2Vec <EOL> from marmot . features . feature_extractor import FeatureExtractor <EOL> logging . basicConfig ( format = '<STR_LIT>' , level = logging . INFO ) <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> def left_context ( token_list , token , context_size , idx ) : <EOL> left_window = [ ] <EOL> if idx <= <NUM_LIT:0> : <EOL> return [ '<STR_LIT>' for i in range ( context_size ) ] <EOL> assert ( token_list [ idx ] == token ) <EOL> for i in range ( idx - context_size , idx ) : <EOL> if i < <NUM_LIT:0> : <EOL> left_window . append ( '<STR_LIT>' ) <EOL> else : <EOL> left_window . append ( token_list [ i ] ) <EOL> return left_window <EOL> def right_context ( token_list , token , context_size , idx ) : <EOL> right_window = [ ] <EOL> if idx >= len ( token_list ) : <EOL> return [ '<STR_LIT>' for i in range ( context_size ) ] <EOL> assert ( token_list [ idx ] == token ) , "<STR_LIT>" . format ( token_list [ idx ] , idx , token ) <EOL> for i in range ( idx + <NUM_LIT:1> , idx + context_size + <NUM_LIT:1> ) : <EOL> if i > len ( token_list ) - <NUM_LIT:1> : <EOL> right_window . append ( '<STR_LIT>' ) <EOL> else : <EOL> right_window . append ( token_list [ i ] ) <EOL> return right_window <EOL> class Word2VecFeatureExtractor ( FeatureExtractor ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , w2v_file , combination = '<STR_LIT>' , context_size = <NUM_LIT:2> ) : <EOL> self . model = Word2Vec . load ( w2v_file ) <EOL> self . default_vector = np . average ( np . array ( [ self . model [ x ] for x in self . model . vocab ] ) , axis = <NUM_LIT:0> ) . reshape ( ( - <NUM_LIT:1> , ) ) <EOL> self . zero_vector = np . zeros ( self . default_vector . shape [ <NUM_LIT:0> ] ) <EOL> self . context_size = context_size <EOL> if combination == '<STR_LIT>' : <EOL> self . combine = np . sum <EOL> elif combination == '<STR_LIT>' : <EOL> self . combine = np . average <EOL> else : <EOL> print ( "<STR_LIT>" . format ( combination ) ) <EOL> def extract_word2vec_vector ( self , token ) : <EOL> if token in self . model . vocab : <EOL> return self . model [ token ] <EOL> elif token == '<STR_LIT>' or token == '<STR_LIT>' : <EOL> return self . zero_vector <EOL> else : <EOL> return self . default_vector <EOL> def get_features ( self , context_obj ) : <EOL> if '<STR_LIT>' not in context_obj or len ( context_obj [ '<STR_LIT>' ] ) == <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" , context_obj ) <EOL> sys . exit ( ) <EOL> if '<STR_LIT:index>' not in context_obj or len ( context_obj [ '<STR_LIT:index>' ] ) == <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" , context_obj ) <EOL> sys . exit ( ) <EOL> if context_obj [ '<STR_LIT:index>' ] [ <NUM_LIT:0> ] == context_obj [ '<STR_LIT:index>' ] [ <NUM_LIT:1> ] : <EOL> print ( "<STR_LIT>" , context_obj [ '<STR_LIT:target>' ] ) <EOL> print ( "<STR_LIT>" . format ( context_obj [ '<STR_LIT:index>' ] [ <NUM_LIT:0> ] , context_obj [ '<STR_LIT:index>' ] [ <NUM_LIT:1> ] ) ) <EOL> phrase_vector = [ ] <EOL> if type ( context_obj [ '<STR_LIT>' ] ) is list or type ( context_obj [ '<STR_LIT>' ] ) is np . ndarray : <EOL> left_window = left_context ( context_obj [ '<STR_LIT:target>' ] , context_obj [ '<STR_LIT>' ] [ <NUM_LIT:0> ] , self . context_size , context_obj [ '<STR_LIT:index>' ] [ <NUM_LIT:0> ] ) <EOL> right_window = right_context ( context_obj [ '<STR_LIT:target>' ] , context_obj [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] , self . context_size , context_obj [ '<STR_LIT:index>' ] [ - <NUM_LIT:1> ] - <NUM_LIT:1> ) <EOL> phrase_vector = [ self . extract_word2vec_vector ( tok ) for tok in context_obj [ '<STR_LIT>' ] ] <EOL> phrase_vector = self . combine ( phrase_vector , axis = <NUM_LIT:0> ) <EOL> else : <EOL> left_window = left_context ( context_obj [ '<STR_LIT:target>' ] , context_obj [ '<STR_LIT>' ] , self . context_size , context_obj [ '<STR_LIT:index>' ] ) <EOL> right_window = right_context ( context_obj [ '<STR_LIT:target>' ] , context_obj [ '<STR_LIT>' ] , self . context_size , context_obj [ '<STR_LIT:index>' ] ) <EOL> phrase_vector = self . extract_word2vec_vector ( context_obj [ '<STR_LIT>' ] ) <EOL> vector = [ ] <EOL> for tok in left_window : <EOL> vector . extend ( self . extract_word2vec_vector ( tok ) ) <EOL> vector . extend ( phrase_vector ) <EOL> for tok in right_window : <EOL> vector . extend ( self . extract_word2vec_vector ( tok ) ) <EOL> return np . hstack ( vector ) <EOL> def get_feature_names ( self ) : <EOL> return [ '<STR_LIT>' + str ( i ) for i in range ( len ( self . default_vector ) * ( self . context_size * <NUM_LIT:2> + <NUM_LIT:1> ) ) ] </s>
