Search in sources :

Example 6 with Result

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];
}
Also used : Pattern(java.util.regex.Pattern) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) BufferedReader(java.io.BufferedReader) URL(java.net.URL) IOException(java.io.IOException) Result(info.ephyra.search.Result)

Example 7 with Result

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;
}
Also used : AnalyzedQuestion(info.ephyra.questionanalysis.AnalyzedQuestion) Result(info.ephyra.search.Result)

Example 8 with Result

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()]);
}
Also used : ArrayList(java.util.ArrayList) Result(info.ephyra.search.Result)

Example 9 with Result

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()]);
}
Also used : ArrayList(java.util.ArrayList) Result(info.ephyra.search.Result)

Example 10 with Result

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();
}
Also used : PrintWriter(java.io.PrintWriter) Result(info.ephyra.search.Result)

Aggregations

Result (info.ephyra.search.Result)68 ArrayList (java.util.ArrayList)36 Query (info.ephyra.querygeneration.Query)11 HashSet (java.util.HashSet)9 Hashtable (java.util.Hashtable)9 AnalyzedQuestion (info.ephyra.questionanalysis.AnalyzedQuestion)8 IOException (java.io.IOException)7 QuestionInterpretation (info.ephyra.questionanalysis.QuestionInterpretation)5 Feature (edu.cmu.minorthird.classify.Feature)4 HashMap (java.util.HashMap)4 Predicate (info.ephyra.nlp.semantics.Predicate)3 BagOfWordsG (info.ephyra.querygeneration.generators.BagOfWordsG)3 BufferedReader (java.io.BufferedReader)3 File (java.io.File)3 URL (java.net.URL)3 TRECTarget (info.ephyra.trec.TRECTarget)2 EOFException (java.io.EOFException)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 InputStreamReader (java.io.InputStreamReader)2