Example #1
0
  private void processTranslationTable(
      @NotNull String queryString,
      @NotNull Document document,
      @NotNull BilingualQueryResultBuilder resultBuilder,
      @NotNull Language sourceLanguage,
      @NotNull Language targetLanguage) {
    // Find main table (german to X)
    String languageIdentifier = sourceLanguage.getIdentifier().toLowerCase();

    Element translationTable = document.getElementById("gridTranslations-" + languageIdentifier);

    // Process the main table with its entries
    if (translationTable != null) {
      // Find all relevant entries, filter them by class and process them
      translationTable
          .getElementsByClass("hover")
          .stream()
          .filter(e -> e.hasClass("default") || e.hasClass("alt"))
          .forEach(
              e -> processEntry(queryString, e, resultBuilder, sourceLanguage, targetLanguage));
      // Extract synonyms
      extractBilingualSynonyms(translationTable, resultBuilder, sourceLanguage);

    } else {
      LOGGER.debug(
          "Translation table for {} -> {} with query \"{}\" is null",
          languageIdentifier,
          targetLanguage.getIdentifier(),
          queryString);
    }
  }
Example #2
0
  /**
   * Returns the one of two languages that is not GERMAN. If none of the two is german, an
   * IllegalArgumentException will be thrown.
   *
   * @param inputLanguage First language to compare.
   * @param outputLanguage Second language to compare.
   * @return The non-german language.
   */
  @NotNull
  private Language findTargetLanguage(
      @NotNull Language inputLanguage, @NotNull Language outputLanguage) {

    if (Language.GERMAN.equals(inputLanguage)) return outputLanguage;
    else if (Language.GERMAN.equals(outputLanguage)) {
      return inputLanguage;
    } else {
      throw new IllegalArgumentException(
          "Expected at least one language to be german - got "
              + inputLanguage.getIdentifier()
              + " and "
              + outputLanguage.getDisplayName());
    }
  }
Example #3
0
  private void findRecommendations(
      @NotNull Document doc, @NotNull BilingualQueryResultBuilder resultBuilder) {
    // Determine all candidate nodes:
    Elements alternativeNodes = doc.select("div.cc > p > *");

    Language currentLanguage = null;

    for (Element node : alternativeNodes) {
      // If the next node is a flagicon, try to determine the language for the next entries from the
      // class name
      if (node.tagName().equals("span") && node.hasClass("flagicon")) {
        Set<String> classNames = node.classNames();
        classNames.remove("flagicon");
        for (String className : classNames) {
          Language candidate = Language.getExistingLanguageById(className);
          if (candidate != null) {
            currentLanguage = candidate;
            break;
          }
        }
      } else if (node.tagName().equals("a")) {
        String recommendationText = node.text();

        DictionaryObjectBuilder objectBuilder = ImmutableDictionaryObject.builder();
        objectBuilder.setLanguage(currentLanguage).setGeneralForm(recommendationText);

        resultBuilder.addSimilarRecommendation(objectBuilder.build());
      }
    }
  }
Example #4
0
  @NotNull
  private URL buildTargetUrl(@NotNull String queryString, @NotNull Language language)
      throws UnsupportedEncodingException, MalformedURLException {
    if (!BASE_URL_PER_LANGUAGE.containsKey(language))
      throw new IllegalArgumentException("Unsupported language request: " + language.toString());

    String encodedQueryString = URLEncoder.encode(queryString, "UTF-8");
    return new URL(
        BASE_URL + BASE_URL_PER_LANGUAGE.get(language) + "/" + encodedQueryString + ".php");
  }