/** Load synonyms with the given {@link SynonymMap.Parser} class. */
  private SynonymMap loadSynonyms(
      ResourceLoader loader, String cname, boolean dedup, Analyzer analyzer)
      throws IOException, ParseException {
    CharsetDecoder decoder =
        Charset.forName("UTF-8")
            .newDecoder()
            .onMalformedInput(CodingErrorAction.REPORT)
            .onUnmappableCharacter(CodingErrorAction.REPORT);

    SynonymMap.Parser parser;
    Class<? extends SynonymMap.Parser> clazz = loader.findClass(cname, SynonymMap.Parser.class);
    try {
      parser =
          clazz
              .getConstructor(boolean.class, boolean.class, Analyzer.class)
              .newInstance(dedup, expand, analyzer);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    File synonymFile = new File(synonyms);
    if (synonymFile.exists()) {
      decoder.reset();
      parser.parse(new InputStreamReader(loader.openResource(synonyms), decoder));
    } else {
      List<String> files = splitFileNames(synonyms);
      for (String file : files) {
        decoder.reset();
        parser.parse(new InputStreamReader(loader.openResource(file), decoder));
      }
    }
    return parser.build();
  }
  /**
   * Loads the hunspell dictionary and affix files defined in the configuration
   *
   * @param loader ResourceLoader used to load the files
   */
  public void inform(ResourceLoader loader) throws IOException {
    assureMatchVersion();
    String dictionaryArg = args.get(PARAM_DICTIONARY);
    if (dictionaryArg == null) {
      throw new IllegalArgumentException("Parameter " + PARAM_DICTIONARY + " is mandatory.");
    }
    String dictionaryFiles[] = args.get(PARAM_DICTIONARY).split(",");
    String affixFile = args.get(PARAM_AFFIX);
    String pic = args.get(PARAM_IGNORE_CASE);
    if (pic != null) {
      if (pic.equalsIgnoreCase(TRUE)) ignoreCase = true;
      else if (pic.equalsIgnoreCase(FALSE)) ignoreCase = false;
      else
        throw new IllegalArgumentException(
            "Unknown value for " + PARAM_IGNORE_CASE + ": " + pic + ". Must be true or false");
    }

    String strictAffixParsingParam = args.get(PARAM_STRICT_AFFIX_PARSING);
    boolean strictAffixParsing = true;
    if (strictAffixParsingParam != null) {
      if (strictAffixParsingParam.equalsIgnoreCase(FALSE)) strictAffixParsing = false;
      else if (strictAffixParsingParam.equalsIgnoreCase(TRUE)) strictAffixParsing = true;
      else
        throw new IllegalArgumentException(
            "Unknown value for "
                + PARAM_STRICT_AFFIX_PARSING
                + ": "
                + strictAffixParsingParam
                + ". Must be true or false");
    }

    InputStream affix = null;
    List<InputStream> dictionaries = new ArrayList<InputStream>();

    try {
      dictionaries = new ArrayList<InputStream>();
      for (String file : dictionaryFiles) {
        dictionaries.add(loader.openResource(file));
      }
      affix = loader.openResource(affixFile);

      this.dictionary =
          new HunspellDictionary(
              affix, dictionaries, luceneMatchVersion, ignoreCase, strictAffixParsing);
    } catch (ParseException e) {
      throw new IOException(
          "Unable to load hunspell data! [dictionary="
              + args.get("dictionary")
              + ",affix="
              + affixFile
              + "]",
          e);
    } finally {
      IOUtils.closeWhileHandlingException(affix);
      IOUtils.closeWhileHandlingException(dictionaries);
    }
  }
Ejemplo n.º 3
0
  private Properties canUpdate() {

    try {
      if (conf == null) return null;
      Properties p = new Properties();
      InputStream confStream = loader.openResource(conf);
      p.load(confStream);
      confStream.close();
      String lastupdate = p.getProperty("lastupdate", "0");
      Long t = new Long(lastupdate);

      if (t > this.lastUpdateTime) {
        this.lastUpdateTime = t.longValue();
        String paths = p.getProperty("files");
        if (paths == null || paths.trim().isEmpty()) return null;
        System.out.println("loading conf");
        return p;
      } else {
        this.lastUpdateTime = t.longValue();
        return null;
      }
    } catch (Exception e) {
      System.err.println("ansj parsing conf NullPointerException~~~~~" + e.getMessage());
      return null;
    }
  }
 // (there are no tests for this functionality)
 private TokenizerFactory loadTokenizerFactory(ResourceLoader loader, String cname)
     throws IOException {
   Class<? extends TokenizerFactory> clazz = loader.findClass(cname, TokenizerFactory.class);
   try {
     TokenizerFactory tokFactory = clazz.getConstructor(Map.class).newInstance(tokArgs);
     if (tokFactory instanceof ResourceLoaderAware) {
       ((ResourceLoaderAware) tokFactory).inform(loader);
     }
     return tokFactory;
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
 }
Ejemplo n.º 5
0
  public void update() throws IOException {
    Properties p = canUpdate();
    if (p != null) {
      List<String> dicPaths = SplitFileNames(p.getProperty("files"));
      List<InputStream> inputStreamList = new ArrayList<InputStream>();
      for (String path : dicPaths) {
        if ((path != null && !path.isEmpty())) {
          InputStream is = loader.openResource(path);

          if (is != null) {
            inputStreamList.add(is);
          }
        }
      }
      if (!inputStreamList.isEmpty()) {
        UserDefineLibrary.reloadMainAndAdd(inputStreamList); // load dic to MainDic
      }
    }
  }
 private List<String> getLines(ResourceLoader loader, String resource) throws IOException {
   return WordlistLoader.getLines(loader.openResource(resource), StandardCharsets.UTF_8);
 }