Difference between revisions of "Example Clients"
From Textserver wiki
Line 16: | Line 16: | ||
== Example Clients for Interactive Jobs == | == Example Clients for Interactive Jobs == | ||
+ | How to call the "tagger" service from python | ||
<syntaxhighlight lang="python" line="1" > | <syntaxhighlight lang="python" line="1" > | ||
− | + | #! /usr/bin/python | |
− | + | ||
− | + | ######################################################################## | |
− | + | # | |
− | + | # Example client to submit an interactive request to TextServer | |
− | + | # SERVICENAME service. | |
− | + | # Input may be a plain text or a text file. No ZIP files. | |
− | + | # Output will be in the requested format (XML, json, conll) | |
+ | # | ||
+ | ######################################################################## | ||
+ | |||
+ | |||
+ | # import required libraries | ||
+ | import urllib2 | ||
+ | # You may need to install "poster" python module for these two: | ||
+ | from poster.encode import multipart_encode | ||
+ | from poster.streaminghttp import register_openers | ||
+ | |||
+ | # Register the streaming http handlers with urllib2 | ||
+ | register_openers() | ||
+ | |||
+ | # set query elements | ||
+ | text = raw_input('Text to analyze: ') | ||
+ | lang = raw_input('Language: ') | ||
+ | out = raw_input('Output format (xml,json,conll,naf): ') | ||
+ | user = raw_input('TextServer Username: ') | ||
+ | pwd = raw_input('TextServer Password: ') | ||
+ | |||
+ | # Encode query in a form-data. | ||
+ | # 'headers' contains the necessary Content-Type and Content-Length. | ||
+ | # 'datagen' is a generator object that yields the encoded parameters. | ||
+ | datagen, headers = multipart_encode({'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 | ||
+ | |||
+ | # Create the Request object | ||
+ | request = urllib2.Request(url, datagen, headers) | ||
+ | try: | ||
+ | # Actually do the request, and get the response | ||
+ | resp = urllib2.urlopen(request).read() | ||
+ | |||
+ | except urllib2.HTTPError, e: | ||
+ | # handle connection errors | ||
+ | print e, "-", e.read() | ||
+ | exit() | ||
+ | |||
+ | # No error, appropriately process response | ||
+ | # (e.g. parsing XML or JSON, and doing clever stuff with the content) | ||
+ | print resp | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 12:45, 20 January 2016
To call a TextServer service from a client program running on your side, this client needs to send the appropriate request in the right format.
The request is a standard HTTP POST request with a multiform part containing the same information you would submit via the web interface.
Below you'll find a list of example client programs in different programming languages.
The same code can be used to access any TextServer service with small variations:
- Simply change the service name in the URL to access a different service. You can find out the service name at the top of the "Execute" form of your subscribed services.
- Replace the parameters in the example client with the parametes that the called service requires. Rules are simple:
- Language identification service does not require a "language" parameter, but the other services do.
- All services require a "interactive" parameter stating whether the job is interactive or batch.
- All services require either a "text_input" parameter (for interactive jobs) or a "input_file" parameter (for batch jobs).
- You can find out the name of any other specific parameters for a service in the "Execute" form of your subscribed services.
Example Clients for Interactive Jobs
How to call the "tagger" service from python
1 #! /usr/bin/python
2
3 ########################################################################
4 #
5 # Example client to submit an interactive request to TextServer
6 # SERVICENAME service.
7 # Input may be a plain text or a text file. No ZIP files.
8 # Output will be in the requested format (XML, json, conll)
9 #
10 ########################################################################
11
12
13 # import required libraries
14 import urllib2
15 # You may need to install "poster" python module for these two:
16 from poster.encode import multipart_encode
17 from poster.streaminghttp import register_openers
18
19 # Register the streaming http handlers with urllib2
20 register_openers()
21
22 # set query elements
23 text = raw_input('Text to analyze: ')
24 lang = raw_input('Language: ')
25 out = raw_input('Output format (xml,json,conll,naf): ')
26 user = raw_input('TextServer Username: ')
27 pwd = raw_input('TextServer Password: ')
28
29 # Encode query in a form-data.
30 # 'headers' contains the necessary Content-Type and Content-Length.
31 # 'datagen' is a generator object that yields the encoded parameters.
32 datagen, headers = multipart_encode({'username':user,
33 'password':pwd,
34 'text_input':text,
35 'language':lang,
36 'output':out,
37 'interactive':'1'
38 } )
39 # service URL
40 service = "SERVICENAME"
41 url = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws/processQuery/"+service
42
43 # Create the Request object
44 request = urllib2.Request(url, datagen, headers)
45 try:
46 # Actually do the request, and get the response
47 resp = urllib2.urlopen(request).read()
48
49 except urllib2.HTTPError, e:
50 # handle connection errors
51 print e, "-", e.read()
52 exit()
53
54 # No error, appropriately process response
55 # (e.g. parsing XML or JSON, and doing clever stuff with the content)
56 print resp