src/share/jaxws_classes/com/sun/xml/internal/bind/v2/util/EditDistance.java

Fri, 23 Aug 2013 09:57:21 +0100

author
mkos
date
Fri, 23 Aug 2013 09:57:21 +0100
changeset 397
b99d7e355d4b
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8022885: Update JAX-WS RI integration to 2.2.9-b14140
8013016: Rebase 8009009 against the latest jdk8/jaxws
Reviewed-by: alanb, chegar

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.bind.v2.util;
ohair@286 27
mkos@397 28 import java.util.AbstractMap;
mkos@397 29 import java.util.Arrays;
ohair@286 30 import java.util.Collection;
mkos@397 31 import java.util.WeakHashMap;
ohair@286 32
ohair@286 33 /**
ohair@286 34 * Computes the string edit distance.
ohair@286 35 *
ohair@286 36 * <p>
ohair@286 37 * Refer to a computer science text book for the definition
ohair@286 38 * of the "string edit distance".
ohair@286 39 *
ohair@286 40 * @author
ohair@286 41 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
ohair@286 42 */
ohair@286 43 public class EditDistance {
ohair@286 44
ohair@286 45 /**
mkos@397 46 * Weak results cache to avoid additional computations.
mkos@397 47 * Because of high complexity caching is required.
mkos@397 48 */
mkos@397 49 private static final WeakHashMap<AbstractMap.SimpleEntry<String,String>, Integer> CACHE = new WeakHashMap<AbstractMap.SimpleEntry<String, String>, Integer>();
mkos@397 50
mkos@397 51 /**
ohair@286 52 * Computes the edit distance between two strings.
ohair@286 53 *
ohair@286 54 * <p>
ohair@286 55 * The complexity is O(nm) where n=a.length() and m=b.length().
ohair@286 56 */
ohair@286 57 public static int editDistance( String a, String b ) {
mkos@397 58 // let's check cache
mkos@397 59 AbstractMap.SimpleEntry<String,String> entry = new AbstractMap.SimpleEntry<String, String>(a, b); // using this class to avoid creation of my own which will handle PAIR of values
mkos@397 60 Integer result = null;
mkos@397 61 if (CACHE.containsKey(entry))
mkos@397 62 result = CACHE.get(entry); // looks like we have it
mkos@397 63
mkos@397 64 if (result == null) {
mkos@397 65 result = new EditDistance(a, b).calc();
mkos@397 66 CACHE.put(entry, result); // cache the result
mkos@397 67 }
mkos@397 68 return result;
ohair@286 69 }
ohair@286 70
ohair@286 71 /**
ohair@286 72 * Finds the string in the <code>group</code> closest to
ohair@286 73 * <code>key</code> and returns it.
ohair@286 74 *
ohair@286 75 * @return null if group.length==0.
ohair@286 76 */
ohair@286 77 public static String findNearest( String key, String[] group ) {
ohair@286 78 return findNearest(key, Arrays.asList(group));
ohair@286 79 }
ohair@286 80
ohair@286 81 /**
ohair@286 82 * Finds the string in the <code>group</code> closest to
ohair@286 83 * <code>key</code> and returns it.
ohair@286 84 *
ohair@286 85 * @return null if group.length==0.
ohair@286 86 */
ohair@286 87 public static String findNearest( String key, Collection<String> group ) {
ohair@286 88 int c = Integer.MAX_VALUE;
ohair@286 89 String r = null;
ohair@286 90
ohair@286 91 for (String s : group) {
ohair@286 92 int ed = editDistance(key,s);
ohair@286 93 if( c>ed ) {
ohair@286 94 c = ed;
ohair@286 95 r = s;
ohair@286 96 }
ohair@286 97 }
ohair@286 98 return r;
ohair@286 99 }
ohair@286 100
ohair@286 101 /** cost vector. */
ohair@286 102 private int[] cost;
ohair@286 103 /** back buffer. */
ohair@286 104 private int[] back;
ohair@286 105
ohair@286 106 /** Two strings to be compared. */
ohair@286 107 private final String a,b;
ohair@286 108
ohair@286 109 private EditDistance( String a, String b ) {
ohair@286 110 this.a=a;
ohair@286 111 this.b=b;
ohair@286 112 cost = new int[a.length()+1];
ohair@286 113 back = new int[a.length()+1]; // back buffer
ohair@286 114
ohair@286 115 for( int i=0; i<=a.length(); i++ )
ohair@286 116 cost[i] = i;
ohair@286 117 }
ohair@286 118
ohair@286 119 /**
ohair@286 120 * Swaps two buffers.
ohair@286 121 */
ohair@286 122 private void flip() {
ohair@286 123 int[] t = cost;
ohair@286 124 cost = back;
ohair@286 125 back = t;
ohair@286 126 }
ohair@286 127
ohair@286 128 private int min(int a,int b,int c) {
ohair@286 129 return Math.min(a,Math.min(b,c));
ohair@286 130 }
ohair@286 131
ohair@286 132 private int calc() {
ohair@286 133 for( int j=0; j<b.length(); j++ ) {
ohair@286 134 flip();
ohair@286 135 cost[0] = j+1;
ohair@286 136 for( int i=0; i<a.length(); i++ ) {
ohair@286 137 int match = (a.charAt(i)==b.charAt(j))?0:1;
ohair@286 138 cost[i+1] = min( back[i]+match, cost[i]+1, back[i+1]+1 );
ohair@286 139 }
ohair@286 140 }
ohair@286 141 return cost[a.length()];
ohair@286 142 }
ohair@286 143 }

mercurial