Search in sources :

Example 1 with QuestionInterpretation

use of info.ephyra.questionanalysis.QuestionInterpretation in project lucida by claritylab.

the class AnswerPatternFilter method apply.

/**
	 * Applies the answer patterns to the answer strings of the
	 * <code>Result</code> objects and creates a new <code>Result</code> for
	 * each extracted unique answer.
	 * 
	 * @param results array of <code>Result</code> objects
	 * @return extended array of <code>Result</code> objects
	 */
public Result[] apply(Result[] results) {
    // extracted factoid answers and corresponding results
    Hashtable<String, Result> factoids = new Hashtable<String, Result>();
    for (Result result : results) {
        // only apply this filter to results for the pattern matching
        // approach
        Query query = result.getQuery();
        QuestionInterpretation qi = query.getInterpretation();
        if (!query.extractWith(ID) || qi == null || result.getScore() > Float.NEGATIVE_INFINITY)
            continue;
        // extract PROPERTY objects
        extractPos(result);
        // create new result for each unique normalized PROPERTY object
        for (int i = 0; i < extr.size(); i++) {
            String po = extr.get(i);
            String[] neTypes = types.get(i);
            String norm = StringUtils.normalize(po);
            String sentence = sents.get(i);
            float conf = aps.get(i).getConfidence();
            Result factoid = factoids.get(norm);
            if (factoid == null) {
                // new answer
                // query, doc ID and sentence can be ambiguous
                factoid = new Result(po, result.getQuery(), result.getDocID());
                factoid.setSentence(sentence);
                factoid.addExtractionTechnique(ID);
                factoids.put(norm, factoid);
            }
            if (neTypes != null)
                for (String neType : neTypes) factoid.addNeType(neType);
            factoid.incScore(conf);
        }
    }
    // keep old results
    Result[] newResults = factoids.values().toArray(new Result[factoids.size()]);
    Result[] allResults = new Result[results.length + newResults.length];
    for (int i = 0; i < results.length; i++) allResults[i] = results[i];
    for (int i = 0; i < newResults.length; i++) allResults[results.length + i] = newResults[i];
    return allResults;
}
Also used : QuestionInterpretation(info.ephyra.questionanalysis.QuestionInterpretation) Query(info.ephyra.querygeneration.Query) Hashtable(java.util.Hashtable) Result(info.ephyra.search.Result)

Example 2 with QuestionInterpretation

use of info.ephyra.questionanalysis.QuestionInterpretation in project lucida by claritylab.

the class AnswerPatternFilter method extractPos.

/**
	 * Applies the answer patterns to the answer string of the
	 * <code>Result</code> object to extract PROPERTY objects.
	 * 
	 * @param result a <code>Result</code> object
	 */
private static void extractPos(Result result) {
    extr = new ArrayList<String>();
    types = new ArrayList<String[]>();
    sents = new ArrayList<String>();
    aps = new ArrayList<AnswerPattern>();
    // get interpretation and answer string
    QuestionInterpretation qi = result.getQuery().getInterpretation();
    String to = qi.getTarget();
    //		String[] cos = qi.getContext();
    // CONTEXT objects are ignored
    String[] cos = new String[0];
    String prop = qi.getProperty();
    String answer = result.getAnswer();
    // get answer patterns
    HashSet<AnswerPattern> patterns = props.get(prop);
    if (patterns == null)
        return;
    // tokenize interpretation
    to = NETagger.tokenizeWithSpaces(to);
    for (int i = 0; i < cos.length; i++) cos[i] = NETagger.tokenizeWithSpaces(cos[i]);
    // split answer string into sentences and tokenize sentences
    String[] originalSentences = OpenNLP.sentDetect(answer);
    String[][] tokens = new String[originalSentences.length][];
    String[] sentences = new String[originalSentences.length];
    for (int i = 0; i < originalSentences.length; i++) {
        tokens[i] = NETagger.tokenize(originalSentences[i]);
        sentences[i] = StringUtils.concatWithSpaces(tokens[i]);
    }
    /*   PrintWriter pw = null;   
                try {
                    pw = new PrintWriter(new FileOutputStream(new File("netagger_data.txt"),true));
                } catch (FileNotFoundException ex) {
                    System.out.println("File not found exception!!");
                }*/
    // extract named entities
    String[][][] nes = NETagger.extractNes(tokens);
    for (int i = 0; i < sentences.length; i++) {
        // prepare sentence for answer extraction
        sentences[i] = prepSentence(sentences[i], to, cos, nes[i]);
        if (sentences[i] == null)
            continue;
        for (AnswerPattern pattern : patterns) {
            // apply answer pattern
            String[] pos = pattern.apply(sentences[i]);
            //  pw2.printf("%s ----- %s ----- %s\n", pattern.getDesc(), sentences[i], pos);
            // get NE types of PROPERTY objects
            String[][] neTypes = new String[pos.length][];
            for (int j = 0; j < pos.length; j++) neTypes[j] = getNeTypes(pos[j], pattern);
            // replace tags and untokenize PROPERTY objects
            for (int j = 0; j < pos.length; j++) {
                pos[j] = replaceTags(pos[j]);
                pos[j] = OpenNLP.untokenize(pos[j], originalSentences[i]);
            }
            // from, the patterns used to extract them and the NE types
            for (int j = 0; j < pos.length; j++) {
                extr.add(pos[j]);
                types.add(neTypes[j]);
                sents.add(originalSentences[i]);
                aps.add(pattern);
            }
        }
    }
//   pw.close();
//     pw2.close();
}
Also used : AnswerPattern(info.ephyra.answerselection.AnswerPattern) QuestionInterpretation(info.ephyra.questionanalysis.QuestionInterpretation)

Example 3 with QuestionInterpretation

use of info.ephyra.questionanalysis.QuestionInterpretation in project lucida by claritylab.

the class QuestionInterpretationG method generateQueries.

/**
	 * Creates queries from question interpretations. The query string is the
	 * concatenation of the target object, any context objects and the keywords
	 * in the question.
	 * 
	 * @param aq analyzed question
	 * @return <code>Query</code> objects
	 */
public Query[] generateQueries(AnalyzedQuestion aq) {
    ArrayList<Query> queries = new ArrayList<Query>();
    QuestionInterpretation[] qis = aq.getInterpretations();
    String[] kws = aq.getKeywords();
    for (QuestionInterpretation qi : qis) {
        // create query string
        String queryString = queryString(qi.getTarget(), qi.getContext(), kws);
        // create query, set answer types and question interpretation
        Query query = new Query(queryString, aq, SCORE);
        query.setExtractionTechniques(EXTRACTION_TECHNIQUES);
        query.setInterpretation(qi);
        queries.add(query);
    }
    return queries.toArray(new Query[queries.size()]);
}
Also used : QuestionInterpretation(info.ephyra.questionanalysis.QuestionInterpretation) Query(info.ephyra.querygeneration.Query) ArrayList(java.util.ArrayList)

Example 4 with QuestionInterpretation

use of info.ephyra.questionanalysis.QuestionInterpretation in project lucida by claritylab.

the class PatternLearner method interpretQuestions.

/**
	 * Interprets the questions and writes target-context-answer-regex
	 * tuples to resource files.
	 * 
	 * @param dir target directory
	 * @return <code>true</code>, iff the interpretations could be written to
	 * 		   resource files
	 */
private static boolean interpretQuestions(String dir) {
    boolean success = true;
    for (int i = 0; i < qss.length; i++) {
        // print original question string
        MsgPrinter.printQuestionString(qss[i]);
        // normalize question
        String qn = QuestionNormalizer.normalize(qss[i]);
        // stem verbs and nouns
        String stemmed = QuestionNormalizer.stemVerbsAndNouns(qn);
        // print normalized and stemmed question string
        MsgPrinter.printNormalization(stemmed);
        // interpret question
        QuestionInterpretation[] qis = QuestionInterpreter.interpret(qn, stemmed);
        MsgPrinter.printInterpretations(qis);
        for (QuestionInterpretation qi : qis) if (!saveInterpretation(dir, qi, ass.get(qss[i]), regexs.get(qss[i])))
            success = false;
    }
    return success;
}
Also used : QuestionInterpretation(info.ephyra.questionanalysis.QuestionInterpretation)

Example 5 with QuestionInterpretation

use of info.ephyra.questionanalysis.QuestionInterpretation in project lucida by claritylab.

the class Logger method logResults.

/**
	 * Logs the results returned by the QA engine.
	 * 
	 * @param results the results
	 * @return true, iff logging was successful
	 */
public static boolean logResults(Result[] results) {
    // logging is disabled or log file is not specified
    if (!enabled || logfile == null)
        return false;
    try {
        PrintWriter out = new PrintWriter(new FileOutputStream(logfile, true));
        for (Result result : results) {
            out.println("\t<result>");
            out.println("\t\t<answer>");
            out.println("\t\t\t" + result.getAnswer());
            out.println("\t\t</answer>");
            out.println("\t\t<score>");
            out.println("\t\t\t" + result.getScore());
            out.println("\t\t</score>");
            if (result.getDocID() != null) {
                out.println("\t\t<docid>");
                out.println("\t\t\t" + result.getDocID());
                out.println("\t\t</docid>");
            }
            QuestionInterpretation qi = result.getQuery().getInterpretation();
            if (qi != null) {
                out.println("\t\t<interpretation>");
                out.println("\t\t\t<property>");
                out.println("\t\t\t\t" + qi.getProperty());
                out.println("\t\t\t</property>");
                out.println("\t\t\t<target>");
                out.println("\t\t\t\t" + qi.getTarget());
                out.println("\t\t\t</target>");
                for (String context : qi.getContext()) {
                    out.println("\t\t\t<context>");
                    out.println("\t\t\t\t" + context);
                    out.println("\t\t\t</context>");
                }
                out.println("\t\t</interpretation>");
            }
            out.println("\t</result>");
        }
        out.close();
    } catch (IOException e) {
        return false;
    }
    return true;
}
Also used : QuestionInterpretation(info.ephyra.questionanalysis.QuestionInterpretation) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) Result(info.ephyra.search.Result)

Aggregations

QuestionInterpretation (info.ephyra.questionanalysis.QuestionInterpretation)11 Result (info.ephyra.search.Result)5 Query (info.ephyra.querygeneration.Query)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 BufferedReader (java.io.BufferedReader)2 FileOutputStream (java.io.FileOutputStream)2 FileReader (java.io.FileReader)2 PrintWriter (java.io.PrintWriter)2 AnswerPattern (info.ephyra.answerselection.AnswerPattern)1 Dossier (info.ephyra.answerselection.definitional.Dossier)1 QuestionInterpretationG (info.ephyra.querygeneration.generators.QuestionInterpretationG)1 File (java.io.File)1 StringReader (java.io.StringReader)1 Hashtable (java.util.Hashtable)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1