使用java查询dbpedia-spotlight

时间:2014-01-23 15:09:21

标签: java semantics jena semantic-web spotlight-dbpedia

我正在尝试调整在DBpedia-SpotLight上报告的一个示例:

DBpediaSpotlightClient.java(必填AnnotationClient.java

使用此示例,将给出描述并查询聚光灯服务以检索注释响应:Web Service

好吧,要运行此程序,必须选择输入文件(带说明)和输出文件(带结果)的路径:您可以在源代码的末尾找到它。

File input = new File("...");
File output = new File("...");

接下来,它接受的参数在此处报告:

GetMethod getMethod = new GetMethod(API_URL + "rest/annotate/?" +
                "confidence=" + CONFIDENCE
                + "&support=" + SUPPORT
                + "&text=" + URLEncoder.encode(text.text(), "utf-8"));
getMethod.addRequestHeader(new Header("Accept", "application/json"));
spotlightResponse = request(getMethod);

我认为它存储了传递给侦听聚光灯服务的参数。

正如Web Service所述,我还会使用其他选项(例如sparql参数)并使用其他查询方法,例如定位或候选人("rest/spot/?""rest/candidates/?"),但我不知道如何继续。

我该如何修改它? 是否需要另一个文件?

谢谢!

编辑:

请看一下我正在运行的代码(API_URL是“http://spotlight.dbpedia.org/”):

        LOG.info("Querying API.");
        String spotlightResponse;
        try {
            GetMethod getMethod = new GetMethod(API_URL + "rest/candidates?" +
                    "confidence=" + CONFIDENCE
                    + "&support=" + SUPPORT
                    + "&text=" + URLEncoder.encode(text.text(), "utf-8"));
            getMethod.addRequestHeader(new Header("Accept", "application/json"));

            spotlightResponse = request(getMethod);
        } catch (UnsupportedEncodingException e) {
            throw new AnnotationException("Could not encode text.", e);
        }

我尝试了你的建议,但我为每个请求找回了这种错误:

INFO 2014-01-28 12:40:41,578 main [DBpediaSpotlightClient] - Querying API.
gen 28, 2014 12:40:54 PM org.apache.commons.httpclient.HttpMethodBase getRespons
eBody
Avvertenza: Going to buffer response body of large or unknown size. Using getRes
ponseBodyAsStream instead is recommended.
ERROR 2014-01-28 12:40:55,089 main [DBpediaSpotlightClient] - org.dbpedia.spotli
ght.exceptions.AnnotationException: Received invalid response from DBpedia Spotl
ight API.
org.dbpedia.spotlight.exceptions.AnnotationException: Received invalid response
from DBpedia Spotlight API.
        at org.dbpedia.spotlight.evaluation.external.DBpediaSpotlightClient.extr
act(DBpediaSpotlightClient.java:74)
        at org.dbpedia.spotlight.evaluation.external.AnnotationClient.saveExtrac
tedEntitiesSet(AnnotationClient.java:138)
        at org.dbpedia.spotlight.evaluation.external.AnnotationClient.evaluateMa
nual(AnnotationClient.java:168)
        at org.dbpedia.spotlight.evaluation.external.AnnotationClient.evaluate(A
nnotationClient.java:164)
        at org.dbpedia.spotlight.evaluation.external.DBpediaSpotlightClient.main
(DBpediaSpotlightClient.java:112)
 INFO 2014-01-28 12:40:55,110 main [DBpediaSpotlightClient] - Extracted entities
 from 5 text items, with 0 successes and 5 errors.
 INFO 2014-01-28 12:40:55,110 main [DBpediaSpotlightClient] - Results saved to:
C:\Users\Alberto\Documents\projects\OmniTourist\apache jena\org\dbpedia\spotligh
t\evaluation\external\output.txt
 INFO 2014-01-28 12:40:55,114 main [DBpediaSpotlightClient] - Average extraction
 time: 0.0 ms

再次感谢你!

1 个答案:

答案 0 :(得分:0)

要使用其他方法,您应该更改GetMethod。 E.g:

// "rest/candidates/?"

GetMethod getMethod = new GetMethod(API_URL + "rest/candidates?" +
                "confidence=" + CONFIDENCE
                + "&support=" + SUPPORT
                + "&text=" + URLEncoder.encode(text.text(), "utf-8"));