immosquare-translate 0.1.6 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/immosquare-translate/translator.rb +19 -16
- data/lib/immosquare-translate/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fdfdc479af37a288d9d1b16fad5aa331509facfaf906982f2eeec13c4f88bf89
|
4
|
+
data.tar.gz: d4025b70638d454d4363abf40dd29f0a6d86bae51406cf60f0b5dd4b3e77646f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e5da8f3f28db2e39bc251d9a9f466cafdadd0f90a2a122b7e62f05ca66b413e6afda8685c634ab78aa3a5dadc15de513596bef9163516a40d97623a87d454662
|
7
|
+
data.tar.gz: b9d956d253702bb30027f90a8052184be56ce86fc246e728211847f038bbc5ceea885dbc6345f0955d1d59fb7ab102948fa81429c5d71129050ddf6ae8ce77de
|
@@ -26,26 +26,27 @@ module ImmosquareTranslate
|
|
26
26
|
"Authorization" => "Bearer #{ImmosquareTranslate.configuration.openai_api_key}"
|
27
27
|
}
|
28
28
|
|
29
|
-
prompt_system = "As a sophisticated translation AI, your role is to
|
30
|
-
"
|
31
|
-
"
|
32
|
-
"
|
33
|
-
"
|
29
|
+
prompt_system = "As a sophisticated translation AI, your role is to translate sentences from a specified source language to multiple target languages.\n" \
|
30
|
+
"Rules to respect:\n" \
|
31
|
+
"- Use ISO 639-1 language codes for specifying languages." \
|
32
|
+
"- Correct any spelling or grammatical errors in the source text before translating.\n" \
|
33
|
+
"- If the source language is also a target language, include the corrected version of the sentence for that language as well, if not dont include it.\n" \
|
34
|
+
"- If string to translate is html, you should return the translated html.\n" \
|
35
|
+
"- If string to translate contains 3 underscores in row ___, keep them, don't translate the them, don't remove them, don't change them for dashes (they correspond to the newline)\n" \
|
36
|
+
"- If string to translate contains 3 dashes in row ---, keep them, don't translate the them, don't remove them, don't change them for underscores (they correspond to the tabulation)\n" \
|
37
|
+
"- Ensure that the output does not include markdown (```json) or any other formatting characters. Adhere to the JSON structure meticulously.\n" \
|
38
|
+
"- Ensure the output strictly follows this JSON format, without any extraneous characters or formatting.\n" \
|
39
|
+
"- Format the translation output as a JSON string adhering to the following structure: {\"datas\":[{\"locale_iso\": \"Translated Text\"}]}, where 'locale_iso' is replaced with the appropriate ISO 639-1 code for each translation."
|
34
40
|
|
35
41
|
|
42
|
+
prompt = "Translate the #{text.size} following #{text.size == 1 ? "sentence" : "sentences"} from the source language (ISO 639-1 code: #{from}) to the target languages specified: #{to_iso.map {|iso, language| "#{language} (ISO 639-1 code: #{iso})" }.join(", ")}. "
|
36
43
|
|
37
44
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
"Format the translation output as a JSON string adhering to the following structure: " \
|
42
|
-
"{\"datas\":[{\"locale_iso\": \"Translated Text\"}]}, where 'locale_iso' is replaced with the appropriate ISO 639-1 code for each translation. " \
|
43
|
-
"Ensure the output strictly follows this JSON format, without any extraneous characters or formatting. " \
|
44
|
-
"Sentences to translate:"
|
45
|
-
|
46
|
-
|
45
|
+
##============================================================##
|
46
|
+
## we replace the \n by ___ to avoid the API to remove them
|
47
|
+
##============================================================##
|
47
48
|
text.each_with_index do |sentence, index|
|
48
|
-
prompt += "\n#{index + 1}: #{sentence}"
|
49
|
+
prompt += "\n#{index + 1}: #{sentence.gsub("\n", "___").gsub("\t", "---")}"
|
49
50
|
end
|
50
51
|
|
51
52
|
|
@@ -90,7 +91,9 @@ module ImmosquareTranslate
|
|
90
91
|
content = JSON.parse(choice["message"]["content"])
|
91
92
|
datas = content["datas"]
|
92
93
|
datas.map do |hash|
|
93
|
-
hash
|
94
|
+
hash
|
95
|
+
.select {|key, _| to.include?(key) }
|
96
|
+
.transform_values {|value| value.gsub("___", "\n").gsub("---", "\t") }
|
94
97
|
end.reject(&:empty?)
|
95
98
|
rescue StandardError => e
|
96
99
|
puts(e.message)
|