Beispiel #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));
        }
Beispiel #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());
        }