Skip to content

Instantly share code, notes, and snippets.

@nmwalsh
Created February 1, 2020 19:54
Show Gist options
  • Save nmwalsh/d273571785ebc0230e901ef4329d5fa4 to your computer and use it in GitHub Desktop.
Save nmwalsh/d273571785ebc0230e901ef4329d5fa4 to your computer and use it in GitHub Desktop.
import boto3
# instantiate local comprehend client
comprehend_client = boto3.client('comprehend')
text = "A classic love heart emoji, used for expressions of love. Displayed in various shades of red on most platforms. A similar emoji exists for the heart suit in a deck of playing cards. On Snapchat, this emoji displays next to a friend when you have been #1 BFs with each other for two consecutive weeks."
# Invoke detect_key_phrases endopint and get response
comprehend_response = comprehend_client.detect_key_phrases(Text=text, LanguageCode='en')
# raw api response
print(comprehend_response)
"""
{'KeyPhrases': [{'Score': 0.9999838471412659, 'Text': 'A classic love heart', 'BeginOffset': 0, 'EndOffset': 20}, {'Score': 1.0, 'Text': 'expressions', 'BeginOffset': 37, 'EndOffset': 48}, {'Score': 0.9999972581863403, 'Text': 'love', 'BeginOffset': 52, 'EndOffset': 56}, {'Score': 0.9999985098838806, 'Text': 'various shades', 'BeginOffset': 71, 'EndOffset': 85}, {'Score': 0.9977516531944275, 'Text': 'red', 'BeginOffset': 89, 'EndOffset': 92}, {'Score': 0.9999953508377075, 'Text': 'most platforms', 'BeginOffset': 96, 'EndOffset': 110}, {'Score': 0.9999998211860657, 'Text': 'A similar emoji', 'BeginOffset': 112, 'EndOffset': 127}, {'Score': 0.9999955296516418, 'Text': 'the heart suit', 'BeginOffset': 139, 'EndOffset': 153}, {'Score': 1.0, 'Text': 'a deck', 'BeginOffset': 157, 'EndOffset': 163}, {'Score': 0.9997950792312622, 'Text': 'playing cards', 'BeginOffset': 167, 'EndOffset': 180}, {'Score': 0.9999970197677612, 'Text': 'Snapchat', 'BeginOffset': 185, 'EndOffset': 193}, {'Score': 0.9999982714653015, 'Text': 'this emoji', 'BeginOffset': 195, 'EndOffset': 205}, {'Score': 0.9999929070472717, 'Text': 'a friend', 'BeginOffset': 223, 'EndOffset': 231}, {'Score': 0.9830252528190613, 'Text': '#1 BFs', 'BeginOffset': 251, 'EndOffset': 257}, {'Score': 0.9832307696342468, 'Text': 'each other', 'BeginOffset': 263, 'EndOffset': 273}, {'Score': 0.9940018057823181, 'Text': 'two consecutive weeks', 'BeginOffset': 278, 'EndOffset': 299}], 'ResponseMetadata': {'RequestId': '0ce20435-31fb-4571-88b3-ef62a9fd5393', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '0ce20435-31fb-4571-88b3-ef62a9fd5393', 'content-type': 'application/x-amz-json-1.1', 'content-length': '1319', 'date': 'Sat, 01 Feb 2020 19:31:15 GMT'}, 'RetryAttempts': 0}}
"""
for item in key_phrases['KeyPhrases']:
print(item)
"""
{'Score': 0.9999838471412659, 'Text': 'A classic love heart', 'BeginOffset': 0, 'EndOffset': 20}
{'Score': 1.0, 'Text': 'expressions', 'BeginOffset': 37, 'EndOffset': 48}
{'Score': 0.9999972581863403, 'Text': 'love', 'BeginOffset': 52, 'EndOffset': 56}
{'Score': 0.9999985098838806, 'Text': 'various shades', 'BeginOffset': 71, 'EndOffset': 85}
{'Score': 0.9977516531944275, 'Text': 'red', 'BeginOffset': 89, 'EndOffset': 92}
{'Score': 0.9999953508377075, 'Text': 'most platforms', 'BeginOffset': 96, 'EndOffset': 110}
{'Score': 0.9999998211860657, 'Text': 'A similar emoji', 'BeginOffset': 112, 'EndOffset': 127}
{'Score': 0.9999955296516418, 'Text': 'the heart suit', 'BeginOffset': 139, 'EndOffset': 153}
{'Score': 1.0, 'Text': 'a deck', 'BeginOffset': 157, 'EndOffset': 163}
{'Score': 0.9997950792312622, 'Text': 'playing cards', 'BeginOffset': 167, 'EndOffset': 180}
{'Score': 0.9999970197677612, 'Text': 'Snapchat', 'BeginOffset': 185, 'EndOffset': 193}
{'Score': 0.9999982714653015, 'Text': 'this emoji', 'BeginOffset': 195, 'EndOffset': 205}
{'Score': 0.9999929070472717, 'Text': 'a friend', 'BeginOffset': 223, 'EndOffset': 231}
{'Score': 0.9830252528190613, 'Text': '#1 BFs', 'BeginOffset': 251, 'EndOffset': 257}
{'Score': 0.9832307696342468, 'Text': 'each other', 'BeginOffset': 263, 'EndOffset': 273}
{'Score': 0.9940018057823181, 'Text': 'two consecutive weeks', 'BeginOffset': 278, 'EndOffset': 299}
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment