Julias Lösung für aufg. 3 um nen bufferedreader ergänzt um variable begriffe einzugeben

parent 4c155326
Pipeline #1442 skipped
package Test.Test1;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.ResultSetFormatter;
import org.wikidata.wdtk.datamodel.interfaces.EntityDocument;
import org.wikidata.wdtk.wikibaseapi.WikibaseDataFetcher;
import org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException;
public class App {
public static void main(String[] args) throws MediaWikiApiErrorException {
// String für Suchbegriffe initialisieren
String inputList = null;
//Liste von Suchbegriffen einlesen und im String "inputlist" speichern
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Suchbegriffe eingeben und durch Kommata (ohne Leerzeichen) trennen: ");
try {
inputList = br.readLine();
} catch (IOException e1) {
e1.printStackTrace();
}
// Liste an Kommata splitten und Strings in ArrayList schreiben
ArrayList<String> list = new ArrayList<String>();
for (String s : inputList.split(",")) {
System.out.println(s.trim());
list.add(s);
}
// EntityId über Titel (= Werte in der ArrayList list) ermitteln und in
// ArrayList ids schreiben
ArrayList<EntityDocument> ids = new ArrayList<EntityDocument>();
WikibaseDataFetcher wdf = WikibaseDataFetcher.getWikidataDataFetcher();
for (int i = 0; i < list.size(); i++) {
EntityDocument ed = wdf.getEntityDocumentByTitle("enwiki", list.get(i).toString());
if (ed != null) {
ids.add(ed);
}
}
Map<String, String> src = new HashMap<String, String>();
// QueryString
String qs = "";
qs += "PREFIX wd: <http://www.wikidata.org/entity/>";
qs += "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>";
qs += "SELECT DISTINCT *";
qs += "WHERE";
qs += "{";
int counter = 1;
for (EntityDocument ent : ids) {
System.out.println(ent.getEntityId());
String item = ent.getEntityId().getId();
qs += "?value ?p_" + counter + " wd:" + item + " .";
counter++;
}
qs += "OPTIONAL {?value rdfs:label ?value_label filter (lang(?value_label) = \"de\") .} ";
qs += "}";
qs += "ORDER BY DESC(?value)";
// new Query Execution: use qs and send to wikidata-sparql endpoint
QueryExecution qe = QueryExecutionFactory.sparqlService("https://query.wikidata.org/sparql",
QueryFactory.create(qs));
// Workaround: Ergebnis als Reslut-Set, dann "Umwandlung" in json
ResultSetFormatter.outputAsJSON(qe.execSelect());
qe.close();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment