LevenshteinTextSummary { public static String summarizeStrings(String first, String second) { if (first == null && second == null) { return ""; } if (first == null) { return second; } if (second == null) { return first; } if ("".equals(first.trim()) && "".equals(second.trim())) { return ""; } if ("".equals(first.trim())) { return "*"; } if ("".equals(second.trim())) { return "*"; } final Word[] one = prepareString(first); final ArrayList<Word> summary = summarizeStrings(one, second); final StringBuilder printBuffer = new StringBuilder(); for (int i = 0; i < summary.size(); i++) { final Word currWord = summary.get(i); if (currWord.ws) { printBuffer.append(' '); } printBuffer.append(currWord.getStr()); } return printBuffer.toString(); } private  LevenshteinTextSummary(); static int getThreshold(); static void setThreshold(int threshold); static Word[] prepareString(String str); static Word[] prepareStringToken(String str); static String summarizeStrings(String first, String second); static int calcDistance(String strA, String strB); static int calcDistance(Word[] wordsA, Word[] wordsB);  }
@Test public void testSummarizeStringsLettersForWords() { final String str1 = "s i t t i n g"; final String str2 = "k i t t e n"; final String expected = "* i t t * n *"; String match = LevenshteinTextSummary.summarizeStrings(str1, str2); String match2 = LevenshteinTextSummary.summarizeStrings(str2, str1); assertEquals("The summary should contain the common letters.", expected, match); assertEquals("The summary should be the same if you switch the parameter order.", expected, match2); }
@Test public void testSummarizeStringsLongSentences() { final String str1 = "Once upon a time, there was a boy who came to the open mainframe community looking for a friend!"; final String str2 = "Once upon a time. was there a girl who came to Google looking for a job:"; final String expected = "Once upon a time* a * who came to * looking for a *"; String match = LevenshteinTextSummary.summarizeStrings(str1, str2); String match2 = LevenshteinTextSummary.summarizeStrings(str2, str1); assertEquals("The summary should contain the common words.", expected, match); assertEquals("The summary should be the same if you switch the parameter order.", expected, match2); }
@Test public void testSummarizeStringsDifferentWordsInMiddle() { final String str1 = "I love cocoa"; final String str2 = "I don't like cocoa"; final String expected = "I * cocoa"; String match = LevenshteinTextSummary.summarizeStrings(str1, str2); String match2 = LevenshteinTextSummary.summarizeStrings(str2, str1); assertEquals("The summary should contain the common words.", expected, match); assertEquals("The summary should be the same if you switch the parameter order.", expected, match2); }
@Test public void testSummarizeStringsDifferentWordAtEnd() { final String str1 = "I love cocoa."; final String str2 = "I love milk"; final String expected = "I love *"; String match = LevenshteinTextSummary.summarizeStrings(str1, str2); String match2 = LevenshteinTextSummary.summarizeStrings(str2, str1); assertEquals("The summary should contain the common words.", expected, match); assertEquals("The summary should be the same if you switch the parameter order.", expected, match2); }
@Test public void testSummarizeStringsSameSentences() { final String str1 = "I love cocoa."; final String str2 = "I love cocoa"; final String expected = "I love cocoa*"; String match = LevenshteinTextSummary.summarizeStrings(str1, str2); String match2 = LevenshteinTextSummary.summarizeStrings(str2, str1); assertEquals("The summary should contain the common words.", expected, match); assertEquals("The summary should be the same if you switch the parameter order.", expected, match2); }
@Test public void testSummarizeStringsDifferentWordInMiddle() { final String str1 = "I hate cocoa."; final String str2 = "I love cocoa"; final String expected = "I * cocoa*"; String match = LevenshteinTextSummary.summarizeStrings(str1, str2); String match2 = LevenshteinTextSummary.summarizeStrings(str2, str1); assertEquals("The summary should contain the common words.", expected, match); assertEquals("The summary should be the same if you switch the parameter order.", expected, match2); }
@Test public void testSummarizeStringsDifferentWordsInMiddlePlusPunctuation() { final String str1 = "I dont love cocoa."; final String str2 = "I love cocoa"; final String expected = "I * love cocoa*"; String match = LevenshteinTextSummary.summarizeStrings(str1, str2); String match2 = LevenshteinTextSummary.summarizeStrings(str2, str1); assertEquals("The summary should contain the common words.", expected, match); assertEquals("The summary should be the same if you switch the parameter order.", expected, match2); }
@Test public void testSummarizeStringsDifferentWordsInMiddlePlusPunctuation2() { final String str1 = "I dont like cocoa."; final String str2 = "I love cocoa"; final String expected = "I * cocoa*"; String match = LevenshteinTextSummary.summarizeStrings(str1, str2); String match2 = LevenshteinTextSummary.summarizeStrings(str2, str1); assertEquals("The summary should contain the common words.", expected, match); assertEquals("The summary should be the same if you switch the parameter order.", expected, match2); }
@Test public void testSummarizeStringsRepeatedWords() { final String str1 = "I I I I dont like cocoa."; final String str2 = "I love cocoa"; final String expected = "* I * cocoa*"; String match = LevenshteinTextSummary.summarizeStrings(str1, str2); String match2 = LevenshteinTextSummary.summarizeStrings(str2, str1); assertEquals("The summary should contain the common words.", expected, match); assertEquals("The summary should be the same if you switch the parameter order.", expected, match2); }
@Test public void testSummarizeStringsExtraWordsAtEnd() { final String str1 = "I like cocoa A B C"; final String str2 = "I like cocoa"; final String expected = "I like cocoa *"; String match = LevenshteinTextSummary.summarizeStrings(str1, str2); String match2 = LevenshteinTextSummary.summarizeStrings(str2, str1); assertEquals("The summary should contain the common words.", expected, match); assertEquals("The summary should be the same if you switch the parameter order.", expected, match2); }
@Test public void testSummarizeStringsDifferentPunctuationAtEnd() { final String str1 = "I like cocoa."; final String str2 = "I like cocoa,"; final String expected = "I like cocoa*"; String match = LevenshteinTextSummary.summarizeStrings(str1, str2); String match2 = LevenshteinTextSummary.summarizeStrings(str2, str1); assertEquals("The summary should contain the common words.", expected, match); assertEquals("The summary should be the same if you switch the parameter order.", expected, match2); }
@Test public void testSummarizeStringsDifferentPunctuationInMiddle() { final String str1 = "I like cocoa. A"; final String str2 = "I like cocoa, A"; final String expected = "I like cocoa* A"; String match = LevenshteinTextSummary.summarizeStrings(str1, str2); String match2 = LevenshteinTextSummary.summarizeStrings(str2, str1); assertEquals("The summary should contain the common words.", expected, match); assertEquals("The summary should be the same if you switch the parameter order.", expected, match2); }
@Test public void testSummarizeStringsEndDifferentLetters() { final String str1 = "I like cocoa. a"; final String str2 = "I like cocoa, b"; final String expected = "I like cocoa*"; String match = LevenshteinTextSummary.summarizeStrings(str1, str2); String match2 = LevenshteinTextSummary.summarizeStrings(str2, str1); assertEquals("The summary should contain the common words.", expected, match); assertEquals("The summary should be the same if you switch the parameter order.", expected, match2); }
@Test public void testSummarizeStringsEndDifferentLetters2() { final String str1 = "I like cocoa. a c"; final String str2 = "I like cocoa, a b"; final String expected = "I like cocoa* a *"; String match = LevenshteinTextSummary.summarizeStrings(str1, str2); String match2 = LevenshteinTextSummary.summarizeStrings(str2, str1); assertEquals("The summary should contain the common words.", expected, match); assertEquals("The summary should be the same if you switch the parameter order.", expected, match2); }
@Test public void testSummarizeStringsFirstWordDifferent() { final String str1 = "black cocoa is awesome"; final String str2 = "brown cocoa is awesome"; final String expected = "* cocoa is awesome"; String match = LevenshteinTextSummary.summarizeStrings(str1, str2); String match2 = LevenshteinTextSummary.summarizeStrings(str2, str1); assertEquals("The summary should contain the common words.", expected, match); assertEquals("The summary should be the same if you switch the parameter order.", expected, match2); }
@Test public void testSummarizeStringsFirstNull() { final String str1 = null; final String str2 = "I like turtles"; String match = LevenshteinTextSummary.summarizeStrings(str1, str2); assertEquals("Should be the same as str2.", str2, match); }
@Test public void testSummarizeStringsSecondNull() { final String str1 = "I like turtles"; final String str2 = null; String match = LevenshteinTextSummary.summarizeStrings(str1, str2); assertEquals("Should be the same as str1.", str1, match); }
@Test public void testSummarizeStringsEmptyStrings() { final String str1 = ""; final String str2 = ""; final String expectedResult = ""; String match = LevenshteinTextSummary.summarizeStrings(str2, str1); assertEquals("Should be an empty string.", expectedResult, match); }
@Test public void testSummarizeStringsFirstEmptyString() { final String str1 = ""; final String str2 = "I like turtles"; final String expectedResult = "*"; String match = LevenshteinTextSummary.summarizeStrings(str2, str1); assertEquals("Should be an asterisk.", expectedResult, match); }
@Test public void testSummarizeStringsSecondEmptyString() { final String str1 = "I like turtles"; final String str2 = ""; final String expectedResult = "*"; String match = LevenshteinTextSummary.summarizeStrings(str2, str1); assertEquals("Should be an asterisk.", expectedResult, match); }