]> git.frustrated-labs.net Git - larousseapi/commitdiff
feat: add duden source for german definitions
authorAlexander Goussas <[email protected]>
Tue, 2 Jun 2026 16:26:39 +0000 (11:26 -0500)
committerAlexander Goussas <[email protected]>
Tue, 2 Jun 2026 16:26:39 +0000 (11:26 -0500)
src/main/java/net/frustratedfunctor/definitions/FrenchWordDefinitionRepository.java
src/main/java/net/frustratedfunctor/definitions/GermanWordDefinitionRepository.java [new file with mode: 0644]
src/main/java/net/frustratedfunctor/definitions/LanguageAwareDefinitionsRepository.java

index 0b26252db185e1647b9bbd52fa4f8b51ce1a0592..f8bf961bf893d3732ca6cfca4c6dca79bd2a757b 100644 (file)
@@ -22,7 +22,7 @@ public class FrenchWordDefinitionRepository implements WordDefinitionRepository
                     .map(Element::ownText)
                     .toList();
         } catch (Exception e) {
-            Log.error("Error while fetching word translations", e);
+            Log.error("Error while fetching word definitions", e);
             return new ArrayList<>();
         }
     }
diff --git a/src/main/java/net/frustratedfunctor/definitions/GermanWordDefinitionRepository.java b/src/main/java/net/frustratedfunctor/definitions/GermanWordDefinitionRepository.java
new file mode 100644 (file)
index 0000000..8790df0
--- /dev/null
@@ -0,0 +1,29 @@
+package net.frustratedfunctor.definitions;
+
+import io.quarkus.logging.Log;
+import jakarta.enterprise.context.ApplicationScoped;
+import org.jsoup.Jsoup;
+import org.jsoup.nodes.Element;
+
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.List;
+
+@ApplicationScoped
+public class GermanWordDefinitionRepository implements WordDefinitionRepository {
+    @Override
+    public List<String> definitionsForWord(String word) {
+        try {
+            final var larousseUrl = URI.create("https://www.duden.de/rechtschreibung/" + word).toURL();
+            final var doc = Jsoup.parse(larousseUrl, 10_000);
+            return doc
+                    .select("#bedeutungen .enumeration__text")
+                    .stream()
+                    .map(Element::ownText)
+                    .toList();
+        } catch (Exception e) {
+            Log.error("Error while fetching word definitions", e);
+            return new ArrayList<>();
+        }
+    }
+}
index fd433080b30c927cbff7d8158413f32d2f34a280..a2059e1eed774bbed20b1ae45a7287147696ab23 100644 (file)
@@ -10,9 +10,11 @@ import java.util.List;
 @ApplicationScoped
 public class LanguageAwareDefinitionsRepository implements WordDefinitionRepository {
     private final FrenchWordDefinitionRepository french;
+    private final GermanWordDefinitionRepository german;
 
-    public LanguageAwareDefinitionsRepository(FrenchWordDefinitionRepository french) {
+    public LanguageAwareDefinitionsRepository(FrenchWordDefinitionRepository french, GermanWordDefinitionRepository german) {
         this.french = french;
+        this.german = german;
     }
 
     @Override
@@ -23,7 +25,7 @@ public class LanguageAwareDefinitionsRepository implements WordDefinitionReposit
     public List<String> definitionsForWord(String word, String lang) {
         return switch (Lang.parseOrDefault(lang)) {
             case French -> french.definitionsForWord(word);
-            case German -> List.of("OOPS! NOT IMPLEMENTED");
+            case German -> german.definitionsForWord(word);
         };
     }
 }