Skip to content

Commit

Permalink
功能添加&体验优化
Browse files Browse the repository at this point in the history
1.添加对仅换行格式的txt读取功能
2.优化翻译效果
3.修复JSON格式混乱的BUG
  • Loading branch information
Lilyltt committed Apr 12, 2023
1 parent 05046c7 commit e281150
Show file tree
Hide file tree
Showing 4 changed files with 275 additions and 107 deletions.
Binary file removed GalUpTs.exe
Binary file not shown.
136 changes: 136 additions & 0 deletions TsJson.go
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)
}
87 changes: 87 additions & 0 deletions TsTxt.go
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)
}
Loading

0 comments on commit e281150

Please sign in to comment.