commons-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ki...@apache.org
Subject svn commit: r947984 [16/36] - in /websites/production/commons/content/sandbox/commons-text: ./ apidocs/ apidocs/org/apache/commons/text/diff/ apidocs/org/apache/commons/text/diff/class-use/ apidocs/org/apache/commons/text/names/ apidocs/org/apache/comm...
Date Fri, 17 Apr 2015 06:46:32 GMT
Modified: websites/production/commons/content/sandbox/commons-text/apidocs/src-html/org/apache/commons/text/similarity/JaroWrinklerDistance.html
==============================================================================
--- websites/production/commons/content/sandbox/commons-text/apidocs/src-html/org/apache/commons/text/similarity/JaroWrinklerDistance.html (original)
+++ websites/production/commons/content/sandbox/commons-text/apidocs/src-html/org/apache/commons/text/similarity/JaroWrinklerDistance.html Fri Apr 17 06:46:28 2015
@@ -1,5 +1,5 @@
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
-<html lang="de">
+<html lang="en">
 <head>
 <title>Source code</title>
 <link rel="stylesheet" type="text/css" href="../../../../../../stylesheet.css" title="Style">
@@ -42,330 +42,339 @@
 <span class="sourceLineNo">034</span> * &lt;p&gt;<a name="line.34"></a>
 <span class="sourceLineNo">035</span> * This code has been adapted from Apache Commons Lang 3.3.<a name="line.35"></a>
 <span class="sourceLineNo">036</span> * &lt;/p&gt;<a name="line.36"></a>
-<span class="sourceLineNo">037</span> */<a name="line.37"></a>
-<span class="sourceLineNo">038</span>public class JaroWrinklerDistance implements StringMetric&lt;Double&gt; {<a name="line.38"></a>
-<span class="sourceLineNo">039</span><a name="line.39"></a>
-<span class="sourceLineNo">040</span>    /**<a name="line.40"></a>
-<span class="sourceLineNo">041</span>     * Represents a failed index search.<a name="line.41"></a>
-<span class="sourceLineNo">042</span>     */<a name="line.42"></a>
-<span class="sourceLineNo">043</span>    public static final int INDEX_NOT_FOUND = -1;<a name="line.43"></a>
-<span class="sourceLineNo">044</span><a name="line.44"></a>
-<span class="sourceLineNo">045</span>    /**<a name="line.45"></a>
-<span class="sourceLineNo">046</span>     * Find the Jaro Winkler Distance which indicates the similarity score<a name="line.46"></a>
-<span class="sourceLineNo">047</span>     * between two CharSequences.<a name="line.47"></a>
-<span class="sourceLineNo">048</span>     *<a name="line.48"></a>
-<span class="sourceLineNo">049</span>     * &lt;pre&gt;<a name="line.49"></a>
-<span class="sourceLineNo">050</span>     * distance.compare(null, null)          = IllegalArgumentException<a name="line.50"></a>
-<span class="sourceLineNo">051</span>     * distance.compare("","")               = 0.0<a name="line.51"></a>
-<span class="sourceLineNo">052</span>     * distance.compare("","a")              = 0.0<a name="line.52"></a>
-<span class="sourceLineNo">053</span>     * distance.compare("aaapppp", "")       = 0.0<a name="line.53"></a>
-<span class="sourceLineNo">054</span>     * distance.compare("frog", "fog")       = 0.93<a name="line.54"></a>
-<span class="sourceLineNo">055</span>     * distance.compare("fly", "ant")        = 0.0<a name="line.55"></a>
-<span class="sourceLineNo">056</span>     * distance.compare("elephant", "hippo") = 0.44<a name="line.56"></a>
-<span class="sourceLineNo">057</span>     * distance.compare("hippo", "elephant") = 0.44<a name="line.57"></a>
-<span class="sourceLineNo">058</span>     * distance.compare("hippo", "zzzzzzzz") = 0.0<a name="line.58"></a>
-<span class="sourceLineNo">059</span>     * distance.compare("hello", "hallo")    = 0.88<a name="line.59"></a>
-<span class="sourceLineNo">060</span>     * distance.compare("ABC Corporation", "ABC Corp") = 0.91<a name="line.60"></a>
-<span class="sourceLineNo">061</span>     * distance.compare("D N H Enterprises Inc", "D &amp;amp; H Enterprises, Inc.") = 0.93<a name="line.61"></a>
-<span class="sourceLineNo">062</span>     * distance.compare("My Gym Children's Fitness Center", "My Gym. Childrens Fitness") = 0.94<a name="line.62"></a>
-<span class="sourceLineNo">063</span>     * distance.compare("PENNSYLVANIA", "PENNCISYLVNIA")    = 0.9<a name="line.63"></a>
-<span class="sourceLineNo">064</span>     * &lt;/pre&gt;<a name="line.64"></a>
-<span class="sourceLineNo">065</span>     *<a name="line.65"></a>
-<span class="sourceLineNo">066</span>     * @param left the first String, must not be null<a name="line.66"></a>
-<span class="sourceLineNo">067</span>     * @param right the second String, must not be null<a name="line.67"></a>
-<span class="sourceLineNo">068</span>     * @return result distance<a name="line.68"></a>
-<span class="sourceLineNo">069</span>     * @throws IllegalArgumentException if either String input {@code null}<a name="line.69"></a>
-<span class="sourceLineNo">070</span>     */<a name="line.70"></a>
-<span class="sourceLineNo">071</span>    @Override<a name="line.71"></a>
-<span class="sourceLineNo">072</span>    public Double compare(CharSequence left, CharSequence right) {<a name="line.72"></a>
-<span class="sourceLineNo">073</span>        final double DEFAULT_SCALING_FACTOR = 0.1;<a name="line.73"></a>
-<span class="sourceLineNo">074</span><a name="line.74"></a>
-<span class="sourceLineNo">075</span>        if (left == null || right == null) {<a name="line.75"></a>
-<span class="sourceLineNo">076</span>            throw new IllegalArgumentException("Strings must not be null");<a name="line.76"></a>
-<span class="sourceLineNo">077</span>        }<a name="line.77"></a>
-<span class="sourceLineNo">078</span><a name="line.78"></a>
-<span class="sourceLineNo">079</span>        final double jaro = score(left, right);<a name="line.79"></a>
-<span class="sourceLineNo">080</span>        final int cl = commonPrefixLength(left, right);<a name="line.80"></a>
-<span class="sourceLineNo">081</span>        final double matchScore = Math.round((jaro + (DEFAULT_SCALING_FACTOR<a name="line.81"></a>
-<span class="sourceLineNo">082</span>                * cl * (1.0 - jaro))) *100.0)/100.0;<a name="line.82"></a>
-<span class="sourceLineNo">083</span><a name="line.83"></a>
-<span class="sourceLineNo">084</span>        return matchScore;<a name="line.84"></a>
-<span class="sourceLineNo">085</span>    }<a name="line.85"></a>
-<span class="sourceLineNo">086</span><a name="line.86"></a>
-<span class="sourceLineNo">087</span>    /**<a name="line.87"></a>
-<span class="sourceLineNo">088</span>     * Calculates the number of characters from the beginning of the strings<a name="line.88"></a>
-<span class="sourceLineNo">089</span>     * that match exactly one-to-one, up to a maximum of four (4) characters.<a name="line.89"></a>
-<span class="sourceLineNo">090</span>     *<a name="line.90"></a>
-<span class="sourceLineNo">091</span>     * @param first The first string.<a name="line.91"></a>
-<span class="sourceLineNo">092</span>     * @param second The second string.<a name="line.92"></a>
-<span class="sourceLineNo">093</span>     * @return A number between 0 and 4.<a name="line.93"></a>
-<span class="sourceLineNo">094</span>     */<a name="line.94"></a>
-<span class="sourceLineNo">095</span>    private static int commonPrefixLength(final CharSequence first,<a name="line.95"></a>
-<span class="sourceLineNo">096</span>            final CharSequence second) {<a name="line.96"></a>
-<span class="sourceLineNo">097</span>        final int result = getCommonPrefix(first.toString(), second.toString())<a name="line.97"></a>
-<span class="sourceLineNo">098</span>                .length();<a name="line.98"></a>
-<span class="sourceLineNo">099</span><a name="line.99"></a>
-<span class="sourceLineNo">100</span>        // Limit the result to 4.<a name="line.100"></a>
-<span class="sourceLineNo">101</span>        return result &gt; 4 ? 4 : result;<a name="line.101"></a>
-<span class="sourceLineNo">102</span>    }<a name="line.102"></a>
-<span class="sourceLineNo">103</span><a name="line.103"></a>
-<span class="sourceLineNo">104</span>    /**<a name="line.104"></a>
-<span class="sourceLineNo">105</span>     * Compares all Strings in an array and returns the initial sequence of<a name="line.105"></a>
-<span class="sourceLineNo">106</span>     * characters that is common to all of them.<a name="line.106"></a>
-<span class="sourceLineNo">107</span>     *<a name="line.107"></a>
-<span class="sourceLineNo">108</span>     * &lt;p&gt;<a name="line.108"></a>
-<span class="sourceLineNo">109</span>     * For example,<a name="line.109"></a>
-<span class="sourceLineNo">110</span>     * &lt;code&gt;getCommonPrefix(new String[] {"i am a machine", "i am a robot"}) -&amp;gt; "i am a "&lt;/code&gt;<a name="line.110"></a>
-<span class="sourceLineNo">111</span>     * &lt;/p&gt;<a name="line.111"></a>
-<span class="sourceLineNo">112</span>     *<a name="line.112"></a>
-<span class="sourceLineNo">113</span>     * &lt;pre&gt;<a name="line.113"></a>
-<span class="sourceLineNo">114</span>     * getCommonPrefix(null) = ""<a name="line.114"></a>
-<span class="sourceLineNo">115</span>     * getCommonPrefix(new String[] {}) = ""<a name="line.115"></a>
-<span class="sourceLineNo">116</span>     * getCommonPrefix(new String[] {"abc"}) = "abc"<a name="line.116"></a>
-<span class="sourceLineNo">117</span>     * getCommonPrefix(new String[] {null, null}) = ""<a name="line.117"></a>
-<span class="sourceLineNo">118</span>     * getCommonPrefix(new String[] {"", ""}) = ""<a name="line.118"></a>
-<span class="sourceLineNo">119</span>     * getCommonPrefix(new String[] {"", null}) = ""<a name="line.119"></a>
-<span class="sourceLineNo">120</span>     * getCommonPrefix(new String[] {"abc", null, null}) = ""<a name="line.120"></a>
-<span class="sourceLineNo">121</span>     * getCommonPrefix(new String[] {null, null, "abc"}) = ""<a name="line.121"></a>
-<span class="sourceLineNo">122</span>     * getCommonPrefix(new String[] {"", "abc"}) = ""<a name="line.122"></a>
-<span class="sourceLineNo">123</span>     * getCommonPrefix(new String[] {"abc", ""}) = ""<a name="line.123"></a>
-<span class="sourceLineNo">124</span>     * getCommonPrefix(new String[] {"abc", "abc"}) = "abc"<a name="line.124"></a>
-<span class="sourceLineNo">125</span>     * getCommonPrefix(new String[] {"abc", "a"}) = "a"<a name="line.125"></a>
-<span class="sourceLineNo">126</span>     * getCommonPrefix(new String[] {"ab", "abxyz"}) = "ab"<a name="line.126"></a>
-<span class="sourceLineNo">127</span>     * getCommonPrefix(new String[] {"abcde", "abxyz"}) = "ab"<a name="line.127"></a>
-<span class="sourceLineNo">128</span>     * getCommonPrefix(new String[] {"abcde", "xyz"}) = ""<a name="line.128"></a>
-<span class="sourceLineNo">129</span>     * getCommonPrefix(new String[] {"xyz", "abcde"}) = ""<a name="line.129"></a>
-<span class="sourceLineNo">130</span>     * getCommonPrefix(new String[] {"i am a machine", "i am a robot"}) = "i am a "<a name="line.130"></a>
-<span class="sourceLineNo">131</span>     * &lt;/pre&gt;<a name="line.131"></a>
-<span class="sourceLineNo">132</span>     *<a name="line.132"></a>
-<span class="sourceLineNo">133</span>     * @param strs array of String objects, entries may be null<a name="line.133"></a>
-<span class="sourceLineNo">134</span>     * @return the initial sequence of characters that are common to all Strings<a name="line.134"></a>
-<span class="sourceLineNo">135</span>     *         in the array; empty String if the array is null, the elements are<a name="line.135"></a>
-<span class="sourceLineNo">136</span>     *         all null or if there is no common prefix.<a name="line.136"></a>
-<span class="sourceLineNo">137</span>     */<a name="line.137"></a>
-<span class="sourceLineNo">138</span>    public static String getCommonPrefix(final String... strs) {<a name="line.138"></a>
-<span class="sourceLineNo">139</span>        if (strs == null || strs.length == 0) {<a name="line.139"></a>
-<span class="sourceLineNo">140</span>            return "";<a name="line.140"></a>
-<span class="sourceLineNo">141</span>        }<a name="line.141"></a>
-<span class="sourceLineNo">142</span>        final int smallestIndexOfDiff = indexOfDifference(strs);<a name="line.142"></a>
-<span class="sourceLineNo">143</span>        if (smallestIndexOfDiff == INDEX_NOT_FOUND) {<a name="line.143"></a>
-<span class="sourceLineNo">144</span>            // all strings were identical<a name="line.144"></a>
-<span class="sourceLineNo">145</span>            if (strs[0] == null) {<a name="line.145"></a>
-<span class="sourceLineNo">146</span>                return "";<a name="line.146"></a>
-<span class="sourceLineNo">147</span>            }<a name="line.147"></a>
-<span class="sourceLineNo">148</span>            return strs[0];<a name="line.148"></a>
-<span class="sourceLineNo">149</span>        } else if (smallestIndexOfDiff == 0) {<a name="line.149"></a>
-<span class="sourceLineNo">150</span>            // there were no common initial characters<a name="line.150"></a>
-<span class="sourceLineNo">151</span>            return "";<a name="line.151"></a>
-<span class="sourceLineNo">152</span>        } else {<a name="line.152"></a>
-<span class="sourceLineNo">153</span>            // we found a common initial character sequence<a name="line.153"></a>
-<span class="sourceLineNo">154</span>            return strs[0].substring(0, smallestIndexOfDiff);<a name="line.154"></a>
-<span class="sourceLineNo">155</span>        }<a name="line.155"></a>
-<span class="sourceLineNo">156</span>    }<a name="line.156"></a>
-<span class="sourceLineNo">157</span><a name="line.157"></a>
-<span class="sourceLineNo">158</span>    /**<a name="line.158"></a>
-<span class="sourceLineNo">159</span>     * This method returns the Jaro-Winkler score for string matching.<a name="line.159"></a>
-<span class="sourceLineNo">160</span>     *<a name="line.160"></a>
-<span class="sourceLineNo">161</span>     * @param first the first string to be matched<a name="line.161"></a>
-<span class="sourceLineNo">162</span>     * @param second the second string to be machted<a name="line.162"></a>
-<span class="sourceLineNo">163</span>     * @return matching score without scaling factor impact<a name="line.163"></a>
-<span class="sourceLineNo">164</span>     */<a name="line.164"></a>
-<span class="sourceLineNo">165</span>    protected static double score(final CharSequence first,<a name="line.165"></a>
-<span class="sourceLineNo">166</span>            final CharSequence second) {<a name="line.166"></a>
-<span class="sourceLineNo">167</span>        String shorter;<a name="line.167"></a>
-<span class="sourceLineNo">168</span>        String longer;<a name="line.168"></a>
-<span class="sourceLineNo">169</span><a name="line.169"></a>
-<span class="sourceLineNo">170</span>        // Determine which String is longer.<a name="line.170"></a>
-<span class="sourceLineNo">171</span>        if (first.length() &gt; second.length()) {<a name="line.171"></a>
-<span class="sourceLineNo">172</span>            longer = first.toString().toLowerCase();<a name="line.172"></a>
-<span class="sourceLineNo">173</span>            shorter = second.toString().toLowerCase();<a name="line.173"></a>
-<span class="sourceLineNo">174</span>        } else {<a name="line.174"></a>
-<span class="sourceLineNo">175</span>            longer = second.toString().toLowerCase();<a name="line.175"></a>
-<span class="sourceLineNo">176</span>            shorter = first.toString().toLowerCase();<a name="line.176"></a>
-<span class="sourceLineNo">177</span>        }<a name="line.177"></a>
-<span class="sourceLineNo">178</span><a name="line.178"></a>
-<span class="sourceLineNo">179</span>        // Calculate the half length() distance of the shorter String.<a name="line.179"></a>
-<span class="sourceLineNo">180</span>        final int halflength = shorter.length() / 2 + 1;<a name="line.180"></a>
-<span class="sourceLineNo">181</span><a name="line.181"></a>
-<span class="sourceLineNo">182</span>        // Find the set of matching characters between the shorter and longer<a name="line.182"></a>
-<span class="sourceLineNo">183</span>        // strings. Note that<a name="line.183"></a>
-<span class="sourceLineNo">184</span>        // the set of matching characters may be different depending on the<a name="line.184"></a>
-<span class="sourceLineNo">185</span>        // order of the strings.<a name="line.185"></a>
-<span class="sourceLineNo">186</span>        final String m1 = getSetOfMatchingCharacterWithin(shorter, longer,<a name="line.186"></a>
-<span class="sourceLineNo">187</span>                halflength);<a name="line.187"></a>
-<span class="sourceLineNo">188</span>        final String m2 = getSetOfMatchingCharacterWithin(longer, shorter,<a name="line.188"></a>
-<span class="sourceLineNo">189</span>                halflength);<a name="line.189"></a>
-<span class="sourceLineNo">190</span><a name="line.190"></a>
-<span class="sourceLineNo">191</span>        // If one or both of the sets of common characters is empty, then<a name="line.191"></a>
-<span class="sourceLineNo">192</span>        // there is no similarity between the two strings.<a name="line.192"></a>
-<span class="sourceLineNo">193</span>        if (m1.length() == 0 || m2.length() == 0) {<a name="line.193"></a>
-<span class="sourceLineNo">194</span>            return 0.0;<a name="line.194"></a>
-<span class="sourceLineNo">195</span>        }<a name="line.195"></a>
-<span class="sourceLineNo">196</span><a name="line.196"></a>
-<span class="sourceLineNo">197</span>        // If the set of common characters is not the same size, then<a name="line.197"></a>
-<span class="sourceLineNo">198</span>        // there is no similarity between the two strings, either.<a name="line.198"></a>
-<span class="sourceLineNo">199</span>        if (m1.length() != m2.length()) {<a name="line.199"></a>
-<span class="sourceLineNo">200</span>            return 0.0;<a name="line.200"></a>
-<span class="sourceLineNo">201</span>        }<a name="line.201"></a>
-<span class="sourceLineNo">202</span><a name="line.202"></a>
-<span class="sourceLineNo">203</span>        // Calculate the number of transposition between the two sets<a name="line.203"></a>
-<span class="sourceLineNo">204</span>        // of common characters.<a name="line.204"></a>
-<span class="sourceLineNo">205</span>        final int transpositions = transpositions(m1, m2);<a name="line.205"></a>
-<span class="sourceLineNo">206</span><a name="line.206"></a>
-<span class="sourceLineNo">207</span>        // Calculate the distance.<a name="line.207"></a>
-<span class="sourceLineNo">208</span>        final double dist = (m1.length() / ((double) shorter.length())<a name="line.208"></a>
-<span class="sourceLineNo">209</span>                + m2.length() / ((double) longer.length()) + (m1.length() - transpositions)<a name="line.209"></a>
-<span class="sourceLineNo">210</span>                / ((double) m1.length())) / 3.0;<a name="line.210"></a>
-<span class="sourceLineNo">211</span>        return dist;<a name="line.211"></a>
-<span class="sourceLineNo">212</span>    }<a name="line.212"></a>
+<span class="sourceLineNo">037</span> *<a name="line.37"></a>
+<span class="sourceLineNo">038</span> * @since 1.0<a name="line.38"></a>
+<span class="sourceLineNo">039</span> */<a name="line.39"></a>
+<span class="sourceLineNo">040</span>public class JaroWrinklerDistance implements EditDistance&lt;Double&gt; {<a name="line.40"></a>
+<span class="sourceLineNo">041</span><a name="line.41"></a>
+<span class="sourceLineNo">042</span>    /**<a name="line.42"></a>
+<span class="sourceLineNo">043</span>     * The default prefix length limit set to four.<a name="line.43"></a>
+<span class="sourceLineNo">044</span>     */<a name="line.44"></a>
+<span class="sourceLineNo">045</span>    private static final int PREFIX_LENGTH_LIMIT = 4;<a name="line.45"></a>
+<span class="sourceLineNo">046</span>    /**<a name="line.46"></a>
+<span class="sourceLineNo">047</span>     * Represents a failed index search.<a name="line.47"></a>
+<span class="sourceLineNo">048</span>     */<a name="line.48"></a>
+<span class="sourceLineNo">049</span>    public static final int INDEX_NOT_FOUND = -1;<a name="line.49"></a>
+<span class="sourceLineNo">050</span><a name="line.50"></a>
+<span class="sourceLineNo">051</span>    /**<a name="line.51"></a>
+<span class="sourceLineNo">052</span>     * Find the Jaro Winkler Distance which indicates the similarity score<a name="line.52"></a>
+<span class="sourceLineNo">053</span>     * between two CharSequences.<a name="line.53"></a>
+<span class="sourceLineNo">054</span>     *<a name="line.54"></a>
+<span class="sourceLineNo">055</span>     * &lt;pre&gt;<a name="line.55"></a>
+<span class="sourceLineNo">056</span>     * distance.apply(null, null)          = IllegalArgumentException<a name="line.56"></a>
+<span class="sourceLineNo">057</span>     * distance.apply("","")               = 0.0<a name="line.57"></a>
+<span class="sourceLineNo">058</span>     * distance.apply("","a")              = 0.0<a name="line.58"></a>
+<span class="sourceLineNo">059</span>     * distance.apply("aaapppp", "")       = 0.0<a name="line.59"></a>
+<span class="sourceLineNo">060</span>     * distance.apply("frog", "fog")       = 0.93<a name="line.60"></a>
+<span class="sourceLineNo">061</span>     * distance.apply("fly", "ant")        = 0.0<a name="line.61"></a>
+<span class="sourceLineNo">062</span>     * distance.apply("elephant", "hippo") = 0.44<a name="line.62"></a>
+<span class="sourceLineNo">063</span>     * distance.apply("hippo", "elephant") = 0.44<a name="line.63"></a>
+<span class="sourceLineNo">064</span>     * distance.apply("hippo", "zzzzzzzz") = 0.0<a name="line.64"></a>
+<span class="sourceLineNo">065</span>     * distance.apply("hello", "hallo")    = 0.88<a name="line.65"></a>
+<span class="sourceLineNo">066</span>     * distance.apply("ABC Corporation", "ABC Corp") = 0.91<a name="line.66"></a>
+<span class="sourceLineNo">067</span>     * distance.apply("D N H Enterprises Inc", "D &amp;amp; H Enterprises, Inc.") = 0.93<a name="line.67"></a>
+<span class="sourceLineNo">068</span>     * distance.apply("My Gym Children's Fitness Center", "My Gym. Childrens Fitness") = 0.94<a name="line.68"></a>
+<span class="sourceLineNo">069</span>     * distance.apply("PENNSYLVANIA", "PENNCISYLVNIA")    = 0.9<a name="line.69"></a>
+<span class="sourceLineNo">070</span>     * &lt;/pre&gt;<a name="line.70"></a>
+<span class="sourceLineNo">071</span>     *<a name="line.71"></a>
+<span class="sourceLineNo">072</span>     * @param left the first String, must not be null<a name="line.72"></a>
+<span class="sourceLineNo">073</span>     * @param right the second String, must not be null<a name="line.73"></a>
+<span class="sourceLineNo">074</span>     * @return result distance<a name="line.74"></a>
+<span class="sourceLineNo">075</span>     * @throws IllegalArgumentException if either String input {@code null}<a name="line.75"></a>
+<span class="sourceLineNo">076</span>     */<a name="line.76"></a>
+<span class="sourceLineNo">077</span>    @Override<a name="line.77"></a>
+<span class="sourceLineNo">078</span>    public Double apply(CharSequence left, CharSequence right) {<a name="line.78"></a>
+<span class="sourceLineNo">079</span>        final double defaultScalingFactor = 0.1;<a name="line.79"></a>
+<span class="sourceLineNo">080</span>        final double percentageRoundValue = 100.0;<a name="line.80"></a>
+<span class="sourceLineNo">081</span><a name="line.81"></a>
+<span class="sourceLineNo">082</span>        if (left == null || right == null) {<a name="line.82"></a>
+<span class="sourceLineNo">083</span>            throw new IllegalArgumentException("Strings must not be null");<a name="line.83"></a>
+<span class="sourceLineNo">084</span>        }<a name="line.84"></a>
+<span class="sourceLineNo">085</span><a name="line.85"></a>
+<span class="sourceLineNo">086</span>        final double jaro = score(left, right);<a name="line.86"></a>
+<span class="sourceLineNo">087</span>        final int cl = commonPrefixLength(left, right);<a name="line.87"></a>
+<span class="sourceLineNo">088</span>        final double matchScore = Math.round((jaro + defaultScalingFactor<a name="line.88"></a>
+<span class="sourceLineNo">089</span>                * cl * (1.0 - jaro)) * percentageRoundValue) / percentageRoundValue;<a name="line.89"></a>
+<span class="sourceLineNo">090</span><a name="line.90"></a>
+<span class="sourceLineNo">091</span>        return matchScore;<a name="line.91"></a>
+<span class="sourceLineNo">092</span>    }<a name="line.92"></a>
+<span class="sourceLineNo">093</span><a name="line.93"></a>
+<span class="sourceLineNo">094</span>    /**<a name="line.94"></a>
+<span class="sourceLineNo">095</span>     * Calculates the number of characters from the beginning of the strings<a name="line.95"></a>
+<span class="sourceLineNo">096</span>     * that match exactly one-to-one, up to a maximum of four (4) characters.<a name="line.96"></a>
+<span class="sourceLineNo">097</span>     *<a name="line.97"></a>
+<span class="sourceLineNo">098</span>     * @param first The first string.<a name="line.98"></a>
+<span class="sourceLineNo">099</span>     * @param second The second string.<a name="line.99"></a>
+<span class="sourceLineNo">100</span>     * @return A number between 0 and 4.<a name="line.100"></a>
+<span class="sourceLineNo">101</span>     */<a name="line.101"></a>
+<span class="sourceLineNo">102</span>    private static int commonPrefixLength(final CharSequence first,<a name="line.102"></a>
+<span class="sourceLineNo">103</span>            final CharSequence second) {<a name="line.103"></a>
+<span class="sourceLineNo">104</span>        final int result = getCommonPrefix(first.toString(), second.toString())<a name="line.104"></a>
+<span class="sourceLineNo">105</span>                .length();<a name="line.105"></a>
+<span class="sourceLineNo">106</span><a name="line.106"></a>
+<span class="sourceLineNo">107</span>        // Limit the result to 4.<a name="line.107"></a>
+<span class="sourceLineNo">108</span>        return result &gt; PREFIX_LENGTH_LIMIT ? PREFIX_LENGTH_LIMIT : result;<a name="line.108"></a>
+<span class="sourceLineNo">109</span>    }<a name="line.109"></a>
+<span class="sourceLineNo">110</span><a name="line.110"></a>
+<span class="sourceLineNo">111</span>    /**<a name="line.111"></a>
+<span class="sourceLineNo">112</span>     * Compares all Strings in an array and returns the initial sequence of<a name="line.112"></a>
+<span class="sourceLineNo">113</span>     * characters that is common to all of them.<a name="line.113"></a>
+<span class="sourceLineNo">114</span>     *<a name="line.114"></a>
+<span class="sourceLineNo">115</span>     * &lt;p&gt;<a name="line.115"></a>
+<span class="sourceLineNo">116</span>     * For example,<a name="line.116"></a>
+<span class="sourceLineNo">117</span>     * &lt;code&gt;getCommonPrefix(new String[] {"i am a machine", "i am a robot"}) -&amp;gt; "i am a "&lt;/code&gt;<a name="line.117"></a>
+<span class="sourceLineNo">118</span>     * &lt;/p&gt;<a name="line.118"></a>
+<span class="sourceLineNo">119</span>     *<a name="line.119"></a>
+<span class="sourceLineNo">120</span>     * &lt;pre&gt;<a name="line.120"></a>
+<span class="sourceLineNo">121</span>     * getCommonPrefix(null) = ""<a name="line.121"></a>
+<span class="sourceLineNo">122</span>     * getCommonPrefix(new String[] {}) = ""<a name="line.122"></a>
+<span class="sourceLineNo">123</span>     * getCommonPrefix(new String[] {"abc"}) = "abc"<a name="line.123"></a>
+<span class="sourceLineNo">124</span>     * getCommonPrefix(new String[] {null, null}) = ""<a name="line.124"></a>
+<span class="sourceLineNo">125</span>     * getCommonPrefix(new String[] {"", ""}) = ""<a name="line.125"></a>
+<span class="sourceLineNo">126</span>     * getCommonPrefix(new String[] {"", null}) = ""<a name="line.126"></a>
+<span class="sourceLineNo">127</span>     * getCommonPrefix(new String[] {"abc", null, null}) = ""<a name="line.127"></a>
+<span class="sourceLineNo">128</span>     * getCommonPrefix(new String[] {null, null, "abc"}) = ""<a name="line.128"></a>
+<span class="sourceLineNo">129</span>     * getCommonPrefix(new String[] {"", "abc"}) = ""<a name="line.129"></a>
+<span class="sourceLineNo">130</span>     * getCommonPrefix(new String[] {"abc", ""}) = ""<a name="line.130"></a>
+<span class="sourceLineNo">131</span>     * getCommonPrefix(new String[] {"abc", "abc"}) = "abc"<a name="line.131"></a>
+<span class="sourceLineNo">132</span>     * getCommonPrefix(new String[] {"abc", "a"}) = "a"<a name="line.132"></a>
+<span class="sourceLineNo">133</span>     * getCommonPrefix(new String[] {"ab", "abxyz"}) = "ab"<a name="line.133"></a>
+<span class="sourceLineNo">134</span>     * getCommonPrefix(new String[] {"abcde", "abxyz"}) = "ab"<a name="line.134"></a>
+<span class="sourceLineNo">135</span>     * getCommonPrefix(new String[] {"abcde", "xyz"}) = ""<a name="line.135"></a>
+<span class="sourceLineNo">136</span>     * getCommonPrefix(new String[] {"xyz", "abcde"}) = ""<a name="line.136"></a>
+<span class="sourceLineNo">137</span>     * getCommonPrefix(new String[] {"i am a machine", "i am a robot"}) = "i am a "<a name="line.137"></a>
+<span class="sourceLineNo">138</span>     * &lt;/pre&gt;<a name="line.138"></a>
+<span class="sourceLineNo">139</span>     *<a name="line.139"></a>
+<span class="sourceLineNo">140</span>     * @param strs array of String objects, entries may be null<a name="line.140"></a>
+<span class="sourceLineNo">141</span>     * @return the initial sequence of characters that are common to all Strings<a name="line.141"></a>
+<span class="sourceLineNo">142</span>     *         in the array; empty String if the array is null, the elements are<a name="line.142"></a>
+<span class="sourceLineNo">143</span>     *         all null or if there is no common prefix.<a name="line.143"></a>
+<span class="sourceLineNo">144</span>     */<a name="line.144"></a>
+<span class="sourceLineNo">145</span>    public static String getCommonPrefix(final String... strs) {<a name="line.145"></a>
+<span class="sourceLineNo">146</span>        if (strs == null || strs.length == 0) {<a name="line.146"></a>
+<span class="sourceLineNo">147</span>            return "";<a name="line.147"></a>
+<span class="sourceLineNo">148</span>        }<a name="line.148"></a>
+<span class="sourceLineNo">149</span>        final int smallestIndexOfDiff = indexOfDifference(strs);<a name="line.149"></a>
+<span class="sourceLineNo">150</span>        if (smallestIndexOfDiff == INDEX_NOT_FOUND) {<a name="line.150"></a>
+<span class="sourceLineNo">151</span>            // all strings were identical<a name="line.151"></a>
+<span class="sourceLineNo">152</span>            if (strs[0] == null) {<a name="line.152"></a>
+<span class="sourceLineNo">153</span>                return "";<a name="line.153"></a>
+<span class="sourceLineNo">154</span>            }<a name="line.154"></a>
+<span class="sourceLineNo">155</span>            return strs[0];<a name="line.155"></a>
+<span class="sourceLineNo">156</span>        } else if (smallestIndexOfDiff == 0) {<a name="line.156"></a>
+<span class="sourceLineNo">157</span>            // there were no common initial characters<a name="line.157"></a>
+<span class="sourceLineNo">158</span>            return "";<a name="line.158"></a>
+<span class="sourceLineNo">159</span>        } else {<a name="line.159"></a>
+<span class="sourceLineNo">160</span>            // we found a common initial character sequence<a name="line.160"></a>
+<span class="sourceLineNo">161</span>            return strs[0].substring(0, smallestIndexOfDiff);<a name="line.161"></a>
+<span class="sourceLineNo">162</span>        }<a name="line.162"></a>
+<span class="sourceLineNo">163</span>    }<a name="line.163"></a>
+<span class="sourceLineNo">164</span><a name="line.164"></a>
+<span class="sourceLineNo">165</span>    /**<a name="line.165"></a>
+<span class="sourceLineNo">166</span>     * This method returns the Jaro-Winkler score for string matching.<a name="line.166"></a>
+<span class="sourceLineNo">167</span>     *<a name="line.167"></a>
+<span class="sourceLineNo">168</span>     * @param first the first string to be matched<a name="line.168"></a>
+<span class="sourceLineNo">169</span>     * @param second the second string to be machted<a name="line.169"></a>
+<span class="sourceLineNo">170</span>     * @return matching score without scaling factor impact<a name="line.170"></a>
+<span class="sourceLineNo">171</span>     */<a name="line.171"></a>
+<span class="sourceLineNo">172</span>    protected static double score(final CharSequence first,<a name="line.172"></a>
+<span class="sourceLineNo">173</span>            final CharSequence second) {<a name="line.173"></a>
+<span class="sourceLineNo">174</span>        String shorter;<a name="line.174"></a>
+<span class="sourceLineNo">175</span>        String longer;<a name="line.175"></a>
+<span class="sourceLineNo">176</span><a name="line.176"></a>
+<span class="sourceLineNo">177</span>        // Determine which String is longer.<a name="line.177"></a>
+<span class="sourceLineNo">178</span>        if (first.length() &gt; second.length()) {<a name="line.178"></a>
+<span class="sourceLineNo">179</span>            longer = first.toString().toLowerCase();<a name="line.179"></a>
+<span class="sourceLineNo">180</span>            shorter = second.toString().toLowerCase();<a name="line.180"></a>
+<span class="sourceLineNo">181</span>        } else {<a name="line.181"></a>
+<span class="sourceLineNo">182</span>            longer = second.toString().toLowerCase();<a name="line.182"></a>
+<span class="sourceLineNo">183</span>            shorter = first.toString().toLowerCase();<a name="line.183"></a>
+<span class="sourceLineNo">184</span>        }<a name="line.184"></a>
+<span class="sourceLineNo">185</span><a name="line.185"></a>
+<span class="sourceLineNo">186</span>        // Calculate the half length() distance of the shorter String.<a name="line.186"></a>
+<span class="sourceLineNo">187</span>        final int halflength = shorter.length() / 2 + 1;<a name="line.187"></a>
+<span class="sourceLineNo">188</span><a name="line.188"></a>
+<span class="sourceLineNo">189</span>        // Find the set of matching characters between the shorter and longer<a name="line.189"></a>
+<span class="sourceLineNo">190</span>        // strings. Note that<a name="line.190"></a>
+<span class="sourceLineNo">191</span>        // the set of matching characters may be different depending on the<a name="line.191"></a>
+<span class="sourceLineNo">192</span>        // order of the strings.<a name="line.192"></a>
+<span class="sourceLineNo">193</span>        final String m1 = getSetOfMatchingCharacterWithin(shorter, longer,<a name="line.193"></a>
+<span class="sourceLineNo">194</span>                halflength);<a name="line.194"></a>
+<span class="sourceLineNo">195</span>        final String m2 = getSetOfMatchingCharacterWithin(longer, shorter,<a name="line.195"></a>
+<span class="sourceLineNo">196</span>                halflength);<a name="line.196"></a>
+<span class="sourceLineNo">197</span><a name="line.197"></a>
+<span class="sourceLineNo">198</span>        // If one or both of the sets of common characters is empty, then<a name="line.198"></a>
+<span class="sourceLineNo">199</span>        // there is no similarity between the two strings.<a name="line.199"></a>
+<span class="sourceLineNo">200</span>        if (m1.length() == 0 || m2.length() == 0) {<a name="line.200"></a>
+<span class="sourceLineNo">201</span>            return 0.0;<a name="line.201"></a>
+<span class="sourceLineNo">202</span>        }<a name="line.202"></a>
+<span class="sourceLineNo">203</span><a name="line.203"></a>
+<span class="sourceLineNo">204</span>        // If the set of common characters is not the same size, then<a name="line.204"></a>
+<span class="sourceLineNo">205</span>        // there is no similarity between the two strings, either.<a name="line.205"></a>
+<span class="sourceLineNo">206</span>        if (m1.length() != m2.length()) {<a name="line.206"></a>
+<span class="sourceLineNo">207</span>            return 0.0;<a name="line.207"></a>
+<span class="sourceLineNo">208</span>        }<a name="line.208"></a>
+<span class="sourceLineNo">209</span><a name="line.209"></a>
+<span class="sourceLineNo">210</span>        // Calculate the number of transposition between the two sets<a name="line.210"></a>
+<span class="sourceLineNo">211</span>        // of common characters.<a name="line.211"></a>
+<span class="sourceLineNo">212</span>        final int transpositions = transpositions(m1, m2);<a name="line.212"></a>
 <span class="sourceLineNo">213</span><a name="line.213"></a>
-<span class="sourceLineNo">214</span>    /**<a name="line.214"></a>
-<span class="sourceLineNo">215</span>     * Calculates the number of transposition between two strings.<a name="line.215"></a>
-<span class="sourceLineNo">216</span>     *<a name="line.216"></a>
-<span class="sourceLineNo">217</span>     * @param first The first string.<a name="line.217"></a>
-<span class="sourceLineNo">218</span>     * @param second The second string.<a name="line.218"></a>
-<span class="sourceLineNo">219</span>     * @return The number of transposition between the two strings.<a name="line.219"></a>
-<span class="sourceLineNo">220</span>     */<a name="line.220"></a>
-<span class="sourceLineNo">221</span>    protected static int transpositions(final CharSequence first,<a name="line.221"></a>
-<span class="sourceLineNo">222</span>            final CharSequence second) {<a name="line.222"></a>
-<span class="sourceLineNo">223</span>        int transpositions = 0;<a name="line.223"></a>
-<span class="sourceLineNo">224</span>        for (int i = 0; i &lt; first.length(); i++) {<a name="line.224"></a>
-<span class="sourceLineNo">225</span>            if (first.charAt(i) != second.charAt(i)) {<a name="line.225"></a>
-<span class="sourceLineNo">226</span>                transpositions++;<a name="line.226"></a>
-<span class="sourceLineNo">227</span>            }<a name="line.227"></a>
-<span class="sourceLineNo">228</span>        }<a name="line.228"></a>
-<span class="sourceLineNo">229</span>        return transpositions / 2;<a name="line.229"></a>
-<span class="sourceLineNo">230</span>    }<a name="line.230"></a>
-<span class="sourceLineNo">231</span><a name="line.231"></a>
-<span class="sourceLineNo">232</span>    /**<a name="line.232"></a>
-<span class="sourceLineNo">233</span>     * Compares all CharSequences in an array and returns the index at which the<a name="line.233"></a>
-<span class="sourceLineNo">234</span>     * CharSequences begin to differ.<a name="line.234"></a>
-<span class="sourceLineNo">235</span>     *<a name="line.235"></a>
-<span class="sourceLineNo">236</span>     * &lt;p&gt;<a name="line.236"></a>
-<span class="sourceLineNo">237</span>     * For example,<a name="line.237"></a>
-<span class="sourceLineNo">238</span>     * &lt;code&gt;indexOfDifference(new String[] {"i am a machine", "i am a robot"}) -&amp;gt; 7&lt;/code&gt;<a name="line.238"></a>
-<span class="sourceLineNo">239</span>     * &lt;/p&gt;<a name="line.239"></a>
-<span class="sourceLineNo">240</span>     *<a name="line.240"></a>
-<span class="sourceLineNo">241</span>     * &lt;pre&gt;<a name="line.241"></a>
-<span class="sourceLineNo">242</span>     * distance.indexOfDifference(null) = -1<a name="line.242"></a>
-<span class="sourceLineNo">243</span>     * distance.indexOfDifference(new String[] {}) = -1<a name="line.243"></a>
-<span class="sourceLineNo">244</span>     * distance.indexOfDifference(new String[] {"abc"}) = -1<a name="line.244"></a>
-<span class="sourceLineNo">245</span>     * distance.indexOfDifference(new String[] {null, null}) = -1<a name="line.245"></a>
-<span class="sourceLineNo">246</span>     * distance.indexOfDifference(new String[] {"", ""}) = -1<a name="line.246"></a>
-<span class="sourceLineNo">247</span>     * distance.indexOfDifference(new String[] {"", null}) = 0<a name="line.247"></a>
-<span class="sourceLineNo">248</span>     * distance.indexOfDifference(new String[] {"abc", null, null}) = 0<a name="line.248"></a>
-<span class="sourceLineNo">249</span>     * distance.indexOfDifference(new String[] {null, null, "abc"}) = 0<a name="line.249"></a>
-<span class="sourceLineNo">250</span>     * distance.indexOfDifference(new String[] {"", "abc"}) = 0<a name="line.250"></a>
-<span class="sourceLineNo">251</span>     * distance.indexOfDifference(new String[] {"abc", ""}) = 0<a name="line.251"></a>
-<span class="sourceLineNo">252</span>     * distance.indexOfDifference(new String[] {"abc", "abc"}) = -1<a name="line.252"></a>
-<span class="sourceLineNo">253</span>     * distance.indexOfDifference(new String[] {"abc", "a"}) = 1<a name="line.253"></a>
-<span class="sourceLineNo">254</span>     * distance.indexOfDifference(new String[] {"ab", "abxyz"}) = 2<a name="line.254"></a>
-<span class="sourceLineNo">255</span>     * distance.indexOfDifference(new String[] {"abcde", "abxyz"}) = 2<a name="line.255"></a>
-<span class="sourceLineNo">256</span>     * distance.indexOfDifference(new String[] {"abcde", "xyz"}) = 0<a name="line.256"></a>
-<span class="sourceLineNo">257</span>     * distance.indexOfDifference(new String[] {"xyz", "abcde"}) = 0<a name="line.257"></a>
-<span class="sourceLineNo">258</span>     * distance.indexOfDifference(new String[] {"i am a machine", "i am a robot"}) = 7<a name="line.258"></a>
-<span class="sourceLineNo">259</span>     * &lt;/pre&gt;<a name="line.259"></a>
-<span class="sourceLineNo">260</span>     *<a name="line.260"></a>
-<span class="sourceLineNo">261</span>     * @param css array of CharSequences, entries may be null<a name="line.261"></a>
-<span class="sourceLineNo">262</span>     * @return the index where the strings begin to differ; -1 if they are all<a name="line.262"></a>
-<span class="sourceLineNo">263</span>     *         equal<a name="line.263"></a>
-<span class="sourceLineNo">264</span>     */<a name="line.264"></a>
-<span class="sourceLineNo">265</span>    protected static int indexOfDifference(final CharSequence... css) {<a name="line.265"></a>
-<span class="sourceLineNo">266</span>        if (css == null || css.length &lt;= 1) {<a name="line.266"></a>
-<span class="sourceLineNo">267</span>            return INDEX_NOT_FOUND;<a name="line.267"></a>
-<span class="sourceLineNo">268</span>        }<a name="line.268"></a>
-<span class="sourceLineNo">269</span>        boolean anyStringNull = false;<a name="line.269"></a>
-<span class="sourceLineNo">270</span>        boolean allStringsNull = true;<a name="line.270"></a>
-<span class="sourceLineNo">271</span>        final int arrayLen = css.length;<a name="line.271"></a>
-<span class="sourceLineNo">272</span>        int shortestStrLen = Integer.MAX_VALUE;<a name="line.272"></a>
-<span class="sourceLineNo">273</span>        int longestStrLen = 0;<a name="line.273"></a>
-<span class="sourceLineNo">274</span><a name="line.274"></a>
-<span class="sourceLineNo">275</span>        // find the min and max string lengths; this avoids checking to make<a name="line.275"></a>
-<span class="sourceLineNo">276</span>        // sure we are not exceeding the length of the string each time through<a name="line.276"></a>
-<span class="sourceLineNo">277</span>        // the bottom loop.<a name="line.277"></a>
-<span class="sourceLineNo">278</span>        for (int i = 0; i &lt; arrayLen; i++) {<a name="line.278"></a>
-<span class="sourceLineNo">279</span>            if (css[i] == null) {<a name="line.279"></a>
-<span class="sourceLineNo">280</span>                anyStringNull = true;<a name="line.280"></a>
-<span class="sourceLineNo">281</span>                shortestStrLen = 0;<a name="line.281"></a>
-<span class="sourceLineNo">282</span>            } else {<a name="line.282"></a>
-<span class="sourceLineNo">283</span>                allStringsNull = false;<a name="line.283"></a>
-<span class="sourceLineNo">284</span>                shortestStrLen = Math.min(css[i].length(), shortestStrLen);<a name="line.284"></a>
-<span class="sourceLineNo">285</span>                longestStrLen = Math.max(css[i].length(), longestStrLen);<a name="line.285"></a>
-<span class="sourceLineNo">286</span>            }<a name="line.286"></a>
-<span class="sourceLineNo">287</span>        }<a name="line.287"></a>
-<span class="sourceLineNo">288</span><a name="line.288"></a>
-<span class="sourceLineNo">289</span>        // handle lists containing all nulls or all empty strings<a name="line.289"></a>
-<span class="sourceLineNo">290</span>        if (allStringsNull || longestStrLen == 0 &amp;&amp; !anyStringNull) {<a name="line.290"></a>
-<span class="sourceLineNo">291</span>            return INDEX_NOT_FOUND;<a name="line.291"></a>
-<span class="sourceLineNo">292</span>        }<a name="line.292"></a>
-<span class="sourceLineNo">293</span><a name="line.293"></a>
-<span class="sourceLineNo">294</span>        // handle lists containing some nulls or some empty strings<a name="line.294"></a>
-<span class="sourceLineNo">295</span>        if (shortestStrLen == 0) {<a name="line.295"></a>
-<span class="sourceLineNo">296</span>            return 0;<a name="line.296"></a>
-<span class="sourceLineNo">297</span>        }<a name="line.297"></a>
-<span class="sourceLineNo">298</span><a name="line.298"></a>
-<span class="sourceLineNo">299</span>        // find the position with the first difference across all strings<a name="line.299"></a>
-<span class="sourceLineNo">300</span>        int firstDiff = -1;<a name="line.300"></a>
-<span class="sourceLineNo">301</span>        for (int stringPos = 0; stringPos &lt; shortestStrLen; stringPos++) {<a name="line.301"></a>
-<span class="sourceLineNo">302</span>            final char comparisonChar = css[0].charAt(stringPos);<a name="line.302"></a>
-<span class="sourceLineNo">303</span>            for (int arrayPos = 1; arrayPos &lt; arrayLen; arrayPos++) {<a name="line.303"></a>
-<span class="sourceLineNo">304</span>                if (css[arrayPos].charAt(stringPos) != comparisonChar) {<a name="line.304"></a>
-<span class="sourceLineNo">305</span>                    firstDiff = stringPos;<a name="line.305"></a>
-<span class="sourceLineNo">306</span>                    break;<a name="line.306"></a>
-<span class="sourceLineNo">307</span>                }<a name="line.307"></a>
-<span class="sourceLineNo">308</span>            }<a name="line.308"></a>
-<span class="sourceLineNo">309</span>            if (firstDiff != -1) {<a name="line.309"></a>
-<span class="sourceLineNo">310</span>                break;<a name="line.310"></a>
-<span class="sourceLineNo">311</span>            }<a name="line.311"></a>
-<span class="sourceLineNo">312</span>        }<a name="line.312"></a>
-<span class="sourceLineNo">313</span><a name="line.313"></a>
-<span class="sourceLineNo">314</span>        if (firstDiff == -1 &amp;&amp; shortestStrLen != longestStrLen) {<a name="line.314"></a>
-<span class="sourceLineNo">315</span>            // we compared all of the characters up to the length of the<a name="line.315"></a>
-<span class="sourceLineNo">316</span>            // shortest string and didn't find a match, but the string lengths<a name="line.316"></a>
-<span class="sourceLineNo">317</span>            // vary, so return the length of the shortest string.<a name="line.317"></a>
-<span class="sourceLineNo">318</span>            return shortestStrLen;<a name="line.318"></a>
-<span class="sourceLineNo">319</span>        }<a name="line.319"></a>
-<span class="sourceLineNo">320</span>        return firstDiff;<a name="line.320"></a>
-<span class="sourceLineNo">321</span>    }<a name="line.321"></a>
+<span class="sourceLineNo">214</span>        final double defaultDenominator = 3.0;<a name="line.214"></a>
+<span class="sourceLineNo">215</span><a name="line.215"></a>
+<span class="sourceLineNo">216</span>        // Calculate the distance.<a name="line.216"></a>
+<span class="sourceLineNo">217</span>        final double dist = (m1.length() / ((double) shorter.length())<a name="line.217"></a>
+<span class="sourceLineNo">218</span>                + m2.length() / ((double) longer.length()) + (m1.length() - transpositions)<a name="line.218"></a>
+<span class="sourceLineNo">219</span>                / ((double) m1.length())) / defaultDenominator;<a name="line.219"></a>
+<span class="sourceLineNo">220</span>        return dist;<a name="line.220"></a>
+<span class="sourceLineNo">221</span>    }<a name="line.221"></a>
+<span class="sourceLineNo">222</span><a name="line.222"></a>
+<span class="sourceLineNo">223</span>    /**<a name="line.223"></a>
+<span class="sourceLineNo">224</span>     * Calculates the number of transposition between two strings.<a name="line.224"></a>
+<span class="sourceLineNo">225</span>     *<a name="line.225"></a>
+<span class="sourceLineNo">226</span>     * @param first The first string.<a name="line.226"></a>
+<span class="sourceLineNo">227</span>     * @param second The second string.<a name="line.227"></a>
+<span class="sourceLineNo">228</span>     * @return The number of transposition between the two strings.<a name="line.228"></a>
+<span class="sourceLineNo">229</span>     */<a name="line.229"></a>
+<span class="sourceLineNo">230</span>    protected static int transpositions(final CharSequence first,<a name="line.230"></a>
+<span class="sourceLineNo">231</span>            final CharSequence second) {<a name="line.231"></a>
+<span class="sourceLineNo">232</span>        int transpositions = 0;<a name="line.232"></a>
+<span class="sourceLineNo">233</span>        for (int i = 0; i &lt; first.length(); i++) {<a name="line.233"></a>
+<span class="sourceLineNo">234</span>            if (first.charAt(i) != second.charAt(i)) {<a name="line.234"></a>
+<span class="sourceLineNo">235</span>                transpositions++;<a name="line.235"></a>
+<span class="sourceLineNo">236</span>            }<a name="line.236"></a>
+<span class="sourceLineNo">237</span>        }<a name="line.237"></a>
+<span class="sourceLineNo">238</span>        return transpositions / 2;<a name="line.238"></a>
+<span class="sourceLineNo">239</span>    }<a name="line.239"></a>
+<span class="sourceLineNo">240</span><a name="line.240"></a>
+<span class="sourceLineNo">241</span>    /**<a name="line.241"></a>
+<span class="sourceLineNo">242</span>     * Compares all CharSequences in an array and returns the index at which the<a name="line.242"></a>
+<span class="sourceLineNo">243</span>     * CharSequences begin to differ.<a name="line.243"></a>
+<span class="sourceLineNo">244</span>     *<a name="line.244"></a>
+<span class="sourceLineNo">245</span>     * &lt;p&gt;<a name="line.245"></a>
+<span class="sourceLineNo">246</span>     * For example,<a name="line.246"></a>
+<span class="sourceLineNo">247</span>     * &lt;code&gt;indexOfDifference(new String[] {"i am a machine", "i am a robot"}) -&amp;gt; 7&lt;/code&gt;<a name="line.247"></a>
+<span class="sourceLineNo">248</span>     * &lt;/p&gt;<a name="line.248"></a>
+<span class="sourceLineNo">249</span>     *<a name="line.249"></a>
+<span class="sourceLineNo">250</span>     * &lt;pre&gt;<a name="line.250"></a>
+<span class="sourceLineNo">251</span>     * distance.indexOfDifference(null) = -1<a name="line.251"></a>
+<span class="sourceLineNo">252</span>     * distance.indexOfDifference(new String[] {}) = -1<a name="line.252"></a>
+<span class="sourceLineNo">253</span>     * distance.indexOfDifference(new String[] {"abc"}) = -1<a name="line.253"></a>
+<span class="sourceLineNo">254</span>     * distance.indexOfDifference(new String[] {null, null}) = -1<a name="line.254"></a>
+<span class="sourceLineNo">255</span>     * distance.indexOfDifference(new String[] {"", ""}) = -1<a name="line.255"></a>
+<span class="sourceLineNo">256</span>     * distance.indexOfDifference(new String[] {"", null}) = 0<a name="line.256"></a>
+<span class="sourceLineNo">257</span>     * distance.indexOfDifference(new String[] {"abc", null, null}) = 0<a name="line.257"></a>
+<span class="sourceLineNo">258</span>     * distance.indexOfDifference(new String[] {null, null, "abc"}) = 0<a name="line.258"></a>
+<span class="sourceLineNo">259</span>     * distance.indexOfDifference(new String[] {"", "abc"}) = 0<a name="line.259"></a>
+<span class="sourceLineNo">260</span>     * distance.indexOfDifference(new String[] {"abc", ""}) = 0<a name="line.260"></a>
+<span class="sourceLineNo">261</span>     * distance.indexOfDifference(new String[] {"abc", "abc"}) = -1<a name="line.261"></a>
+<span class="sourceLineNo">262</span>     * distance.indexOfDifference(new String[] {"abc", "a"}) = 1<a name="line.262"></a>
+<span class="sourceLineNo">263</span>     * distance.indexOfDifference(new String[] {"ab", "abxyz"}) = 2<a name="line.263"></a>
+<span class="sourceLineNo">264</span>     * distance.indexOfDifference(new String[] {"abcde", "abxyz"}) = 2<a name="line.264"></a>
+<span class="sourceLineNo">265</span>     * distance.indexOfDifference(new String[] {"abcde", "xyz"}) = 0<a name="line.265"></a>
+<span class="sourceLineNo">266</span>     * distance.indexOfDifference(new String[] {"xyz", "abcde"}) = 0<a name="line.266"></a>
+<span class="sourceLineNo">267</span>     * distance.indexOfDifference(new String[] {"i am a machine", "i am a robot"}) = 7<a name="line.267"></a>
+<span class="sourceLineNo">268</span>     * &lt;/pre&gt;<a name="line.268"></a>
+<span class="sourceLineNo">269</span>     *<a name="line.269"></a>
+<span class="sourceLineNo">270</span>     * @param css array of CharSequences, entries may be null<a name="line.270"></a>
+<span class="sourceLineNo">271</span>     * @return the index where the strings begin to differ; -1 if they are all<a name="line.271"></a>
+<span class="sourceLineNo">272</span>     *         equal<a name="line.272"></a>
+<span class="sourceLineNo">273</span>     */<a name="line.273"></a>
+<span class="sourceLineNo">274</span>    protected static int indexOfDifference(final CharSequence... css) {<a name="line.274"></a>
+<span class="sourceLineNo">275</span>        if (css == null || css.length &lt;= 1) {<a name="line.275"></a>
+<span class="sourceLineNo">276</span>            return INDEX_NOT_FOUND;<a name="line.276"></a>
+<span class="sourceLineNo">277</span>        }<a name="line.277"></a>
+<span class="sourceLineNo">278</span>        boolean anyStringNull = false;<a name="line.278"></a>
+<span class="sourceLineNo">279</span>        boolean allStringsNull = true;<a name="line.279"></a>
+<span class="sourceLineNo">280</span>        final int arrayLen = css.length;<a name="line.280"></a>
+<span class="sourceLineNo">281</span>        int shortestStrLen = Integer.MAX_VALUE;<a name="line.281"></a>
+<span class="sourceLineNo">282</span>        int longestStrLen = 0;<a name="line.282"></a>
+<span class="sourceLineNo">283</span><a name="line.283"></a>
+<span class="sourceLineNo">284</span>        // find the min and max string lengths; this avoids checking to make<a name="line.284"></a>
+<span class="sourceLineNo">285</span>        // sure we are not exceeding the length of the string each time through<a name="line.285"></a>
+<span class="sourceLineNo">286</span>        // the bottom loop.<a name="line.286"></a>
+<span class="sourceLineNo">287</span>        for (int i = 0; i &lt; arrayLen; i++) {<a name="line.287"></a>
+<span class="sourceLineNo">288</span>            if (css[i] == null) {<a name="line.288"></a>
+<span class="sourceLineNo">289</span>                anyStringNull = true;<a name="line.289"></a>
+<span class="sourceLineNo">290</span>                shortestStrLen = 0;<a name="line.290"></a>
+<span class="sourceLineNo">291</span>            } else {<a name="line.291"></a>
+<span class="sourceLineNo">292</span>                allStringsNull = false;<a name="line.292"></a>
+<span class="sourceLineNo">293</span>                shortestStrLen = Math.min(css[i].length(), shortestStrLen);<a name="line.293"></a>
+<span class="sourceLineNo">294</span>                longestStrLen = Math.max(css[i].length(), longestStrLen);<a name="line.294"></a>
+<span class="sourceLineNo">295</span>            }<a name="line.295"></a>
+<span class="sourceLineNo">296</span>        }<a name="line.296"></a>
+<span class="sourceLineNo">297</span><a name="line.297"></a>
+<span class="sourceLineNo">298</span>        // handle lists containing all nulls or all empty strings<a name="line.298"></a>
+<span class="sourceLineNo">299</span>        if (allStringsNull || longestStrLen == 0 &amp;&amp; !anyStringNull) {<a name="line.299"></a>
+<span class="sourceLineNo">300</span>            return INDEX_NOT_FOUND;<a name="line.300"></a>
+<span class="sourceLineNo">301</span>        }<a name="line.301"></a>
+<span class="sourceLineNo">302</span><a name="line.302"></a>
+<span class="sourceLineNo">303</span>        // handle lists containing some nulls or some empty strings<a name="line.303"></a>
+<span class="sourceLineNo">304</span>        if (shortestStrLen == 0) {<a name="line.304"></a>
+<span class="sourceLineNo">305</span>            return 0;<a name="line.305"></a>
+<span class="sourceLineNo">306</span>        }<a name="line.306"></a>
+<span class="sourceLineNo">307</span><a name="line.307"></a>
+<span class="sourceLineNo">308</span>        // find the position with the first difference across all strings<a name="line.308"></a>
+<span class="sourceLineNo">309</span>        int firstDiff = -1;<a name="line.309"></a>
+<span class="sourceLineNo">310</span>        for (int stringPos = 0; stringPos &lt; shortestStrLen; stringPos++) {<a name="line.310"></a>
+<span class="sourceLineNo">311</span>            final char comparisonChar = css[0].charAt(stringPos);<a name="line.311"></a>
+<span class="sourceLineNo">312</span>            for (int arrayPos = 1; arrayPos &lt; arrayLen; arrayPos++) {<a name="line.312"></a>
+<span class="sourceLineNo">313</span>                if (css[arrayPos].charAt(stringPos) != comparisonChar) {<a name="line.313"></a>
+<span class="sourceLineNo">314</span>                    firstDiff = stringPos;<a name="line.314"></a>
+<span class="sourceLineNo">315</span>                    break;<a name="line.315"></a>
+<span class="sourceLineNo">316</span>                }<a name="line.316"></a>
+<span class="sourceLineNo">317</span>            }<a name="line.317"></a>
+<span class="sourceLineNo">318</span>            if (firstDiff != -1) {<a name="line.318"></a>
+<span class="sourceLineNo">319</span>                break;<a name="line.319"></a>
+<span class="sourceLineNo">320</span>            }<a name="line.320"></a>
+<span class="sourceLineNo">321</span>        }<a name="line.321"></a>
 <span class="sourceLineNo">322</span><a name="line.322"></a>
-<span class="sourceLineNo">323</span>    /**<a name="line.323"></a>
-<span class="sourceLineNo">324</span>     * Gets a set of matching characters between two strings.<a name="line.324"></a>
-<span class="sourceLineNo">325</span>     *<a name="line.325"></a>
-<span class="sourceLineNo">326</span>     * &lt;p&gt;<a name="line.326"></a>
-<span class="sourceLineNo">327</span>     * Two characters from the first string and the second string are<a name="line.327"></a>
-<span class="sourceLineNo">328</span>     * considered matching if the character's respective positions are no<a name="line.328"></a>
-<span class="sourceLineNo">329</span>     * farther than the limit value.<a name="line.329"></a>
-<span class="sourceLineNo">330</span>     * &lt;/p&gt;<a name="line.330"></a>
-<span class="sourceLineNo">331</span>     *<a name="line.331"></a>
-<span class="sourceLineNo">332</span>     * @param first The first string.<a name="line.332"></a>
-<span class="sourceLineNo">333</span>     * @param second The second string.<a name="line.333"></a>
-<span class="sourceLineNo">334</span>     * @param limit The maximum distance to consider.<a name="line.334"></a>
-<span class="sourceLineNo">335</span>     * @return A string contain the set of common characters.<a name="line.335"></a>
-<span class="sourceLineNo">336</span>     */<a name="line.336"></a>
-<span class="sourceLineNo">337</span>    protected static String getSetOfMatchingCharacterWithin(<a name="line.337"></a>
-<span class="sourceLineNo">338</span>            final CharSequence first, final CharSequence second, final int limit) {<a name="line.338"></a>
-<span class="sourceLineNo">339</span>        final StringBuilder common = new StringBuilder();<a name="line.339"></a>
-<span class="sourceLineNo">340</span>        final StringBuilder copy = new StringBuilder(second);<a name="line.340"></a>
-<span class="sourceLineNo">341</span><a name="line.341"></a>
-<span class="sourceLineNo">342</span>        for (int i = 0; i &lt; first.length(); i++) {<a name="line.342"></a>
-<span class="sourceLineNo">343</span>            final char ch = first.charAt(i);<a name="line.343"></a>
-<span class="sourceLineNo">344</span>            boolean found = false;<a name="line.344"></a>
-<span class="sourceLineNo">345</span><a name="line.345"></a>
-<span class="sourceLineNo">346</span>            // See if the character is within the limit positions away from the<a name="line.346"></a>
-<span class="sourceLineNo">347</span>            // original position of that character.<a name="line.347"></a>
-<span class="sourceLineNo">348</span>            for (int j = Math.max(0, i - limit); !found<a name="line.348"></a>
-<span class="sourceLineNo">349</span>                    &amp;&amp; j &lt; Math.min(i + limit, second.length()); j++) {<a name="line.349"></a>
-<span class="sourceLineNo">350</span>                if (copy.charAt(j) == ch) {<a name="line.350"></a>
-<span class="sourceLineNo">351</span>                    found = true;<a name="line.351"></a>
-<span class="sourceLineNo">352</span>                    common.append(ch);<a name="line.352"></a>
-<span class="sourceLineNo">353</span>                    copy.setCharAt(j, '*');<a name="line.353"></a>
-<span class="sourceLineNo">354</span>                }<a name="line.354"></a>
-<span class="sourceLineNo">355</span>            }<a name="line.355"></a>
-<span class="sourceLineNo">356</span>        }<a name="line.356"></a>
-<span class="sourceLineNo">357</span>        return common.toString();<a name="line.357"></a>
-<span class="sourceLineNo">358</span>    }<a name="line.358"></a>
-<span class="sourceLineNo">359</span><a name="line.359"></a>
-<span class="sourceLineNo">360</span>}<a name="line.360"></a>
+<span class="sourceLineNo">323</span>        if (firstDiff == -1 &amp;&amp; shortestStrLen != longestStrLen) {<a name="line.323"></a>
+<span class="sourceLineNo">324</span>            // we compared all of the characters up to the length of the<a name="line.324"></a>
+<span class="sourceLineNo">325</span>            // shortest string and didn't find a match, but the string lengths<a name="line.325"></a>
+<span class="sourceLineNo">326</span>            // vary, so return the length of the shortest string.<a name="line.326"></a>
+<span class="sourceLineNo">327</span>            return shortestStrLen;<a name="line.327"></a>
+<span class="sourceLineNo">328</span>        }<a name="line.328"></a>
+<span class="sourceLineNo">329</span>        return firstDiff;<a name="line.329"></a>
+<span class="sourceLineNo">330</span>    }<a name="line.330"></a>
+<span class="sourceLineNo">331</span><a name="line.331"></a>
+<span class="sourceLineNo">332</span>    /**<a name="line.332"></a>
+<span class="sourceLineNo">333</span>     * Gets a set of matching characters between two strings.<a name="line.333"></a>
+<span class="sourceLineNo">334</span>     *<a name="line.334"></a>
+<span class="sourceLineNo">335</span>     * &lt;p&gt;<a name="line.335"></a>
+<span class="sourceLineNo">336</span>     * Two characters from the first string and the second string are<a name="line.336"></a>
+<span class="sourceLineNo">337</span>     * considered matching if the character's respective positions are no<a name="line.337"></a>
+<span class="sourceLineNo">338</span>     * farther than the limit value.<a name="line.338"></a>
+<span class="sourceLineNo">339</span>     * &lt;/p&gt;<a name="line.339"></a>
+<span class="sourceLineNo">340</span>     *<a name="line.340"></a>
+<span class="sourceLineNo">341</span>     * @param first The first string.<a name="line.341"></a>
+<span class="sourceLineNo">342</span>     * @param second The second string.<a name="line.342"></a>
+<span class="sourceLineNo">343</span>     * @param limit The maximum distance to consider.<a name="line.343"></a>
+<span class="sourceLineNo">344</span>     * @return A string contain the set of common characters.<a name="line.344"></a>
+<span class="sourceLineNo">345</span>     */<a name="line.345"></a>
+<span class="sourceLineNo">346</span>    protected static String getSetOfMatchingCharacterWithin(<a name="line.346"></a>
+<span class="sourceLineNo">347</span>            final CharSequence first, final CharSequence second, final int limit) {<a name="line.347"></a>
+<span class="sourceLineNo">348</span>        final StringBuilder common = new StringBuilder();<a name="line.348"></a>
+<span class="sourceLineNo">349</span>        final StringBuilder copy = new StringBuilder(second);<a name="line.349"></a>
+<span class="sourceLineNo">350</span><a name="line.350"></a>
+<span class="sourceLineNo">351</span>        for (int i = 0; i &lt; first.length(); i++) {<a name="line.351"></a>
+<span class="sourceLineNo">352</span>            final char ch = first.charAt(i);<a name="line.352"></a>
+<span class="sourceLineNo">353</span>            boolean found = false;<a name="line.353"></a>
+<span class="sourceLineNo">354</span><a name="line.354"></a>
+<span class="sourceLineNo">355</span>            // See if the character is within the limit positions away from the<a name="line.355"></a>
+<span class="sourceLineNo">356</span>            // original position of that character.<a name="line.356"></a>
+<span class="sourceLineNo">357</span>            for (int j = Math.max(0, i - limit); !found<a name="line.357"></a>
+<span class="sourceLineNo">358</span>                    &amp;&amp; j &lt; Math.min(i + limit, second.length()); j++) {<a name="line.358"></a>
+<span class="sourceLineNo">359</span>                if (copy.charAt(j) == ch) {<a name="line.359"></a>
+<span class="sourceLineNo">360</span>                    found = true;<a name="line.360"></a>
+<span class="sourceLineNo">361</span>                    common.append(ch);<a name="line.361"></a>
+<span class="sourceLineNo">362</span>                    copy.setCharAt(j, '*');<a name="line.362"></a>
+<span class="sourceLineNo">363</span>                }<a name="line.363"></a>
+<span class="sourceLineNo">364</span>            }<a name="line.364"></a>
+<span class="sourceLineNo">365</span>        }<a name="line.365"></a>
+<span class="sourceLineNo">366</span>        return common.toString();<a name="line.366"></a>
+<span class="sourceLineNo">367</span>    }<a name="line.367"></a>
+<span class="sourceLineNo">368</span><a name="line.368"></a>
+<span class="sourceLineNo">369</span>}<a name="line.369"></a>
 
 
 
@@ -429,4 +438,4 @@
 </pre>
 </div>
 </body>
-</html>
\ No newline at end of file
+</html>



Mime
View raw message