Ver código fonte

解析结果匹配文字库并保存

zhangqian 2 anos atrás
pai
commit
0357939d4a
3 arquivos alterados com 35 adições e 8 exclusões
  1. 11 5
      models/audio.go
  2. 1 1
      models/text.go
  3. 23 2
      service/process.go

+ 11 - 5
models/audio.go

@@ -5,6 +5,7 @@ import (
 	"gorm.io/gorm"
 	"speechAnalysis/constvar"
 	"speechAnalysis/pkg/mysqlx"
+	"strings"
 	"time"
 )
 
@@ -14,7 +15,7 @@ type (
 		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 @@ type (
 		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 @@ func (slf *Audio) AfterFind(tx *gorm.DB) (err error) {
 	} 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()}
 }

+ 1 - 1
models/text.go

@@ -10,7 +10,7 @@ type (
 	// 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"` // 机车号
 	}
 

+ 23 - 2
service/process.go

@@ -13,6 +13,7 @@ import (
 	"speechAnalysis/constvar"
 	"speechAnalysis/models"
 	"speechAnalysis/pkg/logx"
+	"strings"
 )
 
 // Response 结构体用于存储响应体的内容
@@ -110,15 +111,18 @@ func Process(audioId uint) (err error) {
 			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 @@ func Process(audioId uint) (err error) {
 		})
 		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
+}