Data Scientist:Wit.ai Technology Implementation

Data Scientist:Wit.ai Technology Implementation

If this is the first article you read, we recommend that you first review the introduction of details about what Mandy is and What is Wit.ai technology. This will allow you to supplement the PubChem programming code to use the Mandy program – the Smart Companion. On the other hand, if you are just trying to use the PubChem library API then you can continue reading.

artificial intelligence with wit.ai

Wit.ai Technology Implementation

Mandy – The Intelligent Companion is an AI tool that was programmed in python and that you can use to identify situations in which chemical compounds are involved. Wit.ai is the technology that makes you identify the intention of a user when receiving a question or comment at the start of the application.

As you can verify, Wit.ai technology identifies when users are interested in doing things that require them to know about the chemical compatibility, storage intention and information intention of a chemical compound.

Specifically the app identifies the following intents:

• Chemical compatibility search intention: The user wants to know about the chemical compatibility of a specific chemical compound. 
• Storage intention: The user needs to store a chemical compound or product that contains a specific chemical compound. 
• Information search intention: The user wants to know about the properties and characteristics of a chemical compound.


Create a Wit.ai Account

First you need to create a wit.ai account so that you get an access token for your application. Type wit.ai in any browser with access to the internet and follow these steps:

• Click on sign up
• Submit an email, username and password
• Verify the email registered
• Click on the button create a New App

The variable access_token_wit must contain the token that you generated from in you wit.ai account so that you can find this variable at the beginning of the programming code. You will find some code like this one:

access_token_wit=’XXXXXXXXXXXXXXXXXX’

Chemical compound and Intention Identification

Train your robot with data

Once you obtained you wit access token, you can start training your robot to identify chemical compounds and intentions from the users’ message. You can train your robot on the wit.ai website or use the API that is provided so that you automate the training process. Here you can see how to implement the requests so that you submit utterances, intents, etc.

Utterances API Programming Code

Intents API programming Code

The application uses the following python libraries that allow you to get the right information for the user: it • Wit: this library identifies the user intention as something is mentioned on the input box of the application • json: this library is used to handle data receive from the response of the different APIs integrated in the application

This section contains a main function called wit_request that receives the question from the user and the token that was generated at wit.ai. Depth, extract and json_extract are two subfunctions that are used to extract the data received from wit.ai so that you obtain only the chemicals’ names and the intention from the user’s message.

wit_request Main Function

def wit_request(question,access_token_wit):

client = Wit(access_token=access_token_wit)
resp=client.message(msg=question)

data_extraction=json.dumps(resp)
data=json.loads(data_extraction)

def depth(data):
    if "entities" in data:
        return 1 +  max([0] + list(map(depth, data['entities'])))      
    else:
        return 1
levels_with_entities=depth(data)

def json_extract(obj,key):
    arr=[]
    def extract(obj,arr,key):
        if isinstance(obj,dict):
            for k,v in obj.items():
                if isinstance(v,(dict,list)):
                    extract(v,arr,key)
                elif v==key:
                    if obj not in arr:
                        arr.append(obj)
        elif isinstance(obj,list):
            for item in obj:
                extract(item,arr,key)
        return (arr)
    values=extract(obj,arr,key)
    return values
#get intents
intent=resp['intents'][0]['name']
#extract chemicals that wit.ai found
result_confirm=json_extract(data,'chemical_substance')
chemicals=[]
number_chemicals=len(result_confirm)
for q in range(number_chemicals):
    chemicals.append(result_confirm[q]['value'])   

return (chemicals,intent)


Json_Extract and Subfunctions

These functions are used to obtain chemicals’ names and intents from the wit.ai response that is saved in the data variable. Essentially, these functions receive an object which can be a list, array or dictionary and parse the data of those objects until the values of the intents key and chemical_subtance key are reached after iterating over the different objects contained in the json response.

We suggest you consult the flow diagram of Mandy the Intelligent Companion so that you understand the stages and functions that are activated at a certain moment in the process of transcribing the audio of your videos. The youtube module implementation phase is the final phase so you can check the first phase (technology implementation) wit.ai and second (PubChem technology implementation).