immosquare-translate 0.1.6 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/immosquare-translate/translator.rb +20 -15
- data/lib/immosquare-translate/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1faeaa90371b731d6a14a8cfac0fa67f335278dd94c0a18b539cfcf622ca9c01
|
4
|
+
data.tar.gz: 530d4bcd43664a2adb51f909f913901da2350d68dae366dbbdb0bf423870b7f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c90ef3262e2ae2d1ef258fc98c1de0e8870cda69f78cf818c17534e7aa580a35f18522d5f18961581ec419092bc3846118b1ddd703f50a95eb1ebb6af7e36107
|
7
|
+
data.tar.gz: 2f112af6ddde271c8ea31abbbb9b148a82346871a5c9fe54c51522dd4e555f9e7723f931fa15dff73851520fdeec8f277a0f2387983deef3389db6dc2d860f69
|
@@ -26,26 +26,29 @@ 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
|
+
"- Respond with an array of a flat objects in JSON (minified, without any extraneous characters or formatting)\n" \
|
33
|
+
"- Format the translation output as a JSON string adhering to the following structure: {\"datas\":[{\"locale_iso\": \"Translated Text\"}]}" \
|
34
|
+
"- Ensure that the output does not include markdown (```json) or any other formatting characters. Adhere to the JSON structure meticulously.\n" \
|
35
|
+
"- Correct any spelling or grammatical errors in the source text before translating.\n" \
|
36
|
+
"- 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" \
|
37
|
+
"- If string to translate is html, you should return the translated html.\n" \
|
38
|
+
"- If string to translate contains underscores in row, keep them, don't remove them\n" \
|
34
39
|
|
35
40
|
|
36
41
|
|
37
|
-
|
38
|
-
prompt = "Translate the #{text.size} #{text.size == 1 ? "sentence" : "sentences"} provided below 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(", ")}. " \
|
39
|
-
"Before translating, correct any spelling or grammatical errors in each sentence. " \
|
40
|
-
"If the source language is also a target language, include the corrected version of the sentence for that language as well. " \
|
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:"
|
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(", ")}. "
|
45
43
|
|
46
44
|
|
45
|
+
##============================================================##
|
46
|
+
## we replace the \n \t by ___ to avoid JSON parsing errors
|
47
|
+
## We use the same symbol to replace the \n and \t because
|
48
|
+
## if we use different symbols sometimes the API inverse them.
|
49
|
+
##============================================================##
|
47
50
|
text.each_with_index do |sentence, index|
|
48
|
-
prompt += "\n#{index + 1}: #{sentence}"
|
51
|
+
prompt += "\n#{index + 1}: #{sentence.gsub("\n", "___").gsub("\t", "____")}"
|
49
52
|
end
|
50
53
|
|
51
54
|
|
@@ -90,7 +93,9 @@ module ImmosquareTranslate
|
|
90
93
|
content = JSON.parse(choice["message"]["content"])
|
91
94
|
datas = content["datas"]
|
92
95
|
datas.map do |hash|
|
93
|
-
hash
|
96
|
+
hash
|
97
|
+
.select {|key, _| to.include?(key) }
|
98
|
+
.transform_values {|value| value.gsub("____", "\t").gsub("___", "\n") }
|
94
99
|
end.reject(&:empty?)
|
95
100
|
rescue StandardError => e
|
96
101
|
puts(e.message)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: immosquare-translate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- IMMO SQUARE
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-04-
|
11
|
+
date: 2024-04-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|