Apply .gitignore rules
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
使用编辑距离Edit Distance的方式来计算两个字符串的相似类。
|
||||
参考资料:https://en.wikipedia.org/wiki/Edit_distance
|
||||
*/
|
||||
package stringUtil
|
||||
|
||||
// 计算两个字符串的相似度
|
||||
// word1: 第一个字符串
|
||||
// word2: 第二个字符串
|
||||
// 返回值:
|
||||
// 两个字符串的距离,表示两个字符串经过多少次变换,可以变成同一个字符串
|
||||
// 两个字符串的相似度,范围为[0, 1]
|
||||
func Similarity(word1, word2 string) (distance int, similarity float64) {
|
||||
// 内部方法,用于计算最小值、最大值
|
||||
min := func(nums ...int) int {
|
||||
_min := nums[0]
|
||||
for _, v := range nums {
|
||||
if v < _min {
|
||||
_min = v
|
||||
}
|
||||
}
|
||||
return _min
|
||||
}
|
||||
max := func(nums ...int) int {
|
||||
_max := nums[0]
|
||||
for _, v := range nums {
|
||||
if v > _max {
|
||||
_max = v
|
||||
}
|
||||
}
|
||||
return _max
|
||||
}
|
||||
|
||||
// 如果有单词为空,或者单词相同,则直接计算出结果,无需进一步计算
|
||||
m, n := len(word1), len(word2)
|
||||
if m == 0 {
|
||||
distance = n
|
||||
return
|
||||
}
|
||||
if n == 0 {
|
||||
distance = m
|
||||
return
|
||||
}
|
||||
if m == n && word1 == word2 {
|
||||
distance = 0
|
||||
similarity = 1
|
||||
return
|
||||
}
|
||||
|
||||
// 使用动态规划计算编辑距离(Edit Distance)
|
||||
// Step 1: define the data structure
|
||||
dp := make([][]int, m+1, m+1)
|
||||
for i := 0; i <= m; i++ {
|
||||
dp[i] = make([]int, n+1, n+1)
|
||||
}
|
||||
|
||||
// Step 2: init the data
|
||||
for i := 0; i <= m; i++ {
|
||||
for j := 0; j <= n; j++ {
|
||||
if i == 0 {
|
||||
dp[i][j] = j
|
||||
}
|
||||
if j == 0 {
|
||||
dp[i][j] = i
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Step 3: state transition equation
|
||||
for i := 1; i <= m; i++ {
|
||||
for j := 1; j <= n; j++ {
|
||||
if word1[i-1] == word2[j-1] {
|
||||
dp[i][j] = dp[i-1][j-1]
|
||||
} else {
|
||||
dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 得到距离并计算相似度
|
||||
distance = dp[m][n]
|
||||
maxLength := max(m, n)
|
||||
similarity = float64(maxLength-distance) / float64(maxLength)
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"common/connection"
|
||||
)
|
||||
|
||||
func init() {
|
||||
//注册数据库
|
||||
connection.RegisterDBModel(&Game{})
|
||||
}
|
||||
|
||||
type Game struct {
|
||||
GameID int64 `gorm:"column:game_id;primary_key;comment:用户id;autoIncrementIncrement" json:"gameid"`
|
||||
|
||||
//账号
|
||||
Account string `gorm:"column:account;comment:账号" json:"account"`
|
||||
}
|
||||
|
||||
func (Game) TableName() string {
|
||||
return "user"
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
/*获取对应sdk
|
||||
go get -u github.com/tencentcloud/tencentcloud-sdk-go
|
||||
*/
|
||||
package voicemsgMgr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
|
||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
|
||||
vms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vms/v20200902" // 引入 vms
|
||||
"goutil/logUtil"
|
||||
)
|
||||
|
||||
const (
|
||||
//请求方法
|
||||
REG_METHOD = "POST"
|
||||
//域名
|
||||
END_POINT = "vms.tencentcloudapi.com"
|
||||
//签名方式
|
||||
SIGN_METHOD = "TC3-HMAC-SHA256"
|
||||
)
|
||||
|
||||
//语音电话结构体
|
||||
type VoiceMessage struct {
|
||||
//语音发送客户端
|
||||
client *vms.Client
|
||||
|
||||
//配置
|
||||
voiceMessageConfig *VoiceMessageConfig
|
||||
}
|
||||
|
||||
//拨打语音电话
|
||||
//telephone:电话号码(格式:+[国家或地区码][用户号码])
|
||||
//playTimes:播放次数,可选,最多3次,默认2次
|
||||
//para:参数,根据配置的模板传入参数格式,按顺序传入
|
||||
func (this *VoiceMessage) SendVoice(telephone string, playTimes uint64, param ...string) (err error) {
|
||||
//初始化请求参数
|
||||
request := vms.NewSendTtsVoiceRequest()
|
||||
|
||||
// 模板 ID,必须填写在控制台审核通过的模板 ID,可登录 [语音消息控制台] 查看模板 ID
|
||||
request.TemplateId = common.StringPtr(this.voiceMessageConfig.TemplateId)
|
||||
|
||||
//模板构造参数,模板参数,若模板没有参数,则为空数组
|
||||
paramLit := []string{}
|
||||
if len(param) > 0 {
|
||||
for _, val := range param {
|
||||
paramLit = append(paramLit, val)
|
||||
}
|
||||
}
|
||||
request.TemplateParamSet = common.StringPtrs(paramLit)
|
||||
|
||||
/* 被叫手机号码,采用 e.164 标准,格式为+[国家或地区码][用户号码]
|
||||
* 例如:+8613711112222,其中前面有一个+号,86为国家码,13711112222为手机号*/
|
||||
request.CalledNumber = common.StringPtr(telephone)
|
||||
|
||||
//在语音控制台添加应用后生成的实际SdkAppid,示例如1400006666
|
||||
request.VoiceSdkAppid = common.StringPtr(this.voiceMessageConfig.VoiceSdkAppid)
|
||||
|
||||
//播放次数,可选,最多3次,默认2次
|
||||
request.PlayTimes = common.Uint64Ptr(playTimes)
|
||||
|
||||
//用户的 session 内容,腾讯 server 回包中会原样返回
|
||||
request.SessionContext = common.StringPtr("test")
|
||||
|
||||
//通过 client 对象调用想要访问的接口,需要传入请求对象
|
||||
_, err = this.client.SendTtsVoice(request)
|
||||
// 处理异常
|
||||
if _, ok := err.(*errors.TencentCloudSDKError); ok {
|
||||
logUtil.ErrorLog(fmt.Sprintf("SDK异常,语音电话拨打失败,错误信息为:%s", err))
|
||||
return
|
||||
}
|
||||
//非SDK异常,直接失败。实际代码中可以加入其他的处理
|
||||
if err != nil {
|
||||
logUtil.ErrorLog(fmt.Sprintf("非SDK异常,语音电话拨打失败,错误信息为:%s", err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
//创建新的对象
|
||||
//secretId:API密钥Id
|
||||
//secretKey:API密钥key
|
||||
//region:地域
|
||||
//templateId:模板ID
|
||||
//appid:应用后生成的实际SdkAppid
|
||||
func NewVoiceMessage(secretId, secretKey, region, templateId, appid string) *VoiceMessage {
|
||||
//构造配置模型
|
||||
voiceMessageConfigObj := &VoiceMessageConfig{
|
||||
SecretId: secretId,
|
||||
SecretKey: secretKey,
|
||||
Region: region,
|
||||
TemplateId: templateId,
|
||||
VoiceSdkAppid: appid,
|
||||
}
|
||||
|
||||
//通过配置实例化
|
||||
return NewVoiceMessageBuConfig(voiceMessageConfigObj)
|
||||
}
|
||||
|
||||
//通过配置实例化
|
||||
func NewVoiceMessageBuConfig(voiceMessageConfigObj *VoiceMessageConfig) *VoiceMessage {
|
||||
//实例化一个认证对象,入参需要传入腾讯云账户密钥对 secretId 和 secretKey
|
||||
credential := common.NewCredential(
|
||||
voiceMessageConfigObj.SecretId,
|
||||
voiceMessageConfigObj.SecretKey,
|
||||
)
|
||||
|
||||
/* 非必要步骤:
|
||||
* 实例化一个客户端配置对象,可以指定超时时间等配置 */
|
||||
cpf := profile.NewClientProfile()
|
||||
/* SDK 默认使用 POST 方法
|
||||
* 如需使用 GET 方法,可以在此处设置,但 GET 方法无法处理较大的请求 */
|
||||
cpf.HttpProfile.ReqMethod = REG_METHOD
|
||||
/* SDK 有默认的超时时间,非必要请不要进行调整
|
||||
* 如有需要请在代码中查阅以获取最新的默认值 */
|
||||
//cpf.HttpProfile.ReqTimeout = 5
|
||||
/* SDK 会自动指定域名,通常无需指定域名,但访问金融区的服务时必须手动指定域名
|
||||
* 例如 VMS 的上海金融区域名为 vms.ap-shanghai-fsi.tencentcloudapi.com */
|
||||
cpf.HttpProfile.Endpoint = END_POINT
|
||||
/* SDK 默认用 TC3-HMAC-SHA256 进行签名,非必要请不要修改该字段 */
|
||||
cpf.SignMethod = SIGN_METHOD
|
||||
|
||||
//初始化客户端
|
||||
vmsclient, err := vms.NewClient(credential, voiceMessageConfigObj.Region, cpf)
|
||||
if err != nil {
|
||||
logUtil.ErrorLog(fmt.Sprintf("初始化VMS的client 对象失败,错误信息为:%s", err))
|
||||
return nil
|
||||
}
|
||||
|
||||
//构造对象
|
||||
voiceMessageObj := &VoiceMessage{
|
||||
client: vmsclient,
|
||||
voiceMessageConfig: voiceMessageConfigObj,
|
||||
}
|
||||
|
||||
//返回对象
|
||||
return voiceMessageObj
|
||||
}
|
||||
Reference in New Issue
Block a user