<commit_before>/**
 * redpen: a text inspection tool
 * Copyright (C) 2014 Recruit Technologies Co., Ltd. and contributors
 * (see CONTRIBUTORS.md)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.bigram.docvalidator.validator.sentence;

import org.bigram.docvalidator.DocumentValidatorException;
import org.bigram.docvalidator.config.CharacterTable;
import org.bigram.docvalidator.model.Sentence;
import org.bigram.docvalidator.util.FileLoader;
import org.bigram.docvalidator.util.WordListExtractor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.bigram.docvalidator.ValidationError;
import org.bigram.docvalidator.config.ValidatorConfiguration;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Validate input sentences contain invalid expression.
 */
public class InvalidExpressionValidator implements SentenceValidator {
  /**
   * Constructor.
   */
  public InvalidExpressionValidator() {
    invalidExpressions = new HashSet<String>();
  }

  /**
   * Constructor
   * @param config Configuration object
   * @param characterTable  Character settings
   * @throws DocumentValidatorException
   */
  public InvalidExpressionValidator(ValidatorConfiguration config,
                                    CharacterTable characterTable)
      throws DocumentValidatorException {
    initialize(config, characterTable);
  }

  public List<ValidationError> validate(Sentence line) {
    List<ValidationError> result = new ArrayList<ValidationError>();
    String str = line.content;
    for (String w : invalidExpressions) {
      if (str.contains(w)) {
        result.add(new ValidationError(
            this.getClass(),
            "Found invalid expression: \"" + w + "\"", line));
      }
    }
    return result;
  }

  /**
   * Add invalid element. This method is used for testing
   *
   * @parm invalid invalid expression to be added the list
   */
  public void addInvalid(String invalid) {
    invalidExpressions.add(invalid);
  }

  private boolean initialize(ValidatorConfiguration conf,
      CharacterTable characterTable)
      throws DocumentValidatorException {
    String lang = characterTable.getLang();

    WordListExtractor extractor = new WordListExtractor();
    FileLoader loader = new FileLoader(extractor);

    // load default dictionary
    LOG.info("Loading default invalid expression dictionary for " +
        "\"" + lang + "\"");
    String defualtDicitonaryFile = DEFAULT_RESOURCE_PATH + "/invalid-" + lang + ".dat";
    //String defualtDicitonaryFile = "invalid-en.dat";
    InputStream inputStream = getClass()
        .getClassLoader()
        .getResourceAsStream(defualtDicitonaryFile);
    System.out.println("Default dictionary: " + defualtDicitonaryFile);
    if (loader.loadFile(inputStream) == 0) {
      LOG.info("Succeeded to load default dictionary.");
    } else {
      LOG.info("Failed to load default dictionary.");
    }

    // adding user dictionary element
    String confFile = conf.getAttribute("dictionary");
    if (confFile == null || confFile.equals("")) {
      LOG.error("Dictionary file is not specified.");
    } else {
      LOG.info("user dictionary file is " + confFile);
      if (loader.loadFile(confFile) != 0) {
        LOG.info("Succeeded to load specified user dictionary.");
      } else {
        LOG.error("Failed to load user dictionary.");
      }
    }
    invalidExpressions = extractor.get();
    return true;
  }

  private String DEFAULT_RESOURCE_PATH = "dicts/invalid";

  private Set<String> invalidExpressions;

  private static final Logger LOG =
      LoggerFactory.getLogger(InvalidExpressionValidator.class);
}<commit_msg>Fix formatting errors from checkstyle<commit_after>/**
 * redpen: a text inspection tool
 * Copyright (C) 2014 Recruit Technologies Co., Ltd. and contributors
 * (see CONTRIBUTORS.md)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.bigram.docvalidator.validator.sentence;

import org.bigram.docvalidator.DocumentValidatorException;
import org.bigram.docvalidator.config.CharacterTable;
import org.bigram.docvalidator.model.Sentence;
import org.bigram.docvalidator.util.FileLoader;
import org.bigram.docvalidator.util.WordListExtractor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.bigram.docvalidator.ValidationError;
import org.bigram.docvalidator.config.ValidatorConfiguration;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Validate input sentences contain invalid expression.
 */
public class InvalidExpressionValidator implements SentenceValidator {
  /**
   * Constructor.
   */
  public InvalidExpressionValidator() {
    invalidExpressions = new HashSet<String>();
  }

  /**
   * Constructor
   * @param config Configuration object
   * @param characterTable  Character settings
   * @throws DocumentValidatorException
   */
  public InvalidExpressionValidator(ValidatorConfiguration config,
                                    CharacterTable characterTable)
      throws DocumentValidatorException {
    initialize(config, characterTable);
  }

  public List<ValidationError> validate(Sentence line) {
    List<ValidationError> result = new ArrayList<ValidationError>();
    String str = line.content;
    for (String w : invalidExpressions) {
      if (str.contains(w)) {
        result.add(new ValidationError(
            this.getClass(),
            "Found invalid expression: \"" + w + "\"", line));
      }
    }
    return result;
  }

  /**
   * Add invalid element. This method is used for testing
   *
   * @param invalid invalid expression to be added the list
   */
  public void addInvalid(String invalid) {
    invalidExpressions.add(invalid);
  }

  private boolean initialize(ValidatorConfiguration conf,
      CharacterTable characterTable)
      throws DocumentValidatorException {
    String lang = characterTable.getLang();

    WordListExtractor extractor = new WordListExtractor();
    FileLoader loader = new FileLoader(extractor);

    LOG.info("Loading default invalid expression dictionary for " +
        "\"" + lang + "\".");
    String defaultDictionaryFile = DEFAULT_RESOURCE_PATH
        + "/invalid-" + lang + ".dat";
    InputStream inputStream = getClass()
        .getClassLoader()
        .getResourceAsStream(defaultDictionaryFile);
    if (loader.loadFile(inputStream) == 0) {
      LOG.info("Succeeded to load default dictionary.");
    } else {
      LOG.info("Failed to load default dictionary.");
    }

    String confFile = conf.getAttribute("dictionary");
    if (confFile == null || confFile.equals("")) {
      LOG.error("Dictionary file is not specified.");
    } else {
      LOG.info("user dictionary file is " + confFile);
      if (loader.loadFile(confFile) != 0) {
        LOG.info("Succeeded to load specified user dictionary.");
      } else {
        LOG.error("Failed to load user dictionary.");
      }
    }
    invalidExpressions = extractor.get();
    return true;
  }

  private static final String DEFAULT_RESOURCE_PATH = "dicts/invalid";

  private Set<String> invalidExpressions;

  private static final Logger LOG =
      LoggerFactory.getLogger(InvalidExpressionValidator.class);
}
{'fraction_non_alphanumeric': 0.09744389775591024, 'fraction_numerical': 0.0028801152046081845, 'mean_word_length': 3.544165757906216, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7493104', 'n_tokens_mistral': 2201, 'n_tokens_neox': 2166, 'n_words': 645}