-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathazure_query.py
107 lines (85 loc) · 3.23 KB
/
azure_query.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
import requests
subscription_key = '0f27bbcf921f4891b9c52819f96fb41a'
#'371c2fad1bb8416fb36b741444a26aa5'
assert subscription_key
analytics_base_url = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/"
import os
def chunks(l, n):
# For item i in a range that is a length of l,
for i in range(0, len(l), n):
# Create an index range for l of n items:
yield l[i:i+n]
e = {}
def do_forum():
cnt = 0
f = open('author_forum', 'r')
forums = f.read()
lines = forums.split("\n")
documents = list(chunks(lines, 500))
for document in documents:
c_json = {}
c_json['documents'] = []
for line in document:
try:
f_spl = line.split(",")
e[cnt] = line
author = f_spl[0]
post = f_spl[1][:4500]
#print len(post)
c_json['documents'].append( { 'id': cnt, 'text': post })
except:
print line
cnt = cnt+1
text_analytics_base_url = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/"
sentiment_api_url = text_analytics_base_url + "sentiment"
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
response = requests.post(sentiment_api_url, headers=headers, json=c_json)
key_phrases = response.json()
docs = key_phrases['documents']
for doc in docs:
print str(e[int(doc['id'])]) + "," + str(doc['score'])
# print e
#print key_phrases
def do_keywords():
f = open('discs_labeled_indexed.csv','r')
discs = f.read()
c_json = {}
c_json['documents'] = []
b_json = {}
b_json['documents'] = []
A = discs.split("\n")
B = A[:len(A)//2]
C = A[len(A)//2:]
for line in C:
try:
disc = line.split(",")[1]
if len(disc) > 0:
j_str = "{ 'id': '" + line.split(",")[2] + "', 'text': '" + disc + "'}"
c_json['documents'].append( { 'id': line.split(",")[2], 'text': disc })
except:
pass
for line in B:
try:
disc = line.split(",")[1]
if len(disc) > 0:
j_str = "{ 'id': '" + line.split(",")[2] + "', 'text': '" + disc + "'}"
b_json['documents'].append( { 'id': line.split(",")[2], 'text': disc })
except:
pass
text_analytics_base_url = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/"
key_phrase_api_url = text_analytics_base_url + "keyPhrases"
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
response = requests.post(key_phrase_api_url, headers=headers, json=c_json)
key_phrases = response.json()
print key_phrases
text_analytics_base_url = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/"
key_phrase_api_url = text_analytics_base_url + "keyPhrases"
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
response = requests.post(key_phrase_api_url, headers=headers, json=b_json)
key_phrases = response.json()
print key_phrases
do_forum()
#print run_azure_keywords('try to find the keywords')
#thingspeak
#371c2fad1bb8416fb36b741444a26aa5
#4f44a20351ea402bb4ce166c475d3d15