-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
232 additions
and
129 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
117 changes: 117 additions & 0 deletions
117
src/test/java/org/dts/spell/tests/OpenOfficeSpellDictonaryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package org.dts.spell.tests; | ||
|
||
|
||
import org.dts.spell.SpellChecker; | ||
import org.dts.spell.dictionary.OpenOfficeSpellDictionary; | ||
import org.dts.spell.dictionary.SpellDictionary; | ||
import org.dts.spell.dictionary.SpellDictionaryException; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.zip.ZipFile; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class OpenOfficeSpellDictonaryTest extends TestBase { | ||
|
||
private static final String DICTIONARY_ZIP = "en_US.zip"; | ||
|
||
@Override | ||
protected String getDictionary() { | ||
return DICTIONARY_ZIP; | ||
} | ||
|
||
@Test | ||
public void testCheckOpenOfficeDictionary() throws IOException { | ||
try (InputStream affFile = Files.newInputStream(tempDir.resolve("en_US.aff")); | ||
InputStream dicFile = Files.newInputStream(tempDir.resolve("en_US.dic"))) { | ||
SpellDictionary dict = new OpenOfficeSpellDictionary(affFile, dicFile); | ||
SpellChecker checker = new SpellChecker(dict); | ||
checker.setCaseSensitive(false); | ||
|
||
String word = "Hello world!"; | ||
assertThat(checker.isCorrect(word)).isTrue(); | ||
List<String> suggestions = dict.getSuggestions(word); | ||
assertThat(suggestions.size()).isEqualTo(10); | ||
assertThat(suggestions).contains("Worldliness's", "Worldlinesses", "Worldliness", "Worldliest", "Afterworlds", | ||
"Afterworld's", "Afterworld", "Worldwide", "Dreamworlds"); | ||
} | ||
} | ||
|
||
@Test | ||
public void testOpenOfficeDictionaryZipDictionaryStream() throws IOException { | ||
try (InputStream is = getClass().getResourceAsStream(DICTIONARY_ZIP)) { | ||
SpellDictionary dict = new OpenOfficeSpellDictionary(is, personalDictionary, false); | ||
SpellChecker checker = new SpellChecker(dict); | ||
checker.setCaseSensitive(false); | ||
|
||
String word = "Hello world!"; | ||
assertThat(checker.isCorrect(word)).isTrue(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testOpenOfficeDictionaryUnknownWord() throws IOException { | ||
try (InputStream is = getClass().getResourceAsStream(DICTIONARY_ZIP)) { | ||
SpellDictionary dict = new OpenOfficeSpellDictionary(is, personalDictionary, false); | ||
SpellChecker checker = new SpellChecker(dict); | ||
checker.setCaseSensitive(false); | ||
String word = "Cha0 world!"; | ||
assertThat(checker.isCorrect(word)).isFalse(); | ||
List<String> suggestions = dict.getSuggestions(word); | ||
assertThat(suggestions.size()).isEqualTo(9); | ||
} | ||
} | ||
|
||
@Test | ||
public void testOpenOfficeDictionaryZipFile() throws IOException { | ||
URL zipURL = getClass().getResource(DICTIONARY_ZIP); | ||
ZipFile zipFile = new ZipFile(Objects.requireNonNull(zipURL).getFile()); | ||
SpellDictionary dict = new OpenOfficeSpellDictionary(zipFile); | ||
SpellChecker checker = new SpellChecker(dict); | ||
checker.setCaseSensitive(false); | ||
String word = "Hello world!"; | ||
assertThat(checker.isCorrect(word)).isTrue(); | ||
} | ||
|
||
@Test | ||
public void testOpenOfficeDictionaryStreamBackground() throws IOException { | ||
try (InputStream is = getClass().getResourceAsStream(DICTIONARY_ZIP)) { | ||
SpellDictionary dict = new OpenOfficeSpellDictionary(is, personalDictionary, true); | ||
SpellChecker checker = new SpellChecker(dict); | ||
checker.setCaseSensitive(false); | ||
String word = "Hello world!"; | ||
assertThat(checker.isCorrect(word)).isTrue(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testOpenOfficeDictionaryCharSequence() throws IOException { | ||
try (InputStream is = getClass().getResourceAsStream(DICTIONARY_ZIP)) { | ||
SpellDictionary dict = new OpenOfficeSpellDictionary(is, personalDictionary, true); | ||
SpellChecker checker = new SpellChecker(dict); | ||
checker.setCaseSensitive(false); | ||
String word = "Hello world!"; | ||
assertThat(checker.checkSpell(word)).isNullOrEmpty(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testOpenOfficeDictionaryAddWord() throws IOException, SpellDictionaryException { | ||
try (InputStream is = getClass().getResourceAsStream(DICTIONARY_ZIP)) { | ||
SpellDictionary dict = new OpenOfficeSpellDictionary(is, personalDictionary, false); | ||
dict.addWord("Chao"); | ||
SpellChecker checker = new SpellChecker(dict); | ||
checker.setCaseSensitive(false); | ||
|
||
String word = "Chao world!"; | ||
assertThat(checker.isCorrect(word)).isTrue(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.dts.spell.tests; | ||
|
||
|
||
import org.dts.spell.SpellChecker; | ||
import org.dts.spell.dictionary.OpenOfficeSpellDictionary; | ||
import org.dts.spell.dictionary.SpellDictionary; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class SpanishTest extends TestBase { | ||
|
||
private static final String DICTIONARY_ZIP = "es.zip"; | ||
|
||
@Override | ||
protected String getDictionary() { | ||
return DICTIONARY_ZIP; | ||
} | ||
|
||
@Test | ||
public void testSpell() throws IOException { | ||
try (InputStream affFile = Files.newInputStream(tempDir.resolve("es/index.aff")); | ||
InputStream dicFile = Files.newInputStream(tempDir.resolve("es/index.dic"))) { | ||
SpellDictionary dict = new OpenOfficeSpellDictionary(affFile, dicFile); | ||
SpellChecker checker = new SpellChecker(dict); | ||
checker.setCaseSensitive(false); | ||
|
||
String word = "Hola mundo!"; | ||
assertThat(checker.isCorrect(word)).isTrue(); | ||
List<String> suggestions = dict.getSuggestions(word); | ||
assertThat(suggestions.size()).isEqualTo(10); | ||
assertThat(suggestions).contains("Inframundo", "Juzgamundos", "Inframundos", "Vagamundos", | ||
"Vagamundo", "Trotamundos", "Trasmundos", "Trasmundo", "Inmundos", "Submundos"); | ||
} | ||
} | ||
|
||
@Test | ||
public void testOpenOfficeDictionaryZipDictionary() throws IOException { | ||
try (InputStream is = getClass().getResourceAsStream(DICTIONARY_ZIP)) { | ||
SpellDictionary dict = new OpenOfficeSpellDictionary(is, personalDictionary, false); | ||
SpellChecker checker = new SpellChecker(dict); | ||
checker.setCaseSensitive(false); | ||
|
||
String word = "Hola mundo!"; | ||
assertThat(checker.isCorrect(word)).isTrue(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.dts.spell.tests; | ||
|
||
import org.apache.commons.compress.archivers.ArchiveEntry; | ||
import org.apache.commons.compress.archivers.ArchiveException; | ||
import org.apache.commons.compress.archivers.ArchiveInputStream; | ||
import org.apache.commons.compress.archivers.ArchiveStreamFactory; | ||
import org.apache.commons.compress.utils.IOUtils; | ||
import org.apache.commons.io.FileUtils; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
public abstract class TestBase { | ||
protected Path tempDir; | ||
protected File personalDictionary; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
tempDir = Files.createTempDirectory("jmyspell"); | ||
extractZip(getClass().getResourceAsStream(getDictionary()), tempDir); | ||
personalDictionary = Files.createTempFile(tempDir, "personal", ".dic").toFile(); | ||
} | ||
|
||
protected abstract String getDictionary(); | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
FileUtils.deleteDirectory(tempDir.toFile()); | ||
} | ||
|
||
|
||
protected void extractZip(InputStream inputStream, Path extractDirectory) throws IOException, ArchiveException { | ||
ArchiveStreamFactory archiveStreamFactory = new ArchiveStreamFactory(); | ||
ArchiveInputStream archiveInputStream = archiveStreamFactory | ||
.createArchiveInputStream(ArchiveStreamFactory.ZIP, inputStream); | ||
ArchiveEntry archiveEntry; | ||
while ((archiveEntry = archiveInputStream.getNextEntry()) != null) { | ||
Path path = extractDirectory.resolve(archiveEntry.getName()); | ||
File file = path.toFile(); | ||
if (archiveEntry.isDirectory()) { | ||
if (!file.isDirectory()) { | ||
var ignore = file.mkdirs(); | ||
} | ||
} else { | ||
File parent = file.getParentFile(); | ||
if (!parent.isDirectory()) { | ||
var ignore = parent.mkdirs(); | ||
} | ||
try (OutputStream outputStream = Files.newOutputStream(path)) { | ||
IOUtils.copy(archiveInputStream, outputStream); | ||
} | ||
} | ||
} | ||
} | ||
} |
Binary file not shown.