package morfologik.stemming; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import morfologik.fsa.FSA; /** * A dictionary combines {@link FSA} automaton and {@link DictionaryMetadata} * describing the way terms are encoded in the automaton. * *
* A dictionary consists of two files: *
*.dict).
* @return An instantiated dictionary.
* @throws IOException if an I/O error occurs.
*/
public static Dictionary read(Path location) throws IOException {
final Path metadata = DictionaryMetadata.getExpectedMetadataLocation(location);
try (InputStream fsaStream = Files.newInputStream(location);
InputStream metadataStream = Files.newInputStream(metadata)) {
return read(fsaStream, metadataStream);
}
}
/**
* Attempts to load a dictionary using the URL to the FSA file and the
* expected metadata extension.
*
* @param dictURL The URL pointing to the dictionary file (*.dict).
* @return An instantiated dictionary.
* @throws IOException if an I/O error occurs.
*/
public static Dictionary read(URL dictURL) throws IOException {
final URL expectedMetadataURL;
try {
String external = dictURL.toExternalForm();
expectedMetadataURL = new URL(DictionaryMetadata.getExpectedMetadataFileName(external));
} catch (MalformedURLException e) {
throw new IOException("Couldn't construct relative feature map URL for: " + dictURL, e);
}
try (InputStream fsaStream = dictURL.openStream();
InputStream metadataStream = expectedMetadataURL.openStream()) {
return read(fsaStream, metadataStream);
}
}
/**
* Attempts to load a dictionary from opened streams of FSA dictionary data
* and associated metadata. Input streams are not closed automatically.
*
* @param fsaStream The stream with FSA data
* @param metadataStream The stream with metadata
* @return Returns an instantiated {@link Dictionary}.
* @throws IOException if an I/O error occurs.
*/
public static Dictionary read(InputStream fsaStream, InputStream metadataStream) throws IOException {
return new Dictionary(FSA.read(fsaStream), DictionaryMetadata.read(metadataStream));
}
}