<commit_before>package de.danielnaber.languagetool.tagging.disambiguation.rules.en;

import java.io.IOException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import de.danielnaber.languagetool.AnalyzedSentence;
import de.danielnaber.languagetool.Language;
import de.danielnaber.languagetool.rules.patterns.PatternRule;
import de.danielnaber.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule;
import de.danielnaber.languagetool.tagging.disambiguation.rules.DisambiguationRuleLoader;
import de.danielnaber.languagetool.tagging.disambiguation.rules.RuleDisambiguator;

public class EnglishRuleDisambiguator extends RuleDisambiguator {

  static final String DISAMB_FILE = "disambiguation.xml";
  private List<DisambiguationPatternRule> disambiguationRules = null;
  private Language language;
  
  /**
   * Load disambiguation rules from an XML file. Use {@link #addRule} to add
   * these rules to the checking process.
   * 
   * @throws ParserConfigurationException
   * @throws SAXException
   * @throws IOException
   * @return a List of {@link PatternRule} objects
   */
  private List<DisambiguationPatternRule> loadPatternRules(final String filename) throws ParserConfigurationException, SAXException, IOException {
    DisambiguationRuleLoader ruleLoader = new DisambiguationRuleLoader();    
    return ruleLoader.getRules(this.getClass().getResourceAsStream(filename));
  }
  
  public final AnalyzedSentence disambiguate(final AnalyzedSentence input) throws IOException {
    AnalyzedSentence sentence = input;
    try {
      if (disambiguationRules == null) {
        final String defaultPatternFilename = 
          "/resource/en/" + DISAMB_FILE;
        disambiguationRules = loadPatternRules(defaultPatternFilename);
      }
      if (language == null) {
        language = Language.ENGLISH;
      }
      for (DisambiguationPatternRule dr : disambiguationRules) {
        sentence = dr.replace(sentence);
      }

    /*  if (!input.toString().equals(sentence.toString())) {
        System.err.println("INPUT:" + input.toString());
        System.err.println("OUTPUT:" + sentence.toString());
      } */   
    } catch (ParserConfigurationException e) {<commit_msg>better error if file isn't found<commit_after>package de.danielnaber.languagetool.tagging.disambiguation.rules.en;

import java.io.IOException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import de.danielnaber.languagetool.AnalyzedSentence;
import de.danielnaber.languagetool.Language;
import de.danielnaber.languagetool.rules.patterns.PatternRule;
import de.danielnaber.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule;
import de.danielnaber.languagetool.tagging.disambiguation.rules.DisambiguationRuleLoader;
import de.danielnaber.languagetool.tagging.disambiguation.rules.RuleDisambiguator;
import de.danielnaber.languagetool.tools.Tools;

public class EnglishRuleDisambiguator extends RuleDisambiguator {

  static final String DISAMB_FILE = "disambiguation.xml";
  private List<DisambiguationPatternRule> disambiguationRules = null;
  private Language language;
  
  /**
   * Load disambiguation rules from an XML file. Use {@link #addRule} to add
   * these rules to the checking process.
   * 
   * @throws ParserConfigurationException
   * @throws SAXException
   * @throws IOException
   * @return a List of {@link PatternRule} objects
   */
  private List<DisambiguationPatternRule> loadPatternRules(final String filename) throws ParserConfigurationException, SAXException, IOException {
    DisambiguationRuleLoader ruleLoader = new DisambiguationRuleLoader();    
    return ruleLoader.getRules(Tools.getStream(filename));
  }
  
  public final AnalyzedSentence disambiguate(final AnalyzedSentence input) throws IOException {
    AnalyzedSentence sentence = input;
    try {
      if (disambiguationRules == null) {
        final String defaultPatternFilename = 
          "/resource/en/" + DISAMB_FILE;
        disambiguationRules = loadPatternRules(defaultPatternFilename);
      }
      if (language == null) {
        language = Language.ENGLISH;
      }
      for (DisambiguationPatternRule dr : disambiguationRules) {
        sentence = dr.replace(sentence);
      }

    /*  if (!input.toString().equals(sentence.toString())) {
        System.err.println("INPUT:" + input.toString());
        System.err.println("OUTPUT:" + sentence.toString());
      } */   
    } catch (ParserConfigurationException e) {
{'fraction_non_alphanumeric': 0.09059157389216328, 'fraction_numerical': 0.0, 'mean_word_length': 4.384253819036427, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '7255982', 'n_tokens_mistral': 1326, 'n_tokens_neox': 1264, 'n_words': 261}