简体   繁体   中英

ConceptNet Database Connectivity with Java

Does somebody know how to connect ConceptNet Database with Java. I searched different tutorials, checked different forums but I still couldn't find the correct methodology.

Also, how do I get and post data to/from ConceptNet using Java.

Some people tell me that by using JSON or Flat Csv, I will achieve my query's reply, but I am not familiar with these two technologies or how to use them with ConceptNet Database and Java.

If anyone knows, kindly reply me ...

Heres what I did to access a query from ConceptNet. I used the org.apache.commons.io.IOUtils and org.json maven directories. Hope this helps.

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;

import org.apache.commons.io.IOUtils;
import org.json.*;

public class httprequestpractice {
    public static void main(String[] args) {
        try {
            // url containing the word to be indexed
            String obj = "http://api.conceptnet.io/c/en/example";
            // open HttpURLConnection
            HttpURLConnection hp = (HttpURLConnection) new URL(obj)
                    .openConnection();
            // set to request method to get
            // not required since default
            hp.setRequestMethod("GET");
            // get the inputstream in the json format
            hp.setRequestProperty("Accept", "application/json");
            // get inputstream from httpurlconnection
            InputStream is = hp.getInputStream();
            // get text from inputstream using IOUtils
            String jsonText = IOUtils.toString(is, Charset.forName("UTF-8"));
            // get json object from the json String
            JSONObject json = new JSONObject(jsonText);
            // get the edges array from the JSONObject which contains all
            // content
            JSONArray edges = json.getJSONArray("edges");
            // goes through the edges array
            for (int x = 0; x < edges.length(); x++) {
                // convert the first object of the json array into a jsonobject
                // once it is a jsonobject you can use getString or getJSONArray
                // to continue in getting info
                System.out.println(
                        edges.getJSONObject(x));
            }
            is.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM