Source Code Cross Referenced for RefinedSoundex.java in  » Library » Apache-common-codec » org » apache » commons » codec » language » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache common codec » org.apache.commons.codec.language 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2001-2004 The Apache Software Foundation.
003:         * 
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         * 
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         * 
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package org.apache.commons.codec.language;
018:
019:        import org.apache.commons.codec.EncoderException;
020:        import org.apache.commons.codec.StringEncoder;
021:
022:        /**
023:         * Encodes a string into a Refined Soundex value. A refined soundex code is
024:         * optimized for spell checking words. Soundex method originally developed by
025:         * <CITE>Margaret Odell</CITE> and <CITE>Robert Russell</CITE>.
026:         * 
027:         * @author Apache Software Foundation
028:         * @version $Id: RefinedSoundex.java,v 1.21 2004/06/05 18:32:04 ggregory Exp $
029:         */
030:        public class RefinedSoundex implements  StringEncoder {
031:
032:            /**
033:             * This static variable contains an instance of the RefinedSoundex using
034:             * the US_ENGLISH mapping.
035:             */
036:            public static final RefinedSoundex US_ENGLISH = new RefinedSoundex();
037:
038:            /**
039:             * RefinedSoundex is *refined* for a number of reasons one being that the
040:             * mappings have been altered. This implementation contains default
041:             * mappings for US English.
042:             */
043:            public static final char[] US_ENGLISH_MAPPING = "01360240043788015936020505"
044:                    .toCharArray();
045:
046:            /**
047:             * Every letter of the alphabet is "mapped" to a numerical value. This char
048:             * array holds the values to which each letter is mapped. This
049:             * implementation contains a default map for US_ENGLISH
050:             */
051:            private char[] soundexMapping;
052:
053:            /**
054:             * Creates an instance of the RefinedSoundex object using the default US
055:             * English mapping.
056:             */
057:            public RefinedSoundex() {
058:                this (US_ENGLISH_MAPPING);
059:            }
060:
061:            /**
062:             * Creates a refined soundex instance using a custom mapping. This
063:             * constructor can be used to customize the mapping, and/or possibly
064:             * provide an internationalized mapping for a non-Western character set.
065:             * 
066:             * @param mapping
067:             *                  Mapping array to use when finding the corresponding code for
068:             *                  a given character
069:             */
070:            public RefinedSoundex(char[] mapping) {
071:                this .soundexMapping = mapping;
072:            }
073:
074:            /**
075:             * Returns the number of characters in the two encoded Strings that are the
076:             * same. This return value ranges from 0 to the length of the shortest
077:             * encoded String: 0 indicates little or no similarity, and 4 out of 4 (for
078:             * example) indicates strong similarity or identical values. For refined
079:             * Soundex, the return value can be greater than 4.
080:             * 
081:             * @param s1
082:             *                  A String that will be encoded and compared.
083:             * @param s2
084:             *                  A String that will be encoded and compared.
085:             * @return The number of characters in the two encoded Strings that are the
086:             *             same from 0 to to the length of the shortest encoded String.
087:             * 
088:             * @see SoundexUtils#difference(StringEncoder,String,String)
089:             * @see <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_de-dz_8co5.asp">
090:             *          MS T-SQL DIFFERENCE</a>
091:             * 
092:             * @throws EncoderException
093:             *                  if an error occurs encoding one of the strings
094:             * @since 1.3
095:             */
096:            public int difference(String s1, String s2) throws EncoderException {
097:                return SoundexUtils.difference(this , s1, s2);
098:            }
099:
100:            /**
101:             * Encodes an Object using the refined soundex algorithm. This method is
102:             * provided in order to satisfy the requirements of the Encoder interface,
103:             * and will throw an EncoderException if the supplied object is not of type
104:             * java.lang.String.
105:             * 
106:             * @param pObject
107:             *                  Object to encode
108:             * @return An object (or type java.lang.String) containing the refined
109:             *             soundex code which corresponds to the String supplied.
110:             * @throws EncoderException
111:             *                  if the parameter supplied is not of type java.lang.String
112:             */
113:            public Object encode(Object pObject) throws EncoderException {
114:                if (!(pObject instanceof  java.lang.String)) {
115:                    throw new EncoderException(
116:                            "Parameter supplied to RefinedSoundex encode is not of type java.lang.String");
117:                }
118:                return soundex((String) pObject);
119:            }
120:
121:            /**
122:             * Encodes a String using the refined soundex algorithm.
123:             * 
124:             * @param pString
125:             *                  A String object to encode
126:             * @return A Soundex code corresponding to the String supplied
127:             */
128:            public String encode(String pString) {
129:                return soundex(pString);
130:            }
131:
132:            /**
133:             * Returns the mapping code for a given character. The mapping codes are
134:             * maintained in an internal char array named soundexMapping, and the
135:             * default values of these mappings are US English.
136:             * 
137:             * @param c
138:             *                  char to get mapping for
139:             * @return A character (really a numeral) to return for the given char
140:             */
141:            char getMappingCode(char c) {
142:                if (!Character.isLetter(c)) {
143:                    return 0;
144:                }
145:                return this .soundexMapping[Character.toUpperCase(c) - 'A'];
146:            }
147:
148:            /**
149:             * Retreives the Refined Soundex code for a given String object.
150:             * 
151:             * @param str
152:             *                  String to encode using the Refined Soundex algorithm
153:             * @return A soundex code for the String supplied
154:             */
155:            public String soundex(String str) {
156:                if (str == null) {
157:                    return null;
158:                }
159:                str = SoundexUtils.clean(str);
160:                if (str.length() == 0) {
161:                    return str;
162:                }
163:
164:                StringBuffer sBuf = new StringBuffer();
165:                sBuf.append(str.charAt(0));
166:
167:                char last, current;
168:                last = '*';
169:
170:                for (int i = 0; i < str.length(); i++) {
171:
172:                    current = getMappingCode(str.charAt(i));
173:                    if (current == last) {
174:                        continue;
175:                    } else if (current != 0) {
176:                        sBuf.append(current);
177:                    }
178:
179:                    last = current;
180:
181:                }
182:
183:                return sBuf.toString();
184:            }
185:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.