Exemple #1
0
 public static void main(String[] args) throws Exception {
   ensureCorrectUsageOrExit(args);
   final long startTime = System.currentTimeMillis();
   final String[] ruleIds = args[0].split(",");
   final File ruleFile = new File(args[1]);
   final String languageCode = args[2];
   final Language language = Language.getLanguageForShortName(languageCode);
   final File indexDir = new File(args[3]);
   if (args.length > 4 && "--no_limit".equals(args[4])) {
     limitSearch = false;
   }
   final Searcher searcher = new Searcher(new SimpleFSDirectory(indexDir));
   if (!limitSearch) {
     searcher.setMaxHits(100000);
   }
   for (String ruleId : ruleIds) {
     final long ruleStartTime = System.currentTimeMillis();
     for (PatternRule rule : searcher.getRuleById(ruleId, ruleFile)) {
       final SearcherResult searcherResult = searcher.findRuleMatchesOnIndex(rule, language);
       int i = 1;
       if (searcherResult.getMatchingSentences().size() == 0) {
         System.out.println("[no matches]");
       }
       for (MatchingSentence ruleMatch : searcherResult.getMatchingSentences()) {
         System.out.println(
             i + ": " + ruleMatch.getSentence() + " (Source: " + ruleMatch.getSource() + ")");
         i++;
       }
       System.out.println("Time: " + (System.currentTimeMillis() - ruleStartTime) + "ms");
       System.out.println("==============================================================");
     }
   }
   System.out.println("Total time: " + (System.currentTimeMillis() - startTime) + "ms");
 }
  /**
   * Takes an XML file named <tt>rules-xx-language.xml</tt>, e.g. <tt>rules-de-German.xml</tt> and
   * builds a Language object for that language.
   */
  private static Language makeLanguage(final File file, final boolean isAdditional)
      throws IllegalAccessException, InstantiationException {
    Objects.requireNonNull(file, "file cannot be null");
    if (!file.getName().endsWith(".xml")) {
      throw new RuleFilenameException(file);
    }
    final String[] parts = file.getName().split("-");
    final boolean startsWithRules = parts[0].equals("rules");
    final boolean secondPartHasCorrectLength =
        parts.length == 3
            && (parts[1].length() == "en".length()
                || parts[1].length() == "ast".length()
                || parts[1].length() == "en_US".length());
    if (!startsWithRules || !secondPartHasCorrectLength) {
      throw new RuleFilenameException(file);
    }
    // TODO: when the XML file is mergeable with
    // other rules (check this in the XML Rule Loader by using rules[@integrate='add']?),
    // subclass the existing language,
    // and adjust the settings if any are set in the rule file default configuration set

    Language newLanguage;
    if (Language.isLanguageSupported(parts[1])) {
      newLanguage = Language.getLanguageForShortName(parts[1]).getClass().newInstance();
      newLanguage.addExternalRuleFile(file.getAbsolutePath());
      newLanguage.setName(parts[2].replace(".xml", ""));
      newLanguage.makeExternal();
    } else {
      newLanguage =
          new Language() {
            @Override
            public Locale getLocale() {
              return new Locale(getShortName());
            }

            @Override
            public Contributor[] getMaintainers() {
              return null;
            }

            @Override
            public String getShortName() {
              if (parts[1].length() == 2) {
                return parts[1];
              }
              return parts[1].split("_")[0]; // en as in en_US
            }

            @Override
            public String[] getCountries() {
              if (parts[1].length() == 2) {
                return new String[] {""};
              }
              return new String[] {parts[1].split("_")[1]}; // US as in en_US
            }

            @Override
            public String getName() {
              return parts[2].replace(".xml", "");
            }

            @Override
            public void setName(final String name) {
              // cannot be changed for this language
            }

            @Override
            public List<Class<? extends Rule>> getRelevantRules() {
              return Collections.emptyList();
            }

            @Override
            public List<String> getRuleFileNames() {
              final List<String> ruleFiles = new ArrayList<>();
              ruleFiles.add(file.getAbsolutePath());
              return ruleFiles;
            }

            @Override
            public boolean isExternal() {
              return isAdditional;
            }
          };
    }
    return newLanguage;
  }
Exemple #3
0
 public static void main(String[] args) throws IOException, InterruptedException {
   if (args.length != 2 && args.length != 3) {
     System.out.println(
         "Usage: "
             + AtomFeedCheckerCmd.class.getSimpleName()
             + " <atomFeedUrl> <sleepTime> [database.properties]");
     System.out.println("  <atomFeedUrl> is a Wikipedia URL to the latest changes, for example:");
     System.out.println(
         "    https://de.wikipedia.org/w/index.php?title=Spezial:Letzte_%C3%84nderungen&feed=atom&namespace=0");
     System.out.println(
         "  <sleepTime> -1: don't loop at all (run once), 0: run in loop, other number: run in loop and");
     System.out.println("    wait this many milliseconds between runs");
     System.out.println(
         "  [database.properties] (optional) is a file that defines dbUrl, dbUser, and dbPassword,");
     System.out.println("    used to write the results to a database via JDBC");
     System.out.println("");
     System.out.println(
         "  When the database.properties file is specified, this command will store all feed changes that");
     System.out.println(
         "  cause LanguageTool rule matches to the database. If an error is then fixed later, this will");
     System.out.println(
         "  usually also be detected and the rule match in the database will be marked as fixed. One case");
     System.out.println(
         "  where this does not work is if the context of the error gets modified before the error is fixed.");
     System.out.println("");
     System.out.println(
         "  Run this command regularly so that you don't miss any changes from the feed.");
     System.out.println(
         "  As the feed may contain only the latest 50 changes, running it more often than");
     System.out.println("  once per minute may be needed for active Wikipedias.");
     System.exit(1);
   }
   String url = args[0];
   String langCode = url.substring(url.indexOf("//") + 2, url.indexOf("."));
   System.out.println("Using URL: " + url);
   System.out.println("Language code: " + langCode);
   int sleepTimeMillis = Integer.parseInt(args[1]);
   System.out.println("Sleep time: " + sleepTimeMillis + "ms (-1 = don't loop)");
   DatabaseConfig databaseConfig = null;
   if (args.length == 3) {
     String propFile = args[2];
     databaseConfig = new DatabaseConfig(propFile);
     System.out.println("Writing results to database at: " + databaseConfig.getUrl());
   }
   Language language = Language.getLanguageForShortName(langCode);
   AtomFeedChecker atomFeedChecker = new AtomFeedChecker(language, databaseConfig);
   while (true) {
     long startTime = System.currentTimeMillis();
     try {
       atomFeedChecker.runCheck(url);
       System.out.println("Run time: " + (System.currentTimeMillis() - startTime) + "ms");
       if (sleepTimeMillis == -1) {
         // don't loop at all
         break;
       } else {
         System.out.println("Sleeping " + sleepTimeMillis + "ms...");
         Thread.sleep(sleepTimeMillis);
       }
     } catch (Exception e) {
       // e.g. 50x HTTP errors, network problems
       e.printStackTrace();
       System.out.println("Sleeping " + sleepTimeMillis + "ms...");
       Thread.sleep(sleepTimeMillis);
     }
   }
 }