<s> import os <EOL> import sys <EOL> srilm_swig_wrapper = os . environ . get ( "<STR_LIT>" ) <EOL> if ( srilm_swig_wrapper is None ) : <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> sys . path . insert ( <NUM_LIT:1> , srilm_swig_wrapper ) <EOL> from srilm import * <EOL> class SRILangModel ( object ) : <EOL> '''<STR_LIT>''' <EOL> log_normalizer = <NUM_LIT> <EOL> __slots__ = "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" <EOL> def __init__ ( self , lm_order , lmFile , elider_str ) : <EOL> '''<STR_LIT>''' <EOL> self . lm_order = lm_order <EOL> self . LM = initLM ( lm_order ) <EOL> self . elider = elider_str <EOL> self . loadLM ( lmFile ) <EOL> def __del__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> deleteLM ( self . LM ) <EOL> def loadLM ( self , lmFile ) : <EOL> '''<STR_LIT>''' <EOL> readLM ( self . LM , lmFile ) <EOL> def queryLM ( self , phr , phr_len ) : <EOL> '''<STR_LIT>''' <EOL> return getNGramProb ( self . LM , phr , phr_len ) / SRILangModel . log_normalizer <EOL> def queryLMlog10 ( self , phr , phr_len ) : <EOL> '''<STR_LIT>''' <EOL> return getNGramProb ( self . LM , phr , phr_len ) <EOL> def calcUNKLMScore ( self , sent ) : <EOL> '''<STR_LIT>''' <EOL> sent_len = len ( sent . split ( ) ) <EOL> return scoreUNK ( self . LM , sent , sent_len ) / SRILangModel . log_normalizer <EOL> def scorePhrnElide ( self , wordsLst , phr_len , mgramSpans ) : <EOL> '''<STR_LIT>''' <EOL> lm_temp = <NUM_LIT:0.0> <EOL> for ( mgram_beg , mgram_end ) in mgramSpans : <EOL> for i in range ( mgram_beg , mgram_end - ( self . lm_order - <NUM_LIT:1> ) ) : <EOL> lm_temp += getNGramProb ( self . LM , '<STR_LIT:U+0020>' . join ( wordsLst [ i : i + self . lm_order ] ) , self . lm_order ) <EOL> e_tgt = '<STR_LIT:U+0020>' . join ( wordsLst [ <NUM_LIT:0> : self . lm_order - <NUM_LIT:1> ] + [ self . elider ] + wordsLst [ phr_len - ( self . lm_order - <NUM_LIT:1> ) : ] ) <EOL> return ( lm_temp / SRILangModel . log_normalizer , e_tgt ) <EOL> def getLMHeuCost ( self , wordsLst , phr_len ) : <EOL> """<STR_LIT>""" <EOL> lmHueLst = [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] <EOL> if ( phr_len < self . lm_order ) : initWrds = wordsLst <EOL> else : initWrds = wordsLst [ <NUM_LIT:0> : self . lm_order - <NUM_LIT:1> ] <EOL> if ( wordsLst [ <NUM_LIT:0> ] == '<STR_LIT>' ) : <EOL> is_S_rule = True <EOL> else : <EOL> is_S_rule = False <EOL> initWrds . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> part_lm_w_edge = <NUM_LIT:0.0> <EOL> part_lm_wo_edge = <NUM_LIT:0.0> <EOL> for i in range ( <NUM_LIT:1> , len ( initWrds ) ) : <EOL> part_lm_w_edge += getNGramProb ( self . LM , '<STR_LIT:U+0020>' . join ( initWrds [ : ( i + <NUM_LIT:1> ) ] ) , i + <NUM_LIT:1> ) <EOL> if ( is_S_rule ) : <EOL> pass <EOL> else : <EOL> part_lm_wo_edge += getNGramProb ( self . LM , '<STR_LIT:U+0020>' . join ( initWrds [ <NUM_LIT:1> : ( i + <NUM_LIT:1> ) ] ) , i ) <EOL> lm_heu_w_edge = part_lm_w_edge / SRILangModel . log_normalizer <EOL> if ( is_S_rule ) : <EOL> lm_heu_wo_edge = lm_heu_w_edge <EOL> else : <EOL> lm_heu_wo_edge = part_lm_wo_edge / SRILangModel . log_normalizer <EOL> lmHueLst [ <NUM_LIT:1> ] += lm_heu_w_edge <EOL> lmHueLst [ <NUM_LIT:0> ] += lm_heu_wo_edge <EOL> lmHueLst [ <NUM_LIT:2> ] += lm_heu_wo_edge <EOL> last_indx = phr_len - <NUM_LIT:1> <EOL> if ( not is_S_rule and wordsLst [ last_indx ] != "<STR_LIT>" ) : <EOL> if last_indx <= self . lm_order - <NUM_LIT:1> : phr_beg_indx = <NUM_LIT:1> <EOL> else : phr_beg_indx = last_indx - self . lm_order + <NUM_LIT:2> <EOL> phr_end = '<STR_LIT:U+0020>' . join ( wordsLst [ phr_beg_indx : ] ) + "<STR_LIT>" <EOL> phr_end_len = last_indx - phr_beg_indx + <NUM_LIT:2> <EOL> lmHueLst [ <NUM_LIT:2> ] += ( getNGramProb ( self . LM , phr_end , phr_end_len ) / SRILangModel . log_normalizer ) <EOL> return max ( lmHueLst ) </s>
