Skip to content

Instantly share code, notes, and snippets.

@anuragmishra1
Last active January 17, 2018 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anuragmishra1/4c5c05e6c02d5d31a52d59d4571a1eaf to your computer and use it in GitHub Desktop.
Save anuragmishra1/4c5c05e6c02d5d31a52d59d4571a1eaf to your computer and use it in GitHub Desktop.
import boto3
import time
from datetime import timedelta
import sys
import os
import argparse
#We need to get our API credentials in the code for authentication that we have stored as Environment Variables locally.
os.environ.get("AWS_ACCESS_KEY_ID")
os.environ.get("AWS_SECRET_ACCESS_KEY")
os.environ.get("AWS_REGION")
#Following line is used to save all the console outputs in a text file.
sys.stdout = open('output.txt','a')
start_time = time.monotonic()
def input_file(text_file_path):
global text
if os.path.isfile(text_file_path):
with open(text_file_path, 'r') as text_file:
text = text_file.read()
else:
print("File doesn't exist in the directory!")
def dominant_language_text():
#Initialize amazon_comprehend client function
client_comprehend = boto3.client(
'comprehend',
)
dominant_language_response = client_comprehend.detect_dominant_language(
Text = text
)
#Print the Dominant Language
print("Language:", sorted(dominant_language_response['Languages'], key = lambda k: k['LanguageCode'])[0]['LanguageCode'])
def entities_text():
#Initialize amazon_comprehend client function
client_comprehend = boto3.client(
'comprehend',
)
response_entities = client_comprehend.detect_entities(
Text = text,
LanguageCode = 'en'
)
entities = list(set([obj['Type'] for obj in response_entities['Entities']]))
#Print the Entities
print("Entities:",entities)
def key_phrases_text():
#Initialize amazon_comprehend client function
client_comprehend = boto3.client(
'comprehend',
)
response_key_phrases = client_comprehend.detect_key_phrases(
Text = text,
LanguageCode = 'en'
)
key_phrases = list(set([obj['Text'] for obj in response_key_phrases['KeyPhrases']]))
#Print the Key Phrases
print("Key Phrases:", key_phrases)
def sentiment_text():
#Initialize amazon_comprehend client function
client_comprehend = boto3.client(
'comprehend',
)
response_sentiment = client_comprehend.detect_sentiment(
Text = text,
LanguageCode = 'en'
)
sentiment = response_sentiment['Sentiment']
#Print the Sentiment
print("Sentiment Analysis:" , sentiment)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description = __doc__,
formatter_class = argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'text_file_path',
help = 'The complete file path of the text file you want to analyze.')
args = parser.parse_args()
input_file(args.text_file_path)
dominant_language_text()
entities_text()
key_phrases_text()
sentiment_text()
end_time = time.monotonic()
print("Execution_Time:", timedelta(seconds = end_time - start_time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment