Beispiel #1
0
 private void dumpMetadata(String group, List<Example> examples) {
   LispTree tree = LispTree.proto.newList();
   tree.addChild("metadata");
   tree.addChild(LispTree.proto.newList("group", group));
   tree.addChild(LispTree.proto.newList("size", "" + examples.size()));
   tree.print(out);
   out.println();
 }
Beispiel #2
0
 /** supports chains only */
 public boolean hasOpposite(Formula formula) {
   LispTree tree = formula.toLispTree();
   if (tree.isLeaf()) {
     String fbProperty =
         FreebaseInfo.isReverseProperty(tree.value) ? tree.value.substring(1) : tree.value;
     return freebaseInfo.fbPropertyHasOpposite(fbProperty);
   } else {
     // Un-reverse everything.
     String binary1 = tree.child(2).child(0).value;
     binary1 = FreebaseInfo.isReverseProperty(binary1) ? binary1.substring(1) : binary1;
     String binary2 = tree.child(2).child(1).child(0).value;
     binary2 = FreebaseInfo.isReverseProperty(binary2) ? binary2.substring(1) : binary2;
     return freebaseInfo.fbPropertyHasOpposite(binary1)
         && freebaseInfo.fbPropertyHasOpposite(binary2);
   }
 }
Beispiel #3
0
    private void compileExpression(LispTree exp) {

      for (int i = 1; i < exp.children.size(); ++i) {

        LispTree child = exp.child(i);
        int currState = numOfStates - 1;
        LanguageExpToken langExpToken =
            new LanguageExpToken(child.child(0).value, child.child(1).value);

        if (langExpToken.repeat == RepeatType.PLUS) {
          addEdge(currState, currState + 1, langExpToken);
          addEdge(currState + 1, currState + 1, langExpToken);
        } else if (langExpToken.repeat == RepeatType.STAR) {
          addEdge(currState, currState + 1, langExpToken);
          addEdge(currState + 1, currState + 1, langExpToken);
          // deal with previous edges
          Map<LanguageExpToken, Set<Integer>> prevEdges =
              incomingEdges[currState]; // could be NULL - for efficiency I allow this
          if (prevEdges != null) {
            for (LanguageExpToken prevToken : prevEdges.keySet()) {
              for (Integer prevState : prevEdges.get(prevToken)) {
                addEdge(prevState, currState + 1, prevToken);
              }
            }
          }
          // deal with start state
          if (startStates.contains(currState)) startStates.add(currState + 1);
        } else if (langExpToken.repeat == RepeatType.Q_MARK) {
          addEdge(currState, currState + 1, langExpToken);
          // deal with previous edges
          Map<LanguageExpToken, Set<Integer>> prevEdges =
              incomingEdges[currState]; // could be NULL - for efficiency I allow this
          if (prevEdges != null) {
            for (LanguageExpToken prevToken : prevEdges.keySet()) {
              for (Integer prevState : prevEdges.get(prevToken)) {
                addEdge(prevState, currState + 1, prevToken);
              }
            }
          }
          // deal with start state
          if (startStates.contains(currState)) startStates.add(currState + 1);
        } else addEdge(currState, currState + 1, langExpToken);
        numOfStates++;
      }
    }
Beispiel #4
0
  public Formula equivalentFormula(LispTree tree) {

    if (tree.isLeaf()) {
      boolean rev = FreebaseInfo.isReverseProperty(tree.value);
      String fbProperty = rev ? tree.value.substring(1) : tree.value;
      String oppositeProperty = freebaseInfo.getOppositeFbProperty(fbProperty);
      return rev
          ? Formulas.newNameFormula(oppositeProperty)
          : Formulas.newNameFormula("!" + oppositeProperty);
    } else {
      String binary1 = tree.child(2).child(0).value;
      binary1 = FreebaseInfo.isReverseProperty(binary1) ? binary1.substring(1) : binary1;
      String binary2 = tree.child(2).child(1).child(0).value;
      binary2 = FreebaseInfo.isReverseProperty(binary2) ? binary2.substring(1) : binary2;
      String oppositeBinary1 = freebaseInfo.getOppositeFbProperty(binary1);
      String oppositeBinary2 = freebaseInfo.getOppositeFbProperty(binary2);
      boolean rev = FreebaseInfo.isReverseProperty(tree.child(2).child(0).value);
      return buildLambdaFormula(oppositeBinary1, oppositeBinary2, !rev);
    }
  }
Beispiel #5
0
 private void dumpExample(LispTree tree) {
   out.println("(example");
   for (LispTree subtree : tree.children.subList(1, tree.children.size())) {
     if (!subtree.isLeaf() && "derivations".equals(subtree.children.get(0).value)) {
       if (subtree.children.size() == 1) {
         out.println("  (derivations)");
       } else {
         out.println("  (derivations");
         for (LispTree derivation : subtree.children.subList(1, subtree.children.size())) {
           out.write("    ");
           derivation.print(Integer.MAX_VALUE, Integer.MAX_VALUE, out);
           out.write("\n");
         }
         out.println("  )");
       }
     } else {
       out.write("  ");
       subtree.print(Integer.MAX_VALUE, Integer.MAX_VALUE, out);
       out.write("\n");
     }
   }
   out.println(")");
 }
Beispiel #6
0
 /**
  * Language item is defined to contain just a single language expression token
  *
  * @return
  */
 public LanguageExpToken getLangExpToken() {
   return new LanguageExpToken(
       expressionTree.child(1).child(0).value, expressionTree.child(1).child(1).value);
 }
Beispiel #7
0
 public String toString() {
   return expressionTree.toString();
 }
Beispiel #8
0
 /** supports chains only */
 public boolean isReversed(Formula formula) {
   LispTree tree = formula.toLispTree();
   if (tree.isLeaf()) return FreebaseInfo.isReverseProperty(tree.value);
   else return FreebaseInfo.isReverseProperty(tree.child(2).child(0).value);
 }
 public LispTree toLispTree() {
   LispTree tree = LispTree.proto.newList();
   tree.addChild("string");
   tree.addChild(value);
   return tree;
 }
 public StringValue(LispTree tree) {
   this.value = tree.child(1).value;
 }
Beispiel #11
0
  private LispTree exampleToLispTree(ParserState state) {
    LispTree tree = LispTree.proto.newList();
    tree.addChild("example");

    // Basic information
    Example ex = state.ex;
    if (ex.id != null) tree.addChild(LispTree.proto.newList("id", ex.id));
    if (ex.utterance != null) tree.addChild(LispTree.proto.newList("utterance", ex.utterance));
    if (ex.targetFormula != null)
      tree.addChild(LispTree.proto.newList("targetFormula", ex.targetFormula.toLispTree()));
    if (ex.targetValue != null)
      tree.addChild(LispTree.proto.newList("targetValue", ex.targetValue.toLispTree()));
    if (ex.context != null) tree.addChild(ex.context.toLispTree());

    // Language info
    if (ex.languageInfo != null) {
      if (ex.languageInfo.tokens != null)
        tree.addChild(
            LispTree.proto.newList("tokens", LispTree.proto.newList(ex.languageInfo.tokens)));
      if (ex.languageInfo.lemmaTokens != null)
        tree.addChild(
            LispTree.proto.newList(
                "lemmaTokens", LispTree.proto.newList(ex.languageInfo.lemmaTokens)));
      if (ex.languageInfo.posTags != null)
        tree.addChild(
            LispTree.proto.newList("posTags", LispTree.proto.newList(ex.languageInfo.posTags)));
      if (ex.languageInfo.nerTags != null)
        tree.addChild(
            LispTree.proto.newList("nerTags", LispTree.proto.newList(ex.languageInfo.nerTags)));
      if (ex.languageInfo.nerValues != null)
        tree.addChild(
            LispTree.proto.newList("nerValues", LispTree.proto.newList(ex.languageInfo.nerValues)));
    }

    // Derivations
    LispTree derivations = LispTree.proto.newList();
    derivations.addChild("derivations");
    List<Derivation> preds = state.predDerivations;
    for (int i = 0; i < preds.size(); i++) {
      Derivation deriv = preds.get(i);
      if (!isPruned(deriv)) {
        derivations.addChild(deriv.toLispTree());
      }
    }
    tree.addChild(derivations);

    return tree;
  }