public void store(String sentence) {
    String splitSent[] = parseString(sentence);
    Sentence sent = new Sentence();
    if (splitSent.length == 1) sent.setlhsrhs("", splitSent[0]);
    else sent.setlhsrhs(splitSent[0], splitSent[1]);

    String key = sent.rhs.fnName;
    if (KB.containsKey(key)) {
      Rule temp = KB.get(key);
      temp.sentences.add(sent);
      KB.put(key, temp);
    } else {
      Rule r = new Rule();
      r.sentences.add(sent);
      KB.put(key, r);
    }
  }
  public void FOL_BC_ASK() {
    for (Predicate predicate : queries) {

      try {
        for (Map.Entry<String, Rule> entry : KBObj.KB.entrySet()) {
          for (Sentence sent : entry.getValue().sentences) {
            sent.allSubst = null;
          }
        }
        AllSubstitution obj = FOL_BC_OR(predicate, null, 0);
        if (obj == null) System.out.println("FALSE");
        else System.out.println(obj.status ? "TRUE" : "FALSE");

        // substList = new LinkedHashMap<String, LinkedList<AllSubstitution>>();
      } catch (Exception e) {
        System.out.println("FALSE");
      }
    }
  }
예제 #3
0
 /**
  * Turns a sentence into a flat phrasal tree. The structure is S -> tag*. And then each tag goes
  * to a word. The tag is either found from the label or made "WD". The tag and phrasal node have a
  * StringLabel.
  *
  * @param s The Sentence to make the Tree from
  * @param lf The LabelFactory with which to create the new Tree labels
  * @return The one phrasal level Tree
  */
 public static Tree toFlatTree(Sentence<?> s, LabelFactory lf) {
   List<Tree> daughters = new ArrayList<Tree>(s.length());
   for (HasWord word : s) {
     Tree wordNode = new LabeledScoredTreeLeaf(lf.newLabel(word.word()));
     if (word instanceof TaggedWord) {
       TaggedWord taggedWord = (TaggedWord) word;
       wordNode =
           new LabeledScoredTreeNode(
               new StringLabel(taggedWord.tag()), Collections.singletonList(wordNode));
     } else {
       wordNode =
           new LabeledScoredTreeNode(lf.newLabel("WD"), Collections.singletonList(wordNode));
     }
     daughters.add(wordNode);
   }
   return new LabeledScoredTreeNode(new StringLabel("S"), daughters);
 }
  public AllSubstitution UNIFY(Predicate x, Predicate y, Sentence sent) {
    // 1. Find an array of substitutiion tsubstList
    // 2. Now the linked hashmap contains all the array of substitution.
    // 3. If 1 is contained in 2 then return false and delete from hash map
    // else add in the map.

    // This is the returned value.
    AllSubstitution tAllSubst = new AllSubstitution();
    // This is part of return value. This is part of tAllSubst.
    List<Substitution> tSubstList = new LinkedList<Substitution>();
    // Current subst.
    Substitution s = null;
    // Unification success or failure.
    // 1. Fails when same sentence comes with same substitution.
    boolean tStatus = false;

    boolean available = true;
    // Just make sure length of args of x and y are same. Also just check if they are same fnName.
    for (int i = 0; i < x.args.size(); i++) {
      // x and y both can be variable. I don't know what to do.
      if (isVariable(x, i)) {
        s = new Substitution(x.args.get(i), y.args.get(i));
        tStatus = true;
      }

      // x and y both can be variable. I don't know what to do.
      else if (isVariable(y, i)) {
        s = new Substitution(y.args.get(i), x.args.get(i));
        // tSubstList.add(s);
        tStatus = true;
      }

      // x and y both are not variables. Just make status as true.
      else if (x.args.get(i).equals(y.args.get(i))) {
        s = new Substitution(y.args.get(i), x.args.get(i));
        // tSubstList.add(s);
        tStatus = true;
      } else {
        s = null;
        tStatus = false;
        break;
      }

      /*
      for(int j=0; j<tlist.size();j++)
      {
      	Substitution tSub = tlist.get(j);
      	if(tSub!=null && tSub.var.equals(s.var) && tSub.val.equals(s.val))
      	{
      		tStatus = false;
      		available = true;
      		tlist.remove(j);
      		break;
      	}
      }
      */
      if (s != null) {
        tSubstList.add(s);
      }
      /*
      if(!available)
      {
      	tlist.add(s);
      }
      */
    }
    // AllSubstitution temp = new AllSubstitution();
    // temp.SubstList = tlist;

    // LinkedList<AllSubstitution> tglobalSubst = substList.get(x.fnName);
    // if(tglobalSubst == null)
    // tglobalSubst = new AllSubstitution();
    // List<Substitution> tlist=tglobalSubst.SubstList;

    // check tstatus is true or false. If false return.
    if (tStatus) {
      Map<String, String> temp = new HashMap<String, String>();
      //
      for (Substitution substitution : tSubstList) {

        String match = temp.get(substitution.var);
        if (match != null && !match.equals("") && !substitution.val.equals(match)) {
          tStatus = false;
          break;
        } else {
          temp.put(substitution.var, substitution.val);
        }
      }

      // now check if all elements of tSubstList are present in global hashmap.
      if (sent.allSubst == null || sent.allSubst.size() == 0) {
        if (sent.allSubst == null) {
          sent.allSubst = new LinkedList<AllSubstitution>();
        }
        tAllSubst.SubstList = tSubstList;
        tAllSubst.status = tStatus;
        if (sent.lhs.size() != 0) sent.allSubst.add(tAllSubst);
      } else {

        for (AllSubstitution lsubst : sent.allSubst) {
          available = true;
          for (int i = 0; i < tSubstList.size(); i++) {
            if (!tSubstList.get(i).var.equals(lsubst.SubstList.get(i).var)
                || !tSubstList.get(i).val.equals(lsubst.SubstList.get(i).val)) {
              available = false;
              break;
            }
          }
          if (available) {
            tStatus = false;
            // sent.allSubst.remove(lsubst);
            break;
          }
        }

        if (!available) {
          tAllSubst.SubstList = tSubstList;
          tAllSubst.status = tStatus;
          sent.allSubst.add(tAllSubst);
        }
      }
    } else {
      tAllSubst.SubstList = tSubstList;
      tAllSubst.status = tStatus;
    }
    return tAllSubst;
  }