Add files via upload

This commit is contained in:
方柳煜 2026-02-01 18:38:06 +08:00 committed by GitHub
parent 307bd0d63a
commit 1f74485f28
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 22 deletions

View File

@ -3,7 +3,7 @@ package handou
import ( import (
"encoding/json" "encoding/json"
"fmt" "errors"
"net/url" "net/url"
"os" "os"
"slices" "slices"
@ -101,7 +101,7 @@ type baiduAPIData struct {
} `json:"data"` } `json:"data"`
} }
func geiAPIdata(s string) (*idiomJson, error) { func geiAPIdata(s string) (*idiomJSON, error) {
url := "https://hanyuapp.baidu.com/dictapp/swan/termdetail?wd=" + url.QueryEscape(s) + "&client=pc&source_tag=2&lesson_from=xiaodu" url := "https://hanyuapp.baidu.com/dictapp/swan/termdetail?wd=" + url.QueryEscape(s) + "&client=pc&source_tag=2&lesson_from=xiaodu"
logrus.Warningln(url) logrus.Warningln(url)
data, err := web.GetData(url) data, err := web.GetData(url)
@ -115,7 +115,7 @@ func geiAPIdata(s string) (*idiomJson, error) {
return nil, err return nil, err
} }
if apiData.Data.Name == "" { if apiData.Data.Name == "" {
return nil, fmt.Errorf("未找到该成语") return nil, errors.New("未找到该成语")
} }
derivation := "" derivation := ""
for _, v := range apiData.Data.ChuChu { for _, v := range apiData.Data.ChuChu {
@ -127,7 +127,7 @@ func geiAPIdata(s string) (*idiomJson, error) {
explanation := apiData.Data.DefinitionInfo.Definition + apiData.Data.DefinitionInfo.ModernDefinition explanation := apiData.Data.DefinitionInfo.Definition + apiData.Data.DefinitionInfo.ModernDefinition
if derivation == "" && explanation == "" { if derivation == "" && explanation == "" {
return nil, fmt.Errorf("无法获取成语词源和解释") return nil, errors.New("无法获取成语词源和解释")
} }
synonyms := make([]string, len(apiData.Data.Synonyms)) synonyms := make([]string, len(apiData.Data.Synonyms))
for i, synonym := range apiData.Data.Synonyms { for i, synonym := range apiData.Data.Synonyms {
@ -154,7 +154,7 @@ func geiAPIdata(s string) (*idiomJson, error) {
pinyinSlice = strings.Split(apiData.Data.Definition[0].Pinyin, " ") pinyinSlice = strings.Split(apiData.Data.Definition[0].Pinyin, " ")
} }
newIdiom := idiomJson{ newIdiom := idiomJSON{
Word: apiData.Data.Name, Word: apiData.Data.Name,
Chars: chars, Chars: chars,
Pinyin: pinyinSlice, Pinyin: pinyinSlice,
@ -170,7 +170,7 @@ func geiAPIdata(s string) (*idiomJson, error) {
var mu sync.Mutex var mu sync.Mutex
func saveIdiomJson() error { func saveIdiomJSON() error {
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
f, err := os.Create(idiomFilePath) f, err := os.Create(idiomFilePath)

View File

@ -28,7 +28,7 @@ import (
"github.com/wdvxdr1123/ZeroBot/message" "github.com/wdvxdr1123/ZeroBot/message"
) )
type idiomJson struct { type idiomJSON struct {
Word string `json:"word"` // 成语 Word string `json:"word"` // 成语
Chars []string `json:"chars"` // 成语 Chars []string `json:"chars"` // 成语
Pinyin []string `json:"pinyin"` // 拼音 Pinyin []string `json:"pinyin"` // 拼音
@ -106,7 +106,7 @@ var (
) )
pinyinFont []byte pinyinFont []byte
idiomInfoMap = make(map[string]idiomJson) idiomInfoMap = make(map[string]idiomJSON)
habitsIdiomKeys = make([]string, 0) habitsIdiomKeys = make([]string, 0)
errHadGuessed = errors.New("had guessed") errHadGuessed = errors.New("had guessed")
@ -244,7 +244,7 @@ func poolIdiom() string {
return keys[rand.Intn(len(keys))] return keys[rand.Intn(len(keys))]
} }
func newHandouGame(target idiomJson) func(string) (bool, []byte, error) { func newHandouGame(target idiomJSON) func(string) (bool, []byte, error) {
var ( var (
class = len(target.Chars) class = len(target.Chars)
words = target.Word words = target.Word
@ -269,7 +269,7 @@ func newHandouGame(target idiomJson) func(string) (bool, []byte, error) {
return func(s string) (win bool, data []byte, err error) { return func(s string) (win bool, data []byte, err error) {
answer := []rune(s) answer := []rune(s)
var answerData idiomJson var answerData idiomJSON
if s != "" { if s != "" {
if words == s { if words == s {
@ -296,7 +296,7 @@ func newHandouGame(target idiomJson) func(string) (bool, []byte, error) {
logrus.Warningln("通过API获取成语信息: ", newIdiom.Word) logrus.Warningln("通过API获取成语信息: ", newIdiom.Word)
if newIdiom.Word != "" { if newIdiom.Word != "" {
idiomInfoMap[newIdiom.Word] = *newIdiom idiomInfoMap[newIdiom.Word] = *newIdiom
go func() { _ = saveIdiomJson() }() go func() { _ = saveIdiomJSON() }()
} }
if newIdiom.Word != s { if newIdiom.Word != s {
err = errUnknownWord err = errUnknownWord
@ -398,7 +398,7 @@ func newHandouGame(target idiomJson) func(string) (bool, []byte, error) {
} }
existPinyin = append(existPinyin, v) existPinyin = append(existPinyin, v)
} }
tickIdiom := idiomJson{ tickIdiom := idiomJSON{
Chars: tickExistChars, Chars: tickExistChars,
Pinyin: tickTruePinyin, Pinyin: tickTruePinyin,
} }
@ -515,7 +515,7 @@ func newHandouGame(target idiomJson) func(string) (bool, []byte, error) {
} }
// drawHanBlock 绘制汉字方块支持多行显示6字以上时分成两行 // drawHanBlock 绘制汉字方块支持多行显示6字以上时分成两行
func drawHanBlock(hanFontSize, pinFontSize float64, idiom, target idiomJson, exitPinyin ...string) image.Image { func drawHanBlock(hanFontSize, pinFontSize float64, idiom, target idiomJSON, exitPinyin ...string) image.Image {
class := len(target.Chars) class := len(target.Chars)
// 确保切片长度一致 // 确保切片长度一致
@ -575,21 +575,21 @@ func drawHanBlock(hanFontSize, pinFontSize float64, idiom, target idiomJson, exi
} }
// 计算当前字符在哪一行哪一列 // 计算当前字符在哪一行哪一列
idiom_rows := 0 idiomRows := 0
col := i col := i
if rows > 1 { if rows > 1 {
idiom_rows = i / charsPerRow idiomRows = i / charsPerRow
col = i % charsPerRow col = i % charsPerRow
} }
x := float64(space + col*blockPinWidth) x := float64(space + col*blockPinWidth)
// 如果上一层字数是奇数就额外移位 // 如果上一层字数是奇数就额外移位
if idiom_rows%2 == 1 { if idiomRows%2 == 1 {
x += float64(blockPinWidth) / 2 x += float64(blockPinWidth) / 2
} }
y := float64(idiom_rows*(int(pinHeight+hanHeight+boxPadding*2)+space*2) + space) y := float64(idiomRows*(int(pinHeight+hanHeight+boxPadding*2)+space*2) + space)
if len(exitPinyin) > 0 { if len(exitPinyin) > 0 {
y = float64(idiom_rows*(int(pinHeight+hanHeight+boxPadding*2+pinHeight)+space*2) + space) y = float64(idiomRows*(int(pinHeight+hanHeight+boxPadding*2+pinHeight)+space*2) + space)
} }
// 绘制拼音 // 绘制拼音
@ -654,7 +654,7 @@ func drawHanBlock(hanFontSize, pinFontSize float64, idiom, target idiomJson, exi
return ctx.Image() return ctx.Image()
} }
func anserOutString(s idiomJson) string { func anserOutString(s idiomJSON) string {
msg := s.Word msg := s.Word
if s.Baobian != "" && s.Baobian != "-" { if s.Baobian != "" && s.Baobian != "-" {
msg += "\n" + s.Baobian + "词" msg += "\n" + s.Baobian + "词"

View File

@ -3,6 +3,7 @@ package handou
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"math" "math"
"math/rand" "math/rand"
@ -15,6 +16,7 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
// UserHabits 用户习惯
type UserHabits struct { type UserHabits struct {
mu sync.RWMutex mu sync.RWMutex
habits map[string]int // 单字频率 habits map[string]int // 单字频率
@ -38,7 +40,7 @@ func initUserHabits() error {
if file.IsNotExist(userHabitsFile) { if file.IsNotExist(userHabitsFile) {
f, err := os.Create(userHabitsFile) f, err := os.Create(userHabitsFile)
if err != nil { if err != nil {
return fmt.Errorf("创建用户习惯库时发生错误: %v", err) return errors.New("创建用户习惯库时发生错误: " + err.Error())
} }
_ = f.Close() _ = f.Close()
return saveHabits() return saveHabits()
@ -47,7 +49,7 @@ func initUserHabits() error {
// 读取现有习惯数据 // 读取现有习惯数据
habitsFile, err := os.ReadFile(userHabitsFile) habitsFile, err := os.ReadFile(userHabitsFile)
if err != nil { if err != nil {
return fmt.Errorf("读取用户习惯库时发生错误: %v", err) return errors.New("读取用户习惯库时发生错误: " + err.Error())
} }
var savedData struct { var savedData struct {
@ -69,7 +71,7 @@ func initUserHabits() error {
savedData.TotalWords += count savedData.TotalWords += count
} }
} else { } else {
return fmt.Errorf("解析用户习惯库时发生错误: %v", err) return errors.New("解析用户习惯库时发生错误: " + err.Error())
} }
} }