/* LanguageTool, a natural language style checker
 * Copyright (C) 2014 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.dumpcheck;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import org.languagetool.Language;
import org.languagetool.Languages;

/**
 * Command line tool to extract sentences from a (optionally bz2-compressed) Wikipedia XML dump.
 * @since 2.6
 */
class WikipediaSentenceExtractor {

  private void extract(Language language, String xmlDumpPath) throws IOException, CompressorException {
    try (FileInputStream fis = new FileInputStream(xmlDumpPath);
         BufferedInputStream bis = new BufferedInputStream(fis)) {
      InputStream input;
      if (xmlDumpPath.endsWith(".bz2")) {
        input = new CompressorStreamFactory().createCompressorInputStream(bis);
      } else if (xmlDumpPath.endsWith(".xml")) {
        input = bis;
      } else {
        throw new IllegalArgumentException("Unknown file name, expected '.xml' or '.bz2': " + xmlDumpPath);
      }
      int sentenceCount = 0;
      WikipediaSentenceSource source = new WikipediaSentenceSource(input, language);
      while (source.hasNext()) {
        String sentence = source.next().getText();
        if (skipSentence(sentence)) {
          continue;
        }
        System.out.println(sentence);
        sentenceCount++;
        if (sentenceCount % 1000 == 0) {
          System.err.println("Exporting sentence #" + sentenceCount + "...");
        }
      }
    }
  }

  private boolean skipSentence(String sentence) {
    return sentence.trim().length() == 0 || Character.isLowerCase(sentence.trim().charAt(0));
  }

  public static void main(String[] args) throws IOException, CompressorException {
    if (args.length != 2) {
      System.out.println("Usage: " + WikipediaSentenceExtractor.class.getSimpleName() + " <langCode> <wikipediaXmlDump>");
      System.exit(1);
    }
    WikipediaSentenceExtractor extractor = new WikipediaSentenceExtractor();
    extractor.extract(Languages.getLanguageForShortCode(args[0]), args[1]);
  }
}

@@ -912,115 +912,9 @@
 .io.
-BufferedInputStream;%0Aimport java.io.FileInputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream
+*
 ;%0A%0Ai
@@ -1122,16 +1122,141 @@
 uages;%0A%0A
+import java.io.BufferedInputStream;%0Aimport java.io.FileInputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0A%0A
 /**%0A * C
@@ -1462,16 +1462,35 @@
 DumpPath
+, String outputFile
 ) throws
@@ -1653,16 +1653,69 @@
 eam(fis)
+;%0A         FileWriter fw = new FileWriter(outputFile)
 ) %7B%0A    
@@ -2328,24 +2328,26 @@
   %7D%0A        
+//
 System.out.p
@@ -2360,24 +2360,52 @@
 (sentence);%0A
+        fw.write(sentence);%0A
         sent
@@ -2820,17 +2820,17 @@
 ngth != 
-2
+3
 ) %7B%0A    
@@ -2944,16 +2944,25 @@
 XmlDump%3E
+ %3Coutput%3E
 %22);%0A    
@@ -3135,16 +3135,25 @@
  args%5B1%5D
+, args%5B2%5D
 );%0A  %7D%0A%7D

print to file instead of STDOUT (faster when called from IDE)