如何在javascript中为vspedia中的dbpedia运行sparql查询以获取抽象?

时间:2015-05-23 12:29:28

标签: javascript c# asp.net sparql dbpedia

我想使用sparql从dbpedia中获取摘要。我想在Visual Studio C#中的ASP .NET中运行以下查询。这就是我所做的。

<script>
var query= "\
PREFIX dbpedia: <http://dbpedia.org/resource/>\
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>\
SELECT ?abstract\
 WHERE {\
 ?s dbpedia:Junagadh 
 dbpedia-owl:abstract ?abstract\
 }";


var queryUrl = url + "?query=" + encodeURIComponent(query) + "&format=json";
  $.ajax({
      dataType: "jsonp",
      url: queryUrl,
      success: function (_data) {
          var results = _data.results.bindings;

          for (var i in results) {
              var src = results[i].abstract.value;
              $('body').append(src);


          }
      }
  });

我想在浏览器中对输入的搜索词进行抽象。在这里,我进入&#34;印度&#34;。 当我运行此代码时,我没有在浏览器中获得输出。

所以我的问题是如何从javascript执行此查询并在浏览器中获取结果?

请帮帮我, 谢谢。

编辑#1:

我尝试了建议运行sparql查询的示例链接。 我尝试了在示例中给出的代码但在第17行中得到了错误。

 Version number '1.1' is invalid. Line 1, position 16.
 Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.Xml.XmlException: Version number '1.1' is invalid. Line 1, position 16.
source Error: 

Line 16:         //Make a SELECT query against the Endpoint
Line 17:         SparqlResultSet results = endpoint.QueryWithResultSet("SELECT DISTINCT ?Concept WHERE {[] a ?Concept}");
Line 18:         foreach (SparqlResult result in results)
Line 19:         {


Stack Trace: 
[XmlException: Version number '1.1' is invalid. Line 1, position 16.]
System.Xml.XmlTextReaderImpl.Throw(Exception e) +76
System.Xml.XmlTextReaderImpl.Throw(String res, String arg) +126
System.Xml.XmlTextReaderImpl.ParseXmlDeclaration(Boolean isTextDecl)      +3983351
System.Xml.XmlTextReaderImpl.Read() +239
VDS.RDF.Parsing.SparqlXmlParser.Parse(SparqlXmlParserContext context) in e:\mercurial\dotnetrdf\Libraries\core\net40\Parsing\SPARQLXMLParser.cs:376
VDS.RDF.Parsing.SparqlXmlParser.Load(ISparqlResultsHandler handler, TextReader input) in e:\mercurial\dotnetrdf\Libraries\core\net40\Parsing\SPARQLXMLParser.cs:101
VDS.RDF.Parsing.SparqlXmlParser.Load(ISparqlResultsHandler handler, StreamReader input) in e:\mercurial\dotnetrdf\Libraries\core\net40\Parsing\SPARQLXMLParser.cs:124
VDS.RDF.Query.SparqlRemoteEndpoint.QueryWithResultSet(ISparqlResultsHandler handler, String sparqlQuery) in e:\mercurial\dotnetrdf\Libraries\core\net40\Query\SPARQLRemoteEndpoint.cs:309
VDS.RDF.Query.SparqlRemoteEndpoint.QueryWithResultSet(String sparqlQuery) in e:\mercurial\dotnetrdf\Libraries\core\net40\Query\SPARQLRemoteEndpoint.cs:284
test.Page_Load(Object sender, EventArgs e) in c:\Users\MKS\Documents\Visual Studio 2010\WebSites\WebSite3\test.aspx.cs:17
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

请帮帮我, 谢谢。

1 个答案:

答案 0 :(得分:1)

您的查询完全错误。编写SPARQL查询时,需要询问三?s ?p ?o个问题。如果查看您的查询,那么您有4个项目。因此,如果您需要所有语言的摘要,您需要提出:

SELECT ?abstract
WHERE {
    dbpedia:Junagadh dbpedia-owl:abstract ?abstract.
}

如果您需要抽象的特定语言,则需要相应地对其进行过滤。例如,英文摘要将是:

SELECT ?abstract
WHERE {
    dbpedia:Junagadh dbpedia-owl:abstract ?abstract.
    filter(lang(?abstract)='en')
}

如果您需要知道如何在.net环境中编写SPARQL,请阅读这些examples