Melon

MELON INTENTS




Add your own commands


to add your own commands first you will need to find intents.json which is usually located in datas/intents.json to edit the file you will need to open the json file with an editor.

The basic format for editing is :

{ "intents": [ { "tag": "Your tag", "patterns": ["Some patterns"], "responses": ["Responses to give for the pattern"], "context": [""] } ] }
For example :
I want Melon to give me reply as [see you soon] or [bye] when i tell [bye] or [see you later] ,
For that the code would be { "intents" : [ { "tag": "goodbye", "patterns": ["Bye", "See you later", "Goodbye", "Nice chatting to you, bye", "Till next time"], "responses": ["See you!", "Have a nice day", "Bye! Come back again soon."], "context": [""] } ] }

After editing intents.json run Train_assistant.exe or the assistant will return an error


Executing Commands after giving reply or after taking input

To execute a command after Taking input we will be using the "context" key.
Format :
{ "intents": [ { "tag": "Your tag", "patterns": ["Some patterns"], "responses": ["Responses to give for the pattern"], "context": ["Your command to be executed"] } ] }

For example :


I want melon to tell me the weather when i ask for weather.For that I would first define [weather()] in the assistant.py file so For that you would be creating a function to tell weather in assistant.py
def weather(): from bs4 import BeautifulSoup as bs import requests USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64)" # US english LANGUAGE = "en-US,en;q=0.5" def get_weather_data(url): session = requests.Session() session.headers['User-Agent'] = USER_AGENT session.headers['Accept-Language'] = LANGUAGE session.headers['Content-Language'] = LANGUAGE html = session.get(url) # create a new soup soup = bs(html.text, "html.parser") # store all results on this dictionary result = {} # extract region result['region'] = soup.find("div", attrs={"id": "wob_loc"}).text # extract temperature now result['temp_now'] = soup.find("span", attrs={"id": "wob_tm"}).text # get the day and hour now result['dayhour'] = soup.find("div", attrs={"id": "wob_dts"}).text # get the actual weather result['weather_now'] = soup.find("span", attrs={"id": "wob_dc"}).text # get the precipitation result['precipitation'] = soup.find("span", attrs={"id": "wob_pp"}).text # get the % of humidity result['humidity'] = soup.find("span", attrs={"id": "wob_hm"}).text # extract the wind result['wind'] = soup.find("span", attrs={"id": "wob_ws"}).text # get next few days' weather next_days = [] days = soup.find("div", attrs={"id": "wob_dp"}) for day in days.findAll("div", attrs={"class": "wob_df"}): # extract the name of the day # get weather status for that day weather = day.find("img").attrs["alt"] temp = day.findAll("span", {"class": "wob_t"}) # maximum temparature in Celsius, use temp[1].text if you want fahrenheit max_temp = temp[0].text # minimum temparature in Celsius, use temp[3].text if you want fahrenheit min_temp = temp[2].text next_days.append({"weather": weather, "max_temp": max_temp, "min_temp": min_temp}) # append to result result['next_days'] = next_days return result if __name__ == "__main__": URL = "https://www.google.com/search?lr=lang_en&ie=UTF-8&q=weather" import argparse parser = argparse.ArgumentParser() parser.add_argument("region", nargs="?", default="") # parse arguments args = parser.parse_args() region = args.region URL += region # get data data = get_weather_data(URL) # print data reg = str(data["region"]) now = str(data["dayhour"]) temp = str(data['temp_now']) des = str(data['weather_now']) print("exctracted data\n "+"region :"+reg +"\n time : " +now +"\n temperature :" +temp +"\n description :"+ des) speak("the weather in "+reg+" is "+des+" with the temperature of "+temp+" degree celsius ")
As we have written the code of extracting weather for google weather we make it tell the weather in the end Now we will tell it execute this function when the user asks for weather by adding this to intents.json
{ "intents": [ { "tag": "weather", "patterns": ["what is the weather","will it rain today","weather","weather please" ], "responses": ["Today's weather"], "context": ["weather"] } ] }

Another example for sending mail

For this we first define sendmail in our python file which will be looking like this
def sendmail(): import webbrowser url = "mailto:example@testmail.com&body=sent using melon desktop assistant" #open new tab if browser is already open webbrowser.open_new_tab(url)
Adding Context for performing action
{ "intents": [ "tag": "mail", "patterns": ["mails","new mails" ], "responses": ["opening mail"], "context": ["sendmail"] ] }

Powered by SAS tech