All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.languagetool.rules.AbstractDashRule Maven / Gradle / Ivy

Go to download

LanguageTool is an Open Source proofreading software for English, French, German, Polish, Romanian, and more than 20 other languages. It finds many errors that a simple spell checker cannot detect like mixing up there/their and it detects some grammar problems.

There is a newer version: 6.5
Show newest version
/* LanguageTool, a natural language style checker
 * Copyright (C) 2016 Marcin Miłkowski (http://www.languagetool.org)
 *
 * 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;

import org.languagetool.AnalyzedSentence;
import org.languagetool.JLanguageTool;
import org.languagetool.Language;
import org.languagetool.rules.patterns.PatternRule;
import org.languagetool.rules.patterns.PatternToken;

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

/**
 * Another use of the compounds file -- check for compounds written with
 * dashes instead of hyphens (for example, Rabka — Zdrój).
 * NOTE: this slows down checking a lot when used with large compound lists
 * @since 3.8
 */
public abstract class AbstractDashRule extends Rule {

  private final List dashRules;

  public AbstractDashRule(List dashRules) throws IOException {
    this.dashRules = Objects.requireNonNull(dashRules);
  }

  @Override
  public String getId() {
    return "DASH_RULE";
  }

  @Override
  public abstract String getDescription();

  @Override
  public int estimateContextForSureMatch() {
    return 2;
  }

  @Override
  public RuleMatch[] match(AnalyzedSentence sentence) throws IOException {
    List matches = new ArrayList<>();
    for (PatternRule dashRule : dashRules) {
      for (RuleMatch match : dashRule.match(sentence)) {
        RuleMatch rm = new RuleMatch
            (this, match.getSentence(), match.getFromPos(), match.getToPos(), match.getMessage(),
                match.getShortMessage(), false, "");
        matches.add(rm);
      }
    }
    return matches.toArray(new RuleMatch[0]);
  }

  protected static List loadCompoundFile(String path, String msg, Language lang) {
    List rules = new ArrayList<>();
    List lines = JLanguageTool.getDataBroker().getFromResourceDirAsLines(path);
    int counter = 0;
    for (String line : lines) {
      counter++;
      if (line.isEmpty() || line.charAt(0) == '#') {
        continue;     // ignore comments
      }
      if (line.endsWith("+")) {
        continue; // skip non-hyphenated suggestions
      } else if (line.endsWith("*")) {
        line = removeLastCharacter(line);
      }
      List tokList = new ArrayList<>();
      String[] tokens = line.split("-");
      int tokenCounter = 0;
      for (String token : tokens) {
        tokenCounter++;
          // token
        tokList.add(new PatternToken(token, true, false, false));
        if (tokenCounter < tokens.length) {
          // add dash
          tokList.add(new PatternToken("[—–]", false, true, false));
        }
      }
      PatternRule dashRule = new PatternRule
          ("DASH_RULE" + counter, lang, tokList,
              "", msg + ""+line.replaceAll("[–—]", "-")+".", line.replaceAll("[–—]", "-"));
      rules.add(dashRule);
    }
    return rules;
  }

  private static String removeLastCharacter(String str) {
    return str.substring(0, str.length() - 1);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy