Exemplo n.º 1
0
        /**
         * Retrieves the Soundex code for a given String object.
         *
         * @param str
         *                  String to encode using the Soundex algorithm
         * @return A soundex code for the String supplied
         * @throws IllegalArgumentException
         *                  if a character is not mapped
         */
        public String soundex(String str)
        {
            if (str == null)
            {
                return(null);
            }
            str = SoundexUtils.clean(str);
            if (str.length() == 0)
            {
                return(str);
            }
            char[] outJ = { '0', '0', '0', '0' };
            char   last, mapped;
            int    incount = 1, count = 1;

            outJ[0] = str.charAt(0);
            // getMappingCode() throws IllegalArgumentException
            last = getMappingCode(str, 0);
            while ((incount < str.length()) && (count < outJ.Length))
            {
                mapped = getMappingCode(str, incount++);
                if (mapped != 0)
                {
                    if ((mapped != '0') && (mapped != last))
                    {
                        outJ[count++] = mapped;
                    }
                    last = mapped;
                }
            }
            return(new String(outJ));
        }
Exemplo n.º 2
0
        /**
         * Retreives the Refined Soundex code for a given String object.
         *
         * @param str
         *                  String to encode using the Refined Soundex algorithm
         * @return A soundex code for the String supplied
         */
        public String soundex(String str)
        {
            if (str == null)
            {
                return(null);
            }
            str = SoundexUtils.clean(str);
            if (str.length() == 0)
            {
                return(str);
            }

            java.lang.StringBuffer sBuf = new java.lang.StringBuffer();
            sBuf.append(str.charAt(0));

            char last, current;

            last = '*';

            for (int i = 0; i < str.length(); i++)
            {
                current = getMappingCode(str.charAt(i));
                if (current == last)
                {
                    continue;
                }
                else if (current != 0)
                {
                    sBuf.append(current);
                }

                last = current;
            }

            return(sBuf.toString());
        }
Exemplo n.º 3
0
 /**
  * Encodes the Strings and returns the number of characters in the two encoded Strings that are the same. This
  * return value ranges from 0 through 4: 0 indicates little or no similarity, and 4 indicates strong similarity or
  * identical values.
  *
  * @param s1
  *                  A String that will be encoded and compared.
  * @param s2
  *                  A String that will be encoded and compared.
  * @return The number of characters in the two encoded Strings that are the same from 0 to 4.
  *
  * @see SoundexUtils#difference(StringEncoder,String,String)
  * @see <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp"> MS
  *          T-SQL DIFFERENCE </a>
  *
  * @throws EncoderException
  *                  if an error occurs encoding one of the strings
  * @since 1.3
  */
 public int difference(String s1, String s2)
 {// throws EncoderException {
     return(SoundexUtils.difference(this, s1, s2));
 }