Exemple #1
0
def resolve_remove_destination(clinc_request):
    """Resolve request when state is to remove destination."""
    print("start resolve remove_destination...")
    user_id = clinc_request['external_user_id']
    doc_ref = collection.document(user_id)
    doc = doc_ref.get()
    if not doc.exists:
        doc_ref.set({'sessionId': user_id})
    try:
        city = doc_ref.get().to_dict()['city']
    except:
        city = "-1"
    if clinc_request['slots']:
        destination = capitalize_name(
            clinc_request['slots']['_DESTINATION_']['values'][0]['tokens'])
        last_edit = doc_ref.get().to_dict()['last_edit']
        city_doc_ref = city_collection.document(city)
        city_recommendations = city_doc_ref.get().to_dict()['recommendations']
        clinc_request['slots']['_DESTINATION_']['values'][0][
            'value'] = destination
        if destination in ["This Place", "This", "It", "There", "That"]:
            if last_edit != -1:
                destination = city_recommendations['results'][last_edit][
                    'name']
            else:
                destination = "nowhere"
        clinc_request['slots']['_DESTINATION_']['values'][0][
            'resolved'] = 1  # why the value of 'values' is list???
        clinc_request['slots']['_DESTINATION_']['values'][0][
            'value'] = destination
        added_destinations = doc_ref.get().to_dict()['destinations']
        found_place = 0
        for idx, d in enumerate(added_destinations):
            if destination == d:
                added_destinations.pop(idx)
                doc_ref.update({'destinations': added_destinations})
                found_place = 1
                break
        if found_place == 0:
            clinc_request['slots']['_NOTINLIST_'] = {
                "type":
                "string",
                "values": [{
                    "resolved": 1,
                    "value": destination + " is not in your list."
                }]
            }
    print("finish resolving, send response back to clinc...")
    pp.pprint(clinc_request)
    return jsonify(**clinc_request)
Exemple #2
0
def resolve_remove_destination(clinc_request):
    print("start resolve remove_destination...")
    user_id = clinc_request['external_user_id']
    doc_ref = collection.document(user_id)
    doc = doc_ref.get()
    if not doc.exists:
        doc_ref.set({'sessionId': user_id})

    if clinc_request['slots']:
        destination = capitalize_name(
            clinc_request['slots']['_DESTINATION_']['values'][0]['tokens'])
        # clinc_request['visual_payload'] = {
        #     'destination': destination
        # }
        clinc_request['slots']['_DESTINATION_']['values'][0][
            'value'] = destination
        clinc_request['slots']['_DESTINATION_']['values'][0][
            'resolved'] = 1  # why the value of 'values' is list???
        added_destinations = doc_ref.get().to_dict()['destinations']
        found_place = 0
        for idx, d in enumerate(added_destinations):
            if destination == d:
                added_destinations.pop(idx)
                doc_ref.update({'destinations': added_destinations})
                found_place = 1
                break
        if found_place == 0:
            clinc_request['slots']['_NOTINLIST_'] = {
                "type":
                "string",
                "values": [{
                    "resolved": 1,
                    "value": destination + " is not in your list."
                }]
            }

    print("finish resolving, send response back to clinc...")
    pp.pprint(clinc_request)
    return jsonify(**clinc_request)
