-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle_nlp_api.py
35 lines (30 loc) · 1.12 KB
/
google_nlp_api.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
import httplib2
import sys
from googleapiclient import discovery
from googleapiclient.errors import HttpError
def test_language_api(content):
discovery_url = 'https://{api}.googleapis.com/$discovery/rest?version={apiVersion}'
service = discovery.build(
'language', 'v1',
http=httplib2.Http(),
discoveryServiceUrl=discovery_url,
developerKey="AIzaSyBZ7ud1PX9GF5ZUCrizb1HofTH_zPzAAxI",
)
service_request = service.documents().annotateText(
body={
'document': {
'type': 'PLAIN_TEXT',
'content': content,
},
'features': {
'extract_syntax': True,
'extractEntities': True,
'extractDocumentSentiment': True,
},
'encodingType': 'UTF16' if sys.maxunicode == 65535 else 'UTF32',
})
try:
response = service_request.execute()
except HttpError as e:
response = {'error': e}
return response