LanguageToolChecker extends JCasAnnotator_ImplBase { @Override public void process(JCas aJCas) throws AnalysisEngineProcessException { modelProvider.configure(aJCas.getCas()); String docText = aJCas.getDocumentText(); try { List<RuleMatch> matches = modelProvider.getResource().check(docText); for (RuleMatch match : matches) { GrammarAnomaly annotation = new GrammarAnomaly(aJCas); annotation.setBegin(match.getFromPos()); annotation.setEnd(match.getToPos()); annotation.setDescription(match.getMessage()); List<SuggestedAction> suggestions = new ArrayList<>(); for (String replacement : match.getSuggestedReplacements()) { SuggestedAction action = new SuggestedAction(aJCas, annotation.getBegin(), annotation.getEnd()); action.setReplacement(replacement); suggestions.add(action); } annotation.setSuggestions(createFSArray(aJCas, suggestions)); annotation.addToIndexes(); getContext().getLogger().log(Level.FINEST, "Found: " + annotation); } } catch (IOException e) { throw new AnalysisEngineProcessException(e); } }  @Override void initialize(UimaContext aContext); @Override void process(JCas aJCas); static final String PARAM_LANGUAGE; }
@Test public void grammarCheckerTest() throws Exception { String testDocument = "A sentence with a error in the Hitchhiker's Guide tot he Galaxy ."; AnalysisEngine engine = createEngine(LanguageToolChecker.class, LanguageToolChecker.PARAM_LANGUAGE, "en"); JCas aJCas = engine.newJCas(); TokenBuilder<Token, Sentence> tb = new TokenBuilder<>(Token.class, Sentence.class); tb.buildTokens(aJCas, testDocument); engine.process(aJCas); int count = 0; for (GrammarAnomaly ga : select(aJCas, GrammarAnomaly.class)) { System.out.println("Error " + (count + 1) + " (" + ga.getBegin() + ", " + ga.getEnd() + "):" + ga.getDescription()); for (SuggestedAction action : JCasUtil.select(ga.getSuggestions(), SuggestedAction.class)) { System.out.printf("-> %s (score %f)%n", action.getReplacement(), action.getCertainty()); } count++; } assertEquals(count, 3); }
@Test public void grammarCheckerTestFrench() throws Exception { String testDocument = "comment modifer un compte"; AnalysisEngine engine = createEngine(LanguageToolChecker.class, LanguageToolChecker.PARAM_LANGUAGE, "fr"); JCas aJCas = engine.newJCas(); TokenBuilder<Token, Sentence> tb = new TokenBuilder<>(Token.class, Sentence.class); tb.buildTokens(aJCas, testDocument); engine.process(aJCas); int count = 0; for (GrammarAnomaly ga : select(aJCas, GrammarAnomaly.class)) { System.out.println("Error " + (count + 1) + " (" + ga.getBegin() + ", " + ga.getEnd() + "):" + ga.getDescription()); for (SuggestedAction action : JCasUtil.select(ga.getSuggestions(), SuggestedAction.class)) { System.out.printf("-> %s (score %f)%n", action.getReplacement(), action.getCertainty()); } count++; } assertEquals(count, 2); }