Skip to content

Commit

Permalink
添加仅标点符号,翻译出错的校验
Browse files Browse the repository at this point in the history
  • Loading branch information
Lilyltt committed Apr 21, 2023
1 parent c37411a commit 94d05dc
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 11 deletions.
42 changes: 36 additions & 6 deletions TsJson.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,21 @@ func TsJson(client *openai.Client, filename string, inputdir string, outputdir s
}

var result string
var checkcount int
var tempres string
for _, inputRecord := range inputRecords {
outputString := ""
if inputRecord.Name != "" {
outputString += inputRecord.Name + ":"
}
outputString += inputRecord.Message
//翻译
tsstart:
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,
Expand All @@ -80,13 +82,41 @@ func TsJson(client *openai.Client, filename string, inputdir string, outputdir s
}

content := resp.Choices[0].Message.Content
//仅标点符号校验检查
if OnlyMarkCheck(inputRecord.Message) {
if inputRecord.Name != "" {
tempres += inputRecord.Name + ":"
}
tempres += inputRecord.Message
fmt.Println("[WARN]原文仅有标点符号,跳过该句检查并保持原样输出")
}
//翻译校验检查
if checkcount != 0 {
if GptErrCheck(content) == false {
checkcount = 0
}
}
if GptErrCheck(content) {
checkcount++
if checkcount <= 5 {
fmt.Println("[INFO]译文: " + content)
messages = messages[:len(messages)-3]
fmt.Printf("[ERROR]翻译校验检查出问题,正在进行第 %d 次重试修正:\n", checkcount)
time.Sleep(5 * time.Second)
goto tsstart
} else {
fmt.Printf("[ERROR]翻译校验检查出问题,已进行 %d 次修正依然出错,将跳过该句:\n", checkcount)
checkcount = 0
}
}
//处理
content = strings.Replace(content, ":", ":", -1)
content = strings.Replace(content, "\n", "", -1)
fmt.Println("[INFO]译文: " + content)
tempres = content
tempres = strings.Replace(tempres, ":", ":", -1)
tempres = strings.Replace(tempres, "\n", "", -1)
fmt.Println("[INFO]译文: " + tempres)
//输出
if content != "" {
result = result + content + "\n"
if tempres != "" {
result = result + tempres + "\n"
}
//清记录,补预设
if len(messages) >= 60 {
Expand Down
35 changes: 31 additions & 4 deletions TsTxt.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ func TsTxt(client *openai.Client, filename string, inputdir string, outputdir st
defer file.Close()
scanner := bufio.NewScanner(file)
var result string
var checkcount int
var tempres string
for scanner.Scan() {
//翻译
//加预设
fmt.Println("[INFO]原文: " + scanner.Text())
tsstart:
fmt.Println("[INFO]原文: " + scanner.Text())
messages = append(messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleSystem,
Content: translatehead,
Expand All @@ -55,10 +57,35 @@ func TsTxt(client *openai.Client, filename string, inputdir string, outputdir st
}

content := resp.Choices[0].Message.Content
content = strings.Replace(content, "\n", "", -1)
//仅标点符号校验检查
if OnlyMarkCheck(scanner.Text()) {
tempres = scanner.Text()
fmt.Println("[WARN]原文仅有标点符号,跳过该句检查并保持原样输出")
}
//翻译校验检查
if checkcount != 0 {
if GptErrCheck(content) == false {
checkcount = 0
}
}
if GptErrCheck(content) {
checkcount++
if checkcount <= 5 {
fmt.Println("[INFO]译文: " + content)
messages = messages[:len(messages)-3]
fmt.Printf("[ERROR]翻译校验检查出问题,正在进行第 %d 次重试修正:\n", checkcount)
time.Sleep(5 * time.Second)
goto tsstart
} else {
fmt.Printf("[ERROR]翻译校验检查出问题,已进行 %d 次修正依然出错,将跳过该句:\n", checkcount)
checkcount = 0
}
}
tempres = content
tempres = strings.Replace(tempres, "\n", "", -1)
//输出
fmt.Println("[INFO]译文: " + content)
result = result + content + "\n"
fmt.Println("[INFO]译文: " + tempres)
result = result + tempres + "\n"
//清记录,补预设
if len(messages) >= 60 {
messages = messages[3:]
Expand Down
Binary file modified go_build_GalUpTs.exe
Binary file not shown.
26 changes: 25 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"net/url"
"os"
"path/filepath"
"strings"
"unicode"
)

func main() {
Expand Down Expand Up @@ -36,7 +38,7 @@ func main() {
}
//定义翻译头
var translatehead string
translatehead = "Translate user's words to simplified Chinese.The user will send you the dialogues between characters sentence by sentence. Please only reply user the Chinese translation of current sentence. Do not reply user any other explain or add anything before translation.Do not say no or can't. Just translate, never mind the absence of context."
translatehead = "Translate user's words to simplified Chinese.The user will send you the text from a virtual game sentence by sentence. Please only reply user the Chinese translation of current sentence. Do not reply user any other explain or add anything before translation."
fmt.Println("[INFO]翻译头已设定,暂不支持修改")
//获取输入输出目录
var inputdir string //输入目录
Expand Down Expand Up @@ -89,6 +91,7 @@ func main() {
fmt.Println("[INFO]程序执行完成,请检查你的输出目录" + outputdir + "!")
fmt.Scanln()
}

func SetProxy(apikey string, proxy string) *openai.Client {
config := openai.DefaultConfig(apikey)
proxyUrl, err := url.Parse(proxy)
Expand All @@ -106,4 +109,25 @@ func SetProxy(apikey string, proxy string) *openai.Client {
}
client := openai.NewClientWithConfig(config)
return client
} //代理设置函数

func GptErrCheck(target string) bool {
for _, r := range target {
if unicode.In(r, unicode.Hiragana, unicode.Katakana) {
return true
}
}
if strings.Contains(target, "sorry") || strings.Contains(target, "Sorry") || strings.Contains(target, "Translate") || strings.Contains(target, "Translation") || strings.Contains(target, "translate") || strings.Contains(target, "translation") || strings.Contains(target, "上下文") || strings.Contains(target, "翻译") {
return true
}
return false
} //校验检查函数

func OnlyMarkCheck(target string) bool {
for _, c := range target {
if !unicode.IsPunct(c) && !unicode.IsSpace(c) {
return false
}
}
return true
}

0 comments on commit 94d05dc

Please sign in to comment.