Skip to content

Commit

Permalink
remote synthesizer
Browse files Browse the repository at this point in the history
  • Loading branch information
jaumeortola committed Mar 3, 2023
1 parent f39b2a2 commit 187d80b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.languagetool.server;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.languagetool.AnalyzedToken;
import org.languagetool.Language;
import org.languagetool.Languages;
import org.languagetool.synthesis.Synthesizer;

public class RemoteSynthesizer {

protected List<String> synthesize(String languageCode, String lemma, String postagRegexp) throws IOException {
Language lang = null;
lang = Languages.getLanguageForShortCode(languageCode);
Synthesizer synth = lang.getSynthesizer();
AnalyzedToken at = new AnalyzedToken(lemma, postagRegexp, lemma);
String[] synthesizedForms = synth.synthesize(at, postagRegexp, true);
// removing duplicates. TODO: de-duplicate in the original synthesizer (?)
List<String> results = new ArrayList<>();
for (String s : synthesizedForms) {
if (!results.contains(s)) {
results.add(s);
}
}
return results;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.languagetool.server;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class RemoteSynthesizerTest {

@Test
public void testSynthesis() throws Exception {
RemoteSynthesizer remoteSynth = new RemoteSynthesizer();

assertEquals(remoteSynth.synthesize("de", "Äußerung","SUB:NOM:PLU:FEM").toString(), "[Äußerungen]");
// Removing duplicates!
assertEquals(remoteSynth.synthesize("de", "Äußerung","SUB:.*:PLU:FEM").toString(), "[Äußerungen]");
assertEquals(remoteSynth.synthesize("pt", "resolver","VMIS3S0").toString(), "[resolveu]");
assertEquals(remoteSynth.synthesize("es", "cantar","VMIP1S0").toString(), "[canto]");
assertEquals(remoteSynth.synthesize("es", "señor","NC.P.*").toString(), "[señoras, señores]");
assertEquals(remoteSynth.synthesize("fr", "monde","N m p").toString(), "[mondes]");
assertEquals(remoteSynth.synthesize("fr", "chanter","V ppa.*").toString(), "[chantées, chantée, chantés, chanté]");
assertEquals(remoteSynth.synthesize("en", "be","VBZ").toString(), "[is]");
assertEquals(remoteSynth.synthesize("en-US", "be","VBZ").toString(), "[is]");
assertEquals(remoteSynth.synthesize("en-GB", "be","VBZ").toString(), "[is]");
assertEquals(remoteSynth.synthesize("en", "be","V.*").toString(), "[be, was, were, being, been, are, is]");
assertEquals(remoteSynth.synthesize("en", "be","N.*").toString(), "[]");

}

}

0 comments on commit 187d80b

Please sign in to comment.