【问题标题】:querying dbpedia-spotlight using java使用 java 查询 dbpedia-spotlight
【发布时间】:2014-02-14 05:26:12
【问题描述】:

我正在尝试改编 DBpedia-SpotLight 上报告的示例之一:

DBpediaSpotlightClient.java(必填AnnotationClient.java

使用此示例,给出描述并查询 Spotlight 服务以检索注释响应: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

再次感谢您!

【问题讨论】:

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


    【解决方案1】:

    为了使用其他方法,你应该改变GetMethod。例如:

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

    等等

    【讨论】:

    • 感谢您的回答,但是我尝试了这种方法,它不起作用。我想它不接受这些参数,或者它可能需要另一个标题才能工作。
    • 我现在更仔细地查看,似乎在 new GetMethod(API_URL + "rest/candidates/?" 这一行有错误。你应该使用 "rest/candidates?" 而不是 "休息/候选人/?”。
    • 我按照您的建议做了,但仍然遇到问题。标题和注释一样吗?谢谢。
    • 我修改了原始消息,以便您可以看到我收到的内容。
    • 只是一个疑问:spot 函数的输出是什么?我猜它不是应用程序/json。也许这就是问题所在。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 2010-10-23
    相关资源
    最近更新 更多