SkE/Methods/example: example3_ca.java

File example3_ca.java, 3.5 KB (added by xkovar3, 7 months ago)
Line 
1import org.apache.commons.httpclient.*;
2import org.apache.commons.httpclient.cookie.CookiePolicy;
3import org.apache.commons.httpclient.methods.*;
4import org.json.*;
5import java.util.HashMap;
6import java.util.Map;
7import java.io.*;
8
9/**
10 * @author Vojtech Kovar, xkovar3@fi.muni.cz
11 * @author Milos Jakubicek, xjakub@fi.muni.cz
12 */
13
14class example3_ca {
15
16    static final String root_url = "beta.sketchengine.co.uk";
17    static final String ske_username = "<username>";
18    static final String ske_password = "<password>";
19   
20    public static void main(String[] args) {
21       
22        String corp = "bnc";
23        String method = "view";
24        String base_url = "/auth/preloaded_corpus/" + corp + "/ske/";
25
26        // specifying attributes
27        Map attrs = new HashMap();
28        attrs.put("pagesize", "1");
29        attrs.put("format", "json");
30       
31           // query list can be loaded from a file, ...
32        int qlist_size = 4;
33        String query_list[] = new String[qlist_size];
34        query_list[0] = "[lemma=\"test\"]";
35        query_list[1] = "[lemma=\"drug\"][lemma=\"test\"]";
36        query_list[2] = "[lemma=\"blood\"][lemma=\"test\"]";
37        query_list[3] = "[lemma=\"test\"][lemma=\"result\"]";
38
39        // make HTTP connection
40        HttpClient client = new HttpClient();
41        client.getHostConfiguration().setHost(root_url, 80, "http");
42        client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
43       
44        // retrieve session id
45        GetMethod authget = new GetMethod("/login/");
46        try {
47            client.executeMethod(authget);
48        } catch (IOException ex) {
49            System.err.println("Error: couldn't retrieve session ID from Sketch Engine server.");
50            System.exit(1);
51        }
52        authget.releaseConnection();
53       
54        // login   
55        PostMethod authpost = new PostMethod("/login/");
56        NameValuePair submit   = new NameValuePair("submit", "ok");
57        NameValuePair username = new NameValuePair("username", ske_username);
58        NameValuePair password = new NameValuePair("password", ske_password);
59        authpost.setRequestBody(new NameValuePair[] {submit, username, password});
60           try {
61             client.executeMethod(authpost);
62        } catch (IOException ex) {
63            System.err.println("Error: couldn't login to Sketch Engine server.");
64            System.exit(2);
65        }
66        authpost.releaseConnection();
67
68        // retrieve data
69        for (int i = 0; i < qlist_size; i++) {
70            try {
71                attrs.put("q", "q" + query_list[i]);
72                JSONObject json_query = new JSONObject(attrs);
73                String url_string = base_url + method + "?json=" + json_query.toString();
74                GetMethod getJSON = new GetMethod(new URI(url_string, false).toString());
75                client.executeMethod(getJSON);
76                JSONObject json = new JSONObject(new BufferedReader(new InputStreamReader (getJSON.getResponseBodyAsStream())).readLine());
77                System.out.println(query_list[i] + "\t" + json.get("concsize").toString());
78                getJSON.releaseConnection();
79            } catch (URIException ex) {
80                System.err.println("Error: malformed URI in request.");
81            } catch (JSONException ex) {
82                System.err.println("Error: malformed JSON format.");
83            } catch (IOException ex) {
84                System.err.println("Error: couldn't retrieve JSON data from Sketch Engine server.");
85            }
86        }
87    }
88}