Presentation is loading. Please wait.

Presentation is loading. Please wait.

L9. Matching Program in Java

Similar presentations


Presentation on theme: "L9. Matching Program in Java"— Presentation transcript:

1 L9. Matching Program in Java
Matching.java Solutions to L8ex using Vector Matsumoto’s solution2

2 Unify Unification function, Unify, is to take two atomic sentences p and q and return a substitution that would make p and q look the same. A substitute  unifies atomic sentences p and q if p  =q  For example, p q  Knows(John, x) | Knows(John, Jane) | {x/Jane} Knows(John, x) | Knows(y, OJ) | {y/John, x/OJ} Knows(John, x) | Knows(y, Mother(y)) | {y/John, x/Mother(John)} e.g., Unify(Knows(John, x), Knows(John, Jane)) = {x/Jane} Idea: Unify rule premises with known facts, apply unifier to conclusion. e.g., if we know q and Knows(John, x) Likes(John, x) then we can conclude Likes(John, Jane) Likes(John, OJ) Likes(John, Mother(John)) Premise 前提

3 The three new inference rules added to FOL
R(8) Universal-Elimination: For any sentence , variable v, and ground term g: e. g.,  x Likes(x, IceCream), we can use the substitute {x/Rose} and infer Like(Rose, IceCream). R(9) Existential-Elimination: For any sentence , variable v, and constant symbol k that does not appear elsewhere in the knowledge base: e. g.,  x Kill(x, Victim), we can infer Kill(Murderer, Victim), as long as Murderer does not appear elsewhere in the knowledge base. R(10) Existential-Introduction: For any sentence , variable v that does not occur in , and ground term g that does occur in : e. g., from Likes(Rose, IceCream) we can infer  x Likes(x, IceCream). SUBST({v/g}, )  v  Ground term is a term that contains no variables. v/g SUBST({v/k}, )  v  k  v SUBST({g/v}, )

4 Example of proof Bob is a buffalo | 1. Buffalo(Bob) Pat is a pig | 2. Pig(Pat) Buffaloes outrun pigs | 3.  x, y Buffalo(x)  Pig(y)  Faster(x,y) Bob outruns Pat Apply R(3) to 1 And | 4. Buffalo(Bob)  Pig(Pat) Apply R(8) to 3 {x/Bob, y/Pat} | 5. Buffalo(Bob)  Pig(Pat)  Faster(Bob,Pat) Apply R(1) to 4 And | 6. Faster(Bob,Pat)

5 String matching: string1 = string2
e.g. “rose” = “rose” if string1.equals(string2) “I am Rose” = “I am Rose” “I am ?x” = “I am Rose” “I am ?x” = “?y am Rose” I = ?y am = am ?x = Rose 40: // 同じなら成功 41: if(string1.equals(string2)) return true; ? Check? String  Tokens 36: public boolean matching(String string1,String string2){ 37: System.out.println(string1); 38: System.out.println(string2);   // 同じなら成功 41: if(string1.equals(string2)) return true;   // 各々トークンに分ける 44: st1 = new StringTokenizer(string1); 45: st2 = new StringTokenizer(string2);   // 数が異なったら失敗 48: if (st1.countTokens() != st2.countTokens()) 49: return false; 51: int length = st1.countTokens(); // 定数同士 52: for (int i = 0 ; i < length; i++){ 53: if (!tokenMatching(st1.nextToken(),st2.nextToken())){   // トークンが一つでもマッチングに失敗したら失敗 55: return false; 56: } 57: }

6 Token matching: token1 = token2
e.g. two strings’ matching, “I am ?x” = “?y am Rose” Three pairs of tokens’ matching: I = ?y am = am ?x = Rose 64: boolean tokenMatching(String token1,String token2){ 65: if(token1.equals(token2)) return true; 66: if( var(token1) && !var(token2)) 67: return varMatching(token1,token2); 68: if(!var(token1) && var(token2)) 69: return varMatching(token2,token1); 70: return false; 71: } 73: boolean varMatching(String vartoken,String token){ 74: if(vars.containsKey(vartoken)){ 75: if(token.equals(vars.get(vartoken))){ 76: return true; 77: } else { 78: return false; 79: } 80: } else { 81: vars.put(vartoken,token); 82: } 83: return true; 84: } 86: boolean var(String str1){ 87: // 先頭が ? なら変数 88: return str1.startsWith("?"); 89: } 27: Matcher(){ 28: vars = new Hashtable(); 29: }

7 Exercise Test cases: 3 4 1. Input Matching.java source
(from keyboard, do not use copy) 2. Understand it (read carefully) 3. Run the program (for various strings) 4. Modify the program so that the modified program is not sensitive to uppercase and lowercase Test cases: 3 4 "i am rose" "I am RoSe " F T "I am ?x" "I am Rose " T T "I am ?x" "I Am Rose " F T "I ?x ?x" "I am am " T T "I ?x ?x" "I am AM " F T "I ?x ?x" "I am rose " F F


Download ppt "L9. Matching Program in Java"

Similar presentations


Ads by Google