zhangqian
2024-05-16 0357939d4ab2a10ae3678e2285d012c713c26963
解析结果匹配文字库并保存
3个文件已修改
43 ■■■■ 已修改文件
models/audio.go 16 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/text.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/process.go 25 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/audio.go
@@ -5,6 +5,7 @@
    "gorm.io/gorm"
    "speechAnalysis/constvar"
    "speechAnalysis/pkg/mysqlx"
    "strings"
    "time"
)
@@ -14,7 +15,7 @@
        gorm.Model
        Name             string               `gorm:"index;type:varchar(255);not null;default:'';comment:音频名称" json:"name"`            // 音频名称
        Size             int64                `gorm:"type:int;not null;default:0;comment:文件大小" json:"size"`                            // 音频大小
        FilePath         string               `gorm:"type:varchar(255);not null;default:'';comment:音频名称" json:"-"`                     //音频路径                                               // 音频路径
        FilePath         string               `gorm:"type:varchar(255);not null;default:'';comment:音频路径" json:"-"`                     //音频路径                                               // 音频路径
        AudioStatus      constvar.AudioStatus `gorm:"type:tinyint;not null;default:0;comment:状态" json:"audioStatus"`                   // 音频状态
        LocomotiveNumber string               `gorm:"index;type:varchar(255);not null;default:'';comment:机车号" json:"locomotiveNumber"` // 机车号
        TrainNumber      string               `gorm:"index;type:varchar(255);not null;default:'';comment:车次" json:"trainNumber"`       // 车次
@@ -22,10 +23,11 @@
        Station          string               `gorm:"index;type:varchar(255);not null;default:'';comment:车站号" json:"station"`          // 车站
        OccurrenceAt     time.Time            `json:"-"`
        OccurrenceTime   string               `json:"occurrenceTime" gorm:"-"`
        IsFollowed       constvar.BoolType    `gorm:"type:tinyint;not null;default:2;comment:是否关注"` //是否关注 1关注 2未关注
        Score            float64              `json:"score"`                                        // 置信度
        Words            []string             `json:"words" gorm:"-"`                               //匹配到的文字数组
        AudioText        string               `json:"audioText" gorm:"-"`                           //解析出的文本
        IsFollowed       constvar.BoolType    `gorm:"type:tinyint;not null;default:2;comment:是否关注"`                        //是否关注 1关注 2未关注
        Score            float64              `json:"score"`                                                               // 置信度
        Words            []string             `json:"words" gorm:"-"`                                                      //匹配到的文字数组
        Tags             string               `json:"-" gorm:"type:varchar(255);not null;default:'';comment:匹配到的文字,用逗号拼接"` //匹配到的文字
        AudioText        string               `json:"audioText" gorm:"-"`                                                  //解析出的文本
    }
    AudioSearch struct {
@@ -49,8 +51,12 @@
    } else {
        slf.OccurrenceTime = slf.OccurrenceAt.Format("2006-01-02 15:04:05")
    }
    if slf.Tags != "" {
        slf.Words = strings.Split(slf.Tags, ",")
    }
    return
}
func NewAudioSearch() *AudioSearch {
    return &AudioSearch{Orm: mysqlx.GetDB()}
}
models/text.go
@@ -10,7 +10,7 @@
    // Word 文字
    Word struct {
        gorm.Model
        Content          string `gorm:"uniqueIndex:locomotive_number_Word;type:varchar(255);not null;default:'';comment:音频名称" json:"content"`         // 文字
        Content          string `gorm:"uniqueIndex:locomotive_number_Word;type:varchar(255);not null;default:'';comment:文字" json:"content"`           // 文字
        LocomotiveNumber string `gorm:"uniqueIndex:locomotive_number_Word;type:varchar(255);not null;default:'';comment:机车号" json:"locomotiveNumber"` // 机车号
    }
service/process.go
@@ -13,6 +13,7 @@
    "speechAnalysis/constvar"
    "speechAnalysis/models"
    "speechAnalysis/pkg/logx"
    "strings"
)
// Response 结构体用于存储响应体的内容
@@ -110,15 +111,18 @@
            return
        }
        logx.Infof("AnalysisAudio result: %v", resp)
        words := GetWordFromText(resp.Result, audio)
        err = models.WithTransaction(func(db *gorm.DB) error {
            err = models.NewAudioSearch().SetID(audioId).UpdateByMap(map[string]interface{}{
            err = models.NewAudioSearch().SetOrm(db).SetID(audioId).UpdateByMap(map[string]interface{}{
                "audio_status": constvar.AudioStatusFinish,
                "score":        resp.Score,
                "tags":         strings.Join(words, ","),
            })
            if err != nil {
                return err
            }
            err = models.NewAudioTextSearch().Save(&models.AudioText{
            err = models.NewAudioTextSearch().SetOrm(db).Save(&models.AudioText{
                AudioID:   audio.ID,
                AudioText: resp.Result,
            })
@@ -126,9 +130,26 @@
        })
        if err != nil {
            logx.Infof("AnalysisAudio success but update record failed: %v", err)
            _ = models.NewAudioSearch().SetID(audioId).UpdateByMap(map[string]interface{}{"audio_status": constvar.AudioStatusFailed})
            return
        }
    }()
    return nil
}
func GetWordFromText(text string, audio *models.Audio) (words []string) {
    if audio == nil {
        return nil
    }
    wordRecords, err := models.NewWordSearch().SetLocomotiveNumber(audio.LocomotiveNumber).FindNotTotal()
    if err != nil || len(wordRecords) == 0 {
        return nil
    }
    for _, v := range wordRecords {
        if strings.Contains(text, v.Content) {
            words = append(words, v.Content)
        }
    }
    return words
}