public class RAMDictionary extends Object implements IRAMDictionary
Default implementation of the IRAMDictionary interface. This
implementation is designed to wrap an arbitrary dictionary object; however,
convenience constructors are provided for the most common use cases:
Note: If you receive an OutOfMemoryError while using this
object (this can occur on 32 bit JVMs), try increasing your heap size, for
example, by using the -Xmx switch.
| Modifier and Type | Class and Description |
|---|---|
static class |
RAMDictionary.DataLoader
A
Callable that creates a dictionary data from a specified
dictionary. |
static class |
RAMDictionary.DictionaryData
Object that holds all the dictionary data loaded from the Wordnet files.
|
protected class |
RAMDictionary.HotSwappableExceptionEntryIterator
A hot swappable iterator that iterates over exceptions entries for a
particular part of speech.
|
protected class |
RAMDictionary.HotSwappableIndexWordIterator
A hot swappable iterator for index words.
|
protected class |
RAMDictionary.HotSwappableIterator<E>
An iterator that allows the dictionary to be loaded into memory while it
is iterating.
|
protected class |
RAMDictionary.HotSwappableSenseEntryIterator
A hot swappable iterator that iterates over sense entries.
|
protected class |
RAMDictionary.HotSwappableSynsetIterator
A hot swappable iterator for synsets.
|
protected class |
RAMDictionary.JWIBackgroundDataLoader
This runnable loads the dictionary data into memory and sets the
appropriate variable in the parent dictionary.
|
IRAMDictionary.FileInputStreamFactory, IRAMDictionary.IInputStreamFactory, IRAMDictionary.URLInputStreamFactoryIHasLifecycle.LifecycleState, IHasLifecycle.ObjectClosedException, IHasLifecycle.ObjectOpenException| Modifier and Type | Field and Description |
|---|---|
protected IDictionary |
backing |
protected RAMDictionary.DictionaryData |
data |
static int |
defaultLoadPolicy
The default load policy of a
RAMDictionary is to load data in the
background when opened. |
protected IRAMDictionary.IInputStreamFactory |
factory |
protected Lock |
lifecycleLock |
protected Thread |
loader |
protected Lock |
loadLock |
protected int |
loadPolicy |
protected IHasLifecycle.LifecycleState |
state |
BACKGROUND_LOAD, IMMEDIATE_LOAD, NO_LOAD| Modifier | Constructor and Description |
|---|---|
|
RAMDictionary(File file)
Constructs a new wrapper RAM dictionary that will load the contents the
specified local Wordnet data, with the specified load policy.
|
|
RAMDictionary(File file,
int loadPolicy)
Loads data from the specified File using the specified load policy.
|
|
RAMDictionary(IDictionary dict,
int loadPolicy)
Constructs a new RAMDictionary that will load the contents of
the wrapped dictionary into memory, with the specified load policy.
|
protected |
RAMDictionary(IDictionary backing,
IRAMDictionary.IInputStreamFactory factory,
int loadPolicy)
This is a internal constructor that unifies the constructor decision
matrix.
|
|
RAMDictionary(IRAMDictionary.IInputStreamFactory factory)
Constructs a new RAMDictionary that will load an in-memory image from the
specified stream factory.
|
|
RAMDictionary(URL url)
Constructs a new RAMDictionary that will load the contents the specified
Wordnet data using the default load policy.
|
|
RAMDictionary(URL url,
int loadPolicy)
Loads data from the specified URL using the specified load policy.
|
| Modifier and Type | Method and Description |
|---|---|
protected IHasLifecycle.LifecycleState |
assertLifecycleState()
This is an internal utility method that determines whether this
dictionary should be considered open or closed.
|
void |
close()
This closes the object by disposing of data backing objects or
connections.
|
static IDictionary |
createBackingDictionary(File file)
Creates a
DataSourceDictionary out of the specified file, as long
as the file points to an existing local directory. |
static IDictionary |
createBackingDictionary(URL url)
Creates a
DataSourceDictionary out of the specified url, as long
as the url points to an existing local directory. |
static IRAMDictionary.IInputStreamFactory |
createInputStreamFactory(File file)
Creates an input stream factory out of the specified File.
|
static IRAMDictionary.IInputStreamFactory |
createInputStreamFactory(URL url)
Creates an input stream factory out of the specified URL.
|
static boolean |
export(File in,
OutputStream out)
This is a convenience method that transforms a Wordnet dictionary at the
specified file location into a in-memory image written to the specified
output stream.
|
static boolean |
export(IRAMDictionary.IInputStreamFactory in,
OutputStream out)
This is a convenience method that transforms a Wordnet dictionary drawn
from the specified input stream factory into a in-memory image written to
the specified output stream.
|
protected static boolean |
export(IRAMDictionary dict,
OutputStream out)
Exports a specified RAM Dictionary object to the specified output stream.
|
void |
export(OutputStream out)
Exports the in-memory contents of the to the specified output stream.
|
static boolean |
export(URL in,
OutputStream out)
This is a convenience method that transforms a Wordnet dictionary at the
specified url location into a in-memory image written to the specified
output stream.
|
IDictionary |
getBackingDictionary()
Returns the dictionary that backs this instance.
|
Charset |
getCharset()
Returns the character set associated with this object.
|
IExceptionEntry |
getExceptionEntry(IExceptionEntryID id)
Retrieves the exception entry for the specified id from the database.
|
IExceptionEntry |
getExceptionEntry(String surfaceForm,
POS pos)
Retrieves the exception entry for the specified surface form and part of
speech from the database.
|
Iterator<IExceptionEntry> |
getExceptionEntryIterator(POS pos)
Returns an iterator that will iterate over all exception entries of the
specified part of speech.
|
IIndexWord |
getIndexWord(IIndexWordID id)
Retrieves the specified index word object from the database.
|
IIndexWord |
getIndexWord(String lemma,
POS pos)
This method is identical to
getIndexWord(IIndexWordID) and
is provided as a convenience. |
Iterator<IIndexWord> |
getIndexWordIterator(POS pos)
Returns an iterator that will iterate over all index words of the
specified part of speech.
|
int |
getLoadPolicy()
Returns the load policy for this object, expressed as an integer.
|
ISenseEntry |
getSenseEntry(ISenseKey key)
Retrieves the sense entry for the specified sense key from the database.
|
Iterator<ISenseEntry> |
getSenseEntryIterator()
Returns an iterator that will iterate over all sense entries in the
dictionary.
|
IRAMDictionary.IInputStreamFactory |
getStreamFactory()
Returns the stream factory that backs this instance; may be
null. |
ISynset |
getSynset(ISynsetID id)
Retrieves the synset with the specified id from the database.
|
Iterator<ISynset> |
getSynsetIterator(POS pos)
Returns an iterator that will iterate over all synsets of the specified
part of speech.
|
IVersion |
getVersion()
Returns the associated version for this object.
|
IWord |
getWord(ISenseKey key)
Retrieves the word with the specified sense key from the database.
|
IWord |
getWord(IWordID id)
Retrieves the word with the specified id from the database.
|
boolean |
isLoaded()
Returns whether this object is loaded or not.
|
boolean |
isOpen()
Returns
true if the dictionary is open, that is, ready to
accept queries; returns false otherwise |
void |
load()
Starts a simple, non-blocking load.
|
void |
load(boolean block)
Initiates the loading process.
|
boolean |
open()
This opens the object by performing any required initialization steps.
|
void |
setCharset(Charset charset)
Sets the character set associated with this dictionary.
|
void |
setLoadPolicy(int policy)
Sets the load policy for this object.
|
public static int defaultLoadPolicy
RAMDictionary is to load data in the
background when opened.protected final IDictionary backing
protected final IRAMDictionary.IInputStreamFactory factory
protected final Lock lifecycleLock
protected final Lock loadLock
protected volatile IHasLifecycle.LifecycleState state
protected transient Thread loader
protected int loadPolicy
protected RAMDictionary.DictionaryData data
public RAMDictionary(File file)
file - a file pointing to a local copy of wordnet; may not be
nullNullPointerException - if the specified file is nullpublic RAMDictionary(URL url)
url - a url pointing to a local copy of wordnet; may not be
nullNullPointerException - if the specified url is nullpublic RAMDictionary(File file, int loadPolicy)
file - a file pointing to a local copy of wordnet; may not be
nullloadPolicy - the load policy of the dictionary; see constants in
ILoadPolicy. Note that if the file points to to a
resource that is the exported image of an in-memory
dictionary, the specified load policy is ignored: the
dictionary is loaded into memory immediately.NullPointerException - if the specified file is nullILoadPolicypublic RAMDictionary(URL url, int loadPolicy)
url - a url pointing to a local copy of wordnet; may not be
nullloadPolicy - the load policy of the dictionary; see constants in
ILoadPolicy. Note that if the url points to to a
resource that is the exported image of an in-memory
dictionary, the specified load policy is ignored: the
dictionary is loaded into memory immediately.NullPointerException - if the specified url is nullILoadPolicypublic RAMDictionary(IDictionary dict, int loadPolicy)
dict - the dictionary to be wrapped, may not be nullloadPolicy - the load policy of the dictionary; see constants in
ILoadPolicy.ILoadPolicypublic RAMDictionary(IRAMDictionary.IInputStreamFactory factory)
factory - the stream factory that provides the stream; may not be nullNullPointerException - if the factory is nullprotected RAMDictionary(IDictionary backing, IRAMDictionary.IInputStreamFactory factory, int loadPolicy)
null, otherwise an exception is thrown. If the
factory is non-null, the dictionary will ignore the
specified load policy and set the load policy to "immediate load".backing - the backing dictionary; may be nullfactory - the input stream factory; may be nullloadPolicy - the load policypublic IDictionary getBackingDictionary()
null.public IRAMDictionary.IInputStreamFactory getStreamFactory()
null.nullpublic void setCharset(Charset charset)
IDictionarynull.setCharset in interface IDictionarycharset - the possibly null character set to use when
decoding files.public Charset getCharset()
IHasCharsetnull.getCharset in interface IHasCharsetnullpublic int getLoadPolicy()
ILoadPolicygetLoadPolicy in interface ILoadPolicypublic void setLoadPolicy(int policy)
ILoadPolicysetLoadPolicy in interface ILoadPolicypolicy - the policy to implement; may be one of NO_LOAD,
BACKGROUND_LOAD, IMMEDIATE_LOAD or
an implementation-dependent value.public boolean isLoaded()
ILoadabletrue only if the loading process has completed and the
object is actually loaded; if the object is still in the process of
loading, or failed to load, the method should return false.public void load()
ILoadablepublic void load(boolean block)
throws InterruptedException
ILoadableblock is false), or return
only when the loading process is complete. If the object is already
loaded, the method returns immediately and has no effect. If the object
is in the process of loading, and the method is called in blocking mode,
the method blocks until loading is complete, even if that call of the
method did not initiate the loading process. Some implementors of this
interface may not support the immediate-return functionality.load in interface ILoadableblock - if true, the method returns only when the loading
process is complete; if false, the method returns
immediately.InterruptedException - if the method is blocking, and is interrupted while waiting
for loading to completepublic boolean open()
throws IOException
IHasLifecyclefalse, then subsequent calls to
IHasLifecycle.isOpen() will return false.open in interface IHasLifecycletrue if there were no errors in initialization;
false otherwise.IOException - if there was IO error while performing initializataionpublic boolean isOpen()
IHasLifecycletrue if the dictionary is open, that is, ready to
accept queries; returns false otherwiseisOpen in interface IHasLifecycletrue if the object is open; false
otherwisepublic void close()
IClosableprotected final IHasLifecycle.LifecycleState assertLifecycleState()
public void export(OutputStream out) throws IOException
IRAMDictionaryexport in interface IRAMDictionaryout - the output stream to which the in-memory data will be written;
may not be nullIOException - if there is a problem writing the in-memory data to the
output stream.public IVersion getVersion()
IHasVersionnull.getVersion in interface IHasVersionnull if none.public IIndexWord getIndexWord(String lemma, POS pos)
IDictionarygetIndexWord(IIndexWordID) and
is provided as a convenience.getIndexWord in interface IDictionarylemma - the lemma for the index word requested; may not be
null, empty, or all whitespacepos - the part of speech; may not be nullnull if none is foundpublic IIndexWord getIndexWord(IIndexWordID id)
IDictionarynull.
Note: This call does no stemming on the specified lemma, it is
taken as specified. That is, if you submit the word "dogs", it will
search for "dogs", not "dog"; in the standard Wordnet distribution, there
is no entry for "dogs" and therefore the call will return
null. This is in contrast to the Wordnet API provided by
Princeton. If you want your searches to capture morphological variation,
use the descendants of the IStemmer class.
getIndexWord in interface IDictionaryid - the id of the index word to search for; may not be
nullnull otherwisepublic Iterator<IIndexWord> getIndexWordIterator(POS pos)
IDictionarygetIndexWordIterator in interface IDictionarypos - the part of speech over which to iterate; may not be
nullpublic IWord getWord(IWordID id)
IDictionarynullgetWord in interface IDictionaryid - the id of the word to search for; may not be nullnull otherwisepublic IWord getWord(ISenseKey key)
IDictionarynullgetWord in interface IDictionarykey - the sense key of the word to search for; may not be
nullnull otherwisepublic ISynset getSynset(ISynsetID id)
IDictionarynullgetSynset in interface IDictionaryid - the id of the synset to search for; may not be
nullnull otherwisepublic Iterator<ISynset> getSynsetIterator(POS pos)
IDictionarygetSynsetIterator in interface IDictionarypos - the part of speech over which to iterate; may not be
nullpublic ISenseEntry getSenseEntry(ISenseKey key)
IDictionarynullgetSenseEntry in interface IDictionarykey - the sense key of the entry to search for; may not be
nullnull otherwisepublic Iterator<ISenseEntry> getSenseEntryIterator()
IDictionarygetSenseEntryIterator in interface IDictionarypublic IExceptionEntry getExceptionEntry(String surfaceForm, POS pos)
IDictionarynullgetExceptionEntry in interface IDictionarysurfaceForm - the surface form to be looked up; may not be null
, empty, or all whitespacepos - the part of speech; may not be nullnull otherwisepublic IExceptionEntry getExceptionEntry(IExceptionEntryID id)
IDictionarynullgetExceptionEntry in interface IDictionaryid - the exception entry id of the entry to search for; may not be
nullpublic Iterator<IExceptionEntry> getExceptionEntryIterator(POS pos)
IDictionarygetExceptionEntryIterator in interface IDictionarypos - the part of speech over which to iterate; may not be
nullpublic static IRAMDictionary.IInputStreamFactory createInputStreamFactory(File file)
null.file - the file out of which to make an input stream factory; may not
be nullnull if the url
points to a local directory.NullPointerException - if the specified file is nullpublic static IRAMDictionary.IInputStreamFactory createInputStreamFactory(URL url)
null.url - the url out of which to make an input stream factory; may not
be nullnull if the url
points to a local directory.NullPointerException - if the specified url is nullpublic static IDictionary createBackingDictionary(File file)
DataSourceDictionary out of the specified file, as long
as the file points to an existing local directory.file - the local directory for which to create a data source
dictionary; may not be nullnullNullPointerException - if the specified file is nullpublic static IDictionary createBackingDictionary(URL url)
DataSourceDictionary out of the specified url, as long
as the url points to an existing local directory.url - the local directory for which to create a data source
dictionary; may not be nullnullNullPointerException - if the specified url is nullpublic static boolean export(File in, OutputStream out) throws IOException
in - the file from which the Wordnet data should be loaded; may not
be nullout - the output stream to which the Wordnet data should be written;
may not be nulltrue if the export was successfulNullPointerException - if either argument is nullIOException - if there is an IO problem when opening or exporting the
dictionary.public static boolean export(URL in, OutputStream out) throws IOException
in - the url from which the Wordnet data should be loaded; may not
be nullout - the output stream to which the Wordnet data should be written;
may not be nulltrue if the export was successfulNullPointerException - if either argument is nullIOException - if there is an IO problem when opening or exporting the
dictionary.public static boolean export(IRAMDictionary.IInputStreamFactory in, OutputStream out) throws IOException
in - the file from which the Wordnet data should be loaded; may not
be nullout - the output stream to which the Wordnet data should be written;
may not be nulltrue if the export was successfulNullPointerException - if either argument is nullIOException - if there is an IO problem when opening or exporting the
dictionary.protected static boolean export(IRAMDictionary dict, OutputStream out) throws IOException
dict - the dictionary to be exported; the dictionary will be closed
at the end of the method.out - the output stream to which the data will be written.true if the export was successfulIOException - if there was a IO problem during exportCopyright © 2018. All rights reserved.