import java.io.*;
import gov.nih.nlm.nls.utils.U;         // <-------This comes from the gspell.jar
import gov.nih.nlm.nls.gspell.GSpell;   // <-------This comes from the gspell.jar
import gov.nih.nlm.nls.gspell.Candidate; // <------This comes from the gspell.jar


/* ================================================|PSuggest.java |==== */
/**
* 
* Suggest is an example of how I intend to have the gspell be used.
* 
* GSpell is a spelling suggestion tool. It indexes a set of words or
* terms, and provides a retrieval mechanism to retrieve close suggestions
* to input terms. Java API's are provided for developers to embed this
* application in their applications. This example is to show how to
* retreive suggested terms using the api's. This example assumes that the
* input terms come from standard input. This example will have the
* name of the created dictionary passed in from command line arguments. 

*/
/* ================================================|Suggest.java |==== */
public final class PSuggest
{


// ================================================|Main Method|====
/**
 * main 
 * 
 * @param String argv[] 	The command line input, tokenized
 *
*/
// ================================================|Main Method|====

  public final static void main( String argv[] )
  {

    Candidate    candidates[] = null;
    String              aTerm = null;

    // ---------------------------------------------
    // Get the command line options 
    //   --dictionary=
    //   --help
    // ---------------------------------------------
    try {
      getOpts( argv );
      
    } catch (Exception e ) {
      _usage();
      System.exit(-1);
    }
    
    
    try {
      // -------------------------------------------------
      // Initialize the spelling suggestion system
      // -------------------------------------------------
      GSpell gspell = new GSpell( _dictionaryName, GSpell.READ_ONLY );

      // -------------------------------------------------
      // I grab hold of some term, say via standard input
      // -------------------------------------------------
      BufferedReader stdIn = U.openStandardInput();
      
      while (( aTerm = stdIn.readLine()) != null ) {
	
	// -------------------------------------------------
	// I want to look this term up in the dictionary
	// to see if it exists, or any close neigbhors
	// -------------------------------------------------
	candidates = gspell.find( aTerm );
	
	
	if ( candidates != null ) {
	  // -------------------------------------------------
	  // I display X number of these candidates
	  //    A candidate has the following methods:
	  //           String getName()     the suggested term 
	  //           int    getRank()     the edit distance from the term 
	  //           String getMethod()   the suggestion method 
	  //           String getMessage()  if rank is 0, will display "correct"
	  //           String toString()   
	  // -------------------------------------------------
	  for ( int i = 0; i < candidates.length; i++ ) {
	    System.out.println( aTerm + "|" + candidates[i].toString() );
	  }
	} else {
	  // -------------------------------------------------
	  // Yes, there can be cases where there are no candidates,
	  // that is, candidates will be null. 
	  // Display a suggestionless message
	  // -------------------------------------------------
	  System.out.println( aTerm + "|" + "|" + -9999 + "|" + "|" + "No Suggestions");
	}
	
      } // End of loop through standard input
      
      // -------------------------------------------------
      // cleanup. Close files. 
      // -------------------------------------------------
      gspell.cleanup();
      gspell = null;
    
    } catch ( Exception e ) {
      e.printStackTrace();
    }

   } //*** End main ( String argv[] )

// ================================================|Usage Header |====
/**
 *getOpts
 * This method looks for the  dictionary name and full path to the
 * input file from command line arguments. If either are not found,
 * this method will throw an exception. For simplisity, the values
 * are put into private static variables _dictionaryName and 
 * _inputFileName.
 *	
 * @param String[] argv
 * @exception 
 *
*/
// ================================================|Usage Header |====
  private static final void getOpts( String[] argv ) throws Exception
  {

    String anOption = null;

    for ( int i = 0; i < argv.length; i++ ) {
      anOption = argv[i];
      
      if ( anOption.equals("--help") ) {
	throw new Exception();
	
      } else if ( anOption.indexOf("--dictionary=")> -1 ) {
	_dictionaryName= anOption.substring(13);
	
      }
    }
    if ( _dictionaryName == null )
      throw new Exception();
    
    
  } //*** End getOpts()

// ================================================|Usage Header |====
/**
 *	_usage();
 *	
 *
*/
// ================================================|Usage Header |====
  private static final void _usage()
  {


    StringBuffer buff = new StringBuffer();
    
    buff.append(U.NL);
    buff.append(U.NL);
    buff.append("GSpell Suggest Example:"); 
    buff.append(U.NL);
    buff.append(U.NL);
    buff.append("GSpell is a spelling suggestion tool. It indexes a set of words or");
    buff.append(U.NL);
    buff.append("terms, and provides a retrieval mechanism to retrieve close suggestions");
    buff.append(U.NL);
    buff.append("to input terms. ");
    buff.append(U.NL);
    buff.append("This example is to show how to retreive suggestions using the api's. ");
    buff.append(U.NL);
    buff.append("This example assumes that the input terms come from an input file. ");
    buff.append(U.NL);
    buff.append("This example will have the name of the created dictionary passed ");
    buff.append(U.NL);
    buff.append("in from command line arguments. ");

    buff.append(U.NL);

    buff.append("Dictionary directories containing the indexes created are created");
    buff.append(U.NL);
    buff.append("and placed in the directory 'dictionaries/NameOfDoctionary' " );
    buff.append(U.NL);
    buff.append("The application will search the current classpath for the first");
    buff.append(U.NL);
    buff.append("instance of the directory labeled 'dictionaries' to find this");
    buff.append(U.NL);
    buff.append("location. If no dictionaries directory is found in the classpath,");
    buff.append(U.NL);
    buff.append("This program should fail.");
    buff.append(U.NL);
    buff.append("Each dictionary's properties file contains the ");
    buff.append(U.NL);
    buff.append("default settings for tunable parameters along with parameters that ");
    buff.append(U.NL);
    buff.append("the application needs to run.");
    buff.append(U.NL);
    buff.append(U.NL);
    buff.append(U.NL);
    buff.append( "Usage:");
    buff.append(U.NL);
    buff.append( "   java Suggest --dictionary=XXXX [--help]");
    buff.append(U.NL);
    buff.append( "      --dictionary=XXXXX  The name of dictionary to retrieve suggestions from.");
    buff.append(U.NL);
    buff.append( "      --help              This help.");
    buff.append(U.NL);
    
    System.out.println( buff.toString() );

 

    } //*** End usage()


  // ====================
  // Private Declarations
  // ====================
    private static String _dictionaryName = null;

} // End of the Class Suggest