-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentopinion.py
48 lines (41 loc) · 2.21 KB
/
sentopinion.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
def sentiment_analysis_with_opinion_mining(client, documents):
# documents = [
# "Cops are the worst. I want to defund the police, and end police brutality. Cops are dangerous"
# ]
result = client.analyze_sentiment(documents, show_opinion_mining=True)
doc_result = [doc for doc in result if not doc.is_error]
positive_reviews = [doc for doc in doc_result if doc.sentiment == "positive"]
negative_reviews = [doc for doc in doc_result if doc.sentiment == "negative"]
positive_mined_opinions = []
mixed_mined_opinions = []
negative_mined_opinions = []
for document in doc_result:
print("Document Sentiment: {}".format(document.sentiment))
print("Overall scores: positive={0:.2f}; neutral={1:.2f}; negative={2:.2f} \n".format(
document.confidence_scores.positive,
document.confidence_scores.neutral,
document.confidence_scores.negative,
))
for sentence in document.sentences:
print("Sentence: {}".format(sentence.text))
print("Sentence sentiment: {}".format(sentence.sentiment))
print("Sentence score:\nPositive={0:.2f}\nNeutral={1:.2f}\nNegative={2:.2f}\n".format(
sentence.confidence_scores.positive,
sentence.confidence_scores.neutral,
sentence.confidence_scores.negative,
))
for mined_opinion in sentence.mined_opinions:
aspect = mined_opinion.aspect
print("......'{}' aspect '{}'".format(aspect.sentiment, aspect.text))
print("......Aspect score:\n......Positive={0:.2f}\n......Negative={1:.2f}\n".format(
aspect.confidence_scores.positive,
aspect.confidence_scores.negative,
))
for opinion in mined_opinion.opinions:
print("......'{}' opinion '{}'".format(opinion.sentiment, opinion.text))
print("......Opinion score:\n......Positive={0:.2f}\n......Negative={1:.2f}\n".format(
opinion.confidence_scores.positive,
opinion.confidence_scores.negative,
))
print("\n")
print("\n")