use of info.ephyra.search.Result in project lucida by claritylab.
the class WorldFactbookKA method doSearch.
/**
* Searches the World Factbook for country details and returns an array
* containing a single <code>Result</code> object or an empty array, if the
* search failed.
*
* @return array containing a single <code>Result</code> or an empty array
*/
protected Result[] doSearch() {
try {
// get country name and demanded information
String[] content = getContent().split("#");
String info = content[0];
String country = content[1];
// get URL of country web page
String countryPage = countries.get(country.toLowerCase());
if (countryPage == null)
return new Result[0];
URL page = new URL(URL + countryPage);
// retrieve document
BufferedReader in;
String html = "";
in = new BufferedReader(new InputStreamReader(page.openStream(), Charset.forName("iso-8859-1")));
while (in.ready()) {
html += in.readLine() + " ";
}
in.close();
// extract information
Pattern p = Pattern.compile("(?i).*" + info + ":</div>\\s*</td>" + "\\s*<td .*?>(.*?)</td>.*");
Matcher m = p.matcher(html);
if (m.matches()) {
// extract sentence
String sentence = SentenceExtractor.getSentencesFromHtml(m.group(1))[0];
// create result from that sentence
return getResult(sentence, page.toString());
}
} catch (Exception e) {
// print search error message
MsgPrinter.printSearchError(e);
}
// search failed
return new Result[0];
}
use of info.ephyra.search.Result in project lucida by claritylab.
the class OpenEphyra method askFactoid.
/**
* Asks Ephyra a factoid question and returns up to <code>maxAnswers</code>
* results that have a score of at least <code>absThresh</code>.
*
* @param question factoid question
* @param maxAnswers maximum number of answers
* @param absThresh absolute threshold for scores
* @return array of results
*/
public Result[] askFactoid(String question, int maxAnswers, float absThresh) {
// initialize pipeline
initFactoid();
// analyze question
MsgPrinter.printAnalyzingQuestion();
AnalyzedQuestion aq = QuestionAnalysis.analyze(question);
// get answers
Result[] results = runPipeline(aq, maxAnswers, absThresh);
return results;
}
use of info.ephyra.search.Result in project lucida by claritylab.
the class OpenEphyra method askList.
/**
* Asks Ephyra a list question and returns results that have a score of at
* least <code>relThresh * top score</code>.
*
* @param question list question
* @param relThresh relative threshold for scores
* @return array of results
*/
public Result[] askList(String question, float relThresh) {
question = QuestionNormalizer.transformList(question);
Result[] results = askFactoid(question, Integer.MAX_VALUE, 0);
// get results with a score of at least relThresh * top score
ArrayList<Result> confident = new ArrayList<Result>();
if (results.length > 0) {
float topScore = results[0].getScore();
for (Result result : results) if (result.getScore() >= relThresh * topScore)
confident.add(result);
}
return confident.toArray(new Result[confident.size()]);
}
use of info.ephyra.search.Result in project lucida by claritylab.
the class OpenEphyraServer method askList.
/**
* Asks Ephyra a list question and returns results that have a score of at
* least <code>relThresh * top score</code>.
*
* @param question list question
* @param relThresh relative threshold for scores
* @return array of results
*/
public Result[] askList(String question, float relThresh) {
question = QuestionNormalizer.transformList(question);
Result[] results = askFactoid(question, Integer.MAX_VALUE, 0);
// get results with a score of at least relThresh * top score
ArrayList<Result> confident = new ArrayList<Result>();
if (results.length > 0) {
float topScore = results[0].getScore();
for (Result result : results) if (result.getScore() >= relThresh * topScore)
confident.add(result);
}
return confident.toArray(new Result[confident.size()]);
}
use of info.ephyra.search.Result in project lucida by claritylab.
the class OpenEphyraServer method handle.
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String query_str = request.getQueryString();
System.out.println("Query str: " + query_str);
if (query_str == null) {
response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
baseRequest.setHandled(true);
return;
}
String[] tokens = query_str.split("=");
String question = URLDecoder.decode(tokens[1], "UTF-8");
// response
response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
baseRequest.setHandled(true);
PrintWriter out = response.getWriter();
out.flush();
// determine question type and extract question string
String type;
if (question.matches("(?i)" + FACTOID + ":.*+")) {
// factoid question
type = FACTOID;
question = question.split(":", 2)[1].trim();
} else if (question.matches("(?i)" + LIST + ":.*+")) {
// list question
type = LIST;
question = question.split(":", 2)[1].trim();
} else {
// question type unspecified
// default type
type = FACTOID;
}
// ask question
Result[] results = new Result[0];
if (type.equals(FACTOID)) {
Logger.logFactoidStart(question);
results = askFactoid(question, FACTOID_MAX_ANSWERS, FACTOID_ABS_THRESH);
Logger.logResults(results);
Logger.logFactoidEnd();
} else if (type.equals(LIST)) {
Logger.logListStart(question);
results = askList(question, LIST_REL_THRESH);
Logger.logResults(results);
Logger.logListEnd();
}
String answer = results[0].getAnswer();
if (answer != null)
out.println(answer);
else
out.println("Sorry, I cannot answer your question.");
out.close();
}
Aggregations