<s> from __future__ import print_function <EOL> import os <EOL> import sys <EOL> import errno <EOL> from marmot . features . feature_extractor import FeatureExtractor <EOL> from marmot . util . alignments import train_alignments , align_sentence <EOL> from marmot . util . ngram_window_extractor import left_context , right_context <EOL> from marmot . exceptions . no_data_error import NoDataError <EOL> class AlignmentFeatureExtractor ( FeatureExtractor ) : <EOL> def __init__ ( self , align_model = '<STR_LIT>' , src_file = '<STR_LIT>' , tg_file = '<STR_LIT>' , tmp_dir = None , context_size = <NUM_LIT:1> ) : <EOL> if tmp_dir is None : <EOL> tmp_dir = os . getcwd ( ) <EOL> try : <EOL> os . makedirs ( tmp_dir ) <EOL> except OSError as exc : <EOL> if exc . errno == errno . EEXIST and os . path . isdir ( tmp_dir ) : <EOL> pass <EOL> else : <EOL> raise <EOL> self . tmp_dir = tmp_dir <EOL> self . model = '<STR_LIT>' <EOL> if align_model == '<STR_LIT>' : <EOL> if os . path . isfile ( src_file ) and os . path . isfile ( tg_file ) : <EOL> self . model = train_alignments ( src_file , tg_file , self . tmp_dir ) <EOL> else : <EOL> self . model = align_model <EOL> self . context_size = context_size <EOL> def get_features ( self , context_obj ) : <EOL> if '<STR_LIT:source>' not in context_obj or context_obj [ '<STR_LIT:source>' ] is None : <EOL> raise NoDataError ( '<STR_LIT:source>' , context_obj , '<STR_LIT>' ) <EOL> if '<STR_LIT:target>' not in context_obj or context_obj [ '<STR_LIT:source>' ] is None or context_obj [ '<STR_LIT:target>' ] is None : <EOL> raise NoDataError ( '<STR_LIT:target>' , context_obj , '<STR_LIT>' ) <EOL> if '<STR_LIT>' not in context_obj : <EOL> raise NoDataError ( '<STR_LIT>' , context_obj , '<STR_LIT>' ) <EOL> try : <EOL> align_idx = context_obj [ '<STR_LIT>' ] [ context_obj [ '<STR_LIT:index>' ] ] <EOL> except IndexError : <EOL> print ( "<STR_LIT>" . format ( len ( context_obj [ '<STR_LIT>' ] ) , context_obj [ '<STR_LIT:index>' ] ) ) <EOL> print ( context_obj [ '<STR_LIT>' ] , context_obj [ '<STR_LIT:target>' ] , context_obj [ '<STR_LIT:source>' ] ) <EOL> sys . exit ( ) <EOL> if align_idx == None : <EOL> return [ '<STR_LIT>' , '<STR_LIT:|>' . join ( [ '<STR_LIT>' for i in range ( self . context_size ) ] ) , '<STR_LIT:|>' . join ( [ '<STR_LIT>' for i in range ( self . context_size ) ] ) ] <EOL> else : <EOL> left = '<STR_LIT:|>' . join ( left_context ( context_obj [ '<STR_LIT:source>' ] , context_obj [ '<STR_LIT:source>' ] [ align_idx ] , context_size = self . context_size , idx = align_idx ) ) <EOL> right = '<STR_LIT:|>' . join ( right_context ( context_obj [ '<STR_LIT:source>' ] , context_obj [ '<STR_LIT:source>' ] [ align_idx ] , context_size = self . context_size , idx = align_idx ) ) <EOL> aligned_to = context_obj [ '<STR_LIT:source>' ] [ align_idx ] <EOL> return [ aligned_to , left , right ] <EOL> def get_feature_names ( self ) : <EOL> return [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] </s>
