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

org.languagetool.server.V2TextChecker Maven / Gradle / Ivy

There is a newer version: 6.4
Show newest version
/* LanguageTool, a natural language style checker
 * Copyright (C) 2016 Daniel Naber (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.server;

import com.sun.net.httpserver.HttpExchange;
import org.jetbrains.annotations.NotNull;
import org.languagetool.Language;
import org.languagetool.Languages;
import org.languagetool.markup.AnnotatedText;
import org.languagetool.rules.RuleMatch;
import org.languagetool.tools.StringTools;
import org.languagetool.tools.RuleMatchesAsJsonSerializer;

import java.util.*;

import static org.languagetool.server.ServerTools.setCommonHeaders;

/**
 * Checker for v2 of the API, which returns JSON.
 * @since 3.4
 */
class V2TextChecker extends TextChecker {

  private static final String JSON_CONTENT_TYPE = "application/json";

  V2TextChecker(HTTPServerConfig config, boolean internalServer, Queue workQueue, RequestCounter reqCounter) {
    super(config, internalServer, workQueue, reqCounter);
  }

  @Override
  protected void setHeaders(HttpExchange httpExchange) {
    setCommonHeaders(httpExchange, JSON_CONTENT_TYPE, config.allowOriginUrl);
  }

  @Override
  protected String getResponse(AnnotatedText text, DetectedLanguage lang, Language motherTongue, List matches,
                               List hiddenMatches, String incompleteResultsReason) {
    RuleMatchesAsJsonSerializer serializer = new RuleMatchesAsJsonSerializer();
    return serializer.ruleMatchesToJson(matches, hiddenMatches, text, CONTEXT_SIZE,
            lang.getGivenLanguage(), lang.getDetectedLanguage(), incompleteResultsReason);
  }

  @NotNull
  @Override
  protected List getEnabledRuleIds(Map parameters) {
    String enabledParam = parameters.get("enabledRules");
    List enabledRules = new ArrayList<>();
    if (enabledParam != null) {
      enabledRules.addAll(Arrays.asList(enabledParam.split(",")));
    }
    return enabledRules;
  }

  @NotNull
  @Override
  protected List getDisabledRuleIds(Map parameters) {
    return getCommaSeparatedStrings("disabledRules", parameters);
  }

  @Override
  protected boolean getLanguageAutoDetect(Map parameters) {
    return "auto".equals(parameters.get("language"));
  }

  @Override
  protected void checkParams(Map parameters) {
    super.checkParams(parameters);
    if (StringTools.isEmpty(parameters.get("language"))) {
      throw new IllegalArgumentException("Missing 'language' parameter, e.g. 'language=en-US' for American English or 'language=fr' for French");
    }
    if (parameters.get("enabled") != null) {
      throw new IllegalArgumentException("You specified 'enabled' but the parameter is now called 'enabledRules' in v2 of the API");
    }
    if (parameters.get("disabled") != null) {
      throw new IllegalArgumentException("You specified 'disabled' but the parameter is now called 'disabledRules' in v2 of the API");
    }
    if (parameters.get("preferredvariants") != null) {
      throw new IllegalArgumentException("You specified 'preferredvariants' but the parameter is now called 'preferredVariants' (uppercase 'V') in v2 of the API");
    }
    if (parameters.get("autodetect") != null) {
      throw new IllegalArgumentException("You specified 'autodetect' but automatic language detection is now activated with 'language=auto' in v2 of the API");
    }
  }
  
  @Override
  @NotNull
  protected DetectedLanguage getLanguage(String text, Map parameters, List preferredVariants) {
    String langParam = parameters.get("language");
    Language detectedLang = detectLanguageOfString(text, null, preferredVariants);
    Language givenLang;
    if (getLanguageAutoDetect(parameters)) {
      givenLang = detectedLang;
    } else {
      givenLang = Languages.getLanguageForShortCode(langParam);
    }
    return new DetectedLanguage(givenLang, detectedLang);
  }

  @Override
  @NotNull
  protected List getPreferredVariants(Map parameters) {
    List preferredVariants;
    if (parameters.get("preferredVariants") != null) {
      preferredVariants = Arrays.asList(parameters.get("preferredVariants").split(",\\s*"));
      if (!"auto".equals(parameters.get("language"))) {
        throw new IllegalArgumentException("You specified 'preferredVariants' but you didn't specify 'language=auto'");
      }
    } else {
      preferredVariants = Collections.emptyList();
    }
    return preferredVariants;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy