| 1 |
package morfologik.stemming; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.io.InputStream; |
| 5 |
import java.net.MalformedURLException; |
| 6 |
import java.net.URL; |
| 7 |
import java.nio.file.Files; |
| 8 |
import java.nio.file.Path; |
| 9 |
|
| 10 |
import morfologik.fsa.FSA; |
| 11 |
|
| 12 |
/** |
| 13 |
* A dictionary combines {@link FSA} automaton and {@link DictionaryMetadata} |
| 14 |
* describing the way terms are encoded in the automaton. |
| 15 |
* |
| 16 |
* <p> |
| 17 |
* A dictionary consists of two files: |
| 18 |
* <ul> |
| 19 |
* <li>an actual compressed FSA file, |
| 20 |
* <li>{@link DictionaryMetadata}, describing the way terms are encoded. |
| 21 |
* </ul> |
| 22 |
*/ |
| 23 |
public final class Dictionary { |
| 24 |
/** |
| 25 |
* {@link FSA} automaton with the compiled dictionary data. |
| 26 |
*/ |
| 27 |
public final FSA fsa; |
| 28 |
|
| 29 |
/** |
| 30 |
* Metadata associated with the dictionary. |
| 31 |
*/ |
| 32 |
public final DictionaryMetadata metadata; |
| 33 |
|
| 34 |
/** |
| 35 |
* It is strongly recommended to use static methods in this class for |
| 36 |
* reading dictionaries. |
| 37 |
* |
| 38 |
* @param fsa |
| 39 |
* An instantiated {@link FSA} instance. |
| 40 |
* |
| 41 |
* @param metadata |
| 42 |
* A map of attributes describing the compression format and |
| 43 |
* other settings not contained in the FSA automaton. For an |
| 44 |
* explanation of available attributes and their possible values, |
| 45 |
* see {@link DictionaryMetadata}. |
| 46 |
*/ |
| 47 |
public Dictionary(FSA fsa, DictionaryMetadata metadata) { |
| 48 |
this.fsa = fsa; |
| 49 |
this.metadata = metadata; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Attempts to load a dictionary using the path to the FSA file and the |
| 54 |
* expected metadata extension. |
| 55 |
* |
| 56 |
* @param location The location of the dictionary file (<code>*.dict</code>). |
| 57 |
* @return An instantiated dictionary. |
| 58 |
* @throws IOException if an I/O error occurs. |
| 59 |
*/ |
| 60 |
public static Dictionary read(Path location) throws IOException { |
| 61 |
final Path metadata = DictionaryMetadata.getExpectedMetadataLocation(location); |
| 62 |
|
| 63 |
try (InputStream fsaStream = Files.newInputStream(location); |
| 64 |
InputStream metadataStream = Files.newInputStream(metadata)) { |
| 65 |
return read(fsaStream, metadataStream); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Attempts to load a dictionary using the URL to the FSA file and the |
| 71 |
* expected metadata extension. |
| 72 |
* |
| 73 |
* @param dictURL The URL pointing to the dictionary file (<code>*.dict</code>). |
| 74 |
* @return An instantiated dictionary. |
| 75 |
* @throws IOException if an I/O error occurs. |
| 76 |
*/ |
| 77 |
public static Dictionary read(URL dictURL) throws IOException { |
| 78 |
final URL expectedMetadataURL; |
| 79 |
try { |
| 80 |
String external = dictURL.toExternalForm(); |
| 81 |
expectedMetadataURL = new URL(DictionaryMetadata.getExpectedMetadataFileName(external)); |
| 82 |
} catch (MalformedURLException e) { |
| 83 |
throw new IOException("Couldn't construct relative feature map URL for: " + dictURL, e); |
| 84 |
} |
| 85 |
|
| 86 |
try (InputStream fsaStream = dictURL.openStream(); |
| 87 |
InputStream metadataStream = expectedMetadataURL.openStream()) { |
| 88 |
return read(fsaStream, metadataStream); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Attempts to load a dictionary from opened streams of FSA dictionary data |
| 94 |
* and associated metadata. Input streams are not closed automatically. |
| 95 |
* |
| 96 |
* @param fsaStream The stream with FSA data |
| 97 |
* @param metadataStream The stream with metadata |
| 98 |
* @return Returns an instantiated {@link Dictionary}. |
| 99 |
* @throws IOException if an I/O error occurs. |
| 100 |
*/ |
| 101 |
public static Dictionary read(InputStream fsaStream, InputStream metadataStream) throws IOException { |
| 102 |
return new Dictionary(FSA.read(fsaStream), DictionaryMetadata.read(metadataStream)); |
| 103 |
} |
| 104 |
} |