in src/lambda/meaningful-conversations-lex-lambda.py [0:0]
def get_notes(intent_request):
session_attributes = intent_request['sessionAttributes'] if intent_request['sessionAttributes'] is not None else {}
inr = intent_request['currentIntent']['slots']['invoicenr']
i = 0
notes = ""
phrases = []
for file in input_bucket.objects.all():
i += 1
selected_phrases = ""
input_bucket_text_file = s3.Object(bucket, file.key)
text_file_contents = str(input_bucket_text_file.get()['Body'].read().decode('utf-8'))
detected_entities = comprehend.detect_entities(
Text=text_file_contents,
LanguageCode="en"
)
#print(detected_entities)
#selected_entity_types = ["ORGANIZATION", "OTHER", "DATE", "QUANTITY", "LOCATION"]
for x in detected_entities['Entities']:
if x['Type'] in "OTHER":
detnr = x['Text']
if detnr == inr:
#Comprehend Key Phrases Detection
detected_key_phrases = comprehend.detect_key_phrases(
Text=text_file_contents,
LanguageCode="en"
)
print(detected_key_phrases)
for y in detected_key_phrases['KeyPhrases']:
if y['EndOffset'] > 185 and y['EndOffset'] <= 337:
selected_phrases = " " + y['Text'] + selected_phrases + " "
#phrases.append(selected_phrases)
print("Selected Phrases are: " + selected_phrases)
#notes = notes + ". Notes for Invoice " + str(i) + " are: " + str(phrases[i - 1])
result = "Invoice Notes for " + detnr + ": " + selected_phrases
else:
result = 'Sorry I could not find a match for that Invoice Number. Please request for invoice notes with a valid Invoice Number'
return close(
session_attributes,
'Fulfilled',
{
'contentType': 'PlainText',
'content': result + '. Feel free to try the options again or you can simply close this chat'
}
)