Czy ktoś wie, gdzie można znaleźć trochę jak do na temat korzystania dblue spotlight w java lub scala? A może ktoś mógłby wyjaśnić, jak to się robi? Nie mogę znaleźć żadnych informacji na ten temat ...za pomocą dbpedia spotlight w java lub scala
Odpowiedz
Strony DBpedia Spotlight wiki byłyby dobrym miejscem do rozpoczęcia.
Uważam, że na stronie installation wymieniono najpopularniejsze sposoby (za pomocą słoika lub skonfigurowanie usługi internetowej) do korzystania z aplikacji.
Zawiera instrukcje na temat using the Java/Scala API przy użyciu własnej instalacji lub wywoływania usługi sieci Web.
Istnieje kilka dodatkowych danych, które należy pobrać, aby uruchomić własny serwer, aby zapewnić pełną obsługę, dobry czas na wypicie kawy.
potrzebujesz pobrać bazę danych dbpedia (plik jar), po tym możesz użyć kolejnych dwóch klas (autor pablomendes) i tylko wprowadzić pewne zmiany.
public class db extends AnnotationClient {
//private final static String API_URL = "http://jodaiber.dyndns.org:2222/";
private static String API_URL = "http://spotlight.dbpedia.org:80/";
private static double CONFIDENCE = 0.0;
private static int SUPPORT = 0;
private static String powered_by ="non";
private static String spotter ="CoOccurrenceBasedSelector";//"LingPipeSpotter"=Annotate all spots
//AtLeastOneNounSelector"=No verbs and adjs.
//"CoOccurrenceBasedSelector" =No 'common words'
//"NESpotter"=Only Per.,Org.,Loc.
private static String disambiguator ="Default";//Default ;Occurrences=Occurrence-centric;Document=Document-centric
private static String showScores ="yes";
@SuppressWarnings("static-access")
public void configiration(double CONFIDENCE,int SUPPORT,
String powered_by,String spotter,String disambiguator,String showScores){
this.CONFIDENCE=CONFIDENCE;
this.SUPPORT=SUPPORT;
this.powered_by=powered_by;
this.spotter=spotter;
this.disambiguator=disambiguator;
this.showScores=showScores;
}
public List<DBpediaResource> extract(Text text) throws AnnotationException {
LOG.info("Querying API.");
String spotlightResponse;
try {
String Query=API_URL + "rest/annotate/?" +
"confidence=" + CONFIDENCE
+ "&support=" + SUPPORT
+ "&spotter=" + spotter
+ "&disambiguator=" + disambiguator
+ "&showScores=" + showScores
+ "&powered_by=" + powered_by
+ "&text=" + URLEncoder.encode(text.text(), "utf-8");
LOG.info(Query);
GetMethod getMethod = new GetMethod(Query);
getMethod.addRequestHeader(new Header("Accept", "application/json"));
spotlightResponse = request(getMethod);
} catch (UnsupportedEncodingException e) {
throw new AnnotationException("Could not encode text.", e);
}
assert spotlightResponse != null;
JSONObject resultJSON = null;
JSONArray entities = null;
try {
resultJSON = new JSONObject(spotlightResponse);
entities = resultJSON.getJSONArray("Resources");
} catch (JSONException e) {
//throw new AnnotationException("Received invalid response from DBpedia Spotlight API.");
}
LinkedList<DBpediaResource> resources = new LinkedList<DBpediaResource>();
if(entities!=null)
for(int i = 0; i < entities.length(); i++) {
try {
JSONObject entity = entities.getJSONObject(i);
resources.add(
new DBpediaResource(entity.getString("@URI"),
Integer.parseInt(entity.getString("@support"))));
} catch (JSONException e) {
LOG.error("JSON exception "+e);
}
}
return resources;
}
}
Druga klasa
/**
* @author pablomendes
*/
public abstract class AnnotationClient {
public Logger LOG = Logger.getLogger(this.getClass());
private List<String> RES = new ArrayList<String>();
// Create an instance of HttpClient.
private static HttpClient client = new HttpClient();
public List<String> getResu(){
return RES;
}
public String request(HttpMethod method) throws AnnotationException {
String response = null;
// Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
// Execute the method.
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
LOG.error("Method failed: " + method.getStatusLine());
}
// Read the response body.
byte[] responseBody = method.getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
response = new String(responseBody);
} catch (HttpException e) {
LOG.error("Fatal protocol violation: " + e.getMessage());
throw new AnnotationException("Protocol error executing HTTP request.",e);
} catch (IOException e) {
LOG.error("Fatal transport error: " + e.getMessage());
LOG.error(method.getQueryString());
throw new AnnotationException("Transport error executing HTTP request.",e);
} finally {
// Release the connection.
method.releaseConnection();
}
return response;
}
protected static String readFileAsString(String filePath) throws java.io.IOException{
return readFileAsString(new File(filePath));
}
protected static String readFileAsString(File file) throws IOException {
byte[] buffer = new byte[(int) file.length()];
@SuppressWarnings("resource")
BufferedInputStream f = new BufferedInputStream(new FileInputStream(file));
f.read(buffer);
return new String(buffer);
}
static abstract class LineParser {
public abstract String parse(String s) throws ParseException;
static class ManualDatasetLineParser extends LineParser {
public String parse(String s) throws ParseException {
return s.trim();
}
}
static class OccTSVLineParser extends LineParser {
public String parse(String s) throws ParseException {
String result = s;
try {
result = s.trim().split("\t")[3];
} catch (ArrayIndexOutOfBoundsException e) {
throw new ParseException(e.getMessage(), 3);
}
return result;
}
}
}
public void saveExtractedEntitiesSet(String Question, LineParser parser, int restartFrom) throws Exception {
String text = Question;
int i=0;
//int correct =0 ; int error = 0;int sum = 0;
for (String snippet: text.split("\n")) {
String s = parser.parse(snippet);
if (s!= null && !s.equals("")) {
i++;
if (i<restartFrom) continue;
List<DBpediaResource> entities = new ArrayList<DBpediaResource>();
try {
entities = extract(new Text(snippet.replaceAll("\\s+"," ")));
System.out.println(entities.get(0).getFullUri());
} catch (AnnotationException e) {
// error++;
LOG.error(e);
e.printStackTrace();
}
for (DBpediaResource e: entities) {
RES.add(e.uri());
}
}
}
}
public abstract List<DBpediaResource> extract(Text text) throws AnnotationException;
public void evaluate(String Question) throws Exception {
evaluateManual(Question,0);
}
public void evaluateManual(String Question, int restartFrom) throws Exception {
saveExtractedEntitiesSet(Question,new LineParser.ManualDatasetLineParser(), restartFrom);
}
}
główny()
public static void main(String[] args) throws Exception {
String Question ="Is the Amazon river longer than the Nile River?";
db c = new db();
c.configiration(0.0, 0, "non", "CoOccurrenceBasedSelector", "Default", "yes");
System.out.println("resource : "+c.getResu());
}
Dodam tylko jedną małą poprawkę za odpowiedź. Twój kod działa, jeśli dodać wywołanie evaluate
metoda:
public static void main(String[] args) throws Exception {
String question = "Is the Amazon river longer than the Nile River?";
db c = new db();
c.configiration(0.0, 0, "non", "CoOccurrenceBasedSelector", "Default", "yes");
c.evaluate(question);
System.out.println("resource : "+c.getResu());
}
Lamine
W metodzie drugiej klasy (AnnotationClient
) request
w Adel's answer, autor Pablo Mendes nie zakończyła
TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
co jest irytujące ostrzeżenie, że należy usunąć przez zastąpienie
byte[] responseBody = method.getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
response = new String(responseBody);
z
Reader in = new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8");
StringWriter writer = new StringWriter();
org.apache.commons.io.IOUtils.copy(in, writer);
response = writer.toString();
- 1. Generowanie kodu za pomocą Scala
- 2. Spark: Jak zmapować Pythona za pomocą funkcji zdefiniowanych przez użytkownika Scala lub Java?
- 3. Freebase/DBpedia/wikidata.org - różnice
- 4. bazowa 64 kodowania z Scala lub Java
- 5. Implementacja interfejsu Java w Scala
- 6. Ciecz w języku Java/Scala
- 7. DBpedia SPARQL Zapytanie dla określonego rdfs: label
- 8. Konwersja Java na C# za pomocą narzędzia lub ręcznie?
- 9. uwierzytelnianie za pomocą ntlm (lub Kerberos) przy użyciu java UrlConnection
- 10. Adaptacyjne Mapy w Scala (lub Java) Zachowanie zamówienia reklamowego
- 11. Automatyzacja Centrum jakości HP za pomocą języka Python lub Java
- 12. zmienić domyślną klawiaturę Android za pomocą ADB lub kod Java
- 13. Jak spłaszczyć kolekcję za pomocą Spark/Scala?
- 14. Sudoku GUI za pomocą java
- 15. Jak uzyskać dane DBpedia w określonym języku?
- 16. Składanie listy krotek za pomocą Scala
- 17. wyszukiwanie w aplikacji Spotlight w aplikacji
- 18. Zmienianie przestrzeni nazw XML za pomocą Scala
- 19. Skompiluj plik Java ... za pomocą programu Java
- 20. Symulacja LAN za pomocą Java?
- 21. Java: debugowanie za pomocą SwingUtilities.invokeLater()
- 22. Nagrywanie głosu za pomocą Java
- 23. Regex za pomocą Java String.replaceAll
- 24. Czy mogę dokonać wyszukiwania zawartości mojej aplikacji iPhone'a za pomocą Spotlight?
- 25. Zdarzenia wysłane przez serwer w środowisku Play za pomocą Java
- 26. Pobieranie plików za pomocą Java
- 27. Otwórz folder w Eksploratorze za pomocą Java
- 28. java.util.NoSuchElementException za pomocą iteratora w java
- 29. Tworzenie rekurencyjnej struktury danych za pomocą kombinatorów parsera w scala
- 30. Pobierz tytuł strony za pomocą Selenium WebDriver za pomocą Java