Home > other >  python API cant show non English language of list intents API in Dialogflow v2
python API cant show non English language of list intents API in Dialogflow v2

Time:05-25

I have written python API that can interact with Dialogflow engine and return me the list of intents of Dialogflow. The code is as follows:

def get(self, intent_id=None, get_child=None):
    try:

        print("ManageIntents get method api is called")

        global global_intent_list
        global is_parent
        if get_child is not None:
            return jsonify(self.get_children(intent_id))

        # retry: Union[google.api_core.retry.Retry,
        # timeout: Optional[float] = None
        i = 0
        if intent_id is None and len(global_intent_list) <= 0:
            intents_client = dialogflow.IntentsClient()
            parent = dialogflow.AgentsClient.agent_path(project_id)
            intents = intents_client.list_intents(request={"parent": parent, "intent_view": 'INTENT_VIEW_FULL'})

            for intent in intents:
                print(i)
                i = i   1
                intent_info = self.get_intent_data(intent)
                global_intent_list.append(intent_info)

            json_response = jsonify(global_intent_list)
            return json_response

            # return "success"
        elif intent_id is None and len(global_intent_list) > 0:
            json_response = jsonify(global_intent_list)
            return json_response
        else:
            intents_client = dialogflow.IntentsClient()
            intent_name = intents_client.intent_path(project_id, intent_id)
            intent = intents_client.get_intent(request={"name": intent_name, "intent_view": 'INTENT_VIEW_FULL'})
            # response_dictionary = self.get_intent_data(intent)

            return jsonify(self.get_intent_data(intent))

    except Exception as e:
        print(e)
        return str(e)

But when I am seeing the non English language intents, then I can find that if the request is "আমি কি ১০ টাকার অ্যাকাউন্ট খুলতে পারবো", then the response is "\340\246\225\340\247\203\340\246\267\340". How can I decode this response? Please help me.

CodePudding user response:

Your response "\340\246\225\340\247\203\340\246\267\340" resembles an escape sequence in string literal (resembles however isn't being obtained from a string manipulation):

Escape Sequence Meaning
\ooo            Character with octal value ooo (up to three octal digits)

I can't reproduce your response from your code snippet; therefore, my answer is based on hard-coded data, and shows how Python specific text encodings raw_unicode_escape and unicode_escape work in detail:

test_string = r"\340\246\225\340\247\203\340\246\267"    # hard-coded
print('test_string                  ', test_string)
print('.encode("raw_unicode_escape")',
  test_string.encode( 'raw_unicode_escape'))
print('.decode(    "unicode_escape")',
  test_string.encode( 'raw_unicode_escape').decode( 'unicode_escape'))
print('.encode("latin1").decode()   ', 
  test_string.encode( 'raw_unicode_escape').decode( 'unicode_escape').
              encode( 'latin1').decode( 'utf-8'))

Output: .\SO\72347302.py

test_string                   \340\246\225\340\247\203\340\246\267
.encode("raw_unicode_escape") b'\\340\\246\\225\\340\\247\\203\\340\\246\\267'
.decode(    "unicode_escape") à¦à§à¦·
.encode("latin1").decode()    কৃষ
  • Related