Difference between revisions of "Python3 Interactive Client"
From Textserver wiki
(Created page with "<syntaxhighlight lang="python" line="1" > #!/usr/bin/env python3 import requests # necesari per a la conexio al textserver import requests from xml.dom.minidom import parseS...") |
|||
(4 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
import requests | import requests | ||
− | # | + | # set query elements |
− | + | text = input('Text to analyze: ') | |
− | + | lang = input('Language: ') | |
− | + | out = input('Output format (xml,json,conll,naf): ') | |
− | + | user = input('TextServer Username: ') | |
− | + | pwd = input('TextServer Password: ') | |
− | + | ||
− | + | # Create request | |
− | + | request_data = {'username':user, | |
− | + | 'password':pwd, | |
− | + | 'text_input':text, | |
− | + | 'language':lang, | |
− | + | 'output':out, | |
+ | 'interactive':'1' } | ||
− | + | # service URL | |
− | + | service = "SERVICENAME" | |
− | + | url = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws/processQuery/"+service | |
− | + | ||
− | + | # Send request and get response | |
− | + | resp = requests.post(url, files=request_data) | |
− | + | ||
− | + | # HTTP error, raise exception | |
− | + | if resp.status_code != requests.codes.ok : | |
+ | resp.raise_for_status() | ||
+ | |||
+ | # No error, appropriately process response | ||
+ | # (e.g. parsing XML or JSON, and doing clever stuff with the content) | ||
+ | print(resp.text) | ||
+ | </syntaxhighlight> |
Latest revision as of 16:39, 3 April 2019
1 #!/usr/bin/env python3
2
3 import requests
4
5 # set query elements
6 text = input('Text to analyze: ')
7 lang = input('Language: ')
8 out = input('Output format (xml,json,conll,naf): ')
9 user = input('TextServer Username: ')
10 pwd = input('TextServer Password: ')
11
12 # Create request
13 request_data = {'username':user,
14 'password':pwd,
15 'text_input':text,
16 'language':lang,
17 'output':out,
18 'interactive':'1' }
19
20 # service URL
21 service = "SERVICENAME"
22 url = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws/processQuery/"+service
23
24 # Send request and get response
25 resp = requests.post(url, files=request_data)
26
27 # HTTP error, raise exception
28 if resp.status_code != requests.codes.ok :
29 resp.raise_for_status()
30
31 # No error, appropriately process response
32 # (e.g. parsing XML or JSON, and doing clever stuff with the content)
33 print(resp.text)