/* * [UtwenteENtoEO.java] * * Summary: utwente Esperanto to English. * * 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.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; /** * utwente Esperanto to English. * * @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 UtwenteENtoEO implements Lookup { /** * Save any changes to this dictionary, and disconnect from it. */ public void 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 ) { } /** * Is it possible to define new words in this dictionary? * * @return true if you can add definitions, false otherwise. */ public boolean definePossible() { return false; } /** * Source Language * * @return language translates from e.g. English, Esperanto */ @SuppressWarnings( { "SameReturnValue" } ) public String fromLanguage() { return "English"; } /** * 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. * * @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 ) { URL url; try { url = new URL( "http://wwwtios.cs.utwente.nl/traduk/EN-EO/Traduku/?" + word ); } catch ( MalformedURLException e ) { return null; } try { final BufferedInputStream bis = new BufferedInputStream( url.openStream() ); final InputStreamReader isr = new InputStreamReader( bis ); final BufferedReader br = new BufferedReader( isr, 4 * 1024 ); // ignore first six lines of crap for ( int i = 0; i < 6; i++ ) { if ( br.readLine() == null ) { return null; } } String result = br.readLine(); // result is messy, e.g. // kat/o cat, ~ido kitten // or // No match found in the vortaro. br.close(); if ( result.equals( "No match found in the vortaro." ) ) { return null; } // prune trailing if ( result.endsWith( "" ) ) { result = result.substring( 0, result.length() - 6 ); } // prune trailing

if ( result.endsWith( "

" ) ) { result = result.substring( 0, result.length() - 3 ); } return result.trim(); } catch ( IOException e ) { return null; } catch ( StringIndexOutOfBoundsException e ) { return null; } } /** * Open a connection to this dictionary. */ public void open() { } /** * Target Language * * @return language translates to e.g. English, Esperanto */ @SuppressWarnings( { "SameReturnValue" } ) public String toLanguage() { return "Esperanto"; } } // end UtwentENtoEO