commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1155182 - in /commons/proper/codec/trunk/src: java/org/apache/commons/codec/language/bm/PhoneticEngine.java java/org/apache/commons/codec/language/bm/Rule.java test/org/apache/commons/codec/language/bm/RuleTest.java
Date Tue, 09 Aug 2011 01:16:10 GMT
Author: ggregory
Date: Tue Aug  9 01:16:09 2011
New Revision: 1155182

URL: http://svn.apache.org/viewvc?rev=1155182&view=rev
Log:
[CODEC-125] Implement a Beider-Morse phonetic matching codec. Applied patch https://issues.apache.org/jira/secure/attachment/12489767/comparator.patch

Modified:
    commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/bm/PhoneticEngine.java
    commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/bm/Rule.java
    commons/proper/codec/trunk/src/test/org/apache/commons/codec/language/bm/RuleTest.java

Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/bm/PhoneticEngine.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/bm/PhoneticEngine.java?rev=1155182&r1=1155181&r2=1155182&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/bm/PhoneticEngine.java
(original)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/bm/PhoneticEngine.java
Tue Aug  9 01:16:09 2011
@@ -251,7 +251,7 @@ public class PhoneticEngine {
             return phonemeBuilder;
         }
 
-        Set<Rule.Phoneme> phonemes = new TreeSet<Rule.Phoneme>();
+        Set<Rule.Phoneme> phonemes = new TreeSet<Rule.Phoneme>(Rule.Phoneme.COMPARATOR);
 
         for (Rule.Phoneme phoneme : phonemeBuilder.getPhonemes()) {
             PhonemeBuilder subBuilder = PhonemeBuilder.empty(phoneme.getLanguages());

Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/bm/Rule.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/bm/Rule.java?rev=1155182&r1=1155181&r2=1155182&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/bm/Rule.java (original)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/language/bm/Rule.java Tue
Aug  9 01:16:09 2011
@@ -21,6 +21,7 @@ import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.EnumMap;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -80,7 +81,26 @@ import java.util.regex.Pattern;
  */
 public class Rule {
 
-    public static final class Phoneme implements PhonemeExpr, Comparable<Phoneme> {
+    public static final class Phoneme implements PhonemeExpr {
+        public static final Comparator<Phoneme> COMPARATOR = new Comparator<Phoneme>()
{
+            public int compare(Phoneme o1, Phoneme o2) {
+                for (int i = 0; i < o1.phonemeText.length(); i++) {
+                    if (i >= o2.phonemeText.length()) {
+                        return +1;
+                    }
+                    int c = o1.phonemeText.charAt(i) - o2.phonemeText.charAt(i);
+                    if (c != 0) {
+                        return c;
+                    }
+                }
+
+                if (o1.phonemeText.length() < o2.phonemeText.length()) {
+                    return -1;
+                }
+
+                return 0;
+            }
+        };
 
         private final CharSequence phonemeText;
         private final Languages.LanguageSet languages;
@@ -94,24 +114,6 @@ public class Rule {
             return new Phoneme(this.phonemeText.toString() + str.toString(), this.languages);
         }
 
-        public int compareTo(Phoneme o) {
-            for (int i = 0; i < phonemeText.length(); i++) {
-                if (i >= o.phonemeText.length()) {
-                    return +1;
-                }
-                int c = phonemeText.charAt(i) - o.phonemeText.charAt(i);
-                if (c != 0) {
-                    return c;
-                }
-            }
-
-            if (phonemeText.length() < o.phonemeText.length()) {
-                return -1;
-            }
-
-            return 0;
-        }
-
         public Languages.LanguageSet getLanguages() {
             return this.languages;
         }

Modified: commons/proper/codec/trunk/src/test/org/apache/commons/codec/language/bm/RuleTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/language/bm/RuleTest.java?rev=1155182&r1=1155181&r2=1155182&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/org/apache/commons/codec/language/bm/RuleTest.java
(original)
+++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/language/bm/RuleTest.java
Tue Aug  9 01:16:09 2011
@@ -63,7 +63,7 @@ public class RuleTest {
         for (Rule.Phoneme[] phs : makePhonemes()) {
             for (int i = 0; i < phs.length; i++) {
                 for (int j = i + 1; j < phs.length; j++) {
-                    int c = phs[i].compareTo(phs[j]);
+                    int c = Rule.Phoneme.COMPARATOR.compare(phs[i], phs[j]);
 
                     assertThat("Comparing " + phs[i].getPhonemeText() + " to " + phs[j].getPhonemeText()
+ " should be negative", c,
                             new NegativeIntegerBaseMatcher());
@@ -76,7 +76,8 @@ public class RuleTest {
     public void phonemeComparedToSelfIsZero() {
         for (Rule.Phoneme[] phs : makePhonemes()) {
             for (Rule.Phoneme ph : phs) {
-                assertEquals("Phoneme compared to itself should be zero: " + ph.getPhonemeText(),
0, ph.compareTo(ph));
+                assertEquals("Phoneme compared to itself should be zero: " + ph.getPhonemeText(),
0,
+                        Rule.Phoneme.COMPARATOR.compare(ph, ph));
             }
         }
     }



Mime
View raw message