You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devicemap.apache.org by re...@apache.org on 2015/07/30 02:54:07 UTC

svn commit: r1693376 - in /devicemap/trunk/clients/2.0/reference/src: DeviceMapClient.java Main.java Pattern.java

Author: rezan
Date: Thu Jul 30 00:54:06 2015
New Revision: 1693376

URL: http://svn.apache.org/r1693376
Log:
candidates and ranking

Modified:
    devicemap/trunk/clients/2.0/reference/src/DeviceMapClient.java
    devicemap/trunk/clients/2.0/reference/src/Main.java
    devicemap/trunk/clients/2.0/reference/src/Pattern.java

Modified: devicemap/trunk/clients/2.0/reference/src/DeviceMapClient.java
URL: http://svn.apache.org/viewvc/devicemap/trunk/clients/2.0/reference/src/DeviceMapClient.java?rev=1693376&r1=1693375&r2=1693376&view=diff
==============================================================================
--- devicemap/trunk/clients/2.0/reference/src/DeviceMapClient.java (original)
+++ devicemap/trunk/clients/2.0/reference/src/DeviceMapClient.java Thu Jul 30 00:54:06 2015
@@ -169,7 +169,7 @@ public class DeviceMapClient {
             patterns.put(patternToken, tokenPatterns);
           }
 
-          Main.log(pattern.toString(), 3);
+          Main.log(pattern.toStringFull(), 3);
         }
       }
 
@@ -326,8 +326,54 @@ public class DeviceMapClient {
     }
 
     Main.log("Ngrams: " + ngramTokenStream, 3);
+
+    List<String> matchedTokens = new ArrayList<>();
+    List<Pattern> candidates = new ArrayList<>();
+    Pattern winner = null;
+
+    for(String token : ngramTokenStream) {
+      List<Pattern> matched = patterns.get(token);
+
+      if(matched != null) {
+        matchedTokens.add(token);
+
+        for(Pattern match : matched) {
+          if(match.isStrong()) {
+            winner = match;
+            break;
+          }
+          
+          if(!candidates.contains(match)) {
+            candidates.add(match);
+          }
+        }
+
+        Main.log("Hit: " + token + ", candidates: " + matched, 3);
+      }
+    }
+
+    if(winner == null) {
+      for(Pattern candidate : candidates) {
+        if(candidate.isValid(matchedTokens)) {
+          Main.log("Candidate: " + candidate.toStringRank(), 3);
+          if(winner == null || candidate.getRank() > winner.getRank()) {
+            winner = candidate;
+          }
+        }
+      }
+    }
+    
+    Main.log("Winner: " + (winner == null ? "null" : winner.toStringFull()), 3);
+
+    if(winner == null) {
+      if(defaultId != null) {
+        return defaultId;
+      } else {
+        return null;
+      }
+    }
     