Exemple #3
0
def resolve_add_destination(clinc_request):
    print("start resolve add_destination...")
    user_id = clinc_request['external_user_id']
    doc_ref = collection.document(user_id)
    doc = doc_ref.get()
    if not doc.exists:
        doc_ref.set({'sessionId': user_id})

    city_dict = doc_ref.get().to_dict()
    if "city" not in city_dict or "length_of_visit" not in city_dict or "number_of_people" not in city_dict:
        clinc_request['slots'] = {
            "_NOBASICINFO_": {
                "type":
                "string",
                "values": [{
                    "resolved":
                    1,
                    "value":
                    "Sorry, please provide your basic information before I can add destination. "
                }]
            }
        }
        print("finish resolving, send response back to clinc...")
        pp.pprint(clinc_request)
        return jsonify(**clinc_request)

    # TODO
    # determine if need business transition
    # clinc_request['state'] = "generate_shedule"
    try:
        count = doc_ref.get().to_dict()["count"]
        city = doc_ref.get().to_dict()['city']
        ndays = doc_ref.get().to_dict()['length_of_visit']
        last_edit = doc_ref.get().to_dict()['last_edit']
    except KeyError:
        city = "-1"
        print("No count or city or ndays.")

    if clinc_request['slots']:
        destination = capitalize_name(
            clinc_request['slots']['_DESTINATION_']['values'][0]['tokens'])
        # clinc_request['visual_payload'] = {
        #     'destination': destination
        # }
        clinc_request['slots']['_DESTINATION_']['values'][0][
            'value'] = destination
        clinc_request['slots']['_DESTINATION_']['values'][0][
            'resolved'] = 1  # why the value of 'values' is list???

        city_doc_ref = city_collection.document(city)
        city_recommendations = city_doc_ref.get().to_dict(
        )["recommendations"]["results"]
        city_name_dict = city_doc_ref.get().to_dict()["name_to_index"]

        # print("city_recommendations: ", city_recommendations)
        if destination in ["This Place", "This", "It", "There", "That"]:
            print("count", count)
            if last_edit != -1:
                destination_name = city_recommendations[last_edit]['name']
            else:
                destination_name = "nowhere"
            added_destinations = doc_ref.get().to_dict()['destinations']
            if destination_name not in added_destinations:  # Add successfully
                added_destinations.append(destination_name)
                doc_ref.update({'destinations': added_destinations})
                nplaces = len(added_destinations) - 1
                if float(nplaces) / float(ndays) >= 3:
                    clinc_request['slots']['_SUGGEST_'] = {
                        "type":
                        "string",
                        "values": [{
                            "resolved":
                            1,
                            "value":
                            "You have added " + str(nplaces) +
                            " destinations in your " + str(ndays) +
                            "-day trip. I think that's enough. You may go to generate itinerary."
                        }]
                    }
            else:
                clinc_request['slots']['_ADDTWICE_'] = {
                    "type":
                    "string",
                    "values": [{
                        "resolved":
                        1,
                        "value":
                        destination_name +
                        " is already in the list. No need to add twice."
                    }]
                }

            clinc_request['slots']['_DESTINATION_']['values'][0][
                'value'] = destination_name

        else:  # Directly add place by name
            # print('destination: ', destination)
            # print('city_name_dict.keys():', city_name_dict)

            if destination in city_name_dict:  # destination exists
                print('destination in dict')
            else:  # destination not in recommendation list, cannot add
                clinc_request['slots']['_DESTINATION_']['values'][0][
                    'resolved'] = 0

            if clinc_request['slots']['_DESTINATION_']['values'][0][
                    'resolved'] == 1:
                added_destinations = doc_ref.get().to_dict()['destinations']
                if destination not in added_destinations:  # and haven't been added to the list
                    added_destinations.append(destination)
                    doc_ref.update({'destinations': added_destinations})
                else:  # This place is already in your list. No need to add twice.
                    clinc_request['slots']['_ADDTWICE_'] = {
                        "type":
                        "string",
                        "values": [{
                            "resolved":
                            1,
                            "value":
                            destination +
                            " is already in the list. No need to add twice."
                        }]
                    }

    print("finish resolving, send response back to clinc...")
    pp.pprint(clinc_request)
    return jsonify(**clinc_request)
Exemple #4
0
def resolve_remove_destination(clinc_request):
    print("start resolve remove_destination...")
    user_id = clinc_request['external_user_id']
    doc_ref = collection.document(user_id)
    doc = doc_ref.get()
    if not doc.exists:
        doc_ref.set({'sessionId': user_id})

    try:
        city = doc_ref.get().to_dict()['city']
    except:
        city = "-1"

    if clinc_request['slots']:
        destination = capitalize_name(
            clinc_request['slots']['_DESTINATION_']['values'][0]['tokens'])
        last_edit = doc_ref.get().to_dict()['last_edit']
        city_doc_ref = city_collection.document(city)
        city_recommendations = city_doc_ref.get().to_dict()['recommendations']

        #mapper_values = {}
        #candidates = []
        #for place in city_recommendations:
        #    mapper_values[place['name']] = [place['name']]
        #    candidate_value = {'value' : place['name']}
        #    candidates.append(candidate_value)

        #clinc_request['slots']['_DESTINATION_']['candidates'] = candidates
        #clinc_request['slots']['_DESTINATION_']['mappings'] = [
        #    {
        #        "algorithm" : "partial_ratio",
        #        "threshold" : 0.6,
        #        "type" : "fuzzy",
        #        "values" : mapper_values
        #    }
        #]

        # clinc_request['visual_payload'] = {
        #     'destination': destination
        # }
        clinc_request['slots']['_DESTINATION_']['values'][0][
            'value'] = destination
        if destination in ["This Place", "This", "It", "There", "That"]:
            if last_edit != -1:
                destination = city_recommendations['results'][last_edit][
                    'name']
            else:
                destination = "nowhere"
        clinc_request['slots']['_DESTINATION_']['values'][0][
            'resolved'] = 1  # why the value of 'values' is list???
        clinc_request['slots']['_DESTINATION_']['values'][0][
            'value'] = destination
        added_destinations = doc_ref.get().to_dict()['destinations']

        found_place = 0
        for idx, d in enumerate(added_destinations):
            if destination == d:
                added_destinations.pop(idx)
                doc_ref.update({'destinations': added_destinations})
                found_place = 1
                break
        if found_place == 0:
            clinc_request['slots']['_NOTINLIST_'] = {
                "type":
                "string",
                "values": [{
                    "resolved": 1,
                    "value": destination + " is not in your list."
                }]
            }

    print("finish resolving, send response back to clinc...")
    pp.pprint(clinc_request)
    return jsonify(**clinc_request)
Exemple #5
0
def resolve_destination_info(clinc_request):
    print("start resolve destination_info...")
    user_id = clinc_request['external_user_id']
    doc_ref = collection.document(user_id)
    doc = doc_ref.get()
    if not doc.exists:
        doc_ref.set({'sessionId': user_id})

    city_dict = doc_ref.get().to_dict()

    if "city" not in city_dict:
        clinc_request['slots'] = {
            "_NOCITY_": {
                "type":
                "string",
                "values": [{
                    "resolved":
                    1,
                    "value":
                    "Sorry, please tell me which city you're going to before I can give you information. "
                }]
            }
        }
        pp.pprint(clinc_request)
        return jsonify(**clinc_request)

    try:
        city = doc_ref.get().to_dict()['city']
    except KeyError:
        city = "-1"
        print("No city.")

    if clinc_request['slots']:
        destination = capitalize_name(
            clinc_request['slots']['_DESTINATION_']['values'][0]['tokens'])
        # clinc_request['visual_payload'] = {
        #     'destination': destination
        # }
        city_doc_ref = city_collection.document(city)
        city_recommendations = city_doc_ref.get().to_dict(
        )["recommendations"]["results"]
        city_name_dict = city_doc_ref.get().to_dict()["name_to_index"]

        mapper_values = {}
        existing = {}
        candidates = []
        for place in city_recommendations:
            if place['name'] in existing:
                print("*******Duplicate")
            else:
                existing[place['name']] = 1
                mapper_values[place['name']] = [place['name']]
                candidate_value = {'value': place['name']}
                candidates.append(candidate_value)

        clinc_request['slots']['_DESTINATION_']['candidates'] = candidates
        print("*****candidates*********")
        print(candidates)
        clinc_request['slots']['_DESTINATION_']['mappings'] = [{
            "algorithm":
            "partial_ratio",
            "threshold":
            0.6,
            "type":
            "fuzzy",
            "values":
            mapper_values
        }]

        if destination in city_name_dict:  # destination exists
            print('destination in dict')
            clinc_request['slots']['_DESTINATION_']['values'][0][
                'value'] = destination
            clinc_request['slots']['_DESTINATION_']['values'][0][
                'resolved'] = 1  # why the value of 'values' is list???

            idx = city_name_dict[destination]
            doc_ref.update({'last_edit': idx})
            idx = int(idx)
            clinc_request['visual_payload'] = {
                "intro":
                city_recommendations[idx]['intro'],
                "image":
                city_recommendations[idx]['images'][0]['sizes']['medium']
                ['url'],
                "name":
                city_recommendations[idx]['name']
            }

        else:
            clinc_request['slots']['_DESTINATION_']['values'][0][
                'resolved'] = 0

        if clinc_request['slots']['_DESTINATION_']['values'][0][
                'resolved'] == 1:
            idx = city_name_dict[destination]
            doc_ref.update({'last_edit': idx})

    print("finish resolving, send response back to clinc...")
    pp.pprint(clinc_request)
    # requst clinc
    # get response
    # resolve
    return jsonify(**clinc_request)
Exemple #6
0
def resolve_destination_info(clinc_request):
    print("start resolve destination_info...")
    clinc_request['slots']['_DESTINATION_']['values'][0][
        'value'] = clinc_request['slots']['_DESTINATION_']['values'][0][
            'tokens']
    clinc_request['slots']['_DESTINATION_']['values'][0]['resolved'] = 1

    user_id = clinc_request['external_user_id']
    doc_ref = collection.document(user_id)
    doc = doc_ref.get()
    if not doc.exists:
        doc_ref.set({'sessionId': user_id})

    city_dict = doc_ref.get().to_dict()

    if "city" not in city_dict:
        clinc_request['slots'] = {
            "_NOCITY_": {
                "type":
                "string",
                "values": [{
                    "resolved":
                    1,
                    "value":
                    "Sorry, please tell me which city you're going to before I can give you information. "
                }]
            }
        }
        pp.pprint(clinc_request)
        return jsonify(**clinc_request)

    try:
        city = doc_ref.get().to_dict()['city']
    except KeyError:
        city = "-1"
        print("No city.")

    if clinc_request['slots']:
        destination = capitalize_name(
            clinc_request['slots']['_DESTINATION_']['values'][0]['tokens'])
        # clinc_request['visual_payload'] = {
        #     'destination': destination
        # }

        city_doc_ref = city_collection.document(city)
        city_recommendations = city_doc_ref.get().to_dict()["recommendations"]
        city_name_dict = city_doc_ref.get().to_dict()["name_to_index"]

        if destination in city_name_dict:  # destination exists
            print('destination in dict')
            clinc_request['slots']['_DESTINATION_']['values'][0][
                'value'] = destination
            clinc_request['slots']['_DESTINATION_']['values'][0][
                'resolved'] = 1  # why the value of 'values' is list???
        else:  # destination not in recommendation list, cannot add
            clinc_request['slots']['_DESTINATION_']['values'][0][
                'resolved'] = -1

    # TODO
    # request the trip api to get information about the destination
    # figure out what to return back to the user

    print("finish resolving, send response back to clinc...")
    pp.pprint(clinc_request)
    return jsonify(**clinc_request)