#NEWLINE# def shorten_result( text = '' , keywords = [ ] , maxlen = 240 , fuzz = 60 ) : #NEWLINE# #INDENT# if ( not text ) : #NEWLINE# #INDENT# text = '' #NEWLINE# #UNINDENT# text_low = text . lower ( ) #NEWLINE# beg = ( - 1 ) #NEWLINE# for k in keywords : #NEWLINE# #INDENT# i = text_low . find ( k . lower ( ) ) #NEWLINE# if ( ( ( - 1 ) < i < beg ) or ( beg == ( - 1 ) ) ) : #NEWLINE# #INDENT# beg = i #NEWLINE# #UNINDENT# #UNINDENT# excerpt_beg = 0 #NEWLINE# if ( beg > fuzz ) : #NEWLINE# #INDENT# for sep in ( '.' , ':' , ';' , '=' ) : #NEWLINE# #INDENT# eb = text . find ( sep , ( beg - fuzz ) , ( beg - 1 ) ) #NEWLINE# if ( eb > ( - 1 ) ) : #NEWLINE# #INDENT# eb += 1 #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# eb = ( beg - fuzz ) #NEWLINE# #UNINDENT# excerpt_beg = eb #NEWLINE# #UNINDENT# if ( excerpt_beg < 0 ) : #NEWLINE# #INDENT# excerpt_beg = 0 #NEWLINE# #UNINDENT# msg = text [ excerpt_beg : ( beg + maxlen ) ] #NEWLINE# if ( beg > fuzz ) : #NEWLINE# #INDENT# msg = ( '... ' + msg ) #NEWLINE# #UNINDENT# if ( beg < ( len ( text ) - maxlen ) ) : #NEWLINE# #INDENT# msg += ' ...' #NEWLINE# #UNINDENT# return msg