-    return "";
+    return winner.getPatternId();
   }
 
   public String getDomain() {

Modified: devicemap/trunk/clients/2.0/reference/src/Main.java
URL: http://svn.apache.org/viewvc/devicemap/trunk/clients/2.0/reference/src/Main.java?rev=1693376&r1=1693375&r2=1693376&view=diff
==============================================================================
--- devicemap/trunk/clients/2.0/reference/src/Main.java (original)
+++ devicemap/trunk/clients/2.0/reference/src/Main.java Thu Jul 30 00:54:06 2015
@@ -27,7 +27,7 @@ public class Main {
   private static int verbose = 1;
   
   public static void main(String args[]) throws Exception {
-    log("DeviceMap Reference Client " + DeviceMapClient.VERSION, 0);
+    log("DeviceMap Reference Client " + DeviceMapClient.VERSION, -1);
 
     DeviceMapClient client = new DeviceMapClient();
 
@@ -54,7 +54,7 @@ public class Main {
       } else if(!option.startsWith("-") && testString == null) {
         testString = option;
       } else if(option.equals("-q")) {
-        verbose = 0;
+        verbose = -1;
       } else if(option.equals("-v")) {
         verbose = 2;
       } else if(option.equals("-vv")) {
@@ -88,6 +88,7 @@ public class Main {
     if(testString != null) {
       log("Test string: '" + testString + "'", 0);
       String result = client.classify(testString);
+      log("Test result: " + result, -1);
     }
 
     if(failure) {
@@ -96,15 +97,15 @@ public class Main {
   }
 
   private static void printHelp() {
-    log("Usage: " + Main.class.getName() + " [OPTIONS] [STRING]\n", 0);
-    log("  -p <file>            load DeviceMap 2.0 pattern file (REQUIRED)", 0);
-    log("  -a <file>            load DeviceMap 2.0 attribute file", 0);
-    log("  -t <file>            load DeviceMap 2.0 test file", 0);
-    log("  -h                   print help", 0);
-    log("  -q                   quiet", 0);
-    log("  -v[v]                verbose", 0);
-    log("  STRING               test string", 0);
-    log("", 0);
+    log("Usage: " + Main.class.getName() + " [OPTIONS] [STRING]\n", -1);
+    log("  -p <file>            load DeviceMap 2.0 pattern file (REQUIRED)", -1);
+    log("  -a <file>            load DeviceMap 2.0 attribute file", -1);
+    log("  -t <file>            load DeviceMap 2.0 test file", -1);
+    log("  -h                   print help", -1);
+    log("  -q                   quiet", -1);
+    log("  -v[v]                verbose", -1);
+    log("  STRING               test string", -1);
+    log("", -1);
   }
 
   private static String getParam(String args[], int pos, String error) throws Exception {
@@ -169,15 +170,28 @@ public class Main {
 
           String result = client.classify(input);
 
-          if(1 == 1) {
+          boolean pass = false;
+
+          if(resultPatternId == null) {
+            if(result == null) {
+              pass = true;
+            }
+          } else if(resultPatternId.equals(result)) {
+            pass = true;
+          }
+
+          if(pass) {
             passCount++;
+            log("Passed, expected: " + resultPatternId, 2);
+          } else {
+            log("FAILED, expected: " + resultPatternId + ", result: " + result, 2);
           }
 
           testCount++;
         }
       }
 
-    log("Test passed " + passCount + " out of " + testCount + ". " + (testCount == passCount ? "PASS" : "FAIL"), 0);
+    log("Test passed " + passCount + " out of " + testCount + ". " + (testCount == passCount ? "PASS" : "FAIL"), -1);
 
     return testCount != passCount;
   }

Modified: devicemap/trunk/clients/2.0/reference/src/Pattern.java
URL: http://svn.apache.org/viewvc/devicemap/trunk/clients/2.0/reference/src/Pattern.java?rev=1693376&r1=1693375&r2=1693376&view=diff
==============================================================================
--- devicemap/trunk/clients/2.0/reference/src/Pattern.java (original)
+++ devicemap/trunk/clients/2.0/reference/src/Pattern.java Thu Jul 30 00:54:06 2015
@@ -52,6 +52,16 @@ public class Pattern {
     patternType = json.get("patternType").asText();
     rankType = json.get("rankType").asText();
 
+    if(!patternType.equals("SimpleOrderedAnd") && !patternType.equals("SimpleAnd") &&
+        !patternType.equals("Simple")) {
+      throw new Exception("Invalid patternType: " + patternType);
+    }
+
+    if(!rankType.equals("Strong") && !rankType.equals("Weak") &&
+        !rankType.equals("None")) {
+      throw new Exception("Invalid rankType: " + rankType);
+    }
+
     String rankValueStr = (json.get("rankValue") != null ? json.get("rankValue").asText() : "0");
     rankValue = Integer.parseInt(rankValueStr);
 
@@ -72,10 +82,60 @@ public class Pattern {
     }
   }
 
+  public boolean isValid(List<String> matchedTokens) {
+    int lastFound = -1;
+
+    for(String patternToken : patternTokens) {
+      int found = matchedTokens.indexOf(patternToken);
+
+      if(found == -1 && (isSimpleAnd() || isSimpleOrderedAnd())) {
+        return false;
+      }
+
+      if(found >= 0 && isSimple()) {
+        return true;
+      }
+
+      if(isSimpleOrderedAnd()) {
+        if(found <= lastFound) {
+          return false;
+        } else {
+          lastFound = found;
+        }
+      }
+    }
+
+    if(isSimple()) {
+      return false;
+    } else {
+      return true;
+    }
+  }
+
+  public long getRank() {
+    long rank = rankValue;
+
+    if(isWeak()) {
+      rank += 100000;
+    } else if(isStrong()) {
+      rank += 10000000;
+    }
+
+    return rank;
+  }
+
   @Override
   public String toString() {
+    return patternId;
+  }
+
+  public String toStringRank() {
+    return patternId + "(" + getRank() + ")";
+  }
+
+  public String toStringFull() {
     return "patternId: " + patternId + ", patternType: " + patternType +
-            ", patternTokens: " + patternTokens.size() + ", rankType: " + rankType + ", rankValue: " + rankValue;
+            patternTokens + ", rankType: " + rankType + ":" + rankValue;
   }
 
   public String getPatternId() {
@@ -85,4 +145,28 @@ public class Pattern {
   public List<String> getPatternTokens() {
     return patternTokens;
   }
+
+  public boolean isStrong() {
+    return rankType.equals("Strong");
+  }
+
+  public boolean isWeak() {
+    return rankType.equals("Weak");
+  }
+
+  public boolean isNone() {
+    return rankType.equals("None");
+  }
+
+  public boolean isSimple() {
+    return patternType.equals("Simple");
+  }
+
+  public boolean isSimpleAnd() {
+    return patternType.equals("SimpleAnd");
+  }
+
+  public boolean isSimpleOrderedAnd() {
+    return patternType.equals("SimpleOrderedAnd");
+  }
 }