Skip to content

Commit

Permalink
qa.system updates
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasBluebaum committed Aug 26, 2019
1 parent f69c64d commit 14c30ae
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 23 deletions.
2 changes: 1 addition & 1 deletion qa.systems/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.9.1</version>
<version>1.12.1</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public String createInputJSON(String question) {
public void processResponse(String response, IQuestion question) {
JSONParser parser = new JSONParser();
JSONObject answerjson = null;

try {
answerjson = (JSONObject) parser.parse(response);
} catch (ParseException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,12 @@ public void search(IQuestion question, String language) throws Exception {
throw new Exception(this.name+" Server could not answer due to: " + response.getStatusLine());
}
//Checking if expected format is EQALD or QALD
if(this.isEQALD)
if(this.isEQALD) {
processEQALDResponse(response, question);
else
}
else {
processQALDResp(response, question);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class Gen_HTTP_QA_Sys_JSON extends ASystem {
private Logger log = LoggerFactory.getLogger(Gen_HTTP_QA_Sys_JSON.class);

// String constants
private String url;
private String name;
Expand All @@ -22,7 +26,7 @@ public Gen_HTTP_QA_Sys_JSON(String url, String name) {
public String fetchPostResponse(String url, String json) throws Exception {
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(this.timeout).build();
HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
HttpPost httppost = new HttpPost(this.url);
HttpPost httppost = new HttpPost(url);
StringEntity entity = new StringEntity(json);
httppost.addHeader("Content-Type", "application/json; charset=UTF-8");
httppost.setEntity(entity);
Expand All @@ -42,6 +46,7 @@ public void search(IQuestion question, String language) throws Exception {
return;
}
questionString = question.getLanguageToQuestion().get(language);
log.debug(this.getClass().getSimpleName() + ": " + questionString);

String responseString = fetchPostResponse(this.url, createInputJSON(questionString));

Expand Down
31 changes: 31 additions & 0 deletions qa.systems/src/main/java/org/aksw/qa/systems/QUINT.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package org.aksw.qa.systems;

import java.io.IOException;
import java.util.HashSet;
import java.util.Map;

import org.aksw.qa.commons.datastructure.IQuestion;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;


// not complete yet, since the website is not working correctly at the moment
Expand All @@ -24,10 +33,12 @@ public class QUINT extends Gen_HTTP_QA_Sys {

public QUINT() {
super(URL, "quint", true, false);
this.setQueryKey("question");
}

public QUINT(String url) {
super(url, "quint", true, false);
this.setQueryKey("question");
}

@Override
Expand All @@ -38,4 +49,24 @@ public HttpResponse fetchPostResponse(String url, Map<String, String> paramMap)
paramMap.put("numberDecisionTrees", NUMBER_DECISION_TREES);
return super.fetchPostResponse(url, paramMap);
}

@Override
public void processQALDResp(HttpResponse response, IQuestion question) throws JsonParseException, JsonMappingException, UnsupportedOperationException, IOException {
HashSet<String> resultSet = new HashSet<String>();
Document doc = Jsoup.parse(responseparser.responseToString(response));
Element container = doc.getElementById("result-container");

if(container == null) return;
Elements results = container.select("a");
for(Element result: results) {
//only returns wikipedia links
resultSet.add(result.attr("href"));
}
question.setGoldenAnswers(resultSet);
}

public static void main(String[] args) throws Exception {
ASystem a = new SorokinQA();
System.out.println(a.search("How many children did Benjamin Franklin have?", "en"));
}
}
88 changes: 69 additions & 19 deletions qa.systems/src/main/java/org/aksw/qa/systems/SorokinQA.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,100 @@
package org.aksw.qa.systems;

import java.io.IOException;
import java.util.HashSet;

import org.aksw.qa.commons.datastructure.IQuestion;
import org.aksw.qa.util.ResponseToStringParser;
import org.apache.http.HttpResponse;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
public class SorokinQA extends Gen_HTTP_QA_Sys_JSON {

public class SorokinQA extends Gen_HTTP_QA_Sys{

private static final String URL = "http://semanticparsing.ukp.informatik.tu-darmstadt.de:5000/question-answering/answerforqald/";
//private static final String URL = "http://semanticparsing.ukp.informatik.tu-darmstadt.de:5000/question-answering/answerforqald/";
private static final String URL_UG = "http://semanticparsing.ukp.informatik.tu-darmstadt.de:5000/question-answering/ungroundedgraph/";
private static final String URL_GG = "http://semanticparsing.ukp.informatik.tu-darmstadt.de:5000/question-answering/groundedgraphs/";
private static final String URL_EG = "http://semanticparsing.ukp.informatik.tu-darmstadt.de:5000/question-answering/evaluategraphs/";

public SorokinQA() {
super(URL, "sorokinqa", true, false);
this.setQueryKey("question");
super(URL_UG, "sorokinqa");
}

public SorokinQA(String url) {
super(url, "sorokinqa", true, false);
this.setQueryKey("question");
/**
* Overriding original search method to implement SorokinQA's three step requests for
* QA
*/
@SuppressWarnings("unchecked")
@Override
public void search(IQuestion question, String language) throws Exception {
String questionString;
if (!question.getLanguageToQuestion().containsKey(language)) {
return;
}
questionString = question.getLanguageToQuestion().get(language);

String responseString = fetchPostResponse(URL_UG, createInputJSON(questionString));

JSONParser parser = new JSONParser();
JSONObject answerjson = null;
answerjson = (JSONObject) parser.parse(responseString);
JSONArray entities = (JSONArray) answerjson.get("entities");

// next step only takes the first linking from the first step for each entity
if(entities.size() > 0) {
for(int i=0; i<entities.size(); i++) {
JSONObject element = (JSONObject) entities.get(i);
JSONArray linkings = (JSONArray) element.get("linkings");
JSONArray first = (JSONArray) linkings.get(0);
linkings.clear();
linkings.add(first);
}
}
responseString = fetchPostResponse(URL_GG, answerjson.toString());

answerjson = (JSONObject) parser.parse(responseString);

JSONArray graphs = (JSONArray) answerjson.get("graphs");
JSONArray inputEvaluate = new JSONArray();
//next step only takes the first element from this array
if(graphs.size() > 0) {
JSONArray first = (JSONArray) graphs.get(0);
inputEvaluate.add(first);
}
responseString = fetchPostResponse(URL_EG, inputEvaluate.toString());

processResponse(responseString, question);
}


@SuppressWarnings("unchecked")
@Override
public void processQALDResp(HttpResponse response, IQuestion question) throws JsonParseException, JsonMappingException, UnsupportedOperationException, IOException {
ResponseToStringParser responseparser = new ResponseToStringParser();
public String createInputJSON(String question) {
JSONObject json = new JSONObject();
json.put("question", question);
json.put("simplified_npparser", 1);
return json.toString();
}

@Override
public void processResponse(String response, IQuestion question) {
JSONParser parser = new JSONParser();
String responseString = responseparser.responseToString(response);
JSONArray answerjson = null;
try {
answerjson = (JSONArray) parser.parse(responseString);
answerjson = (JSONArray) parser.parse(response);
} catch (ParseException e) {
e.printStackTrace();
return;
}

HashSet<String> resultSet = new HashSet<String>();
answerjson = (JSONArray) answerjson.get(0);
if(answerjson.size() == 0) {
System.out.println("eee");
return;
}
for(int i = 0; i<answerjson.size(); i++) {
resultSet.add("https://www.wikidata.org/wiki/" + (String) answerjson.get(i));
}
//only returns wikidata ids, nothing else
question.setGoldenAnswers(resultSet);
question.setGoldenAnswers(resultSet);
}
}

0 comments on commit 14c30ae

Please sign in to comment.