Esempio n. 1
0
import db

user = {}
user['username'] = '******'
user['first_name'] = 'Seth'
user['last_name'] = 'Washeck'
user['address'] = '123 Main St.'
user['city'] = 'Springfield'
user['state'] = 'CO'
user['zip'] = '90210'
user['email'] = '*****@*****.**'

db.flush_db()

db.save_user_profile(user)

categories = ['Recreational Activities (Outdoor)', 'Recreational Activities (Indoor)', 'Community Groups', 'Academic Groups',
	'Spiritual Groups']
	
db.save_list('category',categories)

print db.get_list('category')

activities = ['Hiking','Biking','Climbing','Camping']

db.save_list('Recreational Activities (Outdoor)', activities)

print 'Recreational Activities (Outdoor): ', db.get_list('Recreational Activities (Outdoor)')

print db.get_user_profile('swasheck')
Esempio n. 2
0
def process_attraction(parameters, intent, session):
    """
    This method handle all requests about attraction
    """
    if intent == "Attraction-Recommend - ask-more":
        results = search_it_from_results(parameters, "attraction", session)

        # if not results:
        #     db_results = search_name_from_database(parameters, "attraction", session)
        #     if not db_results:
        #         return {
        #             "fulfillmentText": random.choice([
        #                 'im debugging-more'
        #                 # "Unfortunately, I couldn't find any matching attraction for you. Could you try something different?",
        #                 # "I'm sorry, but I wasn't able to find a matching attraction for you. Can you change some of your requests?"
        #             ])
        #         }
        #     report = printout_detailed_result_from_name(db_results[0])
        #     return {
        #             "fulfillmentText": random.choice([
        #                 "{}".format(report)
        #             ])
        #             }

        report = printout_detailed_result_from_name(results[0])
        return {"fulfillmentText": random.choice(["{}".format(report)])}
    if intent == "Attraction-Recommend - choose-name":
        results = search_name_from_results(parameters, "attraction", session)

        if not results:
            db_results = search_name_from_database(parameters, "attraction",
                                                   session)
            if not db_results:
                return {
                    "fulfillmentText":
                    random.choice([
                        "Unfortunately, I couldn't find any matching attraction for you. Could you try something different?",
                        "I'm sorry, but I wasn't able to find a matching attraction for you. Can you change some of your requests?"
                    ])
                }
            report = printout_detailed_result_from_name(db_results[0])
            return {"fulfillmentText": random.choice(["{}".format(report)])}

        report = printout_detailed_result_from_name(results[0])
        return {"fulfillmentText": random.choice(["{}".format(report)])}

    if intent == "Attraction-Recommend - hours":
        user_results = get_user_profile(session)["results"]["attraction"]

        if not user_results:
            return {
                "fulfillmentText":
                "Sorry, I'm not sure which one you are asking about. Would you "
                + "mind adding or changing some information? " + "Thank you!"
            }

        if len(user_results) >= 1:
            report = printout_detailed_result_openhour(user_results[0])
            return {
                "fulfillmentText":
                random.choice([
                    "Yeah.. {}".format(report),
                    "...Here it is: {}".format(report),
                    "Good question. {}".format(report)
                ])
            }

    if intent == "Attraction-Recommend - choose":
        user_results = get_user_profile(session)["results"]["attraction"]

        if not user_results:
            return {
                "fulfillmentText":
                "Sorry, I'm not sure which one you are asking about. Would you "
                + "mind adding or changing some information? " + "Thank you!"
            }

        if len(user_results) > 1:
            index = 0

            if parameters['index_to_choose'] == '1':
                index = 0
            elif parameters['index_to_choose'] == '2':
                index = 1

            report = printout_detailed_result(user_results[index])
            update_the_order_in_results(user_results, session, index)

            return {
                "fulfillmentText":
                random.choice([
                    "Cool :) {}".format(report),
                    "Nice Choice! {}".format(report)
                ])
            }

    continue_search = False
    if intent == "Attraction-Recommend - refine_search":
        # update parameters
        # since this is updating information
        # we should only update parameters that are NOT EMPTY
        updated_user_profile = update_user_parameters(parameters,
                                                      session,
                                                      ignore_empty=True)

        # If we can't update anything new. Prompt the user to try again.
        if updated_user_profile is None:
            return {
                "fulfillmentText":
                random.choice([
                    "Sorry, I couldn't find anything new from what you just said. Would you mind trying again?",
                    "I'm sorry that I couldn't get anything new from what you just said. Could you try again?"
                ])
            }
        user_results = get_user_profile(session)["results"]["attraction"]

        if user_results is None:
            return {
                "fulfillmentText":
                "Sorry, I'm not sure which one you are asking about. Would you "
                + "mind adding or changing some information? " + "Thank you!"
            }

        matched_results = search_from_results(parameters, "attraction",
                                              session)

        if len(matched_results) > 1:
            report = []
            # provide them the first two to compare
            printout0 = printout_result(matched_results[0])
            printout1 = printout_result(matched_results[1])
            if printout1.split()[-4:-3] == printout0.split()[-4:-3]:
                report.append("I found two results for you. \n1) ")
                report.append(printout0)
                report.append("\n2) ")
                report.append(printout1[:-1] + " as well.")
            else:
                report.append("I found two results for you. \n1) ")
                report.append(printout0)
                report.append("\n2) ")
                report.append(printout1)
            update_search_results_for_user(matched_results, "attraction",
                                           session)
            return {
                "fulfillmentText":
                "{}\nWhich one do you prefer?".format(" ".join(report))
            }

        if len(matched_results) == 1:
            report = printout_result(matched_results[0])
            update_search_results_for_user(matched_results, "attraction",
                                           session)
            return {
                "fulfillmentText":
                random.choice(["{}".format(report), "{}".format(report)])
            }

        # prompt the user again if there are no matches
        if len(matched_results) == 0:
            return {
                "fulfillmentText":
                ("Sorry, I can't find the one you're referring to. Would you mind changing "
                 +
                 "some information so that I can get the one place for you? Thanks!"
                 )
            }

        # only continue search with the updated parameters if there is anything new
        # given by the user
        continue_search = True

    if intent == "Attraction-Recommend - postcode":
        user_results = get_user_profile(session)["results"]["attraction"]

        if not user_results:
            return {
                "fulfillmentText":
                "Sorry, I'm not sure which one you are asking about. Would you "
                + "mind adding or changing some information? " + "Thank you!"
            }

        if len(user_results) >= 1:
            report = printout_detailed_result_postcode(user_results[0])
            return {
                "fulfillmentText":
                random.choice([
                    "Yeah.. {}".format(report),
                    "...Here it is: {}".format(report),
                    "Good question. {}".format(report)
                ])
            }

    if intent == "Attraction-Recommend" or continue_search:
        if intent == "Attraction-Recommend":
            # update parameters
            # since this is the beginning of the conversation
            # we should not ignore empty parameters because that is the user's initial request
            #
            # UNLESS this logic is called with should_start_search, meaning this block of code
            # is used just for the search with the already updated parameters from a different
            # intent block
            updated_user_profile = update_user_parameters(parameters,
                                                          session,
                                                          ignore_empty=False)

        # prompt the user to give more information if nothing is given
        if is_empty_parameter_dict(parameters):
            return {
                "fulfillmentText":
                random.choice([
                    "Sure! Can you tell me more?",
                    "My pleasure! Can you tell me more?"
                ])
            }

        if intent == "Attraction-Recommend":
            # we also need to clean any stored results because this is an entirely new request
            #
            # UNLESS this logic is called with should_start_search, meaning this block of code
            # is used just for the search with the already updated parameters from a different
            # intent block
            update_search_results_for_user([], "attraction", session)

        # prompt the user again if nothing is changed
        if updated_user_profile is None:
            return {
                "fulfillmentText":
                random.choice([
                    "Sorry, but you already give all the information. Could you add more?",
                    "Sorry, the information is already given. Would you mind adding some different information?"
                ])
            }

        # get user data
        user_parameters = updated_user_profile["parameters"]
        # print('user_parameters',user_parameters)
        # if no parameter present, ask the user to give some
        if is_empty_parameter_dict(user_parameters):
            return {
                "fulfillmentText":
                random.choice([
                    "Sure! Can you tell me more?",
                    "My pleasure! Can you tell me more?"
                ])
            }

        # otherwise start search and return results
        results = search(user_parameters, "attraction")
        # print('get %d results'%len(results))
        if len(results) == 0:
            return {
                "fulfillmentText":
                random.choice([
                    "I am sorry but I do not have anything matching the criteria you specified. Would you be interested in expanding your field of search?",
                    "Unfortunately, I couldn't find any matching attraction for you. Could you try something different?",
                    "Sorry, but I wasn't able to find a matching attraction for you. Can you change some of your requests?"
                ])
            }

        # update search results here because we need to store them for the next interaction
        # so that we know what was there previously
        # print('update!!!')
        update_search_results_for_user(results, "attraction", session)

        # too many results
        if len(results) > 2:
            # print('cool')
            return {
                "fulfillmentText":
                random.choice([
                    "Wow! I see there are {} {}s that match your needs. Can you add more information to help narrow it down?"
                    .format(len(results), user_parameters['type']),
                    "No problem! There are {} attractions that match your needs. Could you help narrow it down with more information?"
                    .format(len(results)),
                ])
            }

        if len(results) == 1:
            report = printout_result(results[0])
            return {
                "fulfillmentText":
                random.choice([
                    "I found it! {}".format(report),
                    "Good news! {}".format(report)
                ])
            }

        # report details about the alternatives
        return {
            "fulfillmentText":
            random.choice([
                "I found {} attractions for you.".format(len(results)),
                "There are {} attractions that match your needs.".format(
                    len(results))
            ])
        }
    return {
        "fulfillmentText":
        random.choice([
            "Sorry, I cannot find you anything. Please try another way.",
            "Sorry, I am in outer space right now."
        ])
    }
Esempio n. 3
0
user['username'] = '******'
user['first_name'] = 'Seth'
user['last_name'] = 'Washeck'
user['address'] = '123 Main St.'
user['city'] = 'Springfield'
user['state'] = 'CO'
user['zip'] = '90210'
user['email'] = '*****@*****.**'

db.flush_db()

db.save_user_profile(user)

categories = [
    'Recreational Activities (Outdoor)', 'Recreational Activities (Indoor)',
    'Community Groups', 'Academic Groups', 'Spiritual Groups'
]

db.save_list('category', categories)

print db.get_list('category')

activities = ['Hiking', 'Biking', 'Climbing', 'Camping']

db.save_list('Recreational Activities (Outdoor)', activities)

print 'Recreational Activities (Outdoor): ', db.get_list(
    'Recreational Activities (Outdoor)')

print db.get_user_profile('swasheck')