Skip to content

Commit 42498c5

Browse files
authored
Merge pull request #1411 from dkpro/bugfix/1410-LanguageToolChecker-does-not-fill-in-suggestions
#1410 - LanguageToolChecker does not fill in suggestions
2 parents 03793a6 + b162773 commit 42498c5

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

dkpro-core-languagetool-asl/src/main/java/org/dkpro/core/languagetool/LanguageToolChecker.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
*/
1818
package org.dkpro.core.languagetool;
1919

20+
import static org.apache.uima.fit.util.FSCollectionFactory.createFSArray;
21+
2022
import java.io.IOException;
2123
import java.net.URL;
24+
import java.util.ArrayList;
2225
import java.util.List;
2326
import java.util.Properties;
2427

@@ -40,6 +43,7 @@
4043
import org.languagetool.rules.RuleMatch;
4144

4245
import de.tudarmstadt.ukp.dkpro.core.api.anomaly.type.GrammarAnomaly;
46+
import de.tudarmstadt.ukp.dkpro.core.api.anomaly.type.SuggestedAction;
4347
import eu.openminted.share.annotations.api.Component;
4448
import eu.openminted.share.annotations.api.DocumentationResource;
4549
import eu.openminted.share.annotations.api.constants.OperationType;
@@ -120,6 +124,14 @@ public void process(JCas aJCas) throws AnalysisEngineProcessException
120124
annotation.setBegin(match.getFromPos());
121125
annotation.setEnd(match.getToPos());
122126
annotation.setDescription(match.getMessage());
127+
List<SuggestedAction> suggestions = new ArrayList<>();
128+
for (String replacement : match.getSuggestedReplacements()) {
129+
SuggestedAction action = new SuggestedAction(aJCas, annotation.getBegin(),
130+
annotation.getEnd());
131+
action.setReplacement(replacement);
132+
suggestions.add(action);
133+
}
134+
annotation.setSuggestions(createFSArray(aJCas, suggestions));
123135
annotation.addToIndexes();
124136
getContext().getLogger().log(Level.FINEST, "Found: " + annotation);
125137
}

dkpro-core-languagetool-asl/src/test/java/org/dkpro/core/languagetool/LanguageToolCheckerTest.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,21 @@
2323

2424
import org.apache.uima.analysis_engine.AnalysisEngine;
2525
import org.apache.uima.fit.testing.factory.TokenBuilder;
26+
import org.apache.uima.fit.util.JCasUtil;
2627
import org.apache.uima.jcas.JCas;
2728
import org.dkpro.core.testing.DkproTestContext;
2829
import org.junit.Rule;
2930
import org.junit.Test;
3031

3132
import de.tudarmstadt.ukp.dkpro.core.api.anomaly.type.GrammarAnomaly;
33+
import de.tudarmstadt.ukp.dkpro.core.api.anomaly.type.SuggestedAction;
3234
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence;
3335
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token;
3436

3537
public class LanguageToolCheckerTest
3638
{
3739
@Test
38-
public void grammarCheckerTest()
39-
throws Exception
40+
public void grammarCheckerTest() throws Exception
4041
{
4142
String testDocument = "A sentence with a error in the Hitchhiker's Guide tot he Galaxy .";
4243

@@ -54,11 +55,45 @@ public void grammarCheckerTest()
5455
for (GrammarAnomaly ga : select(aJCas, GrammarAnomaly.class)) {
5556
System.out.println("Error " + (count + 1) + " (" + ga.getBegin() + ", " + ga.getEnd()
5657
+ "):" + ga.getDescription());
58+
for (SuggestedAction action : JCasUtil.select(ga.getSuggestions(),
59+
SuggestedAction.class)) {
60+
System.out.printf("-> %s (score %f)%n", action.getReplacement(),
61+
action.getCertainty());
62+
}
5763
count++;
5864
}
5965
assertEquals(count, 3);
6066
}
61-
67+
68+
@Test
69+
public void grammarCheckerTestFrench() throws Exception
70+
{
71+
String testDocument = "comment modifer un compte";
72+
73+
AnalysisEngine engine = createEngine(LanguageToolChecker.class,
74+
LanguageToolChecker.PARAM_LANGUAGE, "fr");
75+
JCas aJCas = engine.newJCas();
76+
77+
TokenBuilder<Token, Sentence> tb = new TokenBuilder<>(Token.class, Sentence.class);
78+
tb.buildTokens(aJCas, testDocument);
79+
80+
engine.process(aJCas);
81+
82+
// copy input match type annotations to an array
83+
int count = 0;
84+
for (GrammarAnomaly ga : select(aJCas, GrammarAnomaly.class)) {
85+
System.out.println("Error " + (count + 1) + " (" + ga.getBegin() + ", " + ga.getEnd()
86+
+ "):" + ga.getDescription());
87+
for (SuggestedAction action : JCasUtil.select(ga.getSuggestions(),
88+
SuggestedAction.class)) {
89+
System.out.printf("-> %s (score %f)%n", action.getReplacement(),
90+
action.getCertainty());
91+
}
92+
count++;
93+
}
94+
assertEquals(count, 2);
95+
}
96+
6297
@Rule
6398
public DkproTestContext testContext = new DkproTestContext();
6499
}

0 commit comments

Comments
 (0)