-
Notifications
You must be signed in to change notification settings - Fork 0
/
linguistic.py
30 lines (26 loc) · 906 Bytes
/
linguistic.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
import http.client, json, nltk, api_key
from nltk.draw.tree import draw_trees
headers = {
# Request headers
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': api_key.LINGUISTIC,
}
# Returns a POS tagged & tokenised version of the message input
def getPOS(message):
body = {
"language" : "en",
"analyzerIds" : ["22a6b758-420f-4745-8a3c-46835a67c0d2"],
"text" : message
};
try:
conn = http.client.HTTPSConnection('api.projectoxford.ai')
conn.request("POST", "/linguistics/v1.0/analyze", json.dumps(body), headers)
response = conn.getresponse()
data = json.loads(response.read().decode())
conn.close()
ct = nltk.tree.Tree.fromstring(data[0]['result'][0])
pos = nltk.tree.Tree.pos(ct)
return pos;
except Exception as e: #Error occurred
print(e)
return False;