Exemple #1
0
        ActionExecuted(ACTION_LISTEN_NAME),
    ]

    tracker = get_tracker(events)

    assert tracker.last_executed_action_has("another") is False


@pytest.mark.parametrize(
    "events, expected_extracted_slots",
    [
        ([], {}),
        ([ActionExecuted("my_form")], {}),
        (
            [ActionExecuted("my_form"),
             SlotSet("my_slot", "some_value")],
            {
                "my_slot": "some_value"
            },
        ),
        (
            [
                ActionExecuted("my_form"),
                SlotSet("my_slot", "some_value"),
                SlotSet("some_other", "some_value2"),
            ],
            {
                "my_slot": "some_value",
                "some_other": "some_value2"
            },
        ),
Exemple #2
0
 def run(self, dispatcher, tracker, domain):
     style = tracker.get_slot('style')
     beer_already_rec = tracker.get_slot('beer')
     beer = get_beer(style, excl=beer_already_rec)
     dispatcher.utter_message("You might like this beer: {}!".format(beer))
     return [SlotSet("beer", beer)]
 def run(self, dispatcher: CollectingDispatcher, tracker: Tracker,
         domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
     # score=tracker.get_slot("hello_flag")
     # score=1
     return [SlotSet("hello_flag", "1")]
Exemple #4
0
 def run(self, dispatcher: CollectingDispatcher, tracker: Tracker,
         domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
     print(tracker.get_latest_entity_values("item"))
     # answer = choice(['success', 'failure'])
     # Wait for ROS respone
     return [SlotSet("bring_status", "success")]
Exemple #5
0
 def run(self, dispatcher, tracker, domain):
     return [
         SlotSet("main", None),
         SlotSet("duration", None),
         SlotSet("satisfied", None)
     ]
Exemple #6
0
    def run(self, dispatcher, tracker, domain):

        start_time = time.time()
        # Read email
        to_user = tracker.get_slot('email')

        # Check if the email address is entered i.e. whether the user wants the results to be sent over email
        if (to_user != "unknown"):
            # Setup email response
            from_user = '******'
            password = '******'
            subject = 'Foodie has found you the restaurants'
            msg = MIMEMultipart()
            msg['From'] = from_user
            msg['TO'] = to_user
            msg['Subject'] = subject
            # Read the search results
            response = tracker.get_slot('emailmsg')
            #formatting the body of the email
            rest_name = []
            rest_address = []
            rest_rating = []
            rest_budget = []
            for res in response[::2]:
                rest_name.append(res.split(' in ')[0])
                rest_address.append(
                    res.split(' in ')[1].split(' has a rating of ')[0])
                rest_rating.append(
                    res.split(' in ')[1].split(' has a rating of ')[1][:-1])
            for budget in response[1::2]:
                rest_budget.append(budget[:-1])
            txt = ""
            for idx in range(len(response) // 2):
                txt = txt + '<b>Restaurant Name:</b> ' + rest_name[idx] + '<br>'
                txt = txt + '<b>Restaurant locality address:</b> ' + rest_address[
                    idx] + '<br>'
                txt = txt + '<b>Budget:</b> ' + rest_budget[idx] + '<br>'
                txt = txt + '<b>Zomato user rating:</b> ' + rest_rating[
                    idx] + '<br>'
                txt = txt + '<br>'
            html = """\
                    <html>
                      <body>
                        <p>Greetings from Foodie!,<br>
                           <h3>Here are the top rated restaurants as per your criteria:</h3> <br>"""
            html = html + txt
            html = html + """
                           <br><b><i>Itadakimasu,</b></i><br><b>Foodie Team</b>
                        </p>
                      </body>
                    </html>
                    """
            msg.attach(MIMEText(html, 'html'))
            text = msg.as_string()
            # Create a secure SSL context
            try:
                context = ssl.create_default_context()
                with smtplib.SMTP_SSL("smtp.gmail.com", 465,
                                      context=context) as server:
                    server.login(from_user, password)
                    server.sendmail(from_user, to_user, text)
                    server.close()
                    # Inform the user
                    dispatcher.utter_message(
                        "The details have been emailed to you, you should receive it shortly. Bon Appetit!"
                    )

            except:
                dispatcher.utter_message(
                    "Sorry, we are having some problem in sending email to you. Please try after some time"
                )

        print("--- email sending %s seconds ---" % (time.time() - start_time))
        return [SlotSet('email', to_user)]
Exemple #7
0
    async def unisec_validate_slots(
        self,
        dispatcher: "CollectingDispatcher",
        tracker: "Tracker",
        domain: Dict[Text, Any],
    ) -> List[EventType]:
        # print("unisec validated slot called")
        slot_dict = []
        validator = UnisecValidator.getInstance()
        #reset local slot
        listSlot = self.required_validation_slot()
        self.validated_slots = {}
        for i in listSlot:
            self.validated_slots[i] = None
            self.validated_slots[i + "_validated"] = None

        for slot in self.validated_slots:
            if tracker.get_slot(slot) != None and slot[-10:] != '_validated':
                try:
                    validate_func = getattr(validator, "validate_" + slot)
                    origin = tracker.get_slot(slot)
                    validation_output = []
                    if isinstance(origin, str):
                        try:
                            validation_output = validate_func(' '.join(
                                tracker.get_slot(slot).split('_')))
                        except:
                            validation_output = validate_func(
                                tracker.get_slot(slot))
                        if validation_output is None:
                            print(
                                "vukihai: something went wrong with validation function: "
                                + "validate_" + slot)
                            validation_output = (1, tracker.get_slot(slot),
                                                 tracker.get_slot(slot))
                        # if validation_output[0] < 0.5:
                        #     validation_output = (1,None,None)
                        self.validated_slots[slot] = validation_output[1]
                        self.validated_slots[
                            slot + "_validated"] = validation_output[2]
                        slot_dict.append(SlotSet(slot, validation_output[1]))
                        slot_dict.append(
                            SlotSet(slot + "_validated", validation_output[2]))
                    else:
                        validation_output = []
                        for i in origin:
                            try:
                                validation_output.append(
                                    validate_func(' '.join(i.split('_'))))
                            except:
                                validation_output.append(validate_func(i))
                            # if validation_output[-1][0] < 0.5:
                            #     validation_output[-1] = (1,None,None)
                        self.validated_slots[slot] = []
                        self.validated_slots[slot + "_validated"] = []
                        for i in validation_output:
                            self.validated_slots[slot].append(i[1])
                            self.validated_slots[slot + "_validated"].append(
                                i[2])
                        slot_dict.append(
                            SlotSet(slot, self.validated_slots[slot]))
                        slot_dict.append(
                            SlotSet(slot + "_validated",
                                    self.validated_slots[slot + "_validated"]))

                    if self.validated_slots[slot] != None:
                        UnisecLogger.log(UnisecLogger.ENTITY_EXTRACTED, slot,
                                         self.validated_slots[slot],
                                         tracker.latest_message.get('text'))
                except:
                    print('vukihai: validate function not found: ' +
                          "validate_" + slot)
        # print("vukihai slot validate event")
        print(slot_dict)

        return slot_dict
Exemple #8
0
    def run(self, dispatcher, tracker, domain):
        config = {"user_key": "d7d2afaf2f19d43708fb09bbe480d6b0"}
        zomato = zomatopy.initialize_app(config)
        loc = tracker.get_slot('location')
        cuisine = tracker.get_slot('cuisine')
        price = tracker.get_slot('price')

        global response
        global cnt

        #find budget boundary
        minBudget, maxBudget = getMinMaxBudget(price)
        location_detail = zomato.get_location(loc, 1)

        d1 = json.loads(location_detail)
        lat = d1["location_suggestions"][0]["latitude"]
        lon = d1["location_suggestions"][0]["longitude"]
        cuisines_dict = {
            'bakery': 5,
            'american': 1,
            'mexican': 73,
            'chinese': 25,
            'cafe': 30,
            'italian': 55,
            'biryani': 7,
            'north indian': 50,
            'south indian': 85
        }
        results = zomato.restaurant_search("", lat, lon,
                                           str(cuisines_dict.get(cuisine)), 50)

        json_results = json.loads(results)

        #filter by budget
        temp_dict = {}
        final_dict = {}

        cnt = json_results['results_found']

        if json_results['results_found'] == 0:
            response = "no results"
        else:
            i = 0
            for restaurant in json_results['restaurants']:
                temp_dict = [
                    restaurant['restaurant']["user_rating"]
                    ["aggregate_rating"], restaurant['restaurant']['name'],
                    restaurant['restaurant']['location']['address'],
                    restaurant['restaurant']['average_cost_for_two']
                ]
                if (temp_dict[3] >= minBudget) and (temp_dict[3] <= maxBudget):
                    final_dict[i] = temp_dict
                    i = i + 1

        #sort by rating
        sorted_dict = {}
        for k, v in sorted(final_dict.items(),
                           key=lambda item: item[1][0],
                           reverse=True):
            sorted_dict[k] = v

        #prepare top 5 response
        response = ""
        if len(sorted_dict) == 0:
            response = "no results"
        else:
            i = 0
            for k, v in sorted_dict.items():
                i = i + 1
                response = response + "Found " + v[1] + " in " + v[
                    2] + " where price for two people is Rs." + str(
                        v[3]) + " and rating is " + str(v[0]) + "..." + "\n"
                if (i == 5):
                    break

        dispatcher.utter_message("-----" + response)

        return [SlotSet('location', loc)]
 def run(self, dispatcher, tracker, domain) -> List[EventType]:
     return [SlotSet("producto_desconocido", tracker.latest_message.get("text"))]
 def run(self, dispatcher, tracker, domain):
     MailID = tracker.get_slot('mail_id')
     sendmail(MailID, response)
     return [SlotSet('mail_id', MailID)]
Exemple #11
0
    def run(self, dispatcher, tracker, domain):
        #Extract the list of tier 1 and 2 cities from wikipage
        url = "https://en.wikipedia.org/wiki/Classification_of_Indian_cities"
        r = requests.get(url, verify=False)
        soup = BeautifulSoup(r.text, "html.parser")

        #Parse the content to extract names
        cities = list(
            map(lambda x: x.text.lower(),
                soup.find('table', class_='wikitable').find_all('a')))

        #Get the list of common synonym names for the cities in tier 1 and 2
        city_synonym = []
        #Extract the common city synonyms from this page
        r = requests.get(
            'https://www.scoopwhoop.com/news/whats-in-a-name/#.45rdcz1m2',
            verify=False)
        synonym_list = BeautifulSoup(r.text, 'html.parser').find(
            'div', class_='article-body').find_all('h2')

        #extract Synonym list
        for city in synonym_list:
            if re.search(r'^[0-9]{1,2}.+', city.text.strip()):
                city_synonym.append(city.text.strip().split()[1].lower())
                city_synonym.append(city.text.strip().split()[-1].lower())

        #Few other city synonyms as gathered from the internet
        city_synonym_2 = [
            'allahabad', 'atal-nagar', 'bangalore', 'baroda', 'bejawada ',
            'belagavi ', 'belgaum', 'benares', 'bengaluru', 'bijapur',
            'bombay', 'calcutta ', 'calicut', 'cannanore', 'cawnpore ',
            'chennai', 'cochin', 'gauhati', 'gulbarga ', 'gurgaon',
            'gurugram ', 'guwahati ', 'hubballi ', 'hubli ', 'indhur',
            'indore', 'jabalpur ', 'jajesmow ', 'jajmau', 'jubbulpore ',
            'kalaburagi ', 'kannur', 'kanpur', 'kochi ', 'kolkata', 'kollam',
            'kozhikode', 'kudanthai', 'kumbakonam ', 'madras', 'mangalore',
            'mangaluru', 'mumbai', 'bombay', 'mysore', 'mysore', 'mysuru',
            'nellore', 'panaji', 'panjim', 'pondicherry', 'poona ',
            'prayagraj', 'puducherry ', 'pune', 'quilon', 'raipur', 'shimla',
            'simhapuri', 'simla ', 'thiruvananthapuram', 'thrissur ',
            'tindivanam ', 'tinnevelly ', 'tinthirivanam ', 'tiruchirapalli',
            'tirunelveli', 'tiruvallikeni ', 'trichinopoly', 'trichur',
            'triplicane ', 'trivandrum ', 'vadodara ', 'varanasi ',
            'vatakara ', 'vijayapura ', 'vijayawada ', 'visakhapatnam ',
            'waltair'
        ]

        #List of all valid cities in India for verification. Parse from this page
        url1 = "https://en.wikipedia.org/wiki/List_of_cities_in_India_by_population"
        r1 = requests.get(url1, verify=False)
        soup1 = BeautifulSoup(r1.text, "html.parser")

        #Extract list of all cities in India
        all_cities_list = []
        temp_city = list(
            map(lambda x: x.text.lower(),
                soup1.find('table', class_='wikitable').find_all('a')))
        for city in temp_city:
            if not re.search(r'^.[0-9]{1,2}.+', city):
                all_cities_list.append(city)

        #get the location from user slot
        loc = tracker.get_slot('location')

        #Check for pincodes. Not supported for now
        if (loc.lower().isdigit()):
            dispatcher.utter_message(
                "Sorry I am naive I dont understand pincodes and digits for now. Please try some actual place!!"
            )
            return [SlotSet('location', None), SlotSet("location_ok", False)]
        #Check for invalid city
        elif not (loc.lower() in all_cities_list or loc.lower() in city_synonym
                  or loc.lower() in city_synonym_2 or loc.lower() in cities):
            dispatcher.utter_message("Sorry did'nt find any such location " +
                                     loc + " Can you please tell again?")
            return [SlotSet('location', None), SlotSet("location_ok", False)]
        #Check for valid city but no servicable by Zomato
        elif not (loc.lower() in city_synonym or loc.lower() in city_synonym_2
                  or loc.lower() in cities):
            dispatcher.utter_message("We do not operate in " + loc +
                                     " yet. Please try some other city.")
            return [SlotSet('location', None), SlotSet("location_ok", False)]
        #If valid city and servicable return true
        else:
            return [SlotSet('location', loc), SlotSet('location_ok', True)]
Exemple #12
0
    async def run(
        self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: "DomainDict",
    ) -> List[Dict[Text, Any]]:
        """
        Executes this action.

        Args:
            dispatcher: the dispatcher
            tracker: the tracker
            domain: the domain

        Returns: list of slots

        """

        slot_names_since_last_user_utterance: List[str] = []
        for event in reversed(tracker.events):
            event_type = event["event"]
            if event_type == "slot":
                slot_names_since_last_user_utterance.append(event["name"])
            elif event_type == "user":
                break

        object_name_or_type = (tracker.get_slot(SLOT_OBJECT_NAME_OR_TYPE)
                               if SLOT_OBJECT_NAME_OR_TYPE
                               in slot_names_since_last_user_utterance else
                               None)

        # found_object_names = (
        #     tracker.get_slot(SLOT_FOUND_OBJECT_NAMES)
        #     if SLOT_FOUND_OBJECT_NAMES in slot_names_since_last_user_utterance
        #     else None
        # )

        # object_activity_provided = (
        #     tracker.get_slot(SLOT_OBJECT_ACTIVITY_PROVIDED)
        #     if SLOT_OBJECT_ACTIVITY_PROVIDED
        #     in slot_names_since_last_user_utterance
        #     else None
        # )

        # If no parameters were set, then quit
        if not any([object_name_or_type]):
            # return [FollowupAction(name=question_answer_action.ACTION_NAME)]
            # return []
            # return [AllSlotsReset()]
            return []

        found_objects_by_name: List[Object] = []
        found_objects_by_type: List[Object] = []
        found_objects_by_things_provided: List[Object] = []

        # TODO: Prioritize finding things that were talked about recently (i.e. in 'found_objects')
        for object in self.objects:
            # Find objects by name
            if object_name_or_type.casefold() == object.name.casefold():
                found_objects_by_name.append(object)

            # Find objects by type
            if object_name_or_type in [
                    type.name.casefold() for type in object.types
            ]:
                found_objects_by_type.append(object)

            # Find objects by things provided
            if object_name_or_type in [
                    thing.name.casefold() for thing in object.things_provided
            ]:
                found_objects_by_things_provided.append(object)

        found_objects = (found_objects_by_name or found_objects_by_type
                         or found_objects_by_things_provided)

        return [
            SlotSet(
                key=SLOT_FOUND_OBJECT_NAMES,
                value=[object.name for object in found_objects]
                if len(found_objects) > 0 else None,
            ),
        ]
Exemple #13
0
    def run(self, dispatcher, tracker, domain):
        dbhost = tracker.get_slot('dbhost')
        dbdriver = tracker.get_slot('dbdriver')
        dbdialect = tracker.get_slot('dbdialect')
        dbusername = tracker.get_slot('dbusername')
        dbpassword = tracker.get_slot('dbpassword')
        dbname = tracker.get_slot('dbname')
        try:
            entities = ""
            engine = create_engine(
                str(dbdriver) + '+' + str(dbdialect) + '://' +
                str(dbusername) + ':' + str(dbpassword) + '@' + str(dbhost) +
                '/' + str(dbname))
            inspector = reflection.Inspector.from_engine(engine)
            tables_names = (inspector.get_table_names())
            column_names = []
            for table in tables_names:
                cols = inspector.get_columns(table)
                for col in cols:
                    column_names.append(col['name'])

            path1 = "data/lookup"
            if path.exists(path1) == True:
                try:
                    shutil.rmtree(path1)
                except Error as er:
                    path1.rmdir()
                os.makedirs(path1)
            else:
                os.makedirs(path1)

            domain = 'domain.yml'
            stream = open(domain, 'r')
            data = yaml.load(stream)

            for table in tables_names:
                path2 = path1 + "/" + table
                os.makedirs(path2)
                tb = path2 + "/" + table + "s.txt"
                t = open(tb, 'a+')
                t.write(table)
                t.close()
                hello = path2 + "/columns"
                os.makedirs(hello)
                cols = inspector.get_columns(table)
                for col in cols:
                    # data['entities'].append(col['name'])
                    # data['slots'][col['name']]={'type':'unfeaturized'}
                    with open('domain.yml', 'w') as fp:
                        fp.write(yaml.dump(data))
                    with engine.connect() as con:
                        rs = con.execute('SELECT {0} FROM {1}'.format(
                            col['name'], table))
                        resultset = [dict(row) for row in rs]

                    yo = hello + "/" + col['name']
                    os.makedirs(yo)
                    fl = yo + "/" + col['name']
                    cl = fl + "synonyms.txt"
                    clmn = open(cl, 'a')
                    clmn.write(col['name'])
                    clmn.close()

                    filename = fl + ".txt"
                    with open(filename, 'a+') as file:
                        for dic in resultset:
                            b = (str)(dic[col['name']]) + "\n"
                            file.write(b)
                    file.close()
                    writer = open('data/nlu.md', 'a+')
                    r = "\t- lookup/" + table + "/columns/" + col[
                        'name'] + "/" + col['name'] + ".txt"
                    writer.write("\n## synonym:" + col['name'] + "\n\t" + r)

            def occurences(searched_word):
                L = []
                with open("data/nlu.md", newline='') as file:
                    result = file.read()
                    words = word_tokenize(result)
                    for index, word in enumerate(words, 0):
                        if word == searched_word:
                            L.append(index)
                return L

            l = occurences("table")
            lcol = occurences("column")
            with open("data/nlu.md", newline='') as file1:
                result = file1.read()
                words = word_tokenize(result)

                for i in l:
                    x = randint(0, (len(tables_names) - 1))
                    syn = list()
                    for synset in wordnet.synsets(tables_names[x]):
                        for lemma in synset.lemmas():
                            syn.append(lemma.name())
                    syn.append(tables_names[x])
                    syn.append(tables_names[x])
                    syn.append(tables_names[x])
                    for j in syn:
                        k = randint(0, (len(syn) - 1))
                        if words[i - 3] == '[':
                            words[i - 3] = words[i - 3] + str(syn[k])
                        else:
                            words[i - 3] = str(syn[k])
                    if words[i + 1] != ')':
                        words[i] = "table"
                        words[i + 1] = ":"
                        words[i + 2] = tables_names[x]
                    else:
                        words[i] = "table:" + tables_names[x]
                for j in lcol:
                    x = randint(0, (len(column_names) - 1))
                    synC = list()
                    for synset in wordnet.synsets(column_names[x]):
                        for lemma in synset.lemmas():
                            synC.append(lemma.name())
                    synC.append(column_names[x])
                    synC.append(column_names[x])
                    synC.append(column_names[x])
                    for a in synC:
                        b = randint(0, (len(synC) - 1))
                        if words[j - 3] == '[':
                            words[j - 3] = '[' + str(synC[b])
                        else:
                            words[j - 3] = str(synC[b])
                    if words[j + 1] != ')':
                        words[j] = "column"
                        words[j + 1] = ":"
                        words[j + 2] = column_names[x]
                    else:
                        words[j] = "column:" + column_names[x]
                for j in range(len(words)):
                    if (words[j] == '-'):
                        words[j - 1] = str(words[j - 1]) + '\n'
                    if (words[j] == '#' and words[j - 1] == '#'):
                        words[j - 2] = str(words[j - 2]) + '\n'

                writer = open('data/nlu.md', 'w')
                for m in range(len(words) - 1):
                    if words[m] == '[' or words[m + 1] == ']' or words[
                            m + 1] == '(' or words[m] == '(' or words[
                                m + 1] == ')' or words[m] == 'intent' or words[
                                    m + 1] == ')\n' or (
                                        words[m] == '#' and words[m + 1] == '#'
                                    ) or words[m] == ':' or words[m +
                                                                  1] == ":":
                        writer.write(words[m])
                    else:
                        writer.write(words[m] + ' ')
                writer.write(words[len(words) - 1])
        except Error as e:
            print("Error while connecting to database", e)
        return [
            SlotSet("tableNames", list(tables_names)),
            SlotSet("columnNames", list(column_names))
        ]
    def run(self, dispatcher, tracker, domain):
        config = {"user_key": "20c9c0971cf659fba29b5b9f78015f8d"}
        zomato = zomatopy.initialize_app(config)
        loc = tracker.get_slot('location')
        if loc is None:
            dispatcher.utter_message(template="utter_ask_location")
            return []
        print("locations is: " + str(loc))

        cuisine = tracker.get_slot('cuisine')
        if cuisine is None:
            dispatcher.utter_message(template="utter_ask_cuisine")
            return [SlotSet('location', loc)]
        print("cuisine is: " + str(cuisine))

        price = tracker.get_slot('price')
        if price is None:
            dispatcher.utter_message(template="utter_ask_price_range")
            return [SlotSet('location', loc), SlotSet('cuisine', cuisine)]
        print("price range is: " + str(price))

        people = tracker.get_slot('people')
        print("people is: " + str(people))

        dispatcher.utter_message("Getting the restaurant details...\n\n")

        location_detail = zomato.get_location(loc, 1)
        d1 = json.loads(location_detail)
        lat = d1["location_suggestions"][0]["latitude"]
        lon = d1["location_suggestions"][0]["longitude"]
        cuisines_dict = {
            'american': 1,
            'chinese': 25,
            'mexican': 73,
            'italian': 55,
            'north indian': 50,
            'south indian': 85
        }

        flag = 0
        list_results = list()

        if price == '<=300':
            cheaper_restaurants = []
            for val in range(0, 100, 20):
                results = zomato.restaurant_search_iteratively(
                    loc,
                    lat,
                    lon,
                    str(cuisines_dict.get(cuisine)),
                    start=val,
                    sort="price",
                    order="asc")
                d = json.loads(results)
                cheaper_restaurants.extend(d['restaurants'])

            cheaper_restaurants = sorted(
                cheaper_restaurants,
                key=lambda restaurant: restaurant['restaurant']['user_rating'][
                    'aggregate_rating'],
                reverse=True)
            list_results = [
                self.get_restaurant_params(restaurant)
                for restaurant in cheaper_restaurants[:10]
            ]
        else:
            for val in range(0, 100, 20):
                results = zomato.restaurant_search_iteratively(
                    loc,
                    lat,
                    lon,
                    str(cuisines_dict.get(cuisine)),
                    start=val,
                    sort="rating",
                    order="desc")
                d = json.loads(results)
                for restaurant in d['restaurants']:
                    # Assuming average cost for single person is 75% of average cost of two person
                    cost_for_two = restaurant['restaurant'][
                        'average_cost_for_two']

                    if price == '300-700' and cost_for_two >= 300 and cost_for_two <= 700 and len(
                            list_results) <= 10:
                        print(self.get_restaurant_params(restaurant))
                        list_results.append(
                            self.get_restaurant_params(restaurant))
                    if price == '>=700' and cost_for_two >= 700 and len(
                            list_results) <= 10:
                        print(self.get_restaurant_params(restaurant))
                        list_results.append(
                            self.get_restaurant_params(restaurant))

                    if len(list_results) > 9:
                        flag = 1
                        break

                if flag == 1:
                    break

        print("list of results - {}".format(list_results))
        dispatcher.utter_message("Showing you top rated restaurants:\n\
        \n----------------------------------------------------\n")
        if len(list_results) > 0:
            final_strn_to_dispatch = ""
            for i, val in enumerate(list_results[:5]):
                print("Dispatching...." + str(val))
                str1 = "{index}. {restaurant_name} in {restaurant_address} has been rated {rating}\
                \nAverage Cost for two person is Rs. {avg_cost_2}\
                \n----------------------------------------------------\n".format(
                    index=i + 1,
                    restaurant_name=val['Restaurant Name'],
                    restaurant_address=val['Restaurant locality address'],
                    rating=val['Zomato User Rating'],
                    avg_cost_2=val['Average budget for two people'])

                final_strn_to_dispatch += str1
            dispatcher.utter_message(final_strn_to_dispatch)
            f = open('restaurants.txt', "w")
            f.write(json.dumps(list_results))
        else:
            dispatcher.utter_message("No results found..")
        return [SlotSet('location', loc)]
Exemple #15
0
    def run(self, dispatcher, tracker, domain):

        start_time = time.time()
        print("i am inside action_check_city")
        #dispatcher.utter_message("i am inside action_check_city")

        # Tier 1 and 2 cities listed in wikipedia: https://en.wikipedia.org/wiki/Classification_of_Indian_cities
        city_list = [
            'Ahmedabad', 'Bangalore', 'Chennai', 'Delhi', 'Hyderabad',
            'Kolkata', 'Mumbai', 'Pune', 'Jaipur', 'Coimbatore', 'Agra',
            'Ajmer', 'Aligarh', 'Amravati', 'Amritsar', 'Asansol',
            'Aurangabad', 'Bareilly', 'Belgaum', 'Bhavnagar', 'Bhiwandi',
            'Bhopal', 'Bhubaneswar', 'Bikaner', 'Bilaspur',
            'Bokaro Steel City', 'Chandigarh', 'Cuttack', 'Dehradun',
            'Dhanbad', 'Bhilai', 'Durgapur', 'Dindigul', 'Erode', 'Faridabad',
            'Firozabad', 'Ghaziabad', 'Gorakhpur', 'Gulbarga', 'Guntur',
            'Gwalior', 'Gurgaon', 'Guwahati', 'Hamirpur', 'Hubli-Dharwad',
            'Indore', 'Jabalpur', 'Jalandhar', 'Jammu', 'Jamnagar',
            'Jamshedpur', 'Jhansi', 'Jodhpur', 'Kakinada', 'Kannur', 'Kanpur',
            'Karnal', 'Kochi', 'Kolhapur', 'Kollam', 'Kozhikode', 'Kurnool',
            'Ludhiana', 'Lucknow', 'Madurai', 'Malappuram', 'Mathura',
            'Mangalore', 'Meerut', 'Moradabad', 'Mysore', 'Nagpur', 'Nanded',
            'Nashik', 'Nellore', 'Noida', 'Patna', 'Pondicherry', 'Purulia',
            'Prayagraj', 'Raipur', 'Rajkot', 'Rajahmundry', 'Ranchi',
            'Rourkela', 'Salem', 'Sangli', 'Shimla', 'Siliguri', 'Solapur',
            'Srinagar', 'Surat', 'Thanjavur', 'Thiruvananthapuram', 'Thrissur',
            'Trichy', 'Tirunelveli', 'Ujjain', 'Bijapur', 'Vadodara',
            'Varanasi', 'Vijayawada', 'Visakhapatnam', 'Vellore', 'Warangal'
        ]

        cities = {}
        # Get the soundex code of each city and create a dictionary
        for city in city_list:
            cities.update({get_soundex(city): city})

        # Considering the alternate colloquial names of the cities
        # Source: general knowledge and https://www.scoopwhoop.com/news/whats-in-a-name/#.45rdcz1m2
        # Only those names are considered which generate different Soundex codes than the original city name
        cities_alt = {
            get_soundex('Bendakaluru'): 'Bangalore',
            get_soundex('Madras'): 'Chennai',
            get_soundex('New Delhi'): 'Delhi',
            get_soundex('Calcutta'): 'Kolkata',
            get_soundex('Bombay'): 'Mumbai',
            get_soundex('Belagavi'): 'Belgaum',
            get_soundex('Bokaro'): 'Bokaro Steel City',
            get_soundex('Hubli'): 'Hubli-Dharwad',
            get_soundex('Dharwad'): 'Hubli-Dharwad',
            get_soundex('Cawnpore'): 'Kanpur',
            get_soundex('Cochin'): 'Kochi',
            get_soundex('Calicut'): 'Kozhikode',
            get_soundex('Puducherry'): 'Pondicherry',
            get_soundex('Tanjore'): 'Thanjavur',
            get_soundex('Trivandrum'): 'Thiruvananthapuram',
            get_soundex('Trichinapoly'): 'Trichy',
            get_soundex('Tiruchirappalli'): 'Trichy',
            get_soundex('Baroda'): 'Vadodara',
            get_soundex('Banares'): 'Varanasi',
            get_soundex('Vizag'): 'Visakhapatnam',
            get_soundex('Vasai'): 'Mumbai',
            get_soundex('Virar'): 'Mumbai',
            get_soundex('Vasai-Virar City'): 'Mumbai'
        }

        # Get the user selected location
        loc = tracker.get_slot('location')

        # Check if the user selected city is in the list
        if get_soundex(loc) in cities.keys():
            # Yes, get the correct city name
            city = cities[get_soundex(loc)]
        # else check if the name entered is the alternate name of the city
        elif get_soundex(loc) in cities_alt.keys():
            # Yes, hence map it to the correct city name
            city = cities_alt[get_soundex(loc)]
        else:
            # City is not in the list
            city = 'unlisted'

        # Output pre-defined message if the city is not in the list
        if city == 'unlisted':
            dispatcher.utter_message(
                "Sorry, we don’t operate in this city. Please specify some other location"
            )
            city_id = 'unlisted'
            return [SlotSet('location', city_id), UserUtteranceReverted()]
        # Zomato has a ID for each city. We need that to search for restaurants
        else:
            config = {"user_key": "f9c893da39750138e222b042c8070293"}
            zomato = zomatopy.initialize_app(config)

            location_detail = zomato.get_location(city, 1)
            d = json.loads(location_detail)
            city_id = d["location_suggestions"][0]["city_id"]

        print("--- %s seconds ---" % (time.time() - start_time))
        return [SlotSet('location', city_id)]
 def run(self, dispatcher, tracker, domain) -> List[EventType]:
     dispatcher.utter_message(template="utter_welcome_avatar")
     dispatcher.utter_message(template="utter_welcome_saludo")
     dispatcher.utter_message(template="utter_welcome_angel")
     dispatcher.utter_message(template="utter_welcome_continuar")
     return [SlotSet("conversacion_iniciada", False)]
Exemple #17
0
    def run(self, dispatcher, tracker, domain):

        print("i am inside action_check_price")
        # Default value of price range is mid range
        price_range = "2"

        # Get price range for two from the user
        price = tracker.get_slot('price')
        price = price.lower()
        print(price)
        # Price slot will either have values 1,2,3 (indicating low. mid and high range) or will have a string. For example:
        # 'less than 500', 'more than 300', '<300', etc. So logic should detect both these types and determine the price range accordingly

        # Upper limit words for the price range
        price_low = [
            'maximum', 'max', 'less than', 'lesser than', 'cheaper than'
            'not more than', 'within', 'not above', '<=', '<'
        ]
        # Lower limit words for the price range
        price_hi = [
            'minimum', 'min', 'more than', 'higher than', 'not less than',
            'not lesser than', 'not below', '>=', '>'
        ]

        # Remove the word Rupee or similar words if present as it is not needed
        rupee = ['rs.', 'rs', 'rupees.', 'rupee.', 'rupees', 'rupee']
        for r in rupee:
            if r in price:
                price = price.replace(r, '')

        # Consider a special case where user specifies the price range himself
        # User could say "Find me restaurants between Rs.300 and Rs.500"
        # Check if there are two numbers present in the string
        if len(re.findall(r'\d+', price)) == 2:
            # Consider the upper limit as the price (e.g.: in Rs.300 and Rs.500, it will select 500)
            price = sorted(re.findall(r'\d+', price))[1]

        # If it is a pure number, then it is either the price range option (1, 2 or 3), or directly holds the amount
        if re.search(r'^[\s]*\d+[\s]*$', price):
            # Remove white spaces if any
            price = price.strip(" ")
            # Check if the user has chosen using the dropdown i.e. if the slot 'price' already has the value 1, 2 or 3
            if float(price) in range(1, 4):
                # If yes, then correct price range is mentioned
                price_range = price
            # Check if the user has directly input the price range, then assign the proper value to the slot 'price' accordingly
            elif float(price) < 300:
                # It is in low price range
                price_range = "1"
            elif (float(price) >= 300) and (float(price) <= 700):
                # It is in mid price range
                price_range = "2"
            elif float(price) > 700:
                # It is in high price range
                price_range = "3"
            else:
                # Price is not mentioned
                # Choose default i.e. mid range
                price_range = "2"
        # It is not a pure number. It means it contains a string. Extract the price (which will be a number) and determine the price range
        else:
            # At first, three special cases are considered:
            # User could say "Find me restaurants below Rs. 500" or he could say "Find me restaurants not below Rs. 500"
            # First case is finding a cheaper restaurant and second case is finding an expensive restaurant
            if ('below' in price) and ('not' not in price):
                price = price.replace('below', 'less than')

            # User could say "Find me restaurants above Rs. 500" or he could say "Find me restaurants not above Rs. 500"
            # First case is finding an expensive restaurant and second case is finding a cheaper restaurant
            if ('above' in price) and ('not' not in price):
                price = price.replace('above', 'more than')

            # User could say "Find me restaurants priced = 500"
            # '=' can be considered as user wants to consider restaurant budget below that amount
            if ('=' in price) and ('>=' not in price) and ('<=' not in price):
                price = price.replace('=', 'less than')

            # If the user has provided the upper limit for the price range
            # (for example "within Rs.400", "cheaper than Rs.500", etc)
            for p in price_low:
                if p in price:
                    # Extract the amount
                    amount = float(re.findall(r'\d+', price)[0])
                    if amount < 300:
                        # It is in low price range
                        price_range = "1"
                    elif (amount >= 300) and (amount <= 700):
                        # It is in mid price range
                        price_range = "2"
                    elif amount > 700:
                        # It is in high price range
                        price_range = "3"
                    else:
                        # Choose default i.e. mid range
                        price_range = "2"
                    break

            # If the user has provided the lower limit for the price range
            # (for example "minimum Rs.400", "more than Rs.500", etc)
            for p in price_hi:
                if p in price:
                    # Extract the amount
                    amount = float(re.findall(r'\d+', price)[0])
                    if 700 < amount:
                        # It is in high price range
                        price_range = "3"
                    elif (700 >= amount) and (300 <= amount):
                        # It is in mid price range
                        price_range = "2"
                    elif 300 > amount:
                        # It is in low price range
                        price_range = "1"
                    else:
                        # Choose default i.e. mid range
                        price_range = "2"
                    break
        print(price_range)
        return [SlotSet('price', price_range)]
 def run(self, dispatcher, tracker, domain) -> List[EventType]:
     dispatcher.utter_message(template="utter_comencemos")
     dispatcher.utter_message(template="utter_informar_privacidad")
     dispatcher.utter_message(template="utter_pedir_nombre")
     return [SlotSet("shown_privacy", True), SlotSet("conversacion_iniciada", True)]
Exemple #19
0
    def deactivate(self) -> List[EventType]:
        """Return `Form` event with `None` as name to deactivate the form
            and reset the requested slot"""

        logger.debug(f"Deactivating the form '{self.name()}'")
        return [Form(None), SlotSet(REQUESTED_SLOT, None)]
Exemple #20
0
    def run(self, dispatcher, tracker, domain):

        response_message = ""
        email_message = ""
        search_validity = "valid"

        budget = tracker.get_slot("budget")
        location = tracker.get_slot("location")
        cuisine = tracker.get_slot("cuisine")

        if not location:
            search_validity = "invalid"
        else:
            """
				retrieve location details
			"""
            response = api_zomato.get_location(location)
            location_details = {}

            if response is not None:
                response_json = json.loads(response)
                if response_json["status"] == "success":
                    """
						fetch location details and store 'city_id'
					"""
                    location_details = response_json["location_suggestions"][0]
                    city_id = location_details["city_id"]
                    city_name = location_details["city_name"]
                    """
                        Validate if the location details is of the requested location
                    """
                    if location.lower() == city_name.lower():
                        """
                            fetch all cuisines available in the location
                        """
                        response_cuisine = api_zomato.get_cuisines(city_id)
                        supported_cuisines_names = [
                            "American",
                            "Chinese",
                            "Italian",
                            "Mexican",
                            "North Indian",
                            "South Indian",
                        ]
                        """ 
                            filter only supported cuisines
                        """
                        filtered_cuisine = {
                            key: value
                            for key, value in response_cuisine.items()
                            if value in supported_cuisines_names
                        }

                        if cuisine is not None:
                            cuisine_list = [
                                key for key, value in filtered_cuisine.items()
                                if str(value).lower() == cuisine.lower()
                            ]
                        else:
                            cuisine_list = [
                                key for key, value in filtered_cuisine.items()
                            ]

                        restaurants_found = self.search_restaurant(
                            location, location_details, cuisine_list)

                        if len(restaurants_found) > 0:
                            restaurant_filtered_budget = self.filter_restaurant_by_budget(
                                budget, restaurants_found)
                            number_of_records = 10

                            if len(restaurant_filtered_budget) < 10:
                                number_of_records = len(
                                    restaurant_filtered_budget)

                            for indx in range(0, number_of_records):
                                restaurant = restaurant_filtered_budget[indx]
                                if indx < 5:
                                    response_message = (response_message +
                                                        "\n   " +
                                                        str(indx + 1) + ". " +
                                                        restaurant["name"] +
                                                        " in " +
                                                        restaurant["address"] +
                                                        " has been rated " +
                                                        restaurant["rating"] +
                                                        " out of 5" + "\n")

                                email_message = (
                                    email_message + "\n   " + str(indx + 1) +
                                    ". " + restaurant["name"] + " in " +
                                    restaurant["address"] +
                                    " has been rated " + restaurant["rating"] +
                                    " out of 5. " + "Average cost for 2 : " +
                                    str(restaurant["avg_cost_for_2"]) + "\n")

                        else:
                            search_validity = "invalid"
                    else:
                        search_validity = "invalid"
                else:
                    search_validity = "invalid"
            else:
                search_validity = "invalid"

        if search_validity == "valid":
            dispatcher.utter_message(response_message)

        return [
            SlotSet("search_validity", search_validity),
            SlotSet("email_message", email_message)
        ]
Exemple #21
0
 def run(self, dispatcher, tracker, domain):
     config={ "user_key":"63163217253a26f16084d1dfb85e0f02"}
     zomato = zomatopy.initialize_app(config)
     loc = tracker.get_slot('location')
     cuisine = tracker.get_slot('cuisine')
     price = tracker.get_slot('price')
     location_detail=zomato.get_location(loc, 1)
     d1 = json.loads(location_detail)
     lat=d1["location_suggestions"][0]["latitude"]
     lon=d1["location_suggestions"][0]["longitude"]
     city_id=d1["location_suggestions"][0]["city_id"]
     cuisines_dict={'american': 1, 'andhra': 2, 'asian': 3, 'arabian': 4, 'bakery': 5, 'afghani': 6,'biryani': 7, 'bengali': 10, 'chettinad': 18, 'burmese': 22, 'chinese': 25, 'raw meats': 27,'cafe': 30, 'continental': 35, 'european': 38, 'fast food': 40, 'french': 45, 'goan': 47,'gujarati': 48, 'hyderabadi': 49, 'north indian': 50, 'italian': 55, 'japanese': 60, 'kerala': 62,'konkan': 63, 'kashmiri': 65, 'lebanese': 66, 'korean': 67, 'malaysian': 69, 'mediterranean': 70,'malwani': 71, 'mangalorean': 72, 'mexican': 73, 'mongolian': 74, 'mughlai': 75, 'pizza': 82,'seafood': 83, 'south indian': 85, 'portuguese': 87, 'rajasthani': 88, 'spanish': 89, 'street food': 90,'tibetan': 93, 'thai': 95, 'vietnamese': 99, 'desserts': 100, 'maharashtrian': 102, 'indonesian': 114,'nepalese': 117, 'singaporean': 119, 'cantonese': 121, 'australian': 131, 'belgian': 132, 'british': 133,'german': 134, 'middle eastern': 137, 'pakistani': 139, 'iranian': 140, 'steak': 141, 'turkish': 142,'healthy food': 143, 'egyptian': 146, 'moroccan': 147, 'indian': 148, 'tex-mex': 150,'greek': 156,'lucknowi': 157, 'brazilian': 159, 'coffee and tea': 161, 'peruvian': 162, 'tea': 163, 'juices': 164,'assamese': 165, 'burger': 168, 'armenian': 175, 'sushi': 177, 'kebab': 178, 'grill': 181, 'patisserie': 183,'deli': 192, 'bbq': 193, 'brasserie': 195, 'pan asian': 209, 'swiss': 213, 'israeli': 218, 'bar food': 227,'north eastern': 231, 'ice cream': 233, 'bubble tea': 247, 'drinks only': 268, 'oriya': 269, 'beverages': 270,'finger food': 271, 'fusion': 274, 'oriental': 278, 'parsi': 290, 'awadhi': 292, 'modern european': 294,'fish and chips': 298, 'sandwich': 304, 'vegetarian': 308, 'satay': 312, 'asian fusion': 401, 'falafel': 571,'eastern european': 651, 'crepes': 881, 'modern australian': 969, 'chili': 971, 'south american': 972,'yum cha': 978, 'dumplings': 985, 'panini': 989, 'sindhi': 993, 'charcoal chicken': 994, 'salad': 998,'roast chicken': 1005, 'bihari': 1013, 'cuisine varies': 1014, 'mithai': 1015, 'modern indian': 1018,'hot pot': 1022, 'rolls': 1023, 'wraps': 1024, 'fried chicken': 1025, 'bohri': 1032, 'vegan': 1034,'cafe food': 1039, 'coffee': 1040, 'paan': 1048}
     
     list1 = [0, 20, 40, 60, 80]
     d = []
     for i in list1:
         results = zomato.restaurant_search("", lat, lon, str(cuisines_dict.get(cuisine)), limit=i)
         d1 = json.loads(results)
         d.extend(d1['restaurants'])
     
     response = []
     if len(d) == 0:
         response = []
     else:
         for restaurant in d:
             if str(price) == 'lesser than 300':
                 if restaurant['restaurant']['average_cost_for_two'] <300:
                     response.append([restaurant['restaurant']['name'],restaurant['restaurant']['location']['address'],restaurant['restaurant']['average_cost_for_two'],float(restaurant['restaurant']['user_rating']['aggregate_rating'])])
                 else:
                     response
             elif str(price) == 'between 300 to 700':
                 if 300 <= restaurant['restaurant']['average_cost_for_two'] <700:
                     response.append([restaurant['restaurant']['name'],restaurant['restaurant']['location']['address'],restaurant['restaurant']['average_cost_for_two'],float(restaurant['restaurant']['user_rating']['aggregate_rating'])])
                 else:
                     response
             else:
                 if restaurant['restaurant']['average_cost_for_two'] >=700:
                     response.append([restaurant['restaurant']['name'],restaurant['restaurant']['location']['address'],restaurant['restaurant']['average_cost_for_two'],float(restaurant['restaurant']['user_rating']['aggregate_rating'])])
                 else:
                     response
     
     if len(response) == 0:
         dispatcher.utter_message("Sorry, we couldn't find any restaurant in given budget/location/cuisine.")
     else:
         response = sorted(response, key=operator.itemgetter(3), reverse=True)
         top5 = response[:5]
         response_display = 'Showing you top 5 results in your budget range :' +"\n" + "\n"
         j = 0
         for i in top5:
             j = j + 1
             response_display = response_display + str(j) + ". " + i[0] + ' in ' + i[1] + ' has been rated ' + str(i[3]) + "\n" + "\n"
         dispatcher.utter_message("-----"+str(response_display))
         
         response_email = "Hi," + "\n" + "\n" 'Here is the list of the restaurants you requested - ' + "\n"
         top10 = response[:10]
         k = 0
         for i in top10:
             k = k + 1
             response_email = response_email + str(k) + ". " + i[0] + ' in ' + i[1] + ' has been rated - ' + str(i[3]) + ' and has average price for two people - Rs.' + str(i[2]) +"\n" + "\n"
         response_email = response_email + "Thanks," + "\n" + "Restaurant Chatbot"
         return [SlotSet('search_results',response_display),SlotSet('email_results',response_email )]
Exemple #22
0
    def run(self, dispatcher: CollectingDispatcher, tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

        return [SlotSet("airport", None)]
Exemple #23
0
 def run(self, dispatcher, tracker, domain):
     print("Reseting main slot")
     return [SlotSet("main", None), SlotSet("satisfied", None)]
Exemple #24
0
 async def run(self, dispatcher, tracker, domain):
     return [SlotSet("test", self.some_common_feature())]
Exemple #25
0
 def run(self, dispatcher, tracker, domain):
     return [SlotSet("dietary", None), SlotSet("satisfied", None)]
Exemple #26
0

@pytest.mark.parametrize(
    "slots,expected_slots",
    [
        (
            {
                SLOT_MENTION: None,
                SLOT_ATTRIBUTE: None,
                SLOT_OBJECT_TYPE: "restaurant",
                SLOT_LISTED_OBJECTS: None,
                SLOT_LAST_OBJECT: None,
                SLOT_LAST_OBJECT_TYPE: None,
            },
            [
                SlotSet(SLOT_MENTION, None),
                SlotSet(SLOT_ATTRIBUTE, None),
                SlotSet(SLOT_OBJECT_TYPE, "restaurant"),
                SlotSet(SLOT_LAST_OBJECT, None),
                SlotSet(SLOT_LAST_OBJECT_TYPE, "restaurant"),
                SlotSet(SLOT_LISTED_OBJECTS, [3, 2, 1]),
            ],
        ),
        (
            {
                SLOT_MENTION: None,
                SLOT_ATTRIBUTE: None,
                SLOT_OBJECT_TYPE: "restaurant",
                SLOT_LISTED_OBJECTS: None,
                SLOT_LAST_OBJECT: None,
                SLOT_LAST_OBJECT_TYPE: "restaurant",
 def run(self, dispatcher: CollectingDispatcher, tracker: Tracker,
         domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
     score = tracker.get_slot("scope_counter")
     score = score + 1
     return [SlotSet("scope_counter", score)]
Exemple #28
0
    async def run(self, dispatcher, tracker, domain):
        start_time = time.time()
        #print("i am inside action_search_restaurants")
        # My API Key of Zomato
        config = {"user_key": "f9c893da39750138e222b042c8070293"}
        zomato = zomatopy.initialize_app(config)

        # Price range determination
        price = tracker.get_slot('price')
        print(price)
        # Get the user selected cuisine
        # Slot 'cuisine' would have been updated in the ActionCheckCuisine() with correct cuisine name
        cuisine = tracker.get_slot('cuisine')
        print(cuisine)
        # Get the user selected location
        # Slot 'location' would have been updated in the ActionCheckCity() with correct city ID as per Zomato API
        city_id = tracker.get_slot('location')
        print(city_id)

        # Check if slots are correctly filled or not

        # If location slot is not filled, ask for city name
        if city_id == None:
            print("no city entered 1")
            # As per https://rasa.com/docs/action-server/sdk-dispatcher/
            dispatcher.utter_message(template="utter_ask_location")
            return [FollowupAction("action_listen")]
        # Slot is filled but action was not carried out
        elif get_last_action(tracker.events, "action_check_city") == False:
            print("no city entered 2")
            return [FollowupAction("action_check_city")]

        # If cuisine slot is not filled, ask for cuisine
        if cuisine == None:
            print("no cuisine entered 1")
            dispatcher.utter_message(template="utter_ask_cuisine")
            return [FollowupAction("action_listen")]
        # Slot is filled but action was not carried out
        elif get_last_action(tracker.events, "action_check_cuisine") == False:
            print("no cuisine entered 2")
            return [FollowupAction("action_check_cuisine")]

        # If price slot is not filled, ask for price range
        if price == None:
            print("no price entered 1")
            dispatcher.utter_message(template="utter_ask_price_range")
            return [FollowupAction("action_listen")]
        # Slot is filled but action was not carried out
        elif get_last_action(tracker.events, "action_check_price") == False:
            print("no price entered 2")
            return [FollowupAction("action_check_price")]

        # Cuisines code (used by zomato)
        # Source: https://developers.zomato.com/documentation?lang=tr#/
        cuisines_dict = {
            'american': 1,
            'chinese': 25,
            'mexican': 73,
            'italian': 55,
            'north indian': 50,
            'south indian': 85
        }

        # Prepare the response
        response = []
        initialResponse = ""
        # As long as there is a valid location specified
        if (city_id != 'unlisted'):
            # get_location gets the lat-long coordinates of 'loc'
            #location_detail = zomato.get_location(loc, 1)
            # Store retrieved data as a dict
            #d = json.loads(location_detail)
            #city_id = d["location_suggestions"][0]["city_id"]
            # Fetch results
            # If cuisine is specified, then use that to search restaurants
            if (cuisine != 'unlisted'):
                count = 0
                #Zomato api only lets us fetch 100 records, after that it starts returning blank json
                for offset in range(0, 100, 20):
                    #results = zomato.restaurant_search_by_city(city_id, str(cuisines_dict[cuisine]), offset)
                    loop = asyncio.get_event_loop()
                    if loop.is_running() == False:
                        results = asyncio.run(
                            zomato.restaurant_search_by_city_async(
                                city_id, str(cuisines_dict[cuisine]), offset))
                    else:
                        results = await zomato.restaurant_search_by_city_async(
                            city_id, str(cuisines_dict[cuisine]), offset)

                    d1 = json.loads(results)
                    if offset == 0:
                        maxData = d1['results_found']
                    if (count >= 10) or (offset > maxData):
                        break
                    # If no results match with the user mentioned criteria
                    if maxData == 0:
                        initialResponse = "No restaurants found matching your criteria"
                        dispatcher.utter_message(initialResponse)
                    else:
                        initialResponse = "Here are the top rated restaurants as per your criteria:" + "\n"
                        for restaurant in d1['restaurants']:
                            if count >= 10:
                                break
                            # Getting Top 10 restaurants for the lower price range
                            if ((float(price) == 1)
                                    and (restaurant['restaurant']
                                         ['average_cost_for_two'] < 300) and
                                (cuisine.title()
                                 in restaurant['restaurant']['cuisines'])):
                                response.append(
                                    str(restaurant['restaurant']['name']) +
                                    " in " + str(restaurant['restaurant']
                                                 ['location']['address']) +
                                    " has a rating of " +
                                    str(restaurant['restaurant']['user_rating']
                                        ['aggregate_rating']) + ".")
                                response.append("The average price of Rs." +
                                                str(restaurant['restaurant']
                                                    ['average_cost_for_two']) +
                                                " for two persons" + "\n")
                                count = count + 1

                            # Getting Top 10 restaurants for the mid price range
                            elif ((float(price) == 2)
                                  and (restaurant['restaurant']
                                       ['average_cost_for_two'] >= 300)
                                  and (restaurant['restaurant']
                                       ['average_cost_for_two'] <= 700) and
                                  (cuisine.title()
                                   in restaurant['restaurant']['cuisines'])):
                                response.append(
                                    str(restaurant['restaurant']['name']) +
                                    " in " + str(restaurant['restaurant']
                                                 ['location']['address']) +
                                    " has a rating of " +
                                    str(restaurant['restaurant']['user_rating']
                                        ['aggregate_rating']) + "\n")
                                response.append("The average price of Rs." +
                                                str(restaurant['restaurant']
                                                    ['average_cost_for_two']) +
                                                " for two persons" + "\n")
                                count = count + 1

                            # Getting Top 10 restaurants for the higher price range
                            elif ((float(price) == 3)
                                  and (restaurant['restaurant']
                                       ['average_cost_for_two'] > 700) and
                                  (cuisine.title()
                                   in restaurant['restaurant']['cuisines'])):
                                response.append(
                                    str(restaurant['restaurant']['name']) +
                                    " in " + str(restaurant['restaurant']
                                                 ['location']['address']) +
                                    " has a rating of " +
                                    str(restaurant['restaurant']['user_rating']
                                        ['aggregate_rating']) + "\n")
                                response.append("The average price of Rs." +
                                                str(restaurant['restaurant']
                                                    ['average_cost_for_two']) +
                                                " for two persons" + "\n")
                                count = count + 1

            # Cuisine is not specified, hence search for all cuisine types
            else:
                count = 0
                #print("inside else cuisine")
                for offset in range(0, 100, 20):
                    #results = zomato.restaurant_search_by_city(city_id, "", offset)
                    loop = asyncio.get_event_loop()
                    if loop.is_running() == False:
                        results = asyncio.run(
                            zomato.restaurant_search_by_city_async(
                                city_id, '', offset))
                    else:
                        results = await zomato.restaurant_search_by_city_async(
                            city_id, '', offset)

                    d1 = json.loads(results)
                    if (count >= 10) or (offset > d1['results_found']):
                        break
                    # If no results match with the user mentioned criteria
                    if d1['results_found'] == 0:
                        initialResponse = "No restaurants found matching your criteria"
                        dispatcher.utter_message(initialResponse)
                    else:
                        initialResponse = "Here are the top rated restaurants as per your criteria:" + "\n"
                        for restaurant in d1['restaurants']:
                            if count >= 10:
                                break
                            # Getting Top 10 restaurants for the lower price range
                            if ((float(price) == 1)
                                    and (restaurant['restaurant']
                                         ['average_cost_for_two'] < 300)):
                                response.append(
                                    str(restaurant['restaurant']['name']) +
                                    " in " + str(restaurant['restaurant']
                                                 ['location']['address']) +
                                    " has a rating of " +
                                    str(restaurant['restaurant']['user_rating']
                                        ['aggregate_rating']) + ".")
                                response.append("The average price of Rs." +
                                                str(restaurant['restaurant']
                                                    ['average_cost_for_two']) +
                                                " for two persons" + "\n")
                                count = count + 1

                            # Getting Top 10 restaurants for the mid price range
                            elif ((float(price) == 2)
                                  and (restaurant['restaurant']
                                       ['average_cost_for_two'] >= 300)
                                  and (restaurant['restaurant']
                                       ['average_cost_for_two'] <= 700)):
                                response.append(
                                    str(restaurant['restaurant']['name']) +
                                    " in " + str(restaurant['restaurant']
                                                 ['location']['address']) +
                                    " has a rating of " +
                                    str(restaurant['restaurant']['user_rating']
                                        ['aggregate_rating']) + "\n")
                                response.append("The average price of Rs." +
                                                str(restaurant['restaurant']
                                                    ['average_cost_for_two']) +
                                                " for two persons" + "\n")
                                count = count + 1

                            # Getting Top 10 restaurants for the higher price range
                            elif ((float(price) == 3)
                                  and (restaurant['restaurant']
                                       ['average_cost_for_two'] > 700)):
                                response.append(
                                    str(restaurant['restaurant']['name']) +
                                    " in " + str(restaurant['restaurant']
                                                 ['location']['address']) +
                                    " has a rating of " +
                                    str(restaurant['restaurant']['user_rating']
                                        ['aggregate_rating']) + "\n")
                                response.append("The average price of Rs." +
                                                str(restaurant['restaurant']
                                                    ['average_cost_for_two']) +
                                                " for two persons" + "\n")
                                count = count + 1

            # If no results obtained
            if (count == 0):
                initialResponse = "Sorry, No results found for your criteria. Please type modified criteria?"
                dispatcher.utter_message(initialResponse)
                return [Restarted()]
            # Display the results
            else:
                dispatcher.utter_message(initialResponse)
                for res in response[::2][:5]:
                    dispatcher.utter_message(res)

        # If no valid location is given
        else:
            response = "Sorry, we don’t operate in this city. Please specify some other location"
            dispatcher.utter_message(response)
        print(offset)
        print("--- %s seconds ---" % (time.time() - start_time))
        return [SlotSet('emailmsg', response)]
Exemple #29
0
    def run(self, dispatcher, tracker, domain):

        loc = tracker.get_slot('location')
        cuisine = tracker.get_slot('cuisine')
        price = tracker.get_slot('price')
        print(price)
        location_detail = zomato.get_location(loc, 1)
        d1 = json.loads(location_detail)
        lat = d1["location_suggestions"][0]["latitude"]
        lon = d1["location_suggestions"][0]["longitude"]
        cuisines_dict = {
            'mexican': 5,
            'chinese': 25,
            'american': 30,
            'italian': 55,
            'north indian': 50,
            'south indian': 85
        }
        results = zomato.restaurant_search("", lat, lon,
                                           str(cuisines_dict.get(cuisine)), 10)
        d = json.loads(results)

        def list_sort(list):
            list.sort(key=lambda x: x[3])
            return list

        def Reverse(lst):
            return [ele for ele in reversed(lst)]

        global email_data
        global reverse_list
        response = ""

        if d['results_found'] == 0:
            response = "no results"
        else:
            email_data.clear()
            if price == 'Lesser than Rs. 300':
                for i, restaurant in enumerate(d['restaurants']):
                    averageCostFor2 = restaurant['restaurant'][
                        'average_cost_for_two']
                    if averageCostFor2 <= 300:
                        name = restaurant['restaurant']['name']
                        address = restaurant['restaurant']['location'][
                            'address']
                        # averageCostFor2 = restaurant['restaurant']['average_cost_for_two']
                        userrating = restaurant['restaurant']['user_rating'][
                            'aggregate_rating']

                        temp_list = [
                            name, address, averageCostFor2, userrating
                        ]
                        email_data.append(temp_list)
                        # if len(email_data) == 6:
                        # break

            elif price == 'Rs. 300 to 700':
                for i, restaurant in enumerate(d['restaurants']):
                    averageCostFor2 = restaurant['restaurant'][
                        'average_cost_for_two']
                    if averageCostFor2 >= 300 and averageCostFor2 <= 700:
                        name = restaurant['restaurant']['name']
                        address = restaurant['restaurant']['location'][
                            'address']
                        # averageCostFor2 = restaurant['restaurant']['average_cost_for_two']
                        userrating = restaurant['restaurant']['user_rating'][
                            'aggregate_rating']

                        temp_list = [
                            name, address, averageCostFor2, userrating
                        ]
                        email_data.append(temp_list)
                        # if len(email_data) == 6:
                        # break

            elif price == 'More than 700':
                for i, restaurant in enumerate(d['restaurants']):
                    averageCostFor2 = restaurant['restaurant'][
                        'average_cost_for_two']
                    if averageCostFor2 >= 700:
                        name = restaurant['restaurant']['name']
                        address = restaurant['restaurant']['location'][
                            'address']
                        # averageCostFor2 = restaurant['restaurant']['average_cost_for_two']
                        userrating = restaurant['restaurant']['user_rating'][
                            'aggregate_rating']

                        temp_list = [
                            name, address, averageCostFor2, userrating
                        ]
                        email_data.append(temp_list)
                        # if len(email_data) == 6:
                        # break

        sorted_value = list_sort(email_data)
        print(sorted_value)
        reverse_list = Reverse(sorted_value)

        def Enquiry(lis1):
            if not lis1:
                return 1
            else:
                return 0

        search_validity = "invalid"
        if Enquiry(sorted_value) == 0:
            for i, x in enumerate(reverse_list):
                response = "NAME : " + str(x[0]) + " ADDRESS : " + str(
                    x[1]) + " USER RATING : " + str(x[3])
                #search_validity = "valid"
                #print(search_validity)
                dispatcher.utter_message(response)
                if i == 4:
                    break
            return [SlotSet('search_validity_ok', True)]
        else:
            search_validity = "invalid"
            print(search_validity)
            dispatcher.utter_message("Sorry no result found ")
            return [SlotSet("search_validity", search_validity)]
Exemple #30
0
    def run(self, dispatcher, tracker, domain) -> List[EventType]:
        problem = tracker.latest_message.get("text")

        return [SlotSet("problem_description", problem)]