<s> import urllib2 <EOL> import re <EOL> import vim <EOL> from tq_common_lib import decode_utf_8 , encode_utf_8 , fixurl <EOL> try : <EOL> from StringIO import StringIO <EOL> except ImportError : <EOL> from io import StringIO <EOL> class word_query_handler_thesaurus_lookup : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> self . identifier = "<STR_LIT>" <EOL> self . language = "<STR_LIT>" <EOL> self . header_length = <NUM_LIT:11> <EOL> self . relavent_val_pos = <NUM_LIT:9> <EOL> self . syno_pos = <NUM_LIT:11> <EOL> self . truncation_on_relavance = int ( vim . eval ( "<STR_LIT>" ) ) <EOL> def query_cmd_handler ( self , word ) : <EOL> self . syno_list = [ ] <EOL> query_result_raw = decode_utf_8 ( online_thesaurus_lookup ( word ) ) <EOL> self . query_result = StringIO ( query_result_raw ) <EOL> def synonym_found ( self ) : <EOL> first_line = self . query_result . readline ( ) <EOL> if first_line != u'<STR_LIT:\n>' : <EOL> if u"<STR_LIT>" in first_line : <EOL> self . query_result . close ( ) <EOL> return - <NUM_LIT:1> <EOL> self . query_result . close ( ) <EOL> return <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> def syno_populating ( self ) : <EOL> word_dic = { } <EOL> syno_list_curr = [ ] <EOL> self . query_result . readline ( ) <EOL> while True : <EOL> self . line_curr = self . query_result . readline ( ) <EOL> if self . line_curr [ : self . relavent_val_pos ] != u"<STR_LIT>" : <EOL> break <EOL> self . line_curr = self . line_curr . rstrip ( u'<STR_LIT:\n>' ) <EOL> if self . line_curr [ self . relavent_val_pos ] in word_dic . keys ( ) : <EOL> word_dic [ self . line_curr [ self . relavent_val_pos ] ] . append ( self . line_curr [ self . syno_pos : ] ) <EOL> else : <EOL> word_dic [ self . line_curr [ self . relavent_val_pos ] ] = [ self . line_curr [ self . syno_pos : ] ] <EOL> for key in sorted ( word_dic , reverse = True ) : <EOL> if key <= self . truncation_on_relavance : <EOL> continue <EOL> syno_list_curr = syno_list_curr + word_dic [ key ] <EOL> del word_dic <EOL> return [ not not syno_list_curr , syno_list_curr ] <EOL> def process_query_result ( self ) : <EOL> status = True <EOL> self . line_curr = self . query_result . readline ( ) <EOL> while not ( not self . line_curr or not status ) : <EOL> if self . line_curr [ : self . header_length ] == u'<STR_LIT>' : <EOL> self . line_curr = self . line_curr . rstrip ( u'<STR_LIT:\n>' ) <EOL> definition_curr = self . line_curr [ self . header_length + <NUM_LIT:1> : ] <EOL> self . syno_list . append ( [ definition_curr , [ ] ] ) <EOL> [ status , self . syno_list [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] ] = self . syno_populating ( ) <EOL> else : <EOL> self . line_curr = self . query_result . readline ( ) <EOL> self . query_result . close ( ) <EOL> return status and not not self . syno_list <EOL> def query ( self , word ) : <EOL> self . query_cmd_handler ( word ) <EOL> query_status = self . synonym_found ( ) <EOL> if query_status != <NUM_LIT:0> : <EOL> return [ query_status , self . syno_list ] <EOL> if self . process_query_result ( ) : <EOL> return [ <NUM_LIT:0> , self . syno_list ] <EOL> return [ - <NUM_LIT:1> , [ ] ] <EOL> def online_thesaurus_lookup ( target ) : <EOL> '''<STR_LIT>''' <EOL> output = "<STR_LIT>" <EOL> try : <EOL> response = urllib2 . urlopen ( fixurl ( u'<STR_LIT>' . format ( target ) ) ) <EOL> parser = StringIO ( response . read ( ) ) <EOL> response . close ( ) <EOL> except urllib2 . HTTPError , error : <EOL> output = "<STR_LIT>" . format ( encode_utf_8 ( target ) ) <EOL> return output <EOL> except urllib2 . URLError , error : <EOL> output = "<STR_LIT>" . format ( encode_utf_8 ( target ) ) <EOL> return output <EOL> end_tag_count = <NUM_LIT:2> <EOL> while True : <EOL> line_curr = parser . readline ( ) <EOL> if not line_curr : <EOL> break <EOL> if "<STR_LIT>" in line_curr : <EOL> output = "<STR_LIT>" . format ( target ) <EOL> break <EOL> if "<STR_LIT>" in line_curr : <EOL> end_tag_count = <NUM_LIT:0> <EOL> continue <EOL> elif end_tag_count < <NUM_LIT:2> : <EOL> if "<STR_LIT>" in line_curr : <EOL> end_tag_count += <NUM_LIT:1> <EOL> continue <EOL> fields = re . split ( "<STR_LIT>" , line_curr ) <EOL> if len ( fields ) < <NUM_LIT:3> : <EOL> continue <EOL> elif len ( fields ) < <NUM_LIT:10> : <EOL> if "<STR_LIT>" in fields [ <NUM_LIT:1> ] : <EOL> output += "<STR_LIT>" . format ( fields [ <NUM_LIT:2> ] ) <EOL> continue <EOL> elif "<STR_LIT>" in fields [ <NUM_LIT:1> ] : <EOL> output += "<STR_LIT>" . format ( fields [ <NUM_LIT:2> ] ) <EOL> continue <EOL> elif "<STR_LIT>" in fields [ <NUM_LIT:3> ] : <EOL> output += "<STR_LIT>" . format ( fields [ <NUM_LIT:6> ] , fields [ <NUM_LIT> ] ) <EOL> parser . close ( ) <EOL> return output </s>
