예제 #1
0
 /**
  * Assembly a reply from a decomp rule and the input. If the reassembly rule is goto, return null
  * and give the gotoKey to use. Otherwise return the response.
  */
 String assemble(Decomp d, String reply[], Key gotoKey) {
   String lines[] = new String[3];
   d.stepRule();
   String rule = d.nextRule();
   if (EString.match(rule, "goto *", lines)) {
     // goto rule -- set gotoKey and return false.
     gotoKey.copy(keys.getKey(lines[0]));
     if (gotoKey.key() != null) return null;
     System.out.println("Goto rule did not match key: " + lines[0]);
     return null;
   }
   String work = "";
   while (EString.match(rule, "* (#)*", lines)) {
     // reassembly rule with number substitution
     rule = lines[2]; // there might be more
     int n = 0;
     try {
       n = Integer.parseInt(lines[1]) - 1;
     } catch (NumberFormatException e) {
       System.out.println("Number is wrong in reassembly rule " + lines[1]);
     }
     if (n < 0 || n >= reply.length) {
       System.out.println("Substitution number is bad " + lines[1]);
       return null;
     }
     reply[n] = post.translate(reply[n]);
     work += lines[0] + " " + reply[n];
   }
   work += rule;
   if (d.mem()) {
     mem.save(work);
     return null;
   }
   return work;
 }
예제 #2
0
 /**
  * Decompose a string according to the given key. Try each decomposition rule in order. If it
  * matches, assemble a reply and return it. If assembly fails, try another decomposition rule. If
  * assembly is a goto rule, return null and give the key. If assembly succeeds, return the reply;
  */
 String decompose(Key key, String s, Key gotoKey) {
   String reply[] = new String[10];
   for (int i = 0; i < key.decomp().size(); i++) {
     Decomp d = (Decomp) key.decomp().elementAt(i);
     String pat = d.pattern();
     if (syns.matchDecomp(s, pat, reply)) {
       String rep = assemble(d, reply, gotoKey);
       if (rep != null) return rep;
       if (gotoKey.key() != null) return null;
     }
   }
   return null;
 }
예제 #3
0
 /**
  * Process a sentence. (1) Make pre transformations. (2) Check for quit word. (3) Scan sentence
  * for keys, build key stack. (4) Try decompositions for each key.
  */
 String sentence(String s) {
   s = pre.translate(s);
   s = EString.pad(s);
   if (quit.find(s)) {
     finished = true;
     return finl;
   }
   keys.buildKeyStack(keyStack, s);
   for (int i = 0; i < keyStack.keyTop(); i++) {
     Key gotoKey = new Key();
     String reply = decompose(keyStack.key(i), s, gotoKey);
     if (reply != null) return reply;
     // If decomposition returned gotoKey, try it
     while (gotoKey.key() != null) {
       reply = decompose(gotoKey, s, gotoKey);
       if (reply != null) return reply;
     }
   }
   return null;
 }
예제 #4
0
  public boolean haskey(Key data) // tests if node contains a certain key
      {

    if (data.key().equals(left.key()) && left().present()) // if left key is data
    {
      return true;
    } else if (middle != null
        && data.key().equals(middle.key())
        && middle().present()) // if middle key is data
    {
      return true;
    } else if (right != null
        && data.key().equals(right.key())
        && right().present()) // if right key is data
    {
      return true;
    }

    return false;
  }
예제 #5
0
 /** Constructs a ChainableOrdering. */
 public ChainableOrdering(Key<?> key, SortOrder sortOrder) {
   this(key.key(), sortOrder);
 }
예제 #6
0
  public void insert(String newkey) // inserts new key into the node
      {
    keycount++;
    if (keycount == 2) // if there are now 2 keys
    {
      Key temp = left;
      Key temp2 = new Key(newkey);

      if (temp.key().compareTo(temp2.key()) < 0) {
        left = temp;
        middle = temp2;
      } else {
        left = temp2;
        middle = temp;
      }
    } else {
      Key temp = left;
      Key temp2 = middle;
      Key temp3 = new Key(newkey);

      if (temp.key().compareTo(temp2.key()) < 0 && temp2.key().compareTo(temp3.key()) < 0) {
        right = temp3; // order of temp1, 2, 3
      } else if (temp.key().compareTo(temp3.key()) < 0 && temp3.key().compareTo(temp2.key()) < 0) {
        middle = temp3; // order temp1, 3, 2
        right = temp2;
      } else if (temp2.key().compareTo(temp.key()) < 0 && temp.key().compareTo(temp3.key()) < 0) {
        left = temp2; // order temp2, 1, 3
        middle = temp;
        right = temp3;
      } else if (temp2.key().compareTo(temp3.key()) < 0 && temp3.key().compareTo(temp.key()) < 0) {
        left = temp2; // order temp2, 3, 1
        middle = temp3;
        right = temp;
      } else if (temp3.key().compareTo(temp.key()) < 0 && temp.key().compareTo(temp2.key()) < 0) {
        left = temp3; // order temp3, 1, 2
        middle = temp;
        right = temp2;
      } else {
        left = temp3; // order temp3, 2, 1
        middle = temp2;
        right = temp;
      }
    }
  }
예제 #7
0
  public Node nextnode(
      Key data) // given data, decides which child's subtree data should be located in
      {

    if (keycount == 1) // if only 1 key
    {
      if (data.key().compareTo(left.key()) < 0) // go to leftmost child
      {
        return llnode;
      } else // otherwise go to the second leftmost child
      {
        return lmnode;
      }
    } else if (keycount == 2) // if only 2 keys
    {
      if (data.key().compareTo(left.key()) < 0) // if belongs to the left of the first key
      {
        return llnode; // return leftmost node
      } else if (data.key().compareTo(middle.key())
          < 0) // if belongs to the left of the middle key and right of first key
      {
        return lmnode; // return second leftmost node
      } else // must belong t right of second key
      {
        return mrnode; // second rightmost node
      }
    } else // 3 keys
    {
      if (data.key().compareTo(left.key()) < 0) // if belongs to left of first key
      {
        return llnode; // left most node
      } else if (data.key().compareTo(middle.key())
          < 0) // belongs right of first key left of second
      {
        return lmnode; // second leftmost node
      } else if (data.key().compareTo(right.key()) < 0) // belongs right of second key left of third
      {
        return mrnode; // second rightmost node
      } else // belongs to the right of the third key
      {
        return rrnode; // rightmost node
      }
    }
  }