-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ConvertService and CovertController in backend
- Loading branch information
Showing
10 changed files
with
159 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
backend/src/main/java/de/neuefische/koheis/backend/converter/Conversion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package de.neuefische.koheis.backend.converter; | ||
|
||
public record Conversion( | ||
String kanji, | ||
String kana, | ||
String alphabet | ||
) { | ||
} |
24 changes: 24 additions & 0 deletions
24
backend/src/main/java/de/neuefische/koheis/backend/converter/ConvertController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
backend/src/main/java/de/neuefische/koheis/backend/converter/ConvertService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("", "", ""); | ||
} | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
backend/src/main/java/de/neuefische/koheis/backend/converter/GooConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
backend/src/main/java/de/neuefische/koheis/backend/converter/GooRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package de.neuefische.koheis.backend.converter; | ||
|
||
public record GooRequest( | ||
String app_id, | ||
String sentence, | ||
String output_type | ||
) { | ||
} |
6 changes: 6 additions & 0 deletions
6
backend/src/main/java/de/neuefische/koheis/backend/converter/GooResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package de.neuefische.koheis.backend.converter; | ||
|
||
public record GooResponse( | ||
String converted | ||
) { | ||
} |
20 changes: 20 additions & 0 deletions
20
backend/src/main/java/de/neuefische/koheis/backend/converter/KanaConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} |