JazzyChecker extends JCasAnnotator_ImplBase { @Override public void process(final JCas jcas) throws AnalysisEngineProcessException { AnnotationChecker.requireExists(this, jcas, this.getLogger(), Token.class); AnnotationChecker.requireNotExists(this, jcas, this.getLogger(), SpellingAnomaly.class, SuggestedAction.class); for (Token t : select(jcas, Token.class)) { String tokenText = t.getText(); if (tokenText.matches("[\\.\\?\\!]")) { continue; } if (!dict.isCorrect(tokenText)) { SpellingAnomaly anomaly = new SpellingAnomaly(jcas, t.getBegin(), t.getEnd()); if (tokenText.length() == 1 && !Character.isLetter(tokenText.charAt(0))) { continue; } @SuppressWarnings("unchecked") List<Word> suggestions = dict.getSuggestions(tokenText, scoreThreshold); SuggestionCostTuples tuples = new SuggestionCostTuples(); for (Word suggestion : suggestions) { String suggestionString = suggestion.getWord(); int cost = suggestion.getCost(); if (suggestionString != null) { tuples.addTuple(suggestionString, cost); } } if (tuples.size() > 0) { FSArray actions = new FSArray(jcas, tuples.size()); int i = 0; for (SuggestionCostTuple tuple : tuples) { SuggestedAction action = new SuggestedAction(jcas); action.setReplacement(tuple.getSuggestion()); action.setCertainty(tuple.getNormalizedCost(tuples.getMaxCost())); actions.set(i, action); i++; } anomaly.setSuggestions(actions); anomaly.addToIndexes(); } } } }  @Override void initialize(final UimaContext context); @Override void process(final JCas jcas); static final String PARAM_MODEL_LOCATION; static final String PARAM_MODEL_ENCODING; static final String PARAM_SCORE_THRESHOLD; }
@Test public void spellCheckerTest() throws Exception { String testDocumentEnglish = "The cat sta on the mat . Some errosr occur in user " + "discourse morre often . What do you tink ?"; List<String> errorsEnglish = new ArrayList<String>(); errorsEnglish.add("sta"); errorsEnglish.add("errosr"); errorsEnglish.add("morre"); errorsEnglish.add("tink"); AnalysisEngine engine = createEngine(JazzyChecker.class, JazzyChecker.PARAM_MODEL_LOCATION, "src/test/resources/testdict.txt"); JCas aJCas = engine.newJCas(); TokenBuilder<Token, Sentence> tb = TokenBuilder.create(Token.class, Sentence.class); tb.buildTokens(aJCas, testDocumentEnglish); engine.process(aJCas); int i = 0; for (SpellingAnomaly errorAnnotation : select(aJCas, SpellingAnomaly.class)) { assertEquals(errorsEnglish.get(i), errorAnnotation.getCoveredText()); i++; } assertEquals(4, i); }
@Test public void contextualizedSpellCheckerTest() throws Exception { String testDocumentEnglish = "The cat sta on the mat ."; ExternalResourceDescription resource = createResourceDescription( TestFrequencyCountResource.class); AnalysisEngine engine = createEngine( createEngineDescription( createEngineDescription( JazzyChecker.class, JazzyChecker.PARAM_SCORE_THRESHOLD, 3, JazzyChecker.PARAM_MODEL_LOCATION, "src/test/resources/testdict_variants.txt" ), createEngineDescription( CorrectionsContextualizer.class, CorrectionsContextualizer.RES_FREQUENCY_PROVIDER, resource ) ) ); JCas aJCas = engine.newJCas(); TokenBuilder<Token, Sentence> tb = TokenBuilder.create(Token.class, Sentence.class); tb.buildTokens(aJCas, testDocumentEnglish); engine.process(aJCas); int i = 0; for (SpellingAnomaly errorAnnotation : select(aJCas, SpellingAnomaly.class)) { assertEquals("sat", errorAnnotation.getSuggestions(0).getReplacement()); i++; } assertEquals(1, i); }