StringTools { public static List<MatchingWindow> getMatchingWindowsAboveThreshold(String text, String search, double threshold) { if (StringUtils.isBlank(text)) { return new ArrayList<>(); } if (StringUtils.isBlank(search)) { return new ArrayList<>(); } String[] addressWords = search.split(" "); int bagSize = addressWords.length; String[] textWords = text.split(" "); int textWordCount = textWords.length; List<MatchingWindow> windows = new ArrayList<>(); for (int i = 0; i < textWordCount; i++) { MatchingWindow window = takeBag(textWords, i, bagSize); window.setScoreAccordingTo(addressWords); window.setMatchingText(text.substring(window.getBegin(), window.getEnd())); windows.add(window); } Collections.sort(windows); windows = windows.stream().filter(window -> window.isScoreAboveThreshold(threshold)).collect(Collectors.toList()); return windows; }  static int getLevenshteinDistance(String str1, String str2); static Set<String> getApproximatelyMatchingStringList(String sourceString, String search); boolean isNotTooShort(String string); static Set<String> getApproximatelyMatchingStringList(String sourceString, String search, int maxDistance); static String getCompletingString(String string, int begin, int end); static List<MatchingWindow> getMatchingWindowsAboveThreshold(String text, String search, double threshold); static Set<String> splitIntoWordsWithLengthHigherThan(String string, int minLength); static Set<String> splitIntoWordsWithLengthHigherThan(String string, int minLength, String... ignoreWords); static boolean noContentInHtml(String text); static List<String> getRegexMatchesWithMinLength(String text, String regex, int minLength); static String getRandomForeName(); static String getRandomSurname(); static String getFirstHtmlWithContent(String html); static String getMetaDataFromHTML(String html); static String addMetaDataToHtml(String htmlText, String metaData);  }
@Test public void shouldFindWindowOfGivenText() { String string = "I am Ismail Emre Kartoglu. My address changes. It is now 33 Marmora Road, SE22 0RX, London, UK." + " This is some extra text."; String address = "33, London, Marmora Road, SE22 0RX"; MatchingWindow window = StringTools.getMatchingWindowsAboveThreshold(string, address, 0.5f).get(0); assertThat(window.getMatchingText(), equalTo("33 Marmora Road, SE22 0RX, London,")); assertThat(string.substring(window.getBegin(), window.getEnd()), equalTo("33 Marmora Road, SE22 0RX, London,")); }
@Test public void shouldFindWindowOfGivenTextWhenWindowIsAtTheBorder() { String string = "I am Ismail Emre Kartoglu. My address changes. It is now 33 Marmora Road, SE22 0RX, London"; String address = "33, London, Marmora Road, SE22 0RX"; MatchingWindow window = StringTools.getMatchingWindowsAboveThreshold(string, address, 0.5f).get(0); assertThat(window.getMatchingText(), equalTo("33 Marmora Road, SE22 0RX, London")); assertThat(string.substring(window.getBegin(), window.getEnd()), equalTo("33 Marmora Road, SE22 0RX, London")); assertThat(window.isScoreAboveThreshold(0.6f), equalTo(true)); }