-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze.py
148 lines (118 loc) · 5.35 KB
/
analyze.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# -*- coding: utf-8 -*-
import os
from azure.cognitiveservices.language.textanalytics import TextAnalyticsClient
from msrest.authentication import CognitiveServicesCredentials
SUBSCRIPTION_KEY_ENV_NAME = "bc20ced3c3014badbf34d1799e28f2a2"
TEXTANALYTICS_LOCATION = os.environ.get(
"https://text-analysis-smartg.cognitiveservices.azure.com/", "westus2")
def language_extraction(subscription_key):
"""Language extraction.
This example detects the language of several strings.
"""
credentials = CognitiveServicesCredentials(subscription_key)
text_analytics_url = "https://{}.api.cognitive.microsoft.com".format(
TEXTANALYTICS_LOCATION)
text_analytics = TextAnalyticsClient(
endpoint=text_analytics_url, credentials=credentials)
try:
documents = [
{'id': '1', 'text': 'This is a document written in English.'},
{'id': '2', 'text': 'Este es un document escrito en Español.'},
{'id': '3', 'text': '这是一个用中文写的文件'}
]
response = text_analytics.detect_language(documents=documents)
for document in response.documents:
print("Document Id: ", document.id, ", Language: ",
document.detected_languages[0].name)
except Exception as err:
print("Encountered exception. {}".format(err))
def key_phrases(subscription_key,text):
"""Key-phrases.
Returns the key talking points in several text examples.
"""
credentials = CognitiveServicesCredentials(subscription_key)
text_analytics_url = "https://{}.api.cognitive.microsoft.com".format(
TEXTANALYTICS_LOCATION)
text_analytics = TextAnalyticsClient(
endpoint=text_analytics_url, credentials=credentials)
try:
documents = [
{"id": "1", "language": "en", "text": text}
]
for document in documents:
'''print(
#"Asking key-phrases on '{}' (id: {})".format(document['text'], document['id']))'''
response = text_analytics.key_phrases(documents=documents)
for document in response.documents:
#print("Document Id: ", document.id)
#print("\tKey Phrases:")
x=''
for phrase in document.key_phrases:
x+=phrase+'\n'
#print("\t\t", phrase)
ret_list = [documents[0]['text'],x]
return ret_list
except Exception as err:
print("Encountered exception. {}".format(err))
def sentiment(subscription_key):
"""Sentiment.
Scores close to 1 indicate positive sentiment, while scores close to 0 indicate negative sentiment.
"""
credentials = CognitiveServicesCredentials(subscription_key)
text_analytics_url = "https://{}.api.cognitive.microsoft.com".format(
TEXTANALYTICS_LOCATION)
text_analytics = TextAnalyticsClient(
endpoint=text_analytics_url, credentials=credentials)
try:
documents = [
{"id": "1", "language": "en", "text": "I had the best day of my life."},
{"id": "2", "language": "en",
"text": "This was a waste of my time. The speaker put me to sleep."},
{"id": "3", "language": "es", "text": "No tengo dinero ni nada que dar..."},
{"id": "4", "language": "it",
"text": "L'hotel veneziano era meraviglioso. È un bellissimo pezzo di architettura."}
]
response = text_analytics.sentiment(documents=documents)
for document in response.documents:
print("Document Id: ", document.id, ", Sentiment Score: ",
"{:.2f}".format(document.score))
except Exception as err:
print("Encountered exception. {}".format(err))
def entity_extraction(subscription_key,text):
"""EntityExtraction.
Extracts the entities from sentences and prints out their properties.
"""
credentials = CognitiveServicesCredentials(subscription_key)
text_analytics_url = "https://{}.api.cognitive.microsoft.com".format(
TEXTANALYTICS_LOCATION)
text_analytics = TextAnalyticsClient(
endpoint=text_analytics_url, credentials=credentials)
try:
documents = [
{"id": "1", "language": "en", "text": text},
]
response = text_analytics.entities(documents=documents)
for document in response.documents:
print("Document Id: ", document.id)
print("\tKey Entities:")
#return document.entities
for entity in document.entities:
print("\t\t", "NAME: ", entity.name, "\tType: ",
entity.type, "\tSub-type: ", entity.sub_type)
ml = [entity.name, entity.type, entity.sub_type]
return ml
#for match in entity.matches:
#print("\t\t\tOffset: ", match.offset, "\tLength: ", match.length, "\tScore: ","{:.2f}".format(match.entity_type_score))'''
except Exception as err:
print("Encountered exception. {}".format(err))
return 0
if __name__ == "__main__":
import sys
import os.path
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..")))
keys = key_phrases(SUBSCRIPTION_KEY_ENV_NAME,"hey akshat")
ent = entity_extraction(SUBSCRIPTION_KEY_ENV_NAME,"take me to Sydney Town Hall")
#print(keys)
print(ent)
print('input text: ',keys[0])
print('phrases: ',keys[1])