MatchRule10.java
01 package gate.creole.orthomatcher;
02 
03 
04 
05 /**
06  * RULE #10: is one name the reverse of the other
07  * reversing around prepositions only?
08  * e.g. "Department of Defence" == "Defence Department"
09  * Condition(s): case-sensitive match
10  * Applied to: organisation annotations only
11  */
12 
13 public class MatchRule10 implements OrthoMatcherRule {
14 
15   OrthoMatcher orthomatcher;
16     
17   public MatchRule10(OrthoMatcher orthmatcher){
18       this.orthomatcher=orthmatcher;
19   }
20   
21   @Override
22   public boolean value(String s1, String s2) {
23     
24       boolean result=false;
25     
26       String token = null;
27       String previous_token = null;
28       String next_token = null;
29       boolean invoke_rule=false;
30 
31       if (orthomatcher.tokensLongAnnot.size() >= 3
32               && orthomatcher.tokensShortAnnot.size() >= 2) {
33 
34         // first get the tokens before and after the preposition
35         int i = 0;
36         for (; i< orthomatcher.tokensLongAnnot.size(); i++) {
37           token = (String)
38           orthomatcher.tokensLongAnnot.get(i).getFeatures().get(OrthoMatcher.TOKEN_STRING_FEATURE_NAME);
39           if (orthomatcher.prepos.containsKey(token)) {
40             invoke_rule=true;
41             break;
42           }//if
43           previous_token = token;
44         }//while
45 
46         if (! invoke_rule)
47           result = false;
48         else {
49             if (i < orthomatcher.tokensLongAnnot.size()
50                     && previous_token != null) {
51               next_token= (String)
52               orthomatcher.tokensLongAnnot.get(i++).getFeatures().get(OrthoMatcher.TOKEN_STRING_FEATURE_NAME);
53               
54               String s21 = (String)
55               orthomatcher.tokensShortAnnot.get(0).getFeatures().get(OrthoMatcher.TOKEN_STRING_FEATURE_NAME);
56               String s22 = (String)
57               orthomatcher.tokensShortAnnot.get(1).getFeatures().get(OrthoMatcher.TOKEN_STRING_FEATURE_NAME);
58               // then compare (in reverse) with the first two tokens of s2
59               if (OrthoMatcherHelper.straightCompare(next_token,s21,orthomatcher.caseSensitive)
60                       && OrthoMatcherHelper.straightCompare(previous_token, s22,orthomatcher.caseSensitive))
61                 result = true ;
62               }
63             else result = false;
64         }
65       }//if (tokensLongAnnot.countTokens() >= 3
66       
67       if (resultOrthoMatcherHelper.usedRule(10);
68       return result;
69   }
70   
71   @Override
72   public String getId(){
73     return "MatchRule10";
74   }
75 }