Source Code Cross Referenced for TRStringDistance.java in  » Net » lucene-connector » org » apache » lucene » search » spell » 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 » Net » lucene connector » org.apache.lucene.search.spell 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.lucene.search.spell;
002:
003:        /**
004:         * Licensed to the Apache Software Foundation (ASF) under one or more
005:         * contributor license agreements.  See the NOTICE file distributed with
006:         * this work for additional information regarding copyright ownership.
007:         * The ASF licenses this file to You under the Apache License, Version 2.0
008:         * (the "License"); you may not use this file except in compliance with
009:         * the License.  You may obtain a copy of the License at
010:         *
011:         *     http://www.apache.org/licenses/LICENSE-2.0
012:         *
013:         * Unless required by applicable law or agreed to in writing, software
014:         * distributed under the License is distributed on an "AS IS" BASIS,
015:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016:         * See the License for the specific language governing permissions and
017:         * limitations under the License.
018:         */
019:
020:        /**
021:         * Edit distance  class
022:         */
023:        final class TRStringDistance {
024:
025:            final char[] sa;
026:            final int n;
027:            final int[][][] cache = new int[30][][];
028:
029:            /**
030:             * Optimized to run a bit faster than the static getDistance().
031:             * In one benchmark times were 5.3sec using ctr vs 8.5sec w/ static method, thus 37% faster.
032:             */
033:            public TRStringDistance(String target) {
034:                sa = target.toCharArray();
035:                n = sa.length;
036:            }
037:
038:            //*****************************
039:            // Compute Levenshtein distance
040:            //*****************************
041:            public final int getDistance(String other) {
042:                int d[][]; // matrix
043:                int cost; // cost
044:
045:                // Step 1
046:                final char[] ta = other.toCharArray();
047:                final int m = ta.length;
048:                if (n == 0) {
049:                    return m;
050:                }
051:                if (m == 0) {
052:                    return n;
053:                }
054:
055:                if (m >= cache.length) {
056:                    d = form(n, m);
057:                } else if (cache[m] != null) {
058:                    d = cache[m];
059:                } else {
060:                    d = cache[m] = form(n, m);
061:
062:                    // Step 3
063:
064:                }
065:                for (int i = 1; i <= n; i++) {
066:                    final char s_i = sa[i - 1];
067:
068:                    // Step 4
069:
070:                    for (int j = 1; j <= m; j++) {
071:                        final char t_j = ta[j - 1];
072:
073:                        // Step 5
074:
075:                        if (s_i == t_j) { // same
076:                            cost = 0;
077:                        } else { // not a match
078:                            cost = 1;
079:
080:                            // Step 6
081:
082:                        }
083:                        d[i][j] = min3(d[i - 1][j] + 1, d[i][j - 1] + 1,
084:                                d[i - 1][j - 1] + cost);
085:
086:                    }
087:
088:                }
089:
090:                // Step 7
091:                return d[n][m];
092:
093:            }
094:
095:            /**
096:             *
097:             */
098:            private static int[][] form(int n, int m) {
099:                int[][] d = new int[n + 1][m + 1];
100:                // Step 2
101:
102:                for (int i = 0; i <= n; i++) {
103:                    d[i][0] = i;
104:
105:                }
106:                for (int j = 0; j <= m; j++) {
107:                    d[0][j] = j;
108:                }
109:                return d;
110:            }
111:
112:            //****************************
113:            // Get minimum of three values
114:            //****************************
115:            private static int min3(int a, int b, int c) {
116:                int mi = a;
117:                if (b < mi) {
118:                    mi = b;
119:                }
120:                if (c < mi) {
121:                    mi = c;
122:                }
123:                return mi;
124:
125:            }
126:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.