-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpos.py
35 lines (27 loc) · 1.2 KB
/
pos.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
# Our gateway to the holy grail!
# All the API calls are made through this file. Text is passed around through sys args
# Written by Deepraj Pandey
#!/usr/bin/env python
"""Musings with the Google Cloud Natural Language API syntax analysis"""
import json, sys
from google.oauth2 import service_account
import googleapiclient.discovery
text = ''#figured out a way to take a multi-word input from the command line and pass it to the python script
t_index=0 #to keep track of the index of the program name so that it isn't taken into account as a string
for x in sys.argv: #taking the text from the command line and storing into a variable
if(t_index != 0): #this is required to skip the name of the program "pos.py" from the command line
text = text + x + ' '
t_index=1
body = {
'document': {
'type': 'PLAIN_TEXT',
'content': text,
},
'encoding_type': 'UTF32'
}
credentials = service_account.Credentials.from_service_account_file('JSON/file.json')
service = googleapiclient.discovery.build('language', 'v1', credentials=credentials)
request = service.documents().analyzeSyntax(body=body)
response = request.execute()
with open('JSON/partsOfSpeech.json', 'w') as fp:
json.dump(response, fp, indent=2)