private static List<Map<String, String>> loadWords(String filename, Language lang)
          throws IOException {
    List<Map<String, String>> list = new ArrayList<>();
    InputStream stream = JLanguageTool.getDataBroker().getFromRulesDirAsStream(filename);
    try (
      InputStreamReader isr = new InputStreamReader(stream, StandardCharsets.UTF_8);
      BufferedReader br = new BufferedReader(isr)) 
    {
      String line;
      while ((line = br.readLine()) != null) {
        line = line.trim();
        if (line.isEmpty() || line.charAt(0) == '#') { // ignore comments
          continue;
        }

        String[] parts = line.split("=");
        if (parts.length != 2) {
          throw new IOException("Format error in file "
                  + JLanguageTool.getDataBroker().getFromRulesDirAsUrl(filename)
                  + ". Expected exactly 1 '=' character. Line: " + line);
        }

        String[] wrongForms = parts[0].split("\\|"); // multiple incorrect forms
        for (String wrongForm : wrongForms) {
          int wordCount = 0;
          List<String> tokens = lang.getWordTokenizer().tokenize(wrongForm);
          for (String token : tokens) {
            if (!StringTools.isWhitespace(token)) {
              wordCount++;
            }
          }
          // grow if necessary
          for (int i = list.size(); i < wordCount; i++) {
            list.add(new HashMap<>());
          }
          list.get(wordCount - 1).put(wrongForm, parts[1]);
        }
      }
    }
    // seal the result (prevent modification from outside this class)
    List<Map<String,String>> result = new ArrayList<>();
    for (Map<String, String> map : list) {
      result.add(Collections.unmodifiableMap(map));
    }
    return Collections.unmodifiableList(result);
  }
private static List < Map < String , String > > loadWords ( String filename , Language lang ) throws IOException { List < Map < String , String > > list = new ArrayList < > ( ) ; InputStream stream = JLanguageTool . getDataBroker ( ) . getFromRulesDirAsStream ( filename ) ; try ( InputStreamReader isr = new InputStreamReader ( stream , StandardCharsets . UTF_8 ) ; BufferedReader br = new BufferedReader ( isr ) ) { String line ; while ( ( line = br . readLine ( ) ) != null ) { line = line . trim ( ) ; if ( line . isEmpty ( ) || line . charAt ( 0 ) == '#' ) { continue ; } String [ ] parts = line . split ( "=" ) ; if ( parts . length != 2 ) { throw new IOException ( "Format error in file " + JLanguageTool . getDataBroker ( ) . getFromRulesDirAsUrl ( filename ) + ". Expected exactly 1 '=' character. Line: " + line ) ; } String [ ] wrongForms = parts [ 0 ] . split ( "\\|" ) ; for ( String wrongForm : wrongForms ) { int wordCount = 0 ; List < String > tokens = lang . getWordTokenizer ( ) . tokenize ( wrongForm ) ; for ( String token : tokens ) { if ( ! StringTools . isWhitespace ( token ) ) { wordCount ++ ; } } for ( int i = list . size ( ) ; i < wordCount ; i ++ ) { list . add ( new HashMap < > ( ) ) ; } list . get ( wordCount - 1 ) . put ( wrongForm , parts [ 1 ] ) ; } } } List < Map < String , String > > result = new ArrayList < > ( ) ; for ( Map < String , String > map : list ) { result . add ( Collections . unmodifiableMap ( map ) ) ; } return Collections . unmodifiableList ( result ) ; }
Load the list of words.
Same as {@link AbstractSimpleReplaceRule#loadFromPath} but allows multiple words.
@param filename the file from classpath to load
@return the list of maps containing the error-corrections pairs. The n-th map contains key strings of (n+1) words.