Difference between revisions of "Python3 Interactive Client"

From Textserver wiki
Jump to: navigation, search
(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
  
# necesari per a la conexio al textserver
+
# set query elements
import requests
+
text = input('Text to analyze: ')
from xml.dom.minidom import parseString
+
lang = input('Language: ')
  # Create request
+
out = input('Output format (xml,json,conll,naf): ')
    request_data = {'username':'jofre',
+
user = input('TextServer Username: ')
                    'password':'jofre.123',
+
pwd = input('TextServer Password: ')
                    'text_input':text,
+
                    'language':lang,
+
# Create request
                    'output':'xml',
+
request_data = {'username':user,
                    'interactive':'1' }
+
                'password':pwd,
    url = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws/processQuery/tagger"
+
                'text_input':text,
    # Send request and get response   
+
                'language':lang,
    resp = requests.post(url, files=request_data)
+
                'output':out,
 +
                'interactive':'1' }
  
    # process response (posar-la en el format adqueat pel RegexpParser)
+
# service URL
    xml = parseString(resp.text)
+
service = "SERVICENAME"
    result = []
+
url = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws/processQuery/"+service
    for s in xml.getElementsByTagName("sentence"):
+
 
        tks = []
+
# Send request and get response   
        for t in s.getElementsByTagName("token") :
+
resp = requests.post(url, files=request_data)
            tks.append((t.attributes["form"].value, t.attributes["tag"].value))
+
 
           
+
# HTTP error, raise exception
        result.append(tks)
+
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)