/* 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.rules.de;

import morfologik.speller.Speller;
import morfologik.stemming.Dictionary;
import org.apache.commons.lang.StringUtils;
import org.junit.Ignore;
import org.junit.Test;
import org.languagetool.JLanguageTool;
import org.languagetool.TestTools;
import org.languagetool.language.German;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.CharacterCodingException;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class MorfologikGermanyGermanSpellerRuleTest {

  @Test
  public void testMorfologikSpeller() throws IOException {
    final MorfologikGermanyGermanSpellerRule rule =
          new MorfologikGermanyGermanSpellerRule(TestTools.getMessages("en"), new German());
    final JLanguageTool langTool = new JLanguageTool(new German());

    assertEquals(0, rule.match(langTool.getAnalyzedSentence("Hier stimmt jedes Wort!")).length);
    assertEquals(1, rule.match(langTool.getAnalyzedSentence("Hir nicht so ganz.")).length);

    assertEquals(0, rule.match(langTool.getAnalyzedSentence("Überall äußerst böse Umlaute!")).length);
    assertEquals(1, rule.match(langTool.getAnalyzedSentence("Üperall äußerst böse Umlaute!")).length);
  }
  
  @Test
  @Ignore("testing for https://github.com/languagetool-org/languagetool/issues/236")
  public void testFrequency() throws IOException {
    URL fsaURL = JLanguageTool.getDataBroker().getFromResourceDirAsUrl("de/hunspell/de_DE.dict");
    Dictionary dictionary = Dictionary.read(fsaURL);
    Speller speller = new Speller(dictionary, 2);
    System.out.println(speller.getFrequency("ich"));
  }

  @Test
  @Ignore("help testing for https://github.com/morfologik/morfologik-stemming/issues/34")
  public void testCommonMisspellings() throws IOException {
    URL fsaURL = JLanguageTool.getDataBroker().getFromResourceDirAsUrl("de/hunspell/de_DE.dict");
    Dictionary dictionary = Dictionary.read(fsaURL);
    Speller speller = new Speller(dictionary, 2);
    List<String> input = Arrays.asList((
            // tiny subset from from https://de.wikipedia.org/wiki/Wikipedia:Liste_von_Tippfehlern
            "Abenteur Abhängikeit abzuschliessen agerufen Aktivitiäten Aktzeptanz " +
            "Algorhitmus Algoritmus aliiert allgmein Amtsitz änlich Anstoss atakieren begrüsst Bezeichnug chinesiche " +
            "dannach Frima Fahrad Gebaüde gesammt " +
            // from gutefrage.net:
            "gerägelt Aufjedenfall ivh hällt daß woeder oderso anwalt").split(" "));
    for (String word : input) {
      check(word, speller);
    }
  }

  private void check(String word, Speller speller) throws CharacterCodingException {
    List<String> suggestions = speller.findReplacements(word);
    /*if (suggestions.size() > 10) {
      suggestions = suggestions.subList(0, 9);
    }*/
    System.out.println(word + ": " + StringUtils.join(suggestions, ", "));
  }

}

@@ -1342,33 +1342,120 @@
 org.
-junit.Assert.assertEquals
+hamcrest.CoreMatchers.is;%0Aimport static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertThat
 ;%0A%0Ap
@@ -2541,34 +2541,187 @@
 2);%0A    
-System.out.println
+assertThat(speller.getFrequency(%22der%22), is(25));%0A    assertThat(speller.getFrequency(%22Haus%22), is(11));%0A    assertThat(speller.getFrequency(%22sch%C3%B6n%22), is(9));%0A    assertThat
 (speller
@@ -2739,12 +2739,26 @@
 cy(%22
-ich%22
+gippsnicht%22), is(0
 ));%0A
@@ -3194,21 +3194,16 @@
 et from 
-from 
 https://
@@ -3513,16 +3513,368 @@
 gesammt 
+Schrifsteller seperat Septmber Staddteil Rhytmen rhytmisch Maschiene %22 +%0A            %22Lebensmittelg%C3%A4schefte enstand gro%C3%9Fmutter Rytmus %22 +%0A            // from user feedback:%0A            %22Vorstelungsgespr%C3%A4ch Hei%C3%9Fhunge-Attakcen evntl. langwalig Selbstport%C3%A4t Erdgeshoss %22 +%0A            %22kommmischeweise gegensatz Gesichte Suedkaukasus Englisch-sprachigige 
 %22 +%0A    
@@ -3953,16 +3953,20 @@
 llt da%C3%9F 
+mu%C3%9F 
 woeder o
@@ -3978,16 +3978,25 @@
  anwalt%22
+%0A        
 ).split(

improve (ignored) tests