/** * Finds the string in the <code>group</code> closest to * <code>key</code> and returns it. * * @return null if group.length==0. */ public static String findNearest( String key, String[] group ) { return findNearest(key, Arrays.asList(group)); }
/** * Finds a type name that this context recognizes which is * "closest" to the given type name. * * <p> * This method is used for error recovery. */ public String getNearestTypeName(QName name) { String[] all = new String[typeMap.size()]; int i=0; for (QName qn : typeMap.keySet()) { if(qn.getLocalPart().equals(name.getLocalPart())) return qn.toString(); // probably a match, as people often gets confused about namespace. all[i++] = qn.toString(); } String nearest = EditDistance.findNearest(name.toString(), all); if(EditDistance.editDistance(nearest,name.toString())>10) return null; // too far apart. return nearest; }
/** * Computes the edit distance between two strings. * * <p> * The complexity is O(nm) where n=a.length() and m=b.length(). */ public static int editDistance( String a, String b ) { // let's check cache 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 Integer result = null; if (CACHE.containsKey(entry)) result = CACHE.get(entry); // looks like we have it if (result == null) { result = new EditDistance(a, b).calc(); CACHE.put(entry, result); // cache the result } return result; }
/** * Finds the string in the <code>group</code> closest to * <code>key</code> and returns it. * * @return null if group.length==0. */ public static String findNearest( String key, Collection<String> group ) { int c = Integer.MAX_VALUE; String r = null; for (String s : group) { int ed = editDistance(key,s); if( c>ed ) { c = ed; r = s; } } return r; }
/** * Finds the string in the <code>group</code> closest to * <code>key</code> and returns it. * * @return null if group.length==0. */ public static String findNearest( String key, Collection<String> group ) { int c = Integer.MAX_VALUE; String r = null; for (String s : group) { int ed = editDistance(key,s); if( c>ed ) { c = ed; r = s; } } return r; }
/** * Finds the string in the <code>group</code> closest to * <code>key</code> and returns it. * * @return null if group.length==0. */ public static String findNearest( String key, String[] group ) { return findNearest(key, Arrays.asList(group)); }
/** * Finds a type name that this context recognizes which is * "closest" to the given type name. * * <p> * This method is used for error recovery. */ public String getNearestTypeName(QName name) { String[] all = new String[typeMap.size()]; int i=0; for (QName qn : typeMap.keySet()) { if(qn.getLocalPart().equals(name.getLocalPart())) return qn.toString(); // probably a match, as people often gets confused about namespace. all[i++] = qn.toString(); } String nearest = EditDistance.findNearest(name.toString(), all); if(EditDistance.editDistance(nearest,name.toString())>10) return null; // too far apart. return nearest; }
/** * Computes the edit distance between two strings. * * <p> * The complexity is O(nm) where n=a.length() and m=b.length(). */ public static int editDistance( String a, String b ) { // let's check cache 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 Integer result = null; if (CACHE.containsKey(entry)) result = CACHE.get(entry); // looks like we have it if (result == null) { result = new EditDistance(a, b).calc(); CACHE.put(entry, result); // cache the result } return result; }
/** * Finds the string in the <code>group</code> closest to * <code>key</code> and returns it. * * @return null if group.length==0. */ public static String findNearest( String key, Collection<String> group ) { int c = Integer.MAX_VALUE; String r = null; for (String s : group) { int ed = editDistance(key,s); if( c>ed ) { c = ed; r = s; } } return r; }
/** * Finds the string in the <code>group</code> closest to * <code>key</code> and returns it. * * @return null if group.length==0. */ public static String findNearest( String key, String[] group ) { return findNearest(key, Arrays.asList(group)); }
/** * Finds a type name that this context recognizes which is * "closest" to the given type name. * * <p> * This method is used for error recovery. */ public String getNearestTypeName(QName name) { String[] all = new String[typeMap.size()]; int i=0; for (QName qn : typeMap.keySet()) { if(qn.getLocalPart().equals(name.getLocalPart())) return qn.toString(); // probably a match, as people often gets confused about namespace. all[i++] = qn.toString(); } String nearest = EditDistance.findNearest(name.toString(), all); if(EditDistance.editDistance(nearest,name.toString())>10) return null; // too far apart. return nearest; }
/** * Computes the edit distance between two strings. * * <p> * The complexity is O(nm) where n=a.length() and m=b.length(). */ public static int editDistance( String a, String b ) { // let's check cache 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 Integer result = null; if (CACHE.containsKey(entry)) result = CACHE.get(entry); // looks like we have it if (result == null) { result = new EditDistance(a, b).calc(); CACHE.put(entry, result); // cache the result } return result; }
/** * Finds the string in the <code>group</code> closest to * <code>key</code> and returns it. * * @return null if group.length==0. */ public static String findNearest( String key, Collection<String> group ) { int c = Integer.MAX_VALUE; String r = null; for (String s : group) { int ed = editDistance(key,s); if( c>ed ) { c = ed; r = s; } } return r; }
/** * Finds the string in the <code>group</code> closest to * <code>key</code> and returns it. * * @return null if group.length==0. */ public static String findNearest( String key, String[] group ) { return findNearest(key, Arrays.asList(group)); }
/** * Finds a type name that this context recognizes which is * "closest" to the given type name. * * <p> * This method is used for error recovery. */ public String getNearestTypeName(QName name) { String[] all = new String[typeMap.size()]; int i=0; for (QName qn : typeMap.keySet()) { if(qn.getLocalPart().equals(name.getLocalPart())) return qn.toString(); // probably a match, as people often gets confused about namespace. all[i++] = qn.toString(); } String nearest = EditDistance.findNearest(name.toString(), all); if(EditDistance.editDistance(nearest,name.toString())>10) return null; // too far apart. return nearest; }