/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Community System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.htmlcheck.dict;

import java.io.*;

public class LexiconBuilder
{
  private LexiconBuilder()
  { // cannot construct this object
  } // end constructor

  private static final String USAGE = "Usage: LexiconBuilder [-c] outputfile inputfile [...]";

  public static void main(String args[])
  {
    if (args.length==0)
    { // print the usage string and exit
      System.err.println(USAGE);
      System.exit(1);

    } // end if

    boolean compress = false;
    int ofx = 0;
    if (args[0].equals("-c"))
    { // we're compressing the output
      compress = true;
      if (++ofx>=args.length)
      { // no output filename specified!
	System.err.println("No output file specified!");
	System.err.println(USAGE);
	System.exit(1);

      } // end if

    } // end if

    int ifx = ofx + 1;
    if (ifx>=args.length)
    { // no input filename specified!
      System.err.println("No input file specified!");
      System.err.println(USAGE);
      System.exit(1);

    } // end if

    Lexicon lex = new Lexicon();  // initialize lexicon object

    while (ifx<args.length)
    { // load each file in turn
      try
      { // start reading the file
	BufferedReader rdr = new BufferedReader(new FileReader(args[ifx]));
	System.out.println("Loading from file " + args[ifx]);

	int ctr = 0;
	String word = rdr.readLine();
	while (word!=null)
	{ // add each word to the lexicon as we go
	  lex.addWord(word);
	  if (((++ctr) % 1000)==0)
	    System.out.println("...read " + String.valueOf(ctr) + " words");
	  word = rdr.readLine();

	} // end while

	rdr.close();
	System.out.println("Finished reading " + args[ifx] + ", " + String.valueOf(ctr) + " words read");

      } // end try
      catch (IOException e)
      { // just die on IOException
	System.err.println("Error loading " + args[ifx] + ": " + e.getMessage());
	System.exit(1);

      } // end catch

      ifx++;  // go to next file

    } // end while

    System.out.println("All input files loaded, " + String.valueOf(lex.size()) + " words in total");

    try
    { // save the lexicon to its very own file
      lex.saveToFile(args[ofx],compress);

    } // end try
    catch (IOException e)
    { // just die on IOException
      System.err.println("Error writing to " + args[ofx] + ": " + e.getMessage());
      System.exit(1);

    } // end catch

    System.out.println("Wrote " + args[ofx] + ", LexiconBuilder complete.");
    System.exit(0);

  } // end main

} // end class LexiconBuilder
