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

org.languagetool.rules.uk.ExtraDictionaryLoader Maven / Gradle / Ivy

The newest version!
package org.languagetool.rules.uk;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.Arrays;

import org.languagetool.JLanguageTool;

/**
 * Loads extra helper dictionaries in plain text format
 * @since 2.9
 */
public class ExtraDictionaryLoader {

  public static Set loadSet(String path) {
    Set result = new HashSet<>();
    List lines = JLanguageTool.getDataBroker().getFromResourceDirAsLines(path);
    for (String line : lines) {
      if (!line.startsWith("#")) {
        result.add(line);
      }
    }
    return result;
  }

  public static Map loadMap(String path) {
    Set set = loadSet(path);
    return set.stream()
        .map(str -> str.trim().split(" "))
        .collect(Collectors.toMap(x -> x[0], x -> x.length > 1 ? x[1] : ""));
  }
  
  public static Map> loadSpacedLists(String path) {
    Map> result = new HashMap<>();
    List lines = JLanguageTool.getDataBroker().getFromResourceDirAsLines(path);
    for (String line : lines) {
      if(!line.startsWith("#") && !line.trim().isEmpty()) {
        line = line.replaceFirst("#.*", "").trim();
        String[] split = line.split(" |\\|");
        List list = Arrays.asList(split).subList(1, split.length);
        result.put(split[0], list);
      }
    }
    return result;
  }

  public static Map> loadLists(String path) {
    Map> result = new HashMap<>();
    try (InputStream is = JLanguageTool.getDataBroker().getFromRulesDirAsStream(path);
         Scanner scanner = new Scanner(is, "UTF-8")) {
      while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        if( ! line.startsWith("#") && ! line.trim().isEmpty() ) {
          String[] split = line.split(" *= *|\\|");
          List list = Arrays.asList(split).subList(1, split.length);
          result.put(split[0], list);
        }
      }
      return result;
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy