/* * [AllDicts.java] * * Summary: All dictionaries that can lookup English to Esperanto. * * 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; /** * All dictionaries that can lookup English to Esperanto. *

* No prefix/suffix logic. We chain them together to make them * look like one. * * @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 AllDicts implements Lookup { /** * cache of words previously looked up */ private Lookup cache; /** * Utwente online dictionary */ private Lookup englishToEsperanto; /** * local dictionary from Roedy, Geneva, Ergane */ private Lookup mini; /** * words we tried looking up before and failed */ private Lookup unknown; /** * public constructor */ public AllDicts() { } /** * Save any changes to this dictionary, and disconnect from it. */ public void close() { cache.close(); englishToEsperanto.close(); mini.close(); unknown.close(); } // 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 ) { } // declare lookup objects /** * Is it possible to define new words in this dictionary? * * @return true if you can add definitions, false otherwise. */ public boolean definePossible() { return false; } /** * 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 ) { // convert it to lower case word = word.toLowerCase(); // look it up it various local dictionaries first // start with cache String translation = cache.lookup( word ); if ( translation == null ) { // main local dict translation = mini.lookup( word ); } if ( translation == null ) { // if we find it in the nekonato dict, there is no point in looking // it up // on the web. Contains previous failures. translation = unknown.lookup( word ); // If we find it, we find a dummy entry "?" } if ( translation == null ) { // look up in web dictionary translation = englishToEsperanto.lookup( word ); if ( translation != null && translation.length() < 200 ) { // add to local cache dictionary cache.define( word, translation ); } else { // add to local unknown words dictionary translation = null; unknown.define( word, "?" ); } } // correct dummy nekonato entry if ( translation != null && translation.equals( "?" ) ) { translation = null; } return translation; } /** * Open a connection to this dictionary. */ public void open() { // get translation lookup objects unknown = new LocalDict( "unknown.dict", false ); unknown.open(); cache = new LocalDict( "cache.dict", false ); cache.open(); englishToEsperanto = new UtwenteENtoEO(); englishToEsperanto.open(); mini = new LocalDict( "mini.dict", false ); mini.open(); } // end open } // end AllDicts