From 085a68b606e913088fec444973ff59f94f338b3f Mon Sep 17 00:00:00 2001 From: drejmanMacopedia Date: Tue, 9 May 2023 09:53:08 +0200 Subject: [PATCH] Remove notes added by ChatGPT to translation --- .../Client/OpenAiClient/Response/ErrorResponse.php | 9 +++++++-- .../OpenAiTranslator/Translator/OpenAiTranslator.php | 8 +++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Response/ErrorResponse.php b/src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Response/ErrorResponse.php index 9596353..40f456a 100644 --- a/src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Response/ErrorResponse.php +++ b/src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Response/ErrorResponse.php @@ -9,12 +9,17 @@ class ErrorResponse extends Response { public function __construct( - private string $error + private string|array $error ) { } public function getError(): string { - return $this->error; + $error = $this->error; + + if (is_array($error)) { + $error = json_encode($error); + } + return $error; } } diff --git a/src/Macopedia/OpenAiTranslator/Translator/OpenAiTranslator.php b/src/Macopedia/OpenAiTranslator/Translator/OpenAiTranslator.php index 9b8aaa4..6ac423e 100644 --- a/src/Macopedia/OpenAiTranslator/Translator/OpenAiTranslator.php +++ b/src/Macopedia/OpenAiTranslator/Translator/OpenAiTranslator.php @@ -17,8 +17,14 @@ public function __construct( public function translate(string $text, Language $targetLanguageCode): ?string { - return $this + $answer = $this ->openAiClient ->ask('user', sprintf(self::MESSAGE, $targetLanguageCode->asText(), $text)); + + if ($answer !== null) { + $answer = preg_replace('/(\(Note.*)/','',$answer); + } + + return $answer; } }