in functions/source/kendra_search_intent_handler_lambda/helpers.py [0:0]
def answer_result_type(response):
"""
Generate the answer text from the document, plus the URL link to the document.
:param response: Kendra query response
:return: Answer text
"""
try:
document_title = response['resultItems'][0]['documentTitle']['text']
# document_excerpt_text = response['ResultItems'][0]['DocumentExcerpt']['Text']
document_id = response['resultItems'][0]['documentId']
document_text = response['resultItems'][0]['additionalAttributes'][0][
'value']['textWithHighlightsValue']['text']
pos = document_id.rindex("/")
document_key = document_id[(pos + 1):]
logger.info(document_key)
document_url = create_presigned_url(os.environ['KENDRA_DATA_BUCKET'], document_key)
if response['resultItems'][0]['additionalAttributes'][0][
'value']['textWithHighlightsValue']['highlights'][0]['topAnswer']:
begin = int(response['resultItems'][0]['additionalAttributes'][0][
'value']['textWithHighlightsValue']['highlights'][0]['beginOffset'])
end = int(response['resultItems'][0]['additionalAttributes'][0][
'value']['textWithHighlightsValue']['highlights'][0]['endOffset'])
topanswer = response['resultItems'][0]['additionalAttributes'][0][
'value']['textWithHighlightsValue']['text']
answer_text = "On searching the Enterprise repository, I have found" \
" the following answer as a top answer--"
answer_text += "\nDocument Title: " + document_title
# + " --- Excerpt: " + document_excerpt_text + ">"
answer_text += '-- \"' + topanswer[begin:end] + '\"'
answer_text += "\nHere is a document you could review- " + document_url + "\n"
else:
answer_text = "On searching the Enterprise repository, I have found" \
" the following answer in the suggested answers section"
answer_text += " -- " + document_title + " --- \"" + document_text + "\""
answer_text += "\n\n Here is a document you could review-" + document_url + "\n"
except KeyError:
answer_text = "\"Sorry, I could not find the answer in our documents.\""
return answer_text