From 0357939d4ab2a10ae3678e2285d012c713c26963 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期四, 16 五月 2024 16:08:18 +0800
Subject: [PATCH] 解析结果匹配文字库并保存

---
 service/process.go |   25 +++++++++++++++++++++++--
 models/audio.go    |   16 +++++++++++-----
 models/text.go     |    2 +-
 3 files changed, 35 insertions(+), 8 deletions(-)

diff --git a/models/audio.go b/models/audio.go
index 842f8e9..8d1b51f 100644
--- a/models/audio.go
+++ b/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:"-"`                           //瑙f瀽鍑虹殑鏂囨湰
+		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:"-"`                                                  //瑙f瀽鍑虹殑鏂囨湰
 	}
 
 	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()}
 }
diff --git a/models/text.go b/models/text.go
index 1818ee0..bd9360a 100644
--- a/models/text.go
+++ b/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"` // 鏈鸿溅鍙�
 	}
 
diff --git a/service/process.go b/service/process.go
index 1d1595a..29eb5f3 100644
--- a/service/process.go
+++ b/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
+}

--
Gitblit v1.8.0