1

ConceptNet データベースと Java を接続する方法を知っている人はいますか? さまざまなチュートリアルを検索し、さまざまなフォーラムをチェックしましたが、それでも正しい方法論が見つかりませんでした。

また、Java を使用して ConceptNet との間でデータを取得および投稿するにはどうすればよいですか。

JSON または Flat Csv を使用することでクエリの応答が得られると言う人もいますが、私はこれら 2 つのテクノロジや、ConceptNet データベースと Java での使用方法に慣れていません。

知ってる人いたら回答よろしくお願いします...

4

1 に答える 1

0

ConceptNet からクエリにアクセスするために私が行ったことは次のとおりです。org.apache.commons.io.IOUtilsおよびorg.jsonmaven ディレクトリを使用しました。お役に立てれば。

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();
        }
    }
}
于 2017-06-01T16:33:12.127 に答える