Example #1
0
def parse_author(request, session_attributes):
    """
    :param request: JSON data from the Finna API
    :param author: Author term of the current session
    :param session_attributes: session attributes for current session if user
    has given some
    :return: Response to AWS server in JSON format
    """

    # if author was not found
    author = session_attributes.get('author')
    if not author:
        message = "I'm sorry. I couldn't catch the author. Please try again."
        return util.elicit_intent({'author': author}, message)

    result_count = request['resultCount']

    # find all titles if title does not already exist in list. And sort them.
    real_count = 0
    books_written_by = []

    for record in request['records']:
        authors = record.get('nonPresenterAuthors')
        has_written = False
        if authors:
            for a in authors:
                if a.get('name').lower() == author:
                    has_written = True
                    break
            if has_written:
                title = record.get('title')
                if title:
                    if title not in books_written_by:
                        books_written_by.append(title)
                        real_count += 1
    # if books list is empty
    if not len(books_written_by):
        message = "I'm sorry, I didn't found any books written ny " + author
        return util.elicit_intent({}, message)

    # answer will be at most three books
    find = sorted(books_written_by)
    print(str(find))
    if len(find) > 3:
        find = find[:3]
        find.append("others")

    # only one book was found
    if result_count == 1:
        message = author + " has written a book " + find[0]
    # many books was found
    else:
        message = author + " has written books " + util.make_string_list(find)
    return util.elicit_intent({'author': author}, message)
Example #2
0
def drinks(which):
    category = data[which]
    output = util.make_string_list(category.keys())
    message = which.lower() + " include " + output + ". You can ask more " \
                                                     "about these drinks or " \
                                                     "about any other " \
                                                     "category or drink."
    return util.elicit_intent({}, message)
Example #3
0
def help_answer(intent):
    if intent['slots']['what'] is None:
        message = "Hi! I'm Libby help. You can ask me about the weather, " \
                  "roberts coffee menu, locations or more information about " \
                  "some book or author"
        return util.elicit_intent({}, message)
    else:
        data = json.load(open('help.json'))
        what_name = intent['slots']['what']
        if what_name in data:
            message = data[what_name]
        else:
            message = "I'm sorry, I didn't found " + what_name + " help. " \
                    "You can ask me about the weather, roberts coffee menu, " \
                    "locations or more information about some book or author"

        return util.elicit_intent({}, message)
Example #4
0
def intro():
    key, value = data.popitem()
    output = ", ".join(data) + " and " + key
    data.update({key: value})
    message = {
        'contentType': 'PlainText',
        'content': "Robert's Coffee's drink categories are " + output
    }
    return util.elicit_intent({}, message)
Example #5
0
def author_search(intent, subject):

    author = AS.search(intent['inputTranscript'], False)

    # If author is found, make an API call with it.
    if author:
        request = lookfor(subject,
                          filter=["author:\"" + author + "\""])['json']
        return parse_subject(request, subject, {'author': author})

    return util.elicit_intent({
        'subject': subject
    }, "Sorry, I didn't manage to narrow down the search with the extra information I was given."
                              )
Example #6
0
def prices(intent):
    drink = intent['slots']['consumable'].lower()
    consumables = {}
    for i in data.values():
        consumables.update(i)
    sizes = util.parse_prices(
        list(
            sum(sorted(consumables[drink]['size'].items(), key=lambda x: x[1]),
                ())))
    key, value = sizes[len(sizes) - 2], sizes[len(sizes) - 1]
    sizes = sizes[0:len(sizes) - 2]
    output = ""
    if len(sizes) > 0:
        output += ", ".join(sizes) + " and "
    output += key + ", " + value
    message = "Prices for " + drink + " are: " + output
    return util.elicit_intent({}, message)
Example #7
0
def drinks(which):
    category = data[which]
    key, value = category.popitem()
    output = ", ".join(category) + " and " + key
    category.update({key: value})
    message = {
        'contentType':
        'PlainText',
        'content':
        which.lower() + " include " + output +
        ". You can ask more about these drinks or about any other "
        "category or drink."
    }
    """
    slots = {

    }
    """
    return util.elicit_intent({}, message)
Example #8
0
def intro():
    output = util.make_string_list(data.keys())
    message = "Robert's Coffee's drink categories are " + output
    return util.elicit_intent({}, message)
Example #9
0
def parse_subject(request, subject, session_attributes={}):
    """
    :param request: JSON data from the Finna API
    :param subject: Subject or search term of the current session
    :param session_attributes: session attributes for current session if user
    has given some
    :return: Response to AWS server in JSON format
    """
    author = session_attributes.get('author')
    if subject is "":
        if author:
            return parse_author(request, {'author': author})
        return util.elicit_intent(
            session_attributes, "I'm sorry. I was not "
            "able to catch what book"
            " you wanted to find. "
            "Could you please repeat.")

    if request['status'] == 'OK':
        result_count = request['resultCount']

        # no books was found with the subject
        if result_count == 0:
            message = "Oh I'm so sorry, no books was found with search term: "\
                        + subject
            if author:
                message += " written by " + str(author)
            return util.close({
                'subject': subject,
                'author': author
            }, 'Fulfilled', message)

        elif result_count == 1:
            return find_info(request['records'][0]['id'])
        # if less than five books was found, will be checked all different
        # locations in books way that there is not same locations twice.
        elif result_count < 5:
            real_count = 0
            find_locations = []
            while real_count < result_count:
                buildings = request['records'][real_count]['buildings']
                for layer in buildings:
                    if re.compile("1/AALTO/([a-z])*/").match(layer['value']):
                        if layer['translated'] not in find_locations:
                            find_locations.append(layer['translated'])
                real_count += 1

            # if author was also found.
            if author:
                message = subject + " books by " + str(author) \
                 + " can be found in " + util.make_string_list(find_locations)
            else:
                message = subject + " books can be found in " + \
                      util.make_string_list(find_locations)
            return util.close({
                'subject': subject,
                'author': author
            }, 'Fulfilled', message)
        # there is more than five results, this ask more information from user
        else:
            if not author:
                message = "I found " + str(result_count) + " books with term " \
                          + subject + ". Please specify an author or a year, " \
                                      "     so I can narrow down the search."
            else:
                message = "I found " + str(result_count) + " books with " \
                          + subject + " by author " + author + ". Can you " \
                          "give the publication date for example to narrow " \
                          "down the search."
            return util.elicit_intent({
                'subject': subject,
                'author': author
            }, message)
    else:
        return util.close({'author': None}, 'Fulfilled',
                          "Something went wrong")