<s> from lxmls . sequences . label_dictionary import * <EOL> import pdb <EOL> class IDFeatures : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , dataset ) : <EOL> '''<STR_LIT>''' <EOL> self . feature_dict = LabelDictionary ( ) <EOL> self . feature_list = [ ] <EOL> self . add_features = False <EOL> self . dataset = dataset <EOL> self . node_feature_cache = { } <EOL> self . initial_state_feature_cache = { } <EOL> self . final_state_feature_cache = { } <EOL> self . edge_feature_cache = { } <EOL> def get_num_features ( self ) : <EOL> return len ( self . feature_dict ) <EOL> def build_features ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . add_features = True <EOL> for sequence in self . dataset . seq_list : <EOL> initial_features , transition_features , final_features , emission_features = self . get_sequence_features ( sequence ) <EOL> self . feature_list . append ( [ initial_features , transition_features , final_features , emission_features ] ) <EOL> self . add_features = False <EOL> def get_sequence_features ( self , sequence ) : <EOL> '''<STR_LIT>''' <EOL> emission_features = [ ] <EOL> initial_features = [ ] <EOL> transition_features = [ ] <EOL> final_features = [ ] <EOL> features = [ ] <EOL> features = self . add_initial_features ( sequence , sequence . y [ <NUM_LIT:0> ] , features ) <EOL> initial_features . append ( features ) <EOL> for pos , tag in enumerate ( sequence . y ) : <EOL> features = [ ] <EOL> features = self . add_emission_features ( sequence , pos , sequence . y [ pos ] , features ) <EOL> emission_features . append ( features ) <EOL> if pos > <NUM_LIT:0> : <EOL> prev_tag = sequence . y [ pos - <NUM_LIT:1> ] <EOL> features = [ ] <EOL> features = self . add_transition_features ( sequence , pos - <NUM_LIT:1> , tag , prev_tag , features ) <EOL> transition_features . append ( features ) <EOL> features = [ ] <EOL> features = self . add_final_features ( sequence , sequence . y [ - <NUM_LIT:1> ] , features ) <EOL> final_features . append ( features ) <EOL> return initial_features , transition_features , final_features , emission_features <EOL> def get_emission_features ( self , sequence , pos , y ) : <EOL> all_feat = [ ] <EOL> x = sequence . x [ pos ] <EOL> if ( x not in self . node_feature_cache ) : <EOL> self . node_feature_cache [ x ] = { } <EOL> if ( y not in self . node_feature_cache [ x ] ) : <EOL> node_idx = [ ] <EOL> node_idx = self . add_emission_features ( sequence , pos , y , node_idx ) <EOL> self . node_feature_cache [ x ] [ y ] = node_idx <EOL> idx = self . node_feature_cache [ x ] [ y ] <EOL> all_feat = idx [ : ] <EOL> return all_feat <EOL> def get_transition_features ( self , sequence , pos , y , y_prev ) : <EOL> assert ( pos >= <NUM_LIT:0> and pos < len ( sequence . x ) ) , pdb . set_trace ( ) <EOL> if ( y not in self . edge_feature_cache ) : <EOL> self . edge_feature_cache [ y ] = { } <EOL> if ( y_prev not in self . edge_feature_cache [ y ] ) : <EOL> edge_idx = [ ] <EOL> edge_idx = self . add_transition_features ( sequence , pos , y , y_prev , edge_idx ) <EOL> self . edge_feature_cache [ y ] [ y_prev ] = edge_idx <EOL> return self . edge_feature_cache [ y ] [ y_prev ] <EOL> def get_initial_features ( self , sequence , y ) : <EOL> if ( y not in self . initial_state_feature_cache ) : <EOL> edge_idx = [ ] <EOL> edge_idx = self . add_initial_features ( sequence , y , edge_idx ) <EOL> self . initial_state_feature_cache [ y ] = edge_idx <EOL> return self . initial_state_feature_cache [ y ] <EOL> def get_final_features ( self , sequence , y_prev ) : <EOL> if ( y_prev not in self . final_state_feature_cache ) : <EOL> edge_idx = [ ] <EOL> edge_idx = self . add_final_features ( sequence , y_prev , edge_idx ) <EOL> self . final_state_feature_cache [ y_prev ] = edge_idx <EOL> return self . final_state_feature_cache [ y_prev ] <EOL> def add_initial_features ( self , sequence , y , features ) : <EOL> y_name = self . dataset . y_dict . get_label_name ( y ) <EOL> feat_name = "<STR_LIT>" % ( y_name ) <EOL> feat_id = self . add_feature ( feat_name ) <EOL> if ( feat_id != - <NUM_LIT:1> ) : <EOL> features . append ( feat_id ) <EOL> return features <EOL> def add_final_features ( self , sequence , y_prev , features ) : <EOL> y_name = self . dataset . y_dict . get_label_name ( y_prev ) <EOL> feat_name = "<STR_LIT>" % ( y_name ) <EOL> feat_id = self . add_feature ( feat_name ) <EOL> if ( feat_id != - <NUM_LIT:1> ) : <EOL> features . append ( feat_id ) <EOL> return features <EOL> def add_emission_features ( self , sequence , pos , y , features ) : <EOL> '''<STR_LIT>''' <EOL> x = sequence . x [ pos ] <EOL> y_name = self . dataset . y_dict . get_label_name ( y ) <EOL> x_name = self . dataset . x_dict . get_label_name ( x ) <EOL> feat_name = "<STR_LIT>" % ( x_name , y_name ) <EOL> feat_id = self . add_feature ( feat_name ) <EOL> if feat_id != - <NUM_LIT:1> : <EOL> features . append ( feat_id ) <EOL> return features <EOL> def add_transition_features ( self , sequence , pos , y , y_prev , features ) : <EOL> """<STR_LIT>""" <EOL> assert pos < len ( sequence . x ) - <NUM_LIT:1> , pdb . set_trace ( ) <EOL> y_name = self . dataset . y_dict . get_label_name ( y ) <EOL> y_prev_name = self . dataset . y_dict . get_label_name ( y_prev ) <EOL> feat_name = "<STR_LIT>" % ( y_prev_name , y_name ) <EOL> feat_id = self . add_feature ( feat_name ) <EOL> if ( feat_id != - <NUM_LIT:1> ) : <EOL> features . append ( feat_id ) <EOL> return features <EOL> def add_feature ( self , feat_name ) : <EOL> """<STR_LIT>""" <EOL> if ( feat_name in self . feature_dict ) : <EOL> return self . feature_dict [ feat_name ] <EOL> if not self . add_features : <EOL> return - <NUM_LIT:1> <EOL> return self . feature_dict . add ( feat_name ) </s>
