/* * [LocalDict.java] * * Summary: locally resident dictionary, implemented as a Property Hashtable. * * Copyright: (c) 2000-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 2.2 2008-04-06 add build to title, tidy code, fix spelling errors. */ package com.mindprod.esper; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Properties; import static java.lang.System.*; /** * locally resident dictionary, implemented as a Property Hashtable. * * @author Roedy Green, Canadian Mind Products * @version 2.2 2008-04-06 add build to title, tidy code, fix spelling errors. * @since 2000 */ public final class LocalDict implements Lookup { /** * name of dictionary on disk or in jar, e.g. vortaro.dict */ private final String dictName; /** * true if the dictionary data can be found in the jar file, false in the default directory. */ private final boolean inJar; /** * Property Hashtable to hold the dictionary in RAM, not HashMap, legacy interface. */ private Properties h; /** * have there been any changes to the in-RAM copy of the dict since it was loaded? */ private boolean changed = false; /** * public constructor * * @param dictName name of dictionary on disk. * @param inJar true if dictionary data can be found in the jar, false if in the current directory. */ public LocalDict( String dictName, boolean inJar ) { this.dictName = dictName; this.inJar = inJar; } /** * Save any changes to this dictionary, and disconnect from it. */ public void close() { if ( changed ) { save(); } h = null; } // end close /** * Add a definition to the dictionary. * * @param word The word to be defined in the source language. If word already exists in dictionary, new * definition replaces. * @param definition The definition of the word in the target language. If null, removes existing definition. */ public void define( String word, String definition ) { if ( !inJar ) {// would not be able to save back to the jar. h.put( word, definition ); changed = true; } } /** * Is it possible to define new words in this dictionary? * * @return true if you can add definitions, false otherwise. */ public boolean definePossible() { return !inJar; } /** * Looks up a word in a dictionary. It does not do prefix or suffix splitting to help find words. That is the * responsibility of other levels. * * @param word Word to translate. Lead and trailing blanks removed. accents rendered with trailing x convention, at * least for now. Eventually will switch to unicode. * * @return word or phrase for the word translated, possibly with valid HTML, but no excess garbage characters. null * if no translation found. */ public String lookup( String word ) { return ( String ) h.get( word ); } /** * Open a connection to this dictionary. */ public void open() { h = new Properties(); try { InputStream is; if ( inJar ) { // look in jar is = LocalDict.class.getResourceAsStream( dictName ); } else {// look in default directory is = new FileInputStream( dictName ); } if ( is != null ) { // load entire dictionary into RAM h.load( is ); is.close(); } } catch ( IOException e ) { // just have an empty dictionary } } // end open /** * save dictionary in a separate file on disk, whether or not it has changed. */ public void save() { try { OutputStream os = new FileOutputStream( dictName ); // save entire dictionary h.store( os, null ); os.close(); } catch ( IOException e ) { err.println( "unable to save " + dictName ); } } } // end LocalDict