/** Finds the lexical items used to produce the highest scoring parse. */ public List getMaxLexEntries() { List result = new LinkedList(); for (ParseResult p : bestParses) { result.addAll(p.getLexEntries()); } return result; }
/** Finds the lexical items used to produce the highest scoring parse with semantics sem. */ public List getMaxLexEntriesFor(Exp sem) { List result = new LinkedList(); for (ParseResult p : findBestParses(allParses, sem)) { result.addAll(p.getLexEntries()); } return result; }
public static List tokenize(String input) { // first tokenize the string List tokens = new LinkedList(); StringTokenizer st = new StringTokenizer(input); while (st.hasMoreTokens()) { tokens.add(st.nextToken()); } return tokens; }
/** Returns the features for the highest-score current parse with semantics that equal sem. */ public HashVector getFeats(Exp sem) { HashVector result = new HashVector(); List<ParseResult> pr = findBestParses(allParses, sem); for (ParseResult p : pr) { p.getFeats(result); } if (pr.size() > 1) result.divideBy(pr.size()); return result; }
private List<ParseResult> findBestParses(List<ParseResult> all, Exp e) { List<ParseResult> best = new LinkedList<ParseResult>(); double bestScore = -Double.MAX_VALUE; for (ParseResult p : all) { if (p.getExp().inferType() != null) { if ((e == null || p.getExp().equals(e))) { if (p.getScore() == bestScore) best.add(p); if (p.getScore() > bestScore) { bestScore = p.getScore(); best.clear(); best.add(p); } } } } return best; }
private List<ParseResult> removeRepeats(List<ParseResult> all) { System.out.println("----------------------- all.size equals to --------------" + all.size()); List<ParseResult> bestList = new LinkedList<ParseResult>(); for (int i = 0; i < all.size(); i++) { ParseResult e_i = all.get(i); boolean best = true; for (int j = i + 1; j < all.size(); j++) { ParseResult e_j = all.get(j); if (e_i.getExp().equals(e_j.getExp()) && e_i.getScore() <= e_j.getScore()) { best = false; break; } } if (best) bestList.add(e_i); } return bestList; }
/** * Return a parsed chart of the input, pruning each cell to pruneN entries. * * <p>Uses the CKY algorithm. */ public Chart parse(String input, Exp pruningSem, boolean computeInside) { List tokens = tokenize(input); Globals.tokens = tokens; Globals.lastWordIndex = tokens.size() - 1; // create a chart and add the input words Chart chart = new Chart(tokens); if (pruningSem != null) { chart.setPruningSem(pruningSem); } // initialize cells list with the lexical rule lexicon.addCells(chart); if (tempLex != null) { tempLex.addCells(chart); } int size = tokens.size(); List temp = new LinkedList(); // now do the CKY parsing: for (int len = 1; len < size; len++) { for (int begin = 0; begin < size - len; begin++) { for (int split = 0; split < len; split++) { temp.clear(); Iterator leftIter = chart.getCellsIterator(begin, begin + split); while (leftIter.hasNext()) { Cell left = (Cell) leftIter.next(); Iterator rightIter = chart.getCellsIterator(begin + split + 1, begin + len); while (rightIter.hasNext()) { Cell right = (Cell) rightIter.next(); Iterator rules = binaryRules.iterator(); while (rules.hasNext()) { ((BinaryParseRule) rules.next()).newCellsFrom(left, right, temp); } } } chart.addAll(temp); } } } // System.out.println("fffffffffffffffff"); // for(ParseResult pr : chart.getParseResults()){ // System.out.println(pr + "#" + pr.getScore()); // } // System.out.println("fffffffffffffffff"); chart.pruneParseResults(pruneN); // store the parse results allParses = chart.getParseResults(); allParses = removeRepeats(allParses); // find best parses for test and for generating // new lexical items. bestParses = findBestParses(allParses); // System.out.println("&&&&&&&&&&&&&&&&&&&&&"); // System.out.println(input + " " + pruningSem); // for (int len = size-1; len>=0; len--){ // for (int begin = 0; begin<size-len; begin++){ // Iterator i = chart.getCellsIterator(begin,begin+len); // while (i.hasNext()){ // Cell c = (Cell)i.next(); // System.out.println("len = " + len + " begin = " + begin + "\n" + c.lex); // } // } // } // System.out.println("&&&&&&&&&&&&&&&&&&&&&"); return chart; }
public ParseResult bestParse(Exp sem) { List<ParseResult> result = findBestParses(allParses, sem); if (result.size() > 0) return result.get(0); return null; }
/** * Expands the lexicon and updates the weight vector and feature indices. Does not add entries * that are already present. */ public void addLexEntries(List lex) { for (int i = 0; i < lex.size(); i++) { addLexEntry((LexEntry) lex.get(i)); } }