-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlanguage.py
72 lines (59 loc) · 2.43 KB
/
language.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import boto3
import json
session = boto3.Session(aws_access_key_id='AWS_ACCESS_KEY',
aws_secret_access_key='AWS_SECRET_KEY',
region_name='us-east-1')
comprehend = session.client('comprehend')
textBucket = 'hack-the-valley-text'
s3 = boto3.resource('s3')
def getTranscript(bucket):
jsonText = getFirstFile(textBucket)
try:
return jsonText['results']['transcripts'][0]['transcript']
except:
return "error"
def detectKeyPhrases(input):
return comprehend.detect_key_phrases(Text=input, LanguageCode='en')
def detectSentiment(input):
return comprehend.detect_sentiment(Text=input, LanguageCode='en')
def calculateSpeed(input):
time = 90
words = len(input.split())
return words*60/time
'''def processLanguage(name):
content_object = s3.Object('hack-the-valley-text', name)
file_content = content_object.get()['Body'].read().decode('utf-8')
json_content = json.loads(file_content)
input = str(json_content['results']['transcripts'][0]['transcript'])
print(input)
keyPhraseJson = detectKeyPhrases(input)
sentimentJson = detectSentiment(input)
sentiment = sentimentJson['Sentiment']
sentimentPercent = sentimentJson['SentimentScore'][sentiment.lower().capitalize()]
phrases = keyPhraseJson['KeyPhrases']
phraseList = [];
for dictionary in phrases:
phraseList.append(dictionary['Text'])
output = [sentiment, sentimentPercent, phraseList, calculateSpeed(input)]
print(output)
return output'''
def processLanguage(name):
content_object = s3.Object('hack-the-valley-text', name)
file_content = content_object.get()['Body'].read().decode('utf-8')
json_content = json.loads(file_content)
input = str(json_content['results']['transcripts'][0]['transcript'])
print(input)
keyPhraseJson = detectKeyPhrases(input)
sentimentJson = detectSentiment(input)
sentiment = sentimentJson['Sentiment']
sentimentPercent = sentimentJson['SentimentScore'][sentiment.lower().capitalize()]
phrases = keyPhraseJson['KeyPhrases']
phraseList = [];
for dictionary in phrases:
phraseList.append(dictionary['Text'])
output = [sentiment, sentimentPercent, phraseList, calculateSpeed(input)]
print(output)
return output
#except:
# print("error in processing language")
# return ["", -1, [], -1]