edu.illinois.cs.cogcomp.lbj.coref.io
Class WNFileManager

java.lang.Object
  extended by edu.illinois.cs.cogcomp.lbj.coref.io.WNFileManager
All Implemented Interfaces:
FileManagerInterface, java.rmi.Remote

public class WNFileManager
extends java.lang.Object
implements FileManagerInterface

An implementation of FileManagerInterface that reads files from the local file system, including from jar files. The FileManager caches the file position before and after readLineAt in order to eliminate the redundant IO activity that a naive implementation of these methods would necessitate.

Instances of this class are guarded. Operations are synchronized by file.

Modifications (By Eric Bengtson) to this file change the type of file from RandomAccessFile to something compatible with InputStream so that getResourceAsStream can be used to enable loading of the files inside a jar.

Author:
Oliver Steele, steele@cs.brandeis.edu

Nested Class Summary
protected  class WNFileManager.NextLineCache
           
 
Field Summary
protected  java.util.Hashtable<java.lang.String,RandomAccessInput> filenameCache
           
protected static java.lang.String fileSeparator
           
protected static boolean IS_MAC_OS
           
protected static boolean IS_MW_VM
           
protected static boolean IS_WINDOWS_OS
           
protected  WNFileManager.NextLineCache nextLineCache
           
protected  java.lang.String searchDirectory
           
static boolean TRACE_LOOKUP
          Set this to true to enable debugging messages in getIndexedLinePointer.
static java.lang.String VERSION
          The API version, used by RemoteFileManager for constructing a binding name.
 
Constructor Summary
WNFileManager()
          Construct a file manager backed by a set of files contained in the default WN search directory.
WNFileManager(java.lang.String searchDirectory)
          Construct a file manager backed by a set of files contained in searchDirectory.
 
Method Summary
protected  int compare(java.lang.String a, java.lang.String b)
          Return a negative value if a precedes b, a positive value if a follows b, otherwise 0.
protected  RandomAccessInput getFileStream(java.lang.String filename)
          Loads database files located relative to the classpath.
 long getIndexedLinePointer(java.lang.String filename, java.lang.String target)
          Search for the line whose first word is index (that is, that begins with index followed by a space or tab).
 long getMatchingLinePointer(java.lang.String filename, long offset, java.lang.String substring)
          Search for a line whose index word contains substring.
 long getNextLinePointer(java.lang.String filename, long offset)
          Search for the line following the line that begins at offset.
protected static java.lang.String getWNHome()
           
protected static java.lang.String getWNSearchDir()
           
(package private) static java.lang.String mapToWindowsFilename(java.lang.String filename)
           
protected  java.lang.String readLine(RandomAccessInput stream)
           
 java.lang.String readLineAt(java.lang.String filename, long offset)
          Read the line that begins at file offset offset in the file named by filename.
protected  java.lang.String readLineWord(RandomAccessInput stream)
           
protected  void skipLine(RandomAccessInput stream)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VERSION

public static java.lang.String VERSION
The API version, used by RemoteFileManager for constructing a binding name.


TRACE_LOOKUP

public static final boolean TRACE_LOOKUP
Set this to true to enable debugging messages in getIndexedLinePointer.

See Also:
Constant Field Values

IS_MW_VM

protected static final boolean IS_MW_VM

searchDirectory

protected java.lang.String searchDirectory

filenameCache

protected java.util.Hashtable<java.lang.String,RandomAccessInput> filenameCache

nextLineCache

protected WNFileManager.NextLineCache nextLineCache

IS_WINDOWS_OS

protected static final boolean IS_WINDOWS_OS

IS_MAC_OS

protected static final boolean IS_MAC_OS

fileSeparator

protected static final java.lang.String fileSeparator
Constructor Detail

WNFileManager

public WNFileManager()
Construct a file manager backed by a set of files contained in the default WN search directory. The default search directory is the location named by the system property WNSEARCHDIR; or, if this is undefined, by the directory named WNHOME/Database (under MacOS) or WNHOME/dict (otherwise); or, if the WNHOME is undefined, the current directory (under MacOS), "C:\wn16" (WIndows), or "/usr/local/wordnet1.6" (otherwise).


WNFileManager

public WNFileManager(java.lang.String searchDirectory)
Construct a file manager backed by a set of files contained in searchDirectory.

Method Detail

getWNHome

protected static java.lang.String getWNHome()

getWNSearchDir

protected static java.lang.String getWNSearchDir()

mapToWindowsFilename

static java.lang.String mapToWindowsFilename(java.lang.String filename)

getFileStream

protected RandomAccessInput getFileStream(java.lang.String filename)
                                   throws java.io.IOException
Loads database files located relative to the classpath. Modifications from JWordNet code here by Eric Bengtson.

Throws:
java.io.IOException

readLine

protected java.lang.String readLine(RandomAccessInput stream)
                             throws java.io.IOException
Throws:
java.io.IOException

skipLine

protected void skipLine(RandomAccessInput stream)
                 throws java.io.IOException
Throws:
java.io.IOException

readLineAt

public java.lang.String readLineAt(java.lang.String filename,
                                   long offset)
                            throws java.io.IOException
Description copied from interface: FileManagerInterface
Read the line that begins at file offset offset in the file named by filename.

Specified by:
readLineAt in interface FileManagerInterface
Throws:
java.io.IOException

readLineWord

protected java.lang.String readLineWord(RandomAccessInput stream)
                                 throws java.io.IOException
Throws:
java.io.IOException

getNextLinePointer

public long getNextLinePointer(java.lang.String filename,
                               long offset)
                        throws java.io.IOException
Description copied from interface: FileManagerInterface
Search for the line following the line that begins at offset.

Specified by:
getNextLinePointer in interface FileManagerInterface
Returns:
The file offset of the start of the line, or -1 if offset is the last line in the file.
Throws:
java.io.IOException

getMatchingLinePointer

public long getMatchingLinePointer(java.lang.String filename,
                                   long offset,
                                   java.lang.String substring)
                            throws java.io.IOException
Description copied from interface: FileManagerInterface
Search for a line whose index word contains substring.

Specified by:
getMatchingLinePointer in interface FileManagerInterface
Returns:
The file offset of the start of the matchng line, or -1 if no such line exists.
Throws:
java.io.IOException

getIndexedLinePointer

public long getIndexedLinePointer(java.lang.String filename,
                                  java.lang.String target)
                           throws java.io.IOException
Description copied from interface: FileManagerInterface
Search for the line whose first word is index (that is, that begins with index followed by a space or tab). filename must name a file whose lines are sorted by index word.

Specified by:
getIndexedLinePointer in interface FileManagerInterface
Returns:
The file offset of the start of the matching line, or -1 if no such line exists.
Throws:
java.io.IOException

compare

protected int compare(java.lang.String a,
                      java.lang.String b)
Return a negative value if a precedes b, a positive value if a follows b, otherwise 0.