Exemplo n.º 1
0
 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;
 }
Exemplo n.º 2
0
  /**
   * 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;
  }