-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyse.py
73 lines (55 loc) · 2.06 KB
/
analyse.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 os
from dotenv import load_dotenv
from azure.ai.textanalytics import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential
#loading API keys
load_dotenv()
key = os.getenv('key')
endpoint = os.getenv('endpoint')
#authorizing the client
ta_credential = AzureKeyCredential(key)
client = TextAnalyticsClient(endpoint = endpoint, credential = ta_credential)
def sentiment_analysis(document):
'''
Function to analyze the scraped reviews
Argument:
Array of reviews
Returns:
Sentiment analysis
'''
#analyzing the sentiment
response = client.analyze_sentiment(documents = document)[0]
sentiment = response.sentiment
positive_score = response.confidence_scores.positive
negative_score = response.confidence_scores.negative
neutral_score = response.confidence_scores.neutral
overall_sentiment = {
'sentiment' : response.sentiment,
'positive_score' : response.confidence_scores.positive,
'negative_score' : response.confidence_scores.negative,
'neutral_score' : response.confidence_scores.neutral
}
return overall_sentiment
def opinion_mine(document):
'''
Function to get opinion of target words in reviews document
Argument:
An Array of reviews
Returns:
A dictionary with key as a target word and its value as the opinion of the target word
'''
opinions = {}
for doc in document:
arr = [doc]
result = client.analyze_sentiment(arr, show_opinion_mining = True)
doc_result = [k for k in result if not k.is_error]
for d in doc_result:
for s in d.sentences:
for opinion in s.mined_opinions:
target = opinion.target
target_text = target.text
target_positive = target.confidence_scores.positive
target_negative = target.confidence_scores.negative
if target_text not in opinions:
opinions[target_text] = [target_positive, target_negative]
return opinions