gpt4 book ai didi

java - 使用 jena 在 java 中构建维基百科查询表单

转载 作者:太空宇宙 更新时间:2023-11-04 08:40:03 25 4
gpt4 key购买 nike

#!/usr/local/bin/python

import sys
sys.path.append('/usr/home/bobd/lib/python/') # needed for hosted version
from SPARQLWrapper import SPARQLWrapper, JSON
import string
import urllib
import cgi

def main():
form = cgi.FieldStorage()
dir1name = form.getvalue('dir1')
dir2name = form.getvalue('dir2')

sparql = SPARQLWrapper("http://data.linkedmdb.org/sparql")
queryString = """

PREFIX m: <http://data.linkedmdb.org/resource/movie/>
SELECT DISTINCT ?actorName WHERE {

?dir1 m:director_name "DIR1-NAME".
?dir2 m:director_name "DIR2-NAME".
?dir1film m:director ?dir1;
m:actor ?actor.

?dir2film m:director ?dir2;
m:actor ?actor.

?actor m:actor_name ?actorName.
}
"""

queryString = queryString.replace("DIR1-NAME",dir1name)
queryString = queryString.replace("DIR2-NAME",dir2name)

sparql.setQuery(queryString)
sparql.setReturnFormat(JSON)

try:
ret = sparql.query()
results = ret.convert()
requestGood = True
except Exception, e:
results = str(e)
requestGood = False

print """Content-type: text/html

<html>
<head>
<title>results</title>
<link href="simple.css" type="text/css" rel="stylesheet" />
</head>
<body>
"""

if requestGood == False:
print "<h1>Problem communicating with the server</h1>"
print "<p>" + results + "</p>"
elif (len(results["results"]["bindings"]) == 0):
print "<p>No results found.</p>"

else:
for result in results["results"]["bindings"]:
print "<p>" + result["actorName"]["value"] + "</p>"

print "</body></html>"

main()

我从 http://www.ibm.com/developerworks/xml/library/x-wikiquery/#download 得到了上面的代码

现在,我想用 servlet 而不是 cgi 脚本在 java 中做同样的事情。

那么,我如何将查询从 servlet 传递到 http://data.linkedmdb.org/sparql使用 jena 的端点?

我应该如何获取结果并以 html 形式显示它?

请帮忙

最佳答案

这是一些示例代码。请注意,我实际上不会这样做:演示代码和后端查询处理都混合在一个类中。真正的应用程序不会这样做!该示例忠实于您的示例,只是我使用 Jena 的工具来预绑定(bind)一些查询变量,而不是使用 string-bash 查询(因此 DIR1-NAME 变为 ?dir-1-name)。我认为这更简洁,并且可以让您更灵活地选择从客户端传入哪些查询参数。

package example;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.*;

public class ServletExample
extends HttpServlet
{
/***********************************/
/* Constants */
/***********************************/

private static final long serialVersionUID = 1L;

public static final String SPARQL_ENDPOINT = "http://data.linkedmdb.org/sparql";

public static final String QUERY = "PREFIX m: <http://data.linkedmdb.org/resource/movie/>\n" +
"SELECT DISTINCT ?actorName WHERE {\n" +
" ?dir1 m:director_name %dir_name_1%.\n" +
" ?dir2 m:director_name %dir_name_2%.\n" +
" ?dir1film m:director ?dir1;\n" +
" m:actor ?actor.\n" +
" ?dir2film m:director ?dir2;\n" +
" m:actor ?actor.\n" +
" ?actor m:actor_name ?actorName.\n" +
"}\n" +
"";

private static final String HEADER = "<html>\n" +
" <head>\n" +
" <title>results</title>\n" +
" <link href=\"simple.css\" type=\"text/css\" rel=\"stylesheet\" />\n" +
" </head>\n" +
" <body>\n" +
"";

private static final String FOOTER = "</body></html>";

/**
* Respond to HTTP GET request. Will need to be mounted against some URL
* pattern in web.xml
*/
@Override
protected void doGet( HttpServletRequest req, HttpServletResponse resp )
throws ServletException, IOException
{
String dir1 = req.getParameter( "dir1" );
String dir2 = req.getParameter( "dir2" );

if (dir1 == null || dir2 == null || dir1.isEmpty() || dir2.isEmpty()) {
noInput( resp );
}
else {
runQuery( resp, dir1, dir2 );
}
}

protected void noInput( HttpServletResponse resp )
throws IOException
{
header( resp );
resp.getWriter().println( "<p>Please select director names as query params <code>dir1</code> and <code>dir2</code></p>" );
footer( resp );
}

protected void footer( HttpServletResponse resp ) throws IOException {
resp.getWriter().println( FOOTER );
}

protected void header( HttpServletResponse resp ) throws IOException {
resp.getWriter().println( HEADER );
}

protected void runQuery( HttpServletResponse resp, String dir1, String dir2 )
throws IOException
{
PrintWriter out = resp.getWriter();

// Set up the query
String q = QUERY.replace( "%dir_name_1%", "\"" + dir1 + "\"" )
.replace( "%dir_name_2%", "\"" + dir2 + "\"" );

Query query = QueryFactory.create( q ) ;
QueryExecution qexec = QueryExecutionFactory.sparqlService( SPARQL_ENDPOINT, query );

// perform the query
ResultSet results = qexec.execSelect();

// generate the output
header( resp );
if (!results.hasNext()) {
out.println( "<p>No results, sorry.</p>" );
}
else {
out.println( "<h1>Results</h1>" );
while (results.hasNext()) {
QuerySolution qs = results.next();
String actorName = qs.getLiteral( "actorName" ).getLexicalForm();
out.println( String.format( "<div>Actor named: %s</div>", actorName ) );
}
}
footer( resp );
}
}

据我所知,这段代码有效,但我不知道您使用的哪些查询应该返回一些结果。

关于java - 使用 jena 在 java 中构建维基百科查询表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5521067/

25 4 0
文章推荐: c# - ASP 固定 header
文章推荐: python - 字符串系列到数组
文章推荐: c - 流缓冲问题
文章推荐: Python 数组组计数
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com