<s> from logging import getLogger <EOL> from scipy . sparse . construct import hstack <EOL> from sklearn . base import BaseEstimator <EOL> from sklearn . feature_extraction . text import VectorizerMixin <EOL> from commonml . utils import get_nested_value <EOL> import numpy as np <EOL> logger = getLogger ( '<STR_LIT>' ) <EOL> class CustomDictVectorizer ( BaseEstimator , VectorizerMixin ) : <EOL> def __init__ ( self , vect_rules ) : <EOL> self . vect_rules = vect_rules <EOL> def fit ( self , raw_documents , y = None ) : <EOL> for vect_rule in self . vect_rules : <EOL> name = vect_rule . get ( '<STR_LIT:name>' ) <EOL> vect = vect_rule . get ( '<STR_LIT>' ) <EOL> if not hasattr ( vect , '<STR_LIT>' ) : <EOL> vect . fit ( map ( lambda x : get_nested_value ( x , name , '<STR_LIT>' ) , raw_documents ) ) <EOL> def transform ( self , raw_documents ) : <EOL> results = [ ] <EOL> for vect_rule in self . vect_rules : <EOL> name = vect_rule . get ( '<STR_LIT:name>' ) <EOL> vect = vect_rule . get ( '<STR_LIT>' ) <EOL> if hasattr ( vect , '<STR_LIT>' ) : <EOL> data = vect ( map ( lambda x : get_nested_value ( x , name , '<STR_LIT>' ) , raw_documents ) ) <EOL> else : <EOL> data = vect . transform ( map ( lambda x : get_nested_value ( x , name , '<STR_LIT>' ) , raw_documents ) ) <EOL> results . append ( data ) <EOL> return hstack ( results , format = '<STR_LIT>' , dtype = np . float32 ) <EOL> def fit_transform ( self , raw_documents , y = None ) : <EOL> results = [ ] <EOL> for vect_rule in self . vect_rules : <EOL> name = vect_rule . get ( '<STR_LIT:name>' ) <EOL> vect = vect_rule . get ( '<STR_LIT>' ) <EOL> if hasattr ( vect , '<STR_LIT>' ) : <EOL> data = vect ( map ( lambda x : get_nested_value ( x , name , '<STR_LIT>' ) , raw_documents ) ) <EOL> else : <EOL> data = vect . fit_transform ( map ( lambda x : get_nested_value ( x , name , '<STR_LIT>' ) , raw_documents ) ) <EOL> results . append ( data ) <EOL> return hstack ( results , format = '<STR_LIT>' , dtype = np . float32 ) <EOL> def get_feature_names ( self , append_name = True ) : <EOL> results = [ ] <EOL> for vect_rule in self . vect_rules : <EOL> vect = vect_rule . get ( '<STR_LIT>' ) <EOL> if append_name : <EOL> name = vect_rule . get ( '<STR_LIT:name>' ) <EOL> names = map ( lambda x : u'<STR_LIT>' . format ( name , x ) , vect . get_feature_names ( ) ) <EOL> else : <EOL> names = vect . get_feature_names ( ) <EOL> results . extend ( names ) <EOL> return results <EOL> def get_feature_size ( self ) : <EOL> size = <NUM_LIT:0> <EOL> for vect_rule in self . vect_rules : <EOL> vect = vect_rule . get ( '<STR_LIT>' ) <EOL> size += len ( vect . vocabulary_ ) <EOL> return size <EOL> def inverse_transform ( self , X ) : <EOL> names = np . array ( self . get_feature_names ( ) ) <EOL> def get_names ( x ) : <EOL> indices = np . argwhere ( x . toarray ( ) . flatten ( ) > <NUM_LIT:0> ) . flatten ( ) <EOL> if len ( indices ) == <NUM_LIT:0> : <EOL> return [ ] <EOL> else : <EOL> return names [ indices ] <EOL> return map ( lambda x : get_names ( x ) , X ) </s>
