/* LanguageTool, a natural language style checker 
 * Copyright (C) 2013 Daniel Naber (http://www.danielnaber.de)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
package org.languagetool.dev;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List;
import java.util.Scanner;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.languagetool.Language;
import org.languagetool.tokenizers.Tokenizer;

/**
 * Create a Morfologik spelling binary dictionary from plain text data.
 */
final class SpellDictionaryBuilder extends DictionaryBuilder {

  public SpellDictionaryBuilder(File infoFile) throws IOException {
    super(infoFile);
  }

  private static CommandLine parseArguments(String[] args) throws ParseException {
    Options options = new Options();
    Option option = new Option("o", true, "output file");
    option.setRequired(true);
    options.addOption(option);
    
    CommandLineParser parser = new BasicParser();
    CommandLine cmd = parser.parse(options, args);
    return cmd;
  }
  
  public static void main(String[] args) throws Exception {
    CommandLine cmdLine = SpellDictionaryBuilder.parseArguments(args);
    checkUsageOrExit(SpellDictionaryBuilder.class.getSimpleName(), cmdLine);
    
    String languageCode = args[0];
    String plainTextFile = args[1];
    String infoFile = args[2];
    SpellDictionaryBuilder builder = new SpellDictionaryBuilder(new File(infoFile));
    builder.setOutputFilename(cmdLine.getOptionValue("o"));
    
    if (args.length >= 4) {
      String freqListFile = args[3];
      builder.readFreqList(new File(freqListFile));
      builder.build(languageCode, builder.addFreqData(new File(plainTextFile)));
    } else {
      builder.build(languageCode, new File(plainTextFile));
    }
  }

  private static void checkUsageOrExit(String className, CommandLine cmdLine) throws IOException {
    String[] args = cmdLine.getArgs();
    if (args.length < 3 || args.length > 4 || ! cmdLine.hasOption("o")) {
      System.out.println("Usage: " + className + " <languageCode> <dictionary> <infoFile> [frequencyList] -o <outputFile>");
      System.out.println("   <languageCode> like 'en-US' or 'de-DE'");
      System.out.println("   <dictionary> is a plain text dictionary file, e.g. created from a Hunspell dictionary by 'unmunch'");
      System.out.println("   <infoFile> is the *.info properties file, see http://wiki.languagetool.org/developing-a-tagger-dictionary");
      System.out.println("   [frequencyList] is the *.xml file with a frequency wordlist, see http://wiki.languagetool.org/developing-a-tagger-dictionary");
      System.exit(1);
    }
    File dictFile = new File(args[2]);
    if (!dictFile.exists()) {
      throw new IOException("File does not exist: " + dictFile);
    }
  }

  File build(String languageCode, File plainTextDictFile) throws Exception {
    Language language = Language.getLanguageForShortName(languageCode);
    File tempFile = null;
    try {
      tempFile = tokenizeInput(plainTextDictFile, language);
      return buildDict(tempFile, language);
    } finally {
      if (tempFile != null) {
        tempFile.delete();
      }
    }
  }

  private File tokenizeInput(File plainTextDictFile, Language language) throws IOException {
    Tokenizer wordTokenizer = language.getWordTokenizer();
    String encoding = getOption("fsa.dict.encoding");
    String separatorChar = hasOption("fsa.dict.separator") ? getOption("fsa.dict.separator") : "";
    File tempFile = File.createTempFile(SpellDictionaryBuilder.class.getSimpleName(), ".txt");
    try (Scanner scanner = new Scanner(plainTextDictFile, encoding)) {
      try (Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempFile), encoding))) {
        while (scanner.hasNextLine()) {
          String line = scanner.nextLine();
          int sepPos = separatorChar.isEmpty() ? -1 : line.indexOf(separatorChar);
          String occurrences = sepPos != -1 ? line.substring(sepPos + separatorChar.length()) : "";
          String lineWithoutOcc = sepPos != -1 ? line.substring(0, sepPos) : line;
          List<String> tokens = wordTokenizer.tokenize(lineWithoutOcc);
          for (String token : tokens) {
            if (token.length() > 0) {
              out.write(token);
              if (sepPos != -1) {
                out.write(separatorChar);
                if (tokens.size() == 1) {
                  out.write(occurrences);
                } else {
                  // TODO: as the word occurrence data from
                  // https://github.com/mozilla-b2g/gaia/tree/master/apps/keyboard/js/imes/latin/dictionaries
                  // has already been assigned in a previous step, we now cannot just use
                  // that value after having changed the tokenization...
                  out.write("A");  // assume least frequent
                }
              }
              out.write("\n");
            }
          }
        }
      }
    }
    return tempFile;
  }

}

@@ -2520,38 +2520,8 @@
    %0A
-    if (args.length %3E= 4) %7B%0A  
     
@@ -2551,16 +2551,53 @@
 rgs%5B3%5D;%0A
+    if (!freqListFile.equals(%22-%22)) %7B%0A
       bu
@@ -2968,27 +2968,8 @@
 th %3C
- 3 %7C%7C args.length %3E
  4 %7C
@@ -3088,17 +3088,17 @@
 foFile%3E 
-%5B
+%3C
 frequenc
@@ -3102,17 +3102,17 @@
 encyList
-%5D
+%3E
  -o %3Cout
@@ -3492,17 +3492,17 @@
 tln(%22   
-%5B
+%3C
 frequenc
@@ -3506,17 +3506,17 @@
 encyList
-%5D
+%3E
  is the 
@@ -3551,16 +3551,45 @@
 wordlist
+ or '-' for no frequency list
 , see ht

make dictionary building work again if no frequency list has been specified