0
|
1 package de.mpiwg.anteater.ml;
|
|
2
|
|
3
|
|
4 public class SimilarityHelper {
|
|
5
|
|
6 /**
|
|
7 * Following method is based on
|
|
8 * http://diggintojava.blogspot.com/2009/12/longest-matching-substring-in-java.html
|
|
9 * @param s1
|
|
10 * @param s2
|
|
11 * @return biggest substring contained in both strings
|
|
12 */
|
|
13 public static String getBiggestSubstring(String s1, String s2) {
|
|
14 String sub = s1;
|
|
15 int j = s1.length();
|
4
|
16
|
|
17 if (s1 == null || s2 == null)
|
|
18 return "";
|
0
|
19
|
|
20 String subString = "";
|
|
21 String comparingString = s2;
|
|
22 while (j >= 0) {
|
|
23 for (int i = 0; i < j; i++) {
|
|
24 sub = s1.substring(i, j);
|
|
25 if (comparingString.contains(sub)) {
|
|
26 if (sub.length() > subString.length())
|
|
27 subString = sub;
|
|
28 }
|
|
29 }
|
|
30 j--;
|
|
31 }
|
|
32 return subString;
|
|
33 }
|
|
34 }
|