Пример #1
0
 public boolean hasExp(Exp sem) {
   for (ParseResult p : allParses) {
     if (p.getExp().equals(sem)) {
       return true;
     }
   }
   return false;
 }
Пример #2
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;
 }
Пример #3
0
 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;
 }
Пример #4
0
  public boolean isCorrect(String words, Exp sem, Parser parser) {
    List<ParseResult> parses = parser.bestParses();
    if (parses.size() > 0) {
      noAnswer = false;
    } else {
      noAnswer = true;
    }
    if (parses.size() == 1) {
      ParseResult p = parses.get(0);
      Exp e = p.getExp();
      e = e.copy();
      e.simplify();
      List l = p.getLexEntries();
      parsed++;
      if (e.equals(sem)) {
        if (verbose) {
          System.out.println("CORRECT");
          printLex(l);
        }
        int lits = sem.allLitsCount();
        correctParses++;

        return true;
      } else {
        // one parse, it was wrong... oh well...
        if (verbose) {
          System.out.println("WRONG");
          System.out.println(parses.size() + " parses: " + parses);
          printLex(l);
        }
        wrongParses++;

        boolean hasCorrect = parser.hasParseFor(sem);
        if (verbose) {
          System.out.println("Had correct parse: " + hasCorrect);
          System.out.print("Feats: ");
          Exp eb = parser.bestSem();
          Chart c = parser.getChart();
          HashVector h = c.computeExpFeatVals(eb);
          h.divideBy(c.computeNorm(eb));
          h.dropSmallEntries();
          System.out.println(h);
        }
      }
    } else {
      noParses++;
      if (parses.size() > 1) {
        // There are more than one equally high scoring
        // logical forms. If this is the case, we abstain
        // from returning a result.
        if (verbose) {
          System.out.println("too many parses");
          System.out.println(parses.size() + " parses: " + parses);
        }
        Exp e = parses.get(0).getExp();
        ParseResult p = parses.get(0);
        boolean hasCorrect = parser.hasParseFor(sem);
        if (verbose) System.out.println("Had correct parse: " + hasCorrect);
      } else {
        // no parses, potentially reparse with word skipping
        if (verbose) System.out.println("no parses");
        if (emptyTest) {
          List<LexEntry> emps = new LinkedList<LexEntry>();
          for (int j = 0; j < Globals.tokens.size(); j++) {
            List l = Globals.tokens.subList(j, j + 1);
            LexEntry le = new LexEntry(l, Cat.EMP);
            emps.add(le);
          }

          parser.setTempLexicon(new Lexicon(emps));
          String mes = null;
          if (verbose) mes = "EMPTY";
          parser.parseTimed(words, null, mes);
          parser.setTempLexicon(null);
          parses = parser.bestParses();
          if (parses.size() == 1) {
            ParseResult p = parses.get(0);
            List l = p.getLexEntries();
            Exp e = p.getExp();
            e = e.copy();
            e.simplify();
            int noEmpty = p.noEmpty();
            if (e.equals(sem)) {
              if (verbose) {
                System.out.println("CORRECT");
                printLex(l);
              }
              emptyCorrect++;

            } else {
              // one parse, but wrong
              if (verbose) {
                System.out.println("WRONG: " + e);
                printLex(l);
                boolean hasCorrect = parser.hasParseFor(sem);
                System.out.println("Had correct parse: " + hasCorrect);
              }
            }
          } else {
            // too many parses or no parses
            emptyNoParses++;
            if (verbose) {
              System.out.println("WRONG:" + parses);
              boolean hasCorrect = parser.hasParseFor(sem);
              System.out.println("Had correct parse: " + hasCorrect);
            }
          }
        }
      }
    }
    return false;
  }