Search in sources :

Example 11 with QuestionInterpretation

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

the class TREC13To16Parser method loadResults.

/**
	 * Loads the results for a question from a log file.
	 * 
	 * @param question the question
	 * @param type the type of question ("factoid", "list" or "other")
	 * @param logfile the log file
	 * @return array of results or <code>null</code> if the question could not
	 * 		   be found in the log file
	 */
public static Result[] loadResults(String question, String type, String logfile) {
    try {
        // get cached entries for given question type
        ArrayList<String> entries;
        if (type.equals("FACTOID"))
            entries = factoidEntries;
        else if (type.equals("LIST"))
            entries = listEntries;
        else
            entries = otherEntries;
        // get entries from log file if not cached
        if (entries == null) {
            entries = new ArrayList<String>();
            String entry = "";
            BufferedReader in = new BufferedReader(new FileReader(logfile));
            while (in.ready()) {
                String line = in.readLine();
                // handle characters that are not allowed in XML
                for (int i = 0; i < SPECIALCHARS.length; i++) line = line.replace(SPECIALCHARS[i], REPLACEMENTS[i]);
                if (line.matches("<" + type.toLowerCase() + ">"))
                    entry = "";
                entry += line + "\n";
                if (line.matches("</" + type.toLowerCase() + ">"))
                    entries.add(entry);
            }
            // cache entries
            if (type.equals("FACTOID"))
                factoidEntries = entries;
            else if (type.equals("LIST"))
                listEntries = entries;
            else
                otherEntries = entries;
        }
        // traverse entries in reverse order
        for (int i = entries.size() - 1; i >= 0; i--) {
            // create factory object
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            // create DOM parser
            DocumentBuilder parser = factory.newDocumentBuilder();
            // parse entry and build tree
            Document entryD = parser.parse(new InputSource(new StringReader(entries.get(i))));
            // Is this the question we are looking for?
            Element questionE = (Element) entryD.getElementsByTagName("question").item(0);
            String questionS = questionE.getFirstChild().getNodeValue().trim();
            if (!questionS.equals(question))
                continue;
            // get results
            ArrayList<Result> results = new ArrayList<Result>();
            NodeList resultsL = entryD.getElementsByTagName("result");
            for (int j = 0; j < resultsL.getLength(); j++) {
                Element resultE = (Element) resultsL.item(j);
                Element answerE = (Element) resultE.getElementsByTagName("answer").item(0);
                String answerS = answerE.getFirstChild().getNodeValue().trim();
                Element scoreE = (Element) resultE.getElementsByTagName("score").item(0);
                float scoreF = Float.parseFloat(scoreE.getFirstChild().getNodeValue().trim());
                Element docidE = (Element) resultE.getElementsByTagName("docid").item(0);
                String docidS = docidE.getFirstChild().getNodeValue().trim();
                Element qiE = (Element) resultE.getElementsByTagName("interpretation").item(0);
                QuestionInterpretation qi = null;
                if (qiE != null) {
                    Element propertyE = (Element) qiE.getElementsByTagName("property").item(0);
                    String propertyS = propertyE.getFirstChild().getNodeValue().trim();
                    Element targetE = (Element) qiE.getElementsByTagName("target").item(0);
                    String targetS = targetE.getFirstChild().getNodeValue().trim();
                    NodeList contextL = qiE.getElementsByTagName("context");
                    String[] contextS = new String[contextL.getLength()];
                    for (int k = 0; k < contextS.length; k++) {
                        Element contextE = (Element) contextL.item(k);
                        contextS[k] = contextE.getFirstChild().getNodeValue().trim();
                    }
                    qi = new QuestionInterpretation(targetS, contextS, propertyS);
                }
                Query query = new Query(null);
                query.setInterpretation(qi);
                Result result = new Result(answerS, query, docidS);
                result.setScore(scoreF);
                results.add(result);
            }
            return results.toArray(new Result[results.size()]);
        }
        // question not found
        return null;
    } catch (Exception e) {
        MsgPrinter.printErrorMsg("Failed to load or parse log file:");
        MsgPrinter.printErrorMsg(e.toString());
        return null;
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) QuestionInterpretation(info.ephyra.questionanalysis.QuestionInterpretation) Query(info.ephyra.querygeneration.Query) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) IOException(java.io.IOException) Result(info.ephyra.search.Result) DocumentBuilder(javax.xml.parsers.DocumentBuilder) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) FileReader(java.io.FileReader)

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