<commit_before>package cc.redpen.intellij;

import cc.redpen.ConfigurationLoader;
import cc.redpen.RedPen;
import cc.redpen.RedPenException;
import cc.redpen.config.Configuration;
import cc.redpen.parser.DocumentParser;
import cc.redpen.util.LanguageDetector;
import com.google.common.collect.ImmutableMap;
import com.intellij.psi.PsiFile;

import java.util.LinkedHashMap;
import java.util.Map;

public class RedPenProvider {
  private static RedPenProvider instance;
  private Map<String, Configuration> initialConfigs = new LinkedHashMap<>();
  private Map<String, Configuration> configs = new LinkedHashMap<>();
  private String configKey = "en";
  private boolean autodetect = true;

  Map<String, DocumentParser> parsers = ImmutableMap.of(
    "PLAIN_TEXT", DocumentParser.PLAIN,
    "Markdown", DocumentParser.MARKDOWN,
    "AsciiDoc", DocumentParser.ASCIIDOC
  );

  private RedPenProvider() {
    loadConfig("redpen-conf.xml", configs);
    loadConfig("redpen-conf-ja.xml", configs);
    loadConfig("redpen-conf-ja-hankaku.xml", configs);
    loadConfig("redpen-conf-ja-zenkaku2.xml", configs);

    loadConfig("redpen-conf.xml", initialConfigs);
    loadConfig("redpen-conf-ja.xml", initialConfigs);
    loadConfig("redpen-conf-ja-hankaku.xml", initialConfigs);
    loadConfig("redpen-conf-ja-zenkaku2.xml", initialConfigs);
  }

  /** For tests */
  RedPenProvider(Map<String, Configuration> configs) {
    this.configs = configs;
    this.initialConfigs = configs;
  }

  private void loadConfig(String fileName, Map<String, Configuration> target) {
    try {
      Configuration configuration = new ConfigurationLoader().loadFromResource("/" + fileName);
      target.put(configuration.getKey(), configuration);
    }
    catch (RedPenException e) {
      throw new RuntimeException(e);
    }
  }

  public static RedPenProvider getInstance() {
    if (instance == null) instance = new RedPenProvider();
    return instance;
  }

  public RedPen getRedPen() {
    try {
      return new RedPen(configs.get(configKey));
    }
    catch (RedPenException e) {
      throw new RuntimeException(e);
    }
  }

  public RedPen getRedPenFor(String text) {
    if (autodetect) configKey = new LanguageDetector().detectLanguage(text);
    return getRedPen();<commit_msg>redpen 1.4.5 allows cloning configurations instead of loading them twice<commit_after>package cc.redpen.intellij;

import cc.redpen.ConfigurationLoader;
import cc.redpen.RedPen;
import cc.redpen.RedPenException;
import cc.redpen.config.Configuration;
import cc.redpen.parser.DocumentParser;
import cc.redpen.util.LanguageDetector;
import com.google.common.collect.ImmutableMap;
import com.intellij.psi.PsiFile;

import java.util.LinkedHashMap;
import java.util.Map;

import static java.util.stream.Collectors.toMap;

public class RedPenProvider {
  private static RedPenProvider instance;
  private Map<String, Configuration> initialConfigs = new LinkedHashMap<>();
  private Map<String, Configuration> configs = new LinkedHashMap<>();
  private String configKey = "en";
  private boolean autodetect = true;

  Map<String, DocumentParser> parsers = ImmutableMap.of(
    "PLAIN_TEXT", DocumentParser.PLAIN,
    "Markdown", DocumentParser.MARKDOWN,
    "AsciiDoc", DocumentParser.ASCIIDOC
  );

  private RedPenProvider() {
    loadConfig("redpen-conf.xml");
    loadConfig("redpen-conf-ja.xml");
    loadConfig("redpen-conf-ja-hankaku.xml");
    loadConfig("redpen-conf-ja-zenkaku2.xml");

    configs.putAll(initialConfigs.entrySet().stream().collect(toMap(Map.Entry::getKey, e -> e.getValue().clone())));
  }

  /** For tests */
  RedPenProvider(Map<String, Configuration> configs) {
    this.configs = configs;
    this.initialConfigs = configs;
  }

  private void loadConfig(String fileName) {
    try {
      Configuration configuration = new ConfigurationLoader().loadFromResource("/" + fileName);
      initialConfigs.put(configuration.getKey(), configuration);
    }
    catch (RedPenException e) {
      throw new RuntimeException(e);
    }
  }

  public static RedPenProvider getInstance() {
    if (instance == null) instance = new RedPenProvider();
    return instance;
  }

  public RedPen getRedPen() {
    try {
      return new RedPen(configs.get(configKey));
    }
    catch (RedPenException e) {
      throw new RuntimeException(e);
    }
  }

  public RedPen getRedPenFor(String text) {
    if (autodetect) configKey = new LanguageDetector().detectLanguage(text);
    return getRedPen();
{'fraction_non_alphanumeric': 0.11766019200714445, 'fraction_numerical': 0.0013395847287340924, 'mean_word_length': 4.333333333333333, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, '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': '4543706', 'n_tokens_mistral': 1369, 'n_tokens_neox': 1355, 'n_words': 285}