/* LanguageTool, a natural language style checker 
 * Copyright (C) 2005 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 de.danielnaber.languagetool.dev;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import de.danielnaber.languagetool.JLanguageTool;
import de.danielnaber.languagetool.Language;
import de.danielnaber.languagetool.TextFilter;
import de.danielnaber.languagetool.tools.StringTools;
import de.danielnaber.languagetool.tools.Tools;

/**
 * Uses JLanguageTol recursively on the files of the BNC (British National Corpus).
 * 
 * @author Daniel Naber
 */
public final class CheckBNC {

  private JLanguageTool langTool = null;
  private TextFilter textFilter = new BNCTextFilter();
  
  public static void main(String[] args) throws Exception {
    if (args.length != 1) {
      System.out.println("Usage: CheckBNC <directory>");
      System.exit(1);
    }
    CheckBNC prg = new CheckBNC();
    prg.run(new File(args[0]));
  }
  
  private CheckBNC() throws IOException, ParserConfigurationException, SAXException {
    langTool = new JLanguageTool(Language.ENGLISH);
    langTool.activateDefaultPatternRules();
    String[] disRules = new String[] {"UPPERCASE_SENTENCE_START", "COMMA_PARENTHESIS_WHITESPACE",
        "WORD_REPEAT_RULE", "DOUBLE_PUNCTUATION"};
    System.err.println("Note: disabling the following rules:");
    for (int i = 0; i < disRules.length; i++) {
      langTool.disableRule(disRules[i]);
      System.err.println(" " + disRules[i]);
    }
  }

  private void run(final File file) throws IOException {
    if (file.isDirectory()) {
      File[] files = file.listFiles();
      for (int i = 0; i < files.length; i++) {
        run(new File(file, files[i].getName()));
      }
    } else {
      System.out.println("Checking " + file.getAbsolutePath());
      String text = StringTools.readFile(file.getAbsolutePath());
      text = textFilter.filter(text);
      //System.out.println(text);
      Tools.checkText(text, langTool);
    }
  }

}

class BNCTextFilter implements TextFilter {

  public String filter(String text) {
    text = text.replaceAll("(?s)<header.*?>.*?</header>", "");
    text = text.replaceAll("<w.*?>", "");
    text = text.replaceAll("<c.*?>", "");
    text = text.replaceAll("<.*?>", "");
    text = text.replaceAll(" +", " ");
    text = text.replaceAll("&bquo|&equo", "\"");
    text = text.replaceAll("&mdash;?", "--");
    text = text.replaceAll("&amp;?", "&");
    return text;
  }
  
}

@@ -937,32 +937,55 @@
 .io.IOException;
+%0Aimport java.util.List;
 %0A%0Aimport javax.x
@@ -1202,16 +1202,81 @@
 Filter;%0A
+import de.danielnaber.languagetool.tokenizers.SentenceTokenizer;%0A
 import d
@@ -1617,18 +1617,66 @@
 lter();%0A
+%0A
   
+static final boolean CHECK_BY_SENTENCE = true;%0A
 %0A  publi
@@ -2876,36 +2876,272 @@
     
-//System.out.println(text);%0A
+if (CHECK_BY_SENTENCE) %7B%0A        SentenceTokenizer st = new SentenceTokenizer();%0A        List%3CString%3E sentences = st.tokenize(text);%0A        for (String sentence : sentences) %7B%0A          Tools.checkText(sentence, langTool, false, 1000);%0A        %7D%0A      %7D else %7B%0A  
     
@@ -3175,16 +3175,24 @@
 gTool);%0A
+      %7D%0A
     %7D%0A  

print complete sentence of matches