in python/ts-to-word.py [0:0]
def generate_sentiment(segment_list, language_code):
"""
Generates sentiment per speech segment, inserting the results into the input list. This will use
Amazon Comprehend, but we need to map the job language code to one that Comprehend understands
:param segment_list: List of speech segments
:param language_code: Language code to use for the Comprehend job
"""
# Get our botot3 client, then go through each segment
client = boto3.client("comprehend")
for nextSegment in segment_list:
if len(nextSegment.segmentText) >= MIN_SENTIMENT_LENGTH:
nextText = nextSegment.segmentText
response = client.detect_sentiment(Text=nextText, LanguageCode=language_code)
positiveBase = response["SentimentScore"]["Positive"]
negativeBase = response["SentimentScore"]["Negative"]
# If we're over the NEGATIVE threshold then we're negative
if negativeBase >= MIN_SENTIMENT_NEGATIVE:
nextSegment.segmentIsNegative = True
nextSegment.segmentSentimentScore = negativeBase
# Else if we're over the POSITIVE threshold then we're positive,
# otherwise we're either MIXED or NEUTRAL and we don't really care
elif positiveBase >= MIN_SENTIMENT_POSITIVE:
nextSegment.segmentIsPositive = True
nextSegment.segmentSentimentScore = positiveBase
# Store all of the original sentiments for future use
nextSegment.segmentAllSentiments = response["SentimentScore"]
nextSegment.segmentPositive = positiveBase
nextSegment.segmentNegative = negativeBase