Advertisement
arsovski

Untitled

Dec 26th, 2020
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. package bg.sofia.uni.fmi.mjt.spellchecker;
  2.  
  3.  
  4. import org.junit.AfterClass;
  5. import org.junit.BeforeClass;
  6. import org.junit.Test;
  7.  
  8. import java.io.IOException;
  9. import java.io.Reader;
  10. import java.io.StringReader;
  11. import java.io.StringWriter;
  12. import java.io.Writer;
  13. import java.util.List;
  14.  
  15. import static org.junit.Assert.assertTrue;
  16.  
  17. public class NaiveSpellCheckerTest {
  18.  
  19. private final static String METADATA_HEADER = "= = = Metadata = = =";
  20. private final static String FINDINGS_HEADER = "= = = Findings = = =";
  21.  
  22. private static Reader dictionaryReader;
  23. private static Reader stopwordsReader;
  24. private static NaiveSpellChecker testSpellChecker;
  25.  
  26. @BeforeClass
  27. public static void setUp() {
  28. dictionaryReader = new StringReader(String.join(System.lineSeparator(),
  29. List.of("cat", "dog", "bird", "hello")));
  30. stopwordsReader = new StringReader(String.join(System.lineSeparator(), List.of("a", "am", "me", "is")));
  31. testSpellChecker = new NaiveSpellChecker(dictionaryReader, stopwordsReader);
  32. }
  33.  
  34. @AfterClass
  35. public static void tearDown() {
  36. try {
  37. dictionaryReader.close();
  38. stopwordsReader.close();
  39. } catch (IOException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43.  
  44. @Test
  45. public void testAnalyze() {
  46.  
  47. //36 characters, 4 words, 2 spelling-issues
  48. String inputString = "-=-=-CaT is beautiful" //19 characters, 2 words, 1 issue (beautiful not in dictionary)
  49. + System.lineSeparator()
  50. + "//hallo, ==bird=-."; //17 characters, 2 words, 1 spelling-issue (hallo)
  51.  
  52. String outputString;
  53.  
  54. Reader input = new StringReader(inputString);
  55. Writer output = new StringWriter();
  56.  
  57. testSpellChecker.analyze(input, output, 1);
  58. outputString = output.toString();
  59.  
  60. try {
  61. input.close();
  62. output.close();
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. }
  66.  
  67. assertTrue(outputString.contains(inputString));
  68. assertTrue(outputString.contains(METADATA_HEADER) && outputString.contains(FINDINGS_HEADER));
  69. assertTrue(outputString.contains("36 characters, 4 words, 2 spelling issue(s) found"));
  70. assertTrue(outputString.contains("Line #1, {beautiful} - Possible suggestions are {dog}"));
  71. assertTrue(outputString.contains("Line #2, {//hallo} - Possible suggestions are {hello}"));
  72.  
  73. }
  74.  
  75. @Test
  76. public void testAnalyzeRemovesPunctuationMarksInFindingsSection() {
  77. //36 characters, 4 words, 2 spelling-issues
  78. String inputString = "helllo,.?!:;";
  79.  
  80. String outputString;
  81.  
  82. Reader input = new StringReader(inputString);
  83. Writer output = new StringWriter();
  84.  
  85. testSpellChecker.analyze(input, output, 1);
  86. outputString = output.toString();
  87.  
  88. try {
  89. input.close();
  90. output.close();
  91. } catch (IOException e) {
  92. e.printStackTrace();
  93. }
  94. assertTrue(outputString.contains("{helllo}"));
  95. }
  96.  
  97. @Test(expected = IllegalArgumentException.class)
  98. public void testAnalyzeWithNegativeNumberOfSuggestions() {
  99. testSpellChecker.analyze(null, null, -1);
  100. }
  101.  
  102. @Test(expected = IllegalArgumentException.class)
  103. public void testAnalyzeWithGreaterThanDictionarySizeNumberOfSuggestions() {
  104. testSpellChecker.analyze(null, null, 100);
  105. }
  106.  
  107. @Test(expected = IllegalArgumentException.class)
  108. public void testFindClosestWordsWithNullArgumentForWord() {
  109. testSpellChecker.findClosestWords(null, 1);
  110. }
  111.  
  112. @Test(expected = IllegalArgumentException.class)
  113. public void testFindClosestWordsWithZeroNumberOfSuggestions() {
  114. testSpellChecker.findClosestWords("word", 0);
  115. }
  116.  
  117. @Test(expected = IllegalArgumentException.class)
  118. public void testFindClosestWordsWithGreaterThanDictionarySizeNumberOfSuggestions() {
  119. testSpellChecker.findClosestWords("word", 100);
  120. }
  121.  
  122. @Test
  123. public void testMetadataWithSingleLetterWords() {
  124. String inputString = "A B C " //A is contained in stopwords, so it is ignored in words count
  125. + "X Y Z";
  126. Reader input = new StringReader(inputString);
  127.  
  128. Metadata actual = testSpellChecker.metadata(input);
  129.  
  130. try {
  131. input.close();
  132. } catch (IOException e) {
  133. e.printStackTrace();
  134. }
  135.  
  136. assertTrue(actual.words() == 5 && actual.characters() == 6 && actual.mistakes() == 0);
  137. }
  138.  
  139. @Test
  140. public void testMetadataWithRandomWords() {
  141.  
  142. //42 characters, 5 words, 2 spelling-issue (hallo)
  143. String inputString = ";;12fasd hello ===bird--, " //23 characters, 3 words, 1 spelling-issue (hallo)
  144. + " fds //cat==- ===-A-=="; //19 characters, 2 words, 1 spelling-issue (hallo)
  145. Reader input = new StringReader(inputString);
  146.  
  147. Metadata actual = testSpellChecker.metadata(input);
  148.  
  149. try {
  150. input.close();
  151. } catch (IOException e) {
  152. e.printStackTrace();
  153. }
  154.  
  155. assertTrue(actual.words() == 5 && actual.characters() == 42 && actual.mistakes() == 2);
  156. }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement