<s> import vim <EOL> from tq_common_lib import decode_utf_8 , encode_utf_8 <EOL> class Thesaurus_Query_Handler : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , cache_size_max = <NUM_LIT:100> ) : <EOL> self . wordlist_size_max = cache_size_max <EOL> self . restore_thesaurus_query_handler ( ) <EOL> self . query_backend_define ( ) <EOL> def query_backend_define ( self ) : <EOL> """<STR_LIT>""" <EOL> from mthesaur_lookup import word_query_mthesaur_lookup <EOL> from thesaurus_com_lookup import word_query_handler_thesaurus_lookup as word_query_thesaurus_com_lookup <EOL> import datamuse_com_lookup <EOL> import jeck_ru_lookup <EOL> self . query_backends = { } <EOL> backend_thesaurus_com = word_query_thesaurus_com_lookup ( ) <EOL> backend_datamuse_com = datamuse_com_lookup <EOL> backend_mthesaur_txt = word_query_mthesaur_lookup ( ) <EOL> backend_jeck_ru = jeck_ru_lookup <EOL> self . query_backends [ backend_thesaurus_com . identifier ] = backend_thesaurus_com <EOL> self . query_backends [ backend_datamuse_com . identifier ] = backend_datamuse_com <EOL> self . query_backends [ backend_mthesaur_txt . identifier ] = backend_mthesaur_txt <EOL> self . query_backends [ backend_jeck_ru . identifier ] = backend_jeck_ru <EOL> def query ( self , word ) : <EOL> if word in self . word_list : <EOL> return self . word_list [ word ] <EOL> error_encountered = <NUM_LIT:0> <EOL> good_backends = [ ] <EOL> faulty_backends = [ ] <EOL> for query_backend_curr in self . query_backend_priority : <EOL> specified_language = get_variable ( "<STR_LIT>" ) <EOL> if specified_language != "<STR_LIT>" : <EOL> if specified_language == - <NUM_LIT:1> : <EOL> if self . query_backends [ query_backend_curr ] . language != '<STR_LIT>' : <EOL> continue <EOL> elif self . query_backends [ query_backend_curr ] . language not in specified_language : <EOL> continue <EOL> [ state , synonym_list ] = self . query_backends [ query_backend_curr ] . query ( word ) <EOL> if state == - <NUM_LIT:1> : <EOL> error_encountered = <NUM_LIT:1> <EOL> faulty_backends . append ( self . query_backends [ query_backend_curr ] . identifier ) <EOL> continue <EOL> if state == <NUM_LIT:0> : <EOL> good_backends . append ( self . query_backends [ query_backend_curr ] . identifier ) <EOL> break <EOL> for faulty in faulty_backends : <EOL> self . query_backend_priority . remove ( faulty ) <EOL> self . query_backend_priority += faulty_backends <EOL> if int ( vim . eval ( "<STR_LIT>" ) ) == <NUM_LIT:1> : <EOL> for good in good_backends : <EOL> self . query_backend_priority . remove ( good ) <EOL> self . query_backend_priority = good_backends + self . query_backend_priority <EOL> if error_encountered == <NUM_LIT:1> : <EOL> vim . command ( '<STR_LIT>' ) <EOL> if '<STR_LIT:state>' not in locals ( ) : <EOL> vim . command ( '<STR_LIT>' ) <EOL> return [ ] <EOL> if state == <NUM_LIT:0> : <EOL> self . word_list [ word ] = synonym_list <EOL> self . word_list_keys . append ( word ) <EOL> if len ( self . word_list_keys ) > self . wordlist_size_max : <EOL> del self . word_list [ self . word_list_keys . pop ( <NUM_LIT:0> ) ] <EOL> return synonym_list <EOL> def restore_thesaurus_query_handler ( self ) : <EOL> self . query_backend_priority = vim . eval ( "<STR_LIT>" ) <EOL> self . word_list = { } <EOL> self . word_list_keys = [ ] <EOL> if vim . eval ( '<STR_LIT>' ) : <EOL> local_as_primary = vim . eval ( "<STR_LIT>" ) <EOL> else : <EOL> local_as_primary = None <EOL> if local_as_primary == "<STR_LIT:1>" : <EOL> self . query_backend_priority . remove ( "<STR_LIT>" ) <EOL> self . query_backend_priority . insert ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> def get_variable ( v_name ) : <EOL> '''<STR_LIT>''' <EOL> if vim . eval ( "<STR_LIT>" . format ( v_name ) ) == '<STR_LIT:0>' : <EOL> if vim . eval ( "<STR_LIT>" . format ( v_name ) ) == '<STR_LIT:0>' : <EOL> return - <NUM_LIT:1> <EOL> else : <EOL> return vim . eval ( '<STR_LIT>' + v_name ) <EOL> else : <EOL> return vim . eval ( '<STR_LIT>' + v_name ) <EOL> def truncate_synonym_list ( synonym_list ) : <EOL> truncated_flag = <NUM_LIT:0> <EOL> truncate_on_definition = int ( vim . eval ( "<STR_LIT>" ) ) <EOL> truncate_syno_list = int ( vim . eval ( "<STR_LIT>" ) ) <EOL> output_buffer = [ ] <EOL> if truncate_on_definition > <NUM_LIT:0> : <EOL> if truncate_on_definition <= len ( synonym_list ) : <EOL> truncated_flag = <NUM_LIT:1> <EOL> output_buffer_temp = ( synonym_list if truncate_on_definition == - <NUM_LIT:1> else synonym_list [ : truncate_on_definition ] ) <EOL> for definition in output_buffer_temp : <EOL> if truncate_syno_list == - <NUM_LIT:1> : <EOL> output_buffer . append ( definition ) <EOL> else : <EOL> if truncate_syno_list < len ( definition [ <NUM_LIT:1> ] ) : <EOL> truncated_flag = <NUM_LIT:1> <EOL> output_buffer . append ( [ definition [ <NUM_LIT:0> ] , definition [ <NUM_LIT:1> ] [ : truncate_syno_list ] ] ) <EOL> del output_buffer_temp <EOL> return [ truncated_flag , output_buffer ] <EOL> def tq_word_form_reverse ( target_word ) : <EOL> '''<STR_LIT>''' <EOL> wordOriginal = decode_utf_8 ( vim . eval ( '<STR_LIT>' ) ) <EOL> if wordOriginal . isupper ( ) : <EOL> return target_word . upper ( ) <EOL> elif wordOriginal [ <NUM_LIT:0> ] . isupper ( ) : <EOL> return target_word [ <NUM_LIT:0> ] . upper ( ) + target_word [ <NUM_LIT:1> : ] <EOL> return target_word <EOL> def tq_candidate_list_populate ( candidates ) : <EOL> '''<STR_LIT>''' <EOL> waitlist = [ ] <EOL> result_IDed = [ ] <EOL> word_ID = <NUM_LIT:0> <EOL> for syno_case in candidates : <EOL> result_IDed . append ( [ syno_case [ <NUM_LIT:0> ] , [ ] ] ) <EOL> for word_curr in syno_case [ <NUM_LIT:1> ] : <EOL> word_curr = tq_word_form_reverse ( word_curr ) <EOL> result_IDed [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] . append ( u"<STR_LIT>" . format ( word_ID , word_curr ) ) <EOL> waitlist . append ( u"<STR_LIT:{}>" . format ( word_curr ) ) <EOL> word_ID += <NUM_LIT:1> <EOL> return [ word_ID , waitlist , result_IDed ] <EOL> def candidate_list_printing ( result_IDed ) : <EOL> '''<STR_LIT>''' <EOL> for case in result_IDed : <EOL> if case [ <NUM_LIT:0> ] != u"<STR_LIT>" : <EOL> vim . command ( '<STR_LIT>' . format ( encode_utf_8 ( case [ <NUM_LIT:0> ] ) ) ) <EOL> vim . command ( '<STR_LIT>' ) <EOL> col_count = <NUM_LIT:10> <EOL> col_count_max = int ( vim . eval ( "<STR_LIT>" ) ) <EOL> for synonym_i in case [ <NUM_LIT:1> ] : <EOL> if ( col_count + len ( synonym_i ) + <NUM_LIT:1> ) < col_count_max : <EOL> vim . command ( '<STR_LIT>' . format ( encode_utf_8 ( synonym_i ) ) ) <EOL> col_count += len ( synonym_i ) + <NUM_LIT:1> <EOL> else : <EOL> vim . command ( '<STR_LIT>' . format ( encode_utf_8 ( synonym_i ) ) ) <EOL> col_count = <NUM_LIT:10> + len ( synonym_i ) + <NUM_LIT:1> <EOL> vim . command ( '<STR_LIT>' ) <EOL> def tq_replace_cursor_word_from_candidates ( candidate_list ) : <EOL> '''<STR_LIT>''' <EOL> [ truncated_flag , candidates ] = truncate_synonym_list ( candidate_list ) <EOL> [ candidate_num , thesaurus_wait_list , syno_result_IDed ] = tq_candidate_list_populate ( candidates ) <EOL> vim . command ( "<STR_LIT>" . format ( vim . current . line . replace ( '<STR_LIT:\\>' , '<STR_LIT>' ) . replace ( '<STR_LIT:">' , '<STR_LIT>' ) ) ) <EOL> vim . command ( "<STR_LIT>" . format ( vim . eval ( "<STR_LIT>" ) ) ) <EOL> candidate_list_printing ( syno_result_IDed ) <EOL> def obtain_user_choice ( trunc_flag ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> if trunc_flag == <NUM_LIT:0> : <EOL> thesaurus_user_choice = vim . eval ( "<STR_LIT>" ) <EOL> else : <EOL> thesaurus_user_choice = vim . eval ( "<STR_LIT>" ) <EOL> except KeyboardInterrupt : <EOL> return None <EOL> return thesaurus_user_choice <EOL> thesaurus_user_choice = obtain_user_choice ( truncated_flag ) <EOL> if not thesaurus_user_choice : <EOL> return <EOL> if thesaurus_user_choice == "<STR_LIT:A>" : <EOL> tq_generate_thesaurus_buffer ( candidate_list ) <EOL> return <EOL> try : <EOL> thesaurus_user_choice = int ( thesaurus_user_choice ) <EOL> except ValueError : <EOL> vim . command ( '<STR_LIT>' ) <EOL> return <EOL> if thesaurus_user_choice >= candidate_num or thesaurus_user_choice < <NUM_LIT:0> : <EOL> vim . command ( '<STR_LIT>' ) <EOL> return <EOL> vim . command ( "<STR_LIT>" . format ( encode_utf_8 ( thesaurus_wait_list [ thesaurus_user_choice ] ) ) ) <EOL> def tq_generate_thesaurus_buffer ( candidates ) : <EOL> '''<STR_LIT>''' <EOL> vim . command ( "<STR_LIT>" ) <EOL> if int ( vim . eval ( "<STR_LIT>" ) ) > - <NUM_LIT:1> : <EOL> vim . command ( '<STR_LIT>' ) <EOL> else : <EOL> vim . command ( '<STR_LIT>' ) <EOL> vim . command ( '<STR_LIT>' ) <EOL> vim . command ( '<STR_LIT>' ) <EOL> vim . command ( '<STR_LIT>' ) <EOL> tq_thesaurus_buffer = vim . current . buffer <EOL> del tq_thesaurus_buffer [ : ] <EOL> line_count = <NUM_LIT:0> <EOL> tq_thesaurus_buffer . append ( [ "<STR_LIT>" ] ) <EOL> tq_thesaurus_buffer [ line_count ] = "<STR_LIT>" . format ( vim . eval ( '<STR_LIT>' ) ) <EOL> line_count += <NUM_LIT:1> <EOL> for case in candidates : <EOL> tq_thesaurus_buffer . append ( [ "<STR_LIT>" ] ) <EOL> if not case [ <NUM_LIT:0> ] : <EOL> tq_thesaurus_buffer [ line_count ] = '<STR_LIT>' . format ( "<STR_LIT:U+002CU+0020>" . join ( encode_utf_8 ( case [ <NUM_LIT:1> ] ) ) ) <EOL> line_count += <NUM_LIT:1> <EOL> continue <EOL> tq_thesaurus_buffer [ line_count : line_count + <NUM_LIT:2> ] = [ '<STR_LIT>' . format ( encode_utf_8 ( case [ <NUM_LIT:0> ] ) ) , '<STR_LIT>' . format ( encode_utf_8 ( "<STR_LIT:U+002CU+0020>" . join ( case [ <NUM_LIT:1> ] ) ) ) ] <EOL> line_count += <NUM_LIT:2> <EOL> vim . command ( "<STR_LIT>" ) <EOL> vim . command ( "<STR_LIT>" ) <EOL> vim . command ( "<STR_LIT>" ) <EOL> vim . command ( "<STR_LIT>" ) <EOL> vim . command ( "<STR_LIT>" ) <EOL> vim . command ( "<STR_LIT>" ) <EOL> vim . command ( "<STR_LIT>" ) </s>
