-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.添加对仅换行格式的txt读取功能 2.优化翻译效果 3.修复JSON格式混乱的BUG
- Loading branch information
Showing
4 changed files
with
275 additions
and
107 deletions.
There are no files selected for viewing
Binary file not shown.
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,136 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/sashabaranov/go-openai" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
) | ||
|
||
type InputMessage struct { | ||
Name string `json:"name"` | ||
Message string `json:"message"` | ||
} | ||
|
||
func TsJson(client *openai.Client, filename string, inputdir string, outputdir string, translatehead string) { | ||
//注册翻译体 | ||
messages := make([]openai.ChatCompletionMessage, 0) | ||
//读取文件 | ||
fmt.Println("[INFO]开始翻译,如果输出格式不是name|message请用ctrl+c停止程序并重新运行") | ||
// 读取 input.json 文件 | ||
file, err := os.Open(inputdir + filename) | ||
if err != nil { | ||
fmt.Print("[ERROR]") | ||
fmt.Print(err) | ||
fmt.Scanln() | ||
return | ||
} | ||
defer file.Close() | ||
jsonData, err := ioutil.ReadAll(file) | ||
if err != nil { | ||
fmt.Print("[ERROR]") | ||
fmt.Print(err) | ||
fmt.Scanln() | ||
return | ||
} | ||
var inputRecords []InputMessage | ||
err = json.Unmarshal([]byte(jsonData), &inputRecords) | ||
if err != nil { | ||
fmt.Print("[ERROR]") | ||
fmt.Print(err) | ||
fmt.Scanln() | ||
return | ||
} | ||
var result string | ||
var count int | ||
for _, inputRecord := range inputRecords { | ||
outputString := "" | ||
if inputRecord.Name != "" { | ||
outputString += inputRecord.Name + "|" | ||
} | ||
outputString += inputRecord.Message | ||
//翻译 | ||
messages = append(messages, openai.ChatCompletionMessage{ | ||
Role: openai.ChatMessageRoleSystem, | ||
Content: translatehead, | ||
}) | ||
fmt.Println("[INFO]原文: " + outputString) | ||
tsstart: | ||
messages = append(messages, openai.ChatCompletionMessage{ | ||
Role: openai.ChatMessageRoleUser, | ||
Content: outputString, | ||
}) | ||
|
||
resp, err := client.CreateChatCompletion( | ||
context.Background(), | ||
openai.ChatCompletionRequest{ | ||
Model: openai.GPT3Dot5Turbo, | ||
Messages: messages, | ||
}, | ||
) | ||
|
||
if err != nil { | ||
fmt.Printf("[ERROR]翻译出现错误,尝试重新翻译该句,如果持续出错请重启程序或检查代理设置: %v\n", err) | ||
goto tsstart | ||
} | ||
|
||
content := resp.Choices[0].Message.Content | ||
messages = append(messages, openai.ChatCompletionMessage{ | ||
Role: openai.ChatMessageRoleAssistant, | ||
Content: content, | ||
}) | ||
//处理 | ||
content = strings.Replace(content, ":", "|", -1) | ||
content = strings.Replace(content, ":", "|", -1) | ||
fmt.Println("[INFO]译文: " + content) | ||
//输出 | ||
if result != "" { | ||
result = result + content + "\n" | ||
} | ||
//清记录,补预设 | ||
count++ | ||
if count >= 20 { | ||
messages = messages[3:] | ||
} | ||
if count%15 == 0 { | ||
messages = append(messages, openai.ChatCompletionMessage{ | ||
Role: openai.ChatMessageRoleSystem, | ||
Content: translatehead, | ||
}) | ||
messages = messages[1:] | ||
} | ||
} | ||
fmt.Println("[INFO]翻译完毕,正在创建新文件") | ||
// 构建JSON数组 | ||
// 解析结果并输出到json | ||
var outputMessages []InputMessage | ||
for _, entry := range strings.Split(result, "\n") { | ||
parts := strings.SplitN(entry, "|", 2) | ||
if len(parts) > 1 { | ||
outputMessages = append(outputMessages, InputMessage{Name: parts[0], Message: parts[1]}) | ||
} else { | ||
outputMessages = append(outputMessages, InputMessage{Message: entry}) | ||
} | ||
} | ||
|
||
outputBytes, err := json.Marshal(outputMessages) | ||
if err != nil { | ||
fmt.Print("[ERROR]") | ||
fmt.Print(err) | ||
fmt.Scanln() | ||
return | ||
} | ||
|
||
err = ioutil.WriteFile(outputdir+filename, outputBytes, 0644) | ||
if err != nil { | ||
fmt.Print("[ERROR]") | ||
fmt.Print(err) | ||
fmt.Scanln() | ||
return | ||
} | ||
|
||
fmt.Println("[INFO]已写入" + filename) | ||
} |
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,87 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"fmt" | ||
"github.com/sashabaranov/go-openai" | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
func TsTxt(client *openai.Client, filename string, inputdir string, outputdir string, translatehead string) { | ||
messages := make([]openai.ChatCompletionMessage, 0) | ||
//读取文件 | ||
fmt.Println("[INFO]开始翻译,如果输出格式不是name|message请用ctrl+c停止程序并重新运行") | ||
// 读取 filename 文件 | ||
file, err := os.Open(inputdir + filename) | ||
if err != nil { | ||
fmt.Print("[ERROR]") | ||
fmt.Print(err) | ||
fmt.Scanln() | ||
return | ||
} | ||
defer file.Close() | ||
scanner := bufio.NewScanner(file) | ||
var count int | ||
var result string | ||
for scanner.Scan() { | ||
//翻译 | ||
//加预设 | ||
messages = append(messages, openai.ChatCompletionMessage{ | ||
Role: openai.ChatMessageRoleSystem, | ||
Content: translatehead, | ||
}) | ||
fmt.Println("[INFO]原文: " + scanner.Text()) | ||
tsstart: | ||
messages = append(messages, openai.ChatCompletionMessage{ | ||
Role: openai.ChatMessageRoleUser, | ||
Content: scanner.Text(), | ||
}) | ||
|
||
resp, err := client.CreateChatCompletion( | ||
context.Background(), | ||
openai.ChatCompletionRequest{ | ||
Model: openai.GPT3Dot5Turbo, | ||
Messages: messages, | ||
}, | ||
) | ||
|
||
if err != nil { | ||
fmt.Printf("[ERROR]翻译出现错误,尝试重新翻译该句,如果持续出错请重启持续或检查代理设置: %v\n", err) | ||
goto tsstart | ||
} | ||
|
||
content := resp.Choices[0].Message.Content | ||
messages = append(messages, openai.ChatCompletionMessage{ | ||
Role: openai.ChatMessageRoleAssistant, | ||
Content: content, | ||
}) | ||
//输出 | ||
fmt.Println("[INFO]译文: " + content) | ||
result = result + content + "\n" | ||
//清记录,补预设 | ||
count++ | ||
if count >= 20 { | ||
messages = messages[3:] | ||
} | ||
if count%15 == 0 { | ||
messages = append(messages, openai.ChatCompletionMessage{ | ||
Role: openai.ChatMessageRoleSystem, | ||
Content: translatehead, | ||
}) | ||
messages = messages[1:] | ||
} | ||
} | ||
fmt.Println("[INFO]翻译完毕,正在创建新文件") | ||
// 输出到 Translate.txt | ||
err = ioutil.WriteFile(outputdir+filename, []byte(result), 0644) | ||
if err != nil { | ||
fmt.Print("[ERROR]") | ||
fmt.Print(err) | ||
fmt.Scanln() | ||
return | ||
} | ||
|
||
fmt.Println("[INFO]已写入" + filename) | ||
} |
Oops, something went wrong.