From 073d599600f2beaa73d7d1df2b2e87ffa9c9e764 Mon Sep 17 00:00:00 2001 From: Kohei Suzuki Date: Fri, 6 Oct 2023 14:47:48 +0200 Subject: [PATCH] Add ConvertService and CovertController in backend --- README.md | 29 ++------- backend/pom.xml | 9 +++ .../koheis/backend/converter/Conversion.java | 8 +++ .../backend/converter/ConvertController.java | 24 ++++++++ .../backend/converter/ConvertService.java | 61 +++++++++++++++++++ .../koheis/backend/converter/GooConfig.java | 16 +++++ .../koheis/backend/converter/GooRequest.java | 8 +++ .../koheis/backend/converter/GooResponse.java | 6 ++ .../backend/converter/KanaConverter.java | 20 ++++++ .../src/main/resources/application.properties | 2 + 10 files changed, 159 insertions(+), 24 deletions(-) create mode 100644 backend/src/main/java/de/neuefische/koheis/backend/converter/Conversion.java create mode 100644 backend/src/main/java/de/neuefische/koheis/backend/converter/ConvertController.java create mode 100644 backend/src/main/java/de/neuefische/koheis/backend/converter/ConvertService.java create mode 100644 backend/src/main/java/de/neuefische/koheis/backend/converter/GooConfig.java create mode 100644 backend/src/main/java/de/neuefische/koheis/backend/converter/GooRequest.java create mode 100644 backend/src/main/java/de/neuefische/koheis/backend/converter/GooResponse.java create mode 100644 backend/src/main/java/de/neuefische/koheis/backend/converter/KanaConverter.java diff --git a/README.md b/README.md index 6fa3e23..91d4d06 100644 --- a/README.md +++ b/README.md @@ -26,29 +26,10 @@ Children between 5-15 years old learning Japanese as (one of) their home languag (** upcoming features) ## 6. Tech Stack ### Backend -- Java -- Spring Boot -- Maven -- Lombok -- REST -- DeepL API +Java, Spring Boot, Maven, REST, MongoDB, DeepL API, goo API ### Frontend -- JavaScript -- TypeScript -- CSS -- HTML -- React -- Vite -- Axios -- MUI -### Data Bank -- MongoDB +JavaScript, TypeScript, CSS, HTML, React, Vite, MUI ### TDD -- JUnit -- Mockito -- SonarCloud -- DeepL API mock server -### CI/CD -- GitHub Actions -- Docker -- AWS +JUnit, Mockito, SonarLint, SonarCloud +### DevOps, CI/CD +Docker, GitHub Actions, AWS diff --git a/backend/pom.xml b/backend/pom.xml index 29e0249..04ad216 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -68,11 +68,20 @@ org.springframework.boot spring-boot-starter-validation + + org.springframework.boot + spring-boot-starter-webflux + com.deepl.api deepl-java 1.3.0 + + com.ibm.icu + icu4j + 73.2 + kotokoapp diff --git a/backend/src/main/java/de/neuefische/koheis/backend/converter/Conversion.java b/backend/src/main/java/de/neuefische/koheis/backend/converter/Conversion.java new file mode 100644 index 0000000..2c0ff5a --- /dev/null +++ b/backend/src/main/java/de/neuefische/koheis/backend/converter/Conversion.java @@ -0,0 +1,8 @@ +package de.neuefische.koheis.backend.converter; + +public record Conversion( + String kanji, + String kana, + String alphabet +) { +} diff --git a/backend/src/main/java/de/neuefische/koheis/backend/converter/ConvertController.java b/backend/src/main/java/de/neuefische/koheis/backend/converter/ConvertController.java new file mode 100644 index 0000000..3cace5a --- /dev/null +++ b/backend/src/main/java/de/neuefische/koheis/backend/converter/ConvertController.java @@ -0,0 +1,24 @@ +package de.neuefische.koheis.backend.converter; + +import de.neuefische.koheis.backend.translation.Translation; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("api/converter") +public class ConvertController { + + private final ConvertService convertService; + + public ConvertController(ConvertService convertService) { + this.convertService = convertService; + } + + @PostMapping + public Conversion convertTranslationToConversion(@RequestBody Translation translation) { + return convertService.convertTranslationToConversion(translation); + } + +} diff --git a/backend/src/main/java/de/neuefische/koheis/backend/converter/ConvertService.java b/backend/src/main/java/de/neuefische/koheis/backend/converter/ConvertService.java new file mode 100644 index 0000000..5f9ad97 --- /dev/null +++ b/backend/src/main/java/de/neuefische/koheis/backend/converter/ConvertService.java @@ -0,0 +1,61 @@ +package de.neuefische.koheis.backend.converter; + +import de.neuefische.koheis.backend.translation.Translation; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Service; +import org.springframework.web.reactive.function.client.WebClient; +import java.util.Objects; + +@Service +public class ConvertService { + + private final GooConfig gooConfig; + private final WebClient webClient; + private final KanaConverter kanaConverter; + + public ConvertService(GooConfig gooConfig, @Value("${goo.api.url}") String url, KanaConverter kanaConverter) { + this.gooConfig = gooConfig; + this.webClient = WebClient.create(url); + this.kanaConverter = kanaConverter; + } + + public GooRequest getGooRequest(String original, String kanaType) { + String appId = gooConfig.getId(); + return new GooRequest(appId, original, kanaType); + } + + public String convertKanjiToKana(GooRequest gooRequest) { + return Objects.requireNonNull(webClient.post() + .contentType(MediaType.APPLICATION_JSON) + .bodyValue(gooRequest) + .retrieve() + .toEntity(GooResponse.class) + .block()).getBody().converted(); + } + + public String convertKanaToAlphabet(String kana) { + if (kana.matches("^[\\u3040-\\u309F]+$")) { + return kanaConverter.convertHiraganaToAlphabet(kana); + } else if (kana.matches("^[\\u30A0-\\u30FF]+$")) { + return kanaConverter.convertKatakanaToAlphabet(kana); + } else { + return "Invalid input. Input must be hiragana or katakana!"; + } + } + + public Conversion convertTranslationToConversion(Translation translation) { + String translated = translation.getJapanese(); + if (translated.matches("^[\\u4E00-\\u9FFF]+$")) { + String kana = convertKanjiToKana(getGooRequest(translated, "hiragana")); + String alphabet = convertKanaToAlphabet(kana); + return new Conversion(translated, kana, alphabet); + } else if ((translated.matches("^[\\u3040-\\u309F]+$")) || (translation.getJapanese().matches("^[\\u30A0-\\u30FF]+$"))) { + String alphabet = convertKanaToAlphabet(translated); + return new Conversion("", translated, alphabet); + } else { + return new Conversion("", "", ""); + } + } + +} diff --git a/backend/src/main/java/de/neuefische/koheis/backend/converter/GooConfig.java b/backend/src/main/java/de/neuefische/koheis/backend/converter/GooConfig.java new file mode 100644 index 0000000..ec5e0d4 --- /dev/null +++ b/backend/src/main/java/de/neuefische/koheis/backend/converter/GooConfig.java @@ -0,0 +1,16 @@ +package de.neuefische.koheis.backend.converter; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class GooConfig { + + @Value("${goo.api.id}") + private String apiId; + + public String getId() { + return apiId; + } + +} diff --git a/backend/src/main/java/de/neuefische/koheis/backend/converter/GooRequest.java b/backend/src/main/java/de/neuefische/koheis/backend/converter/GooRequest.java new file mode 100644 index 0000000..96aad94 --- /dev/null +++ b/backend/src/main/java/de/neuefische/koheis/backend/converter/GooRequest.java @@ -0,0 +1,8 @@ +package de.neuefische.koheis.backend.converter; + +public record GooRequest( + String app_id, + String sentence, + String output_type +) { +} diff --git a/backend/src/main/java/de/neuefische/koheis/backend/converter/GooResponse.java b/backend/src/main/java/de/neuefische/koheis/backend/converter/GooResponse.java new file mode 100644 index 0000000..21ee8cb --- /dev/null +++ b/backend/src/main/java/de/neuefische/koheis/backend/converter/GooResponse.java @@ -0,0 +1,6 @@ +package de.neuefische.koheis.backend.converter; + +public record GooResponse( + String converted +) { +} diff --git a/backend/src/main/java/de/neuefische/koheis/backend/converter/KanaConverter.java b/backend/src/main/java/de/neuefische/koheis/backend/converter/KanaConverter.java new file mode 100644 index 0000000..6dad23e --- /dev/null +++ b/backend/src/main/java/de/neuefische/koheis/backend/converter/KanaConverter.java @@ -0,0 +1,20 @@ +package de.neuefische.koheis.backend.converter; + +import com.ibm.icu.text.Transliterator; +import org.springframework.stereotype.Component; + +@Component +public class KanaConverter { + + public String convertHiraganaToAlphabet(String hiragana) { + Transliterator trans = Transliterator.getInstance("Hiragana-Latin"); + return trans.transliterate(hiragana); + + } + + public String convertKatakanaToAlphabet(String katakana) { + Transliterator trans = Transliterator.getInstance("Katakana-Latin"); + return trans.transliterate(katakana); + } + +} diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index 2d55748..018854d 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -1,3 +1,5 @@ spring.data.mongodb.uri=${MONGO_DB_URI} server.error.include-message=always deepl.api.key=${DEEPL_API_KEY} +goo.api.id=${GOO_API_ID} +goo.api.url=${GOO_API_URL} \ No newline at end of file