Sketch Engine
  • Login
  • Wiki
  • Timeline
  • View Tickets
  • New Ticket
  • Search
  • Settings

SkE/Methods: example3.java

File example3.java, 2.7 kB (added by vojta, 6 months ago)
Line 
1 package jsonexample;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.io.BufferedReader;
6 import java.io.InputStream;
7 import java.io.InputStreamReader;
8 import java.net.Authenticator;
9 import java.net.PasswordAuthentication;
10 import java.net.URL;
11 import org.json.*;
12
13 /**
14  * @author Vojtech Kovar, xkovar3@fi.muni.cz
15  */
16
17 public class Main {
18    
19     public Main() {
20     }
21    
22     public static void main(String[] args) {
23         int qlist_size = 4;
24         String data, url_string;
25         String base_url = "http://beta.sketchengine.co.uk/auth/corpora/run.cgi/";
26         String method = "view";
27         String query_list[] = new String[qlist_size];
28         Map attrs;
29         JSONObject json_query;
30        
31         final String usr = "<username>";
32         final String passwd = "<password>";
33        
34             // authentication issues
35         Authenticator auth = new Authenticator() {
36             protected PasswordAuthentication  getPasswordAuthentication () {
37                 return new PasswordAuthentication(usr, passwd.toCharArray());
38             }
39         };
40         Authenticator.setDefault(auth);
41        
42         // specifying attributes
43         attrs = new HashMap();
44         attrs.put("corpname", "preloaded/bnc");
45         attrs.put("pagesize", "1");
46         attrs.put("format", "json");
47         // query list can be loaded from a file, ...
48         query_list[0] = "[lemma=\"test\"]";
49         query_list[1] = "[lemma=\"drug\"][lemma=\"test\"]";
50         query_list[2] = "[lemma=\"blood\"][lemma=\"test\"]";
51         query_list[3] = "[lemma=\"test\"][lemma=\"result\"]";
52        
53         for (int i = 0; i < qlist_size; i++) {
54             attrs.put("q", "q" + query_list[i]);
55             json_query = new JSONObject(attrs);
56             url_string = base_url + method + "?json=" + json_query.toString();
57        
58             try {
59                     // connecting the SketchEngine Server
60                 URL url = new URL(url_string);
61                 InputStream stream = url.openStream();
62                 InputStreamReader isr = new InputStreamReader(stream);
63                 BufferedReader reader = new BufferedReader(isr);
64            
65                     // json data receiving
66                 data = reader.readLine(); // json data are on the first line
67             
68                     // now, in the 'data' variable, there is a json string
69                     // that can be parsed for json syntax
70                 JSONObject json = new JSONObject(data);
71                 System.out.println(query_list[i] + "\t" + json.get("concsize").toString());
72             }
73             catch(Exception e) {
74                 e.printStackTrace();
75             }
76         }
77     }
78    
79 }

Download in other formats:

  • Plain Text
  • Original Format

Sketch Engine
Bringing Corpora to the Masses

Lexical Computing Ltd

Brought to you by
Lexical Computing Ltd