def __create_response_for_what_to_V(df): df_after_what_to = df.loc[Nlp_util.get_idx_list_of_idiom_list( ["what to", "how to"], df["base_form"])[0] + 2:, :] words_after_what_to = WordFormatter.Df2Str(df_after_what_to) cmp = [ ["it must be not easy to find how to" + words_after_what_to], ["now you are looking for the way to" + words_after_what_to], ["should be not that easy to find how to" + words_after_what_to], ] encourage = [ [ "but i am sure that thinking about it and speaking out it helps you🤗" ], [ "eventho its always tough to find the right way, you try to figure it out. Thats impressing me😊" ], [ "plz let me know any idea comes to your mind now. it might help you figuring it out☺️" ], [ "tell me if you have any little idea. It could help you finding ur way😊" ], ] random_idx_for_cmp = randint(0, len(cmp) - 1) random_idx_for_encourage = randint(0, len(encourage) - 1) return cmp[random_idx_for_cmp] + encourage[random_idx_for_encourage]
def __alter_repeat_for_need_sent(fixed_df): try: idx_of_need = Nlp_util.get_idx_list_of_word("need", fixed_df["base_form"])[0] row_of_first_noun = Nlp_util.get_wordsDF_of_wordlist_after_idx(fixed_df, Nlp_util.pos_NOUNs + Nlp_util.pos_PRPs, idx_of_need, column_name="pos").iloc[0] if fixed_df.loc[row_of_first_noun.name-1, "pos"] in Nlp_util.pos_ADJECTIVEs + ["PRP$", "DT"]: noun = WordFormatter.Series2Str(fixed_df.loc[row_of_first_noun.name-1:row_of_first_noun.name, "word"]) else: noun = fixed_df.loc[row_of_first_noun.name, "word"] noun_nominative = Nlp_util.convert_objective_noun_to_nominative(noun) options = [ ["so " + noun_nominative + " is very important thing for you..", "and sounds its kinda hard to get it now right😢"], ["so its like its not easy to get " + noun + " now but you really want..", "and it can frustrate you😞"], ["sounds you really want " + noun + "..", "might be tough time for you to seek for it now😓"] ] random_idx_for_options = randint(0, len(options) - 1) return options[random_idx_for_options] except: logging.exception('') repeat_text = WordFormatter.Df2Str(fixed_df)[1:] return [repeat_text]
def request_to_apiai(df): try: message = WordFormatter.Df2Str(df) client_access_token = os.environ.get('client_access_token', None) session_id = os.environ.get('session_id', None) ai = ApiAI(client_access_token) request = ai.text_request() request.session_id = session_id request.query = message response = json.loads(request.getresponse().read().decode('utf-8')) try: if response is not None: if 'action' in response['result'].keys(): candidate = response['result']['action'] if Intent.has_value(candidate): return Intent(candidate) except: logging.exception('') if is_haha_intent(message): return Intent.HAHA return Intent.NORMAL except: logging.exception('') return Intent.NORMAL
def __alter_repeat_for_because_sent(df, repeat_text): if df["base_form"].iloc[0] in ["because", "since"]: repeat_text = "its " + repeat_text return [repeat_text] elif Df_util.anything_isin( ["because of"], df.loc[2:, "base_form"]) and not Df_util.anything_isin( ["it is", "that is"], df.loc[:3, "base_form"]): because_of_idx = Nlp_util.get_idx_list_of_idiom( "because of", df["base_form"])[0] first_part = WordFormatter.Df2Str(df.loc[:because_of_idx - 1, :]) last_part = "and its" + WordFormatter.Df2Str( df.loc[because_of_idx:, :]) return [first_part, last_part] else: raise UnknownError
def __alter_repeat_for_want_to(cls, repeat_df): try: i_idx = Nlp_util.get_idx_list_of_idiom("want to", repeat_df.word)[0] words_after_wanna = WordFormatter.Df2Str(repeat_df[i_idx + 2:])[1:] response_options = [ [words_after_wanna, "That's what you wanna do"], ["So you'd be happy if you can " + words_after_wanna + "🤔"], ["So there is something makes you can't " + words_after_wanna + "😢"], ["So now it's hard for you to " + words_after_wanna + "😓"] ] random_idx = randint(0, len(response_options) - 1) return response_options[random_idx] except: logging.exception('') repeat_text = WordFormatter.Df2Str(repeat_df)[1:] return [repeat_text]
def __generate_repeat(cls, text_df, text_kw_df, sidx_to_repeat): repeat_list = [] for idx, sidx in enumerate(sidx_to_repeat): target_df = text_df[text_df.sidx == sidx].copy().reset_index( drop=True) fixed_df = cls.__replace_word_by_csv(target_df) # TODO fix the convert_df_to_str() so that it would not need [1:] part. repeat_text = WordFormatter.Df2Str(fixed_df) # TODO here has to be same structure as message_type_filter since # TODO one sentence can have "want to" and despising word at the same time. if len(target_df) == 1 and \ (target_df["pos"].iloc[0] in Nlp_util.pos_ADJECTIVEs or target_df["word"].iloc[0] in SENTIMENTAL_NON_ADJ_WORDS.word.tolist()): repeat_list += cls.create_special_repeat_for_only_one_adj_word_sent( target_df) elif cls.__mean_no_friends(target_df): repeat_list += cls.__create_response_for_no_friends() elif cls.__has_what_to_do(target_df): repeat_list += cls.__create_response_for_what_to_V(fixed_df) elif cls.__is_despising_himself(target_df): repeat_list += cls.__alter_repeat_euphemistic(repeat_text) elif cls.__has_nobody_V(target_df): repeat_list += cls.__alter_repeat_euphemistic(repeat_text) elif cls.__does_user_feel_useless(target_df): repeat_list += cls.__create_response_for_healing_useless() elif cls.__has_say_plus_bad_word(target_df): repeat_list += cls.__create_response_for_S_said_bad_word( fixed_df) elif cls.__exists_want_to(target_df): repeat_list += cls.__alter_repeat_for_want_to(fixed_df) elif cls.__exists_make_S_feel_ADJ(target_df): repeat_list += cls.__alter_repeat_for_make_S_feel_ADJ( target_df) elif cls.__has_because(target_df): repeat_list += cls.__alter_repeat_for_because_sent( fixed_df, repeat_text) elif cls.__exists_third_person_BeVerb_pair(target_df): repeat_list += cls.__alter_repeat_for_third_person_BeVerb_pair( repeat_text) elif cls.__has_dont_think_SV_sent(target_df): repeat_list += cls.__alter_repeat_for_dont_think_SV(fixed_df) elif cls.__has_wish_S_V(target_df): repeat_list += cls.__alter_repeat_for_wish(fixed_df) elif cls.__has_need_NN(target_df): repeat_list += cls.__alter_repeat_for_need_sent(fixed_df) elif cls.__exists_keyword(text_kw_df): is_last_sentence = idx == len(sidx_to_repeat) - 1 repeat_list += cls.__alter_repeat_for_keyword( text_df, text_kw_df, idx, repeat_text, is_last_sentence=is_last_sentence) else: repeat_list += cls.__alter_repeat_for_plain_repeat( repeat_text, idx) print('**************111111************\n', repeat_list) return repeat_list