From b162773ee7e6b3dfdd7d30f76128def7eeb29815 Mon Sep 17 00:00:00 2001 From: Richard Eckart de Castilho Date: Wed, 4 Sep 2019 18:05:17 +0200 Subject: [PATCH] #1410 - LanguageToolChecker does not fill in suggestions - Fill in suggestions - Add test using French --- .../languagetool/LanguageToolChecker.java | 12 ++++++ .../languagetool/LanguageToolCheckerTest.java | 41 +++++++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/dkpro-core-languagetool-asl/src/main/java/org/dkpro/core/languagetool/LanguageToolChecker.java b/dkpro-core-languagetool-asl/src/main/java/org/dkpro/core/languagetool/LanguageToolChecker.java index 788b08d6d7..551179fe20 100644 --- a/dkpro-core-languagetool-asl/src/main/java/org/dkpro/core/languagetool/LanguageToolChecker.java +++ b/dkpro-core-languagetool-asl/src/main/java/org/dkpro/core/languagetool/LanguageToolChecker.java @@ -17,8 +17,11 @@ */ package org.dkpro.core.languagetool; +import static org.apache.uima.fit.util.FSCollectionFactory.createFSArray; + import java.io.IOException; import java.net.URL; +import java.util.ArrayList; import java.util.List; import java.util.Properties; @@ -40,6 +43,7 @@ import org.languagetool.rules.RuleMatch; import de.tudarmstadt.ukp.dkpro.core.api.anomaly.type.GrammarAnomaly; +import de.tudarmstadt.ukp.dkpro.core.api.anomaly.type.SuggestedAction; import eu.openminted.share.annotations.api.Component; import eu.openminted.share.annotations.api.DocumentationResource; import eu.openminted.share.annotations.api.constants.OperationType; @@ -120,6 +124,14 @@ public void process(JCas aJCas) throws AnalysisEngineProcessException annotation.setBegin(match.getFromPos()); annotation.setEnd(match.getToPos()); annotation.setDescription(match.getMessage()); + List 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); } diff --git a/dkpro-core-languagetool-asl/src/test/java/org/dkpro/core/languagetool/LanguageToolCheckerTest.java b/dkpro-core-languagetool-asl/src/test/java/org/dkpro/core/languagetool/LanguageToolCheckerTest.java index 503b5a0128..5bb905e3c8 100644 --- a/dkpro-core-languagetool-asl/src/test/java/org/dkpro/core/languagetool/LanguageToolCheckerTest.java +++ b/dkpro-core-languagetool-asl/src/test/java/org/dkpro/core/languagetool/LanguageToolCheckerTest.java @@ -23,20 +23,21 @@ import org.apache.uima.analysis_engine.AnalysisEngine; import org.apache.uima.fit.testing.factory.TokenBuilder; +import org.apache.uima.fit.util.JCasUtil; import org.apache.uima.jcas.JCas; import org.dkpro.core.testing.DkproTestContext; import org.junit.Rule; import org.junit.Test; import de.tudarmstadt.ukp.dkpro.core.api.anomaly.type.GrammarAnomaly; +import de.tudarmstadt.ukp.dkpro.core.api.anomaly.type.SuggestedAction; import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence; import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token; public class LanguageToolCheckerTest { @Test - public void grammarCheckerTest() - throws Exception + public void grammarCheckerTest() throws Exception { String testDocument = "A sentence with a error in the Hitchhiker's Guide tot he Galaxy ."; @@ -54,11 +55,45 @@ public void grammarCheckerTest() 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 tb = new TokenBuilder<>(Token.class, Sentence.class); + tb.buildTokens(aJCas, testDocument); + + engine.process(aJCas); + + // copy input match type annotations to an array + 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); + } + @Rule public DkproTestContext testContext = new DkproTestContext(); }