<commit_before>/* LanguageTool, a natural language style checker 
 * Copyright (C) 2005 <NAME> (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 morfologik.fsa.FSA;
import org.languagetool.JLanguageTool;
import org.languagetool.tools.StringTools;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.*;

/**
 * Export German nouns as a serialized Java HashSet, to be used
 * by jWordSplitter.  
 * 
 * @author <NAME>
 */
public class ExportGermanNouns {

  private static final String DICT_FILENAME = "/de/german.dict";
  
  private ExportGermanNouns() {
  }
  
  private List<String> getSortedWords() throws IOException {
    Set<String> words = getWords();
    List<String> sortedWords = new ArrayList<>(words);
    Collections.sort(sortedWords);
    return sortedWords;
  }
  
  private Set<String> getWords() throws IOException {
    final FSA fsa = FSA.read(JLanguageTool.getDataBroker().getFromResourceDirAsStream(DICT_FILENAME));
    final Set<String> set = new HashSet<>();
    for (ByteBuffer bb : fsa) {
      final byte [] sequence = new byte [bb.remaining()];
      bb.get(sequence);
      final String output = new String(sequence, "iso-8859-1");
      boolean isNoun = output.contains("+SUB:") || (output.contains("+EIG:") && output.contains("COU")); // COU = Country
      if (isNoun && !output.contains(":ADJ") && !StringTools.isAllUppercase(output)) {
        final String[] parts = output.split("\\+");
        final String term = parts[0].toLowerCase();
        set.add(term);
      }
    }
    return set;
  }
  
  public static void main(String[] args) throws IOException {<commit_msg>dictionary update: consider manually added words<commit_after>/* LanguageTool, a natural language style checker 
 * Copyright (C) 2005 <NAME> (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 com.google.common.base.Charsets;
import morfologik.fsa.FSA;
import org.languagetool.JLanguageTool;
import org.languagetool.tools.StringTools;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.util.*;

/**
 * Export German nouns, to be used by jWordSplitter.  
 * 
 * @author <NAME>
 */
public class ExportGermanNouns {

  private static final String DICT_FILENAME = "/de/german.dict";
  private static final String ADDED_DICT_FILENAME = "languagetool-language-modules/de/src/main/resources/org/languagetool/resource/de/added.txt";
  
  private ExportGermanNouns() {
  }
  
  private List<String> getSortedWords() throws IOException {
    Set<String> words1 = getBinaryDictWords();
    Set<String> words2 = getAddedDictWords();
    List<String> sortedWords = new ArrayList<>();
    sortedWords.addAll(words1);
    sortedWords.addAll(words2);
    Collections.sort(sortedWords);
    return sortedWords;
  }

  private Set<String> getBinaryDictWords() throws IOException {
    final FSA fsa = FSA.read(JLanguageTool.getDataBroker().getFromResourceDirAsStream(DICT_FILENAME));
    final Set<String> set = new HashSet<>();
    for (ByteBuffer buffer : fsa) {
      final byte [] sequence = new byte [buffer.remaining()];
      buffer.get(sequence);
      final String output = new String(sequence, "iso-8859-1");
      if (isRelevantNoun(output)) {
        final String[] parts = output.split("\\+");
        final String term = parts[0].toLowerCase();
        set.add(term);
      }
    }
    return set;
  }

  private Set<String> getAddedDictWords() throws IOException {
    final Set<String> set = new HashSet<>();
    List<String> lines = Files.readAllLines(FileSystems.getDefault().getPath(ADDED_DICT_FILENAME), Charsets.UTF_8);
    for (String line : lines) {
      if (isRelevantNoun(line)) {
        final String[] parts = line.split("\t");
        final String term = parts[0].toLowerCase();
        set.add(term);
      }
    }
    return set;
  }

  private boolean isRelevantNoun(String output) {
    boolean isNoun = output.contains("SUB:") || (output.contains("EIG:") && output.contains("COU"));
    return isNoun && !output.contains(":ADJ") && !StringTools.isAllUppercase(output);
  }
  
  public static void main(String[] args) throws IOException {
{'fraction_non_alphanumeric': 0.11029023746701846, 'fraction_numerical': 0.009146877748460862, 'mean_word_length': 3.794266441821248, 'pattern_counts': {'":': 2, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1795111', 'n_tokens_mistral': 1734, 'n_tokens_neox': 1660, 'n_words': 538}