<s> from __future__ import division <EOL> import numpy as np <EOL> from collections import defaultdict <EOL> from nltk import FreqDist <EOL> from gensim . corpora import TextCorpus <EOL> from marmot . features . feature_extractor import FeatureExtractor <EOL> class NumTranslationsFeatureExtractor ( FeatureExtractor ) : <EOL> def __init__ ( self , lex_prob_file , corpus_file ) : <EOL> self . lex_prob = defaultdict ( list ) <EOL> for line in open ( lex_prob_file ) : <EOL> chunks = line [ : - <NUM_LIT:1> ] . split ( ) <EOL> self . lex_prob [ chunks [ <NUM_LIT:1> ] ] . append ( float ( chunks [ <NUM_LIT:2> ] ) ) <EOL> corpus = TextCorpus ( input = corpus_file ) <EOL> self . corpus_freq = FreqDist ( [ word for line in corpus . get_texts ( ) for word in line ] ) <EOL> self . thresholds = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT:0.5> ] <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> return [ <NUM_LIT:0.0> for i in range ( len ( self . thresholds ) * <NUM_LIT:2> ) ] <EOL> translations , translations_weighted = [ ] , [ ] <EOL> for thr in self . thresholds : <EOL> all_words , all_words_weighted = [ ] , [ ] <EOL> for word in context_obj [ '<STR_LIT>' ] : <EOL> trans = [ fl for fl in self . lex_prob [ word ] if fl >= thr ] <EOL> all_words . append ( len ( trans ) ) <EOL> all_words_weighted . append ( len ( trans ) * self . corpus_freq . freq ( word ) ) <EOL> translations . append ( np . average ( all_words ) ) <EOL> translations_weighted . append ( np . average ( all_words_weighted ) ) <EOL> return translations + translations_weighted <EOL> def get_feature_names ( self ) : <EOL> return [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] </s>
