Commit d9864388 by mushishixian

修改匹配逻辑

parent 277a4563
package logic package logic
import ( import (
"bom_server/internal/common"
"bom_server/internal/mapping" "bom_server/internal/mapping"
"bom_server/internal/model" "bom_server/internal/model"
"bom_server/internal/pkg/gredis" "bom_server/internal/pkg/gredis"
...@@ -10,7 +9,6 @@ import ( ...@@ -10,7 +9,6 @@ import (
"github.com/imroc/req" "github.com/imroc/req"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"regexp" "regexp"
"strconv"
"strings" "strings"
) )
...@@ -26,10 +24,8 @@ func GetStandardAttrs(bomItems []model.BomItem) []model.BomItem { ...@@ -26,10 +24,8 @@ func GetStandardAttrs(bomItems []model.BomItem) []model.BomItem {
} else { } else {
attrParams = []string{item.Attrs} attrParams = []string{item.Attrs}
} }
//fmt.Println(attrParams)
params := req.BodyJSON(attrParams) params := req.BodyJSON(attrParams)
resp, err := req.Post("http://192.168.2.232:50053/unit_conversion", params) resp, err := req.Post("http://192.168.2.141:50053/unit_conversion", params)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
...@@ -46,20 +42,17 @@ func GetStandardAttrs(bomItems []model.BomItem) []model.BomItem { ...@@ -46,20 +42,17 @@ func GetStandardAttrs(bomItems []model.BomItem) []model.BomItem {
attrsList = append(attrsList, attr.String()) attrsList = append(attrsList, attr.String())
} }
encapArray := gjson.Get(result, "encap").Array() encapArray := gjson.Get(result, "encap").Array()
fmt.Println(result)
for _, attr := range encapArray { for _, attr := range encapArray {
encapList = append(encapList, attr.String()) encapList = append(encapList, attr.String())
} }
fmt.Println("匹配的型号为 : ", words, "匹配的参数列表为 : ", attrsList) //如果words大于0,并且识别出来的参数为空,则代表是型号
//如果words大于0,则代表是型号 if len(words) > 0 && len(attrsArray) == 0 {
if len(words) > 0 {
item.GoodsName = words[0] item.GoodsName = words[0]
} }
//如果words等于0,但是属性有的情况 //如果words等于0,但是属性有的情况
if len(words) == 0 && len(attrsList) > 0 { if len(words) == 0 && len(attrsList) > 0 {
if item.GoodsName == "" && len(item.AttrList) == 0 { if item.GoodsName != "" {
item.GoodsName = ""
}
if item.GoodsName != "" {
item.GoodsName = "" item.GoodsName = ""
} }
item.AttrList = attrsList item.AttrList = attrsList
...@@ -68,120 +61,21 @@ func GetStandardAttrs(bomItems []model.BomItem) []model.BomItem { ...@@ -68,120 +61,21 @@ func GetStandardAttrs(bomItems []model.BomItem) []model.BomItem {
if item.Encap == "" && len(encapList) > 0 { if item.Encap == "" && len(encapList) > 0 {
item.Encap = encapList[0] item.Encap = encapList[0]
} }
bomItems[key] = item
}
return bomItems
}
//转换参数,使其成为标准参数
func MatchGoodsNameByAttrs(bomItems []model.BomItem) (result []model.BomItem, err error) {
//先根据参数去构建批量查询条件
for key, item := range bomItems {
//如果有型号,但是型号有可能是参数,所以先去匹配下参数,有的话转成对应的型号
bomItems[key].AttrList = TransformAttr(item.Attrs, item.Encap)
}
result = bomItems
return
}
//转换精简参数 var attrsSlice []string
func TransformAttr(attrStr, encap string) []string { for _, attr := range item.AttrList {
//先去切割参数得到参数列表 attr = GetStandardEsAttr(attr)
attrs := splitAttrs(attrStr) if attr != "" {
//当切割出来的参数大于1个的时候,就要去针对每个参数进行里面再一次提纯 attrsSlice = append(attrsSlice, attr)
if len(attrs) > 1 {
for key, attr := range attrs {
attrs[key] = changeStandardUnit(attr)
//切割完以后,还要去对切割后的参数提取值,因为有可能参数没有分隔符
extract := extractAttr(attrs[key])
for ek, eAttr := range extract {
if ek == 0 {
attrs[key] = extract[0]
} else {
attrs = append(attrs, eAttr)
}
} }
} }
} else { //转换成新约定的形式
attrs[0] = changeStandardUnit(attrs[0]) attrsSlice = transformNew(attrsSlice)
attrs = extractAttrsByStr(attrs[0]) item.AttrList = attrsSlice
} bomItems[key] = item
var attrsSlice []string fmt.Println("goooooooooo", item.AttrList)
//去转换每一个参数,得到去查询es的标准格式
for _, attr := range attrs {
attr = GetStandardEsAttr(attr)
if attr != "" {
attrsSlice = append(attrsSlice, attr)
}
}
//转换成新约定的形式
attrsSlice = transformNew(attrsSlice)
return attrsSlice
}
//切割参数
func splitAttrs(attrs string) (result []string) {
result = strings.Split(attrs, " ")
if len(result) > 1 {
return
}
result = strings.Split(attrs, ",")
if len(result) > 1 {
return
}
result = strings.Split(attrs, "|")
if len(result) > 1 {
return
}
result = strings.Split(attrs, ",")
if len(result) > 1 {
return
}
result = strings.Split(attrs, "/")
if len(result) > 1 {
return
}
return
}
//提取参数
func extractAttr(attr string) []string {
regexpUnit := strings.Join(getNeedCheckUnit(), "|")
r, _ := regexp.Compile("(\\d+[\\/\\d. ]*|\\d)(" + regexpUnit + ")" + "")
res := r.FindAllString(attr, 100)
if len(res) == 0 {
return []string{attr}
}
return res
}
//提取参数,从一个字符串里面,注意要提取出两个参数才当作是
func extractAttrsByStr(attr string) []string {
regexpUnit := strings.Join(getNeedCheckUnit(), "|")
r, _ := regexp.Compile("(\\d+[\\/\\d. ]*|\\d)(" + regexpUnit + ")" + "")
res := r.FindAllString(attr, 100)
if len(res) < 2 {
return []string{attr}
}
return res
}
//将写法转成正规写法,转换单位,比如有的人喜欢将Ω输成欧姆或者O,所以要统一转成正常的Ω
func changeStandardUnit(attr string) (result string) {
regulars := mapping.KeywordRegular
for regexpStr, regular := range regulars {
if strings.Contains(attr, "Ω") {
break
}
//先去判断这个属性值是不是属于某个封装或者温漂系数
attrName, _ := redis.String(gredis.HGet("sku_map2", attr))
if attrName != "" {
return attr
}
compile, _ := regexp.Compile(regexpStr)
attr = compile.ReplaceAllString(attr, regular)
} }
return attr return bomItems
} }
//将属性值转成ES的标准值(例如 : 阻值(欧姆)€3000) //将属性值转成ES的标准值(例如 : 阻值(欧姆)€3000)
...@@ -189,52 +83,27 @@ func GetStandardEsAttr(attr string) (result string) { ...@@ -189,52 +83,27 @@ func GetStandardEsAttr(attr string) (result string) {
//先找出单位,通过将数字替换成,第一个字符不是数字的,跳过 //先找出单位,通过将数字替换成,第一个字符不是数字的,跳过
r, _ := regexp.Compile(mapping.GetAttrUnitRegular) r, _ := regexp.Compile(mapping.GetAttrUnitRegular)
attrUnit := strings.Trim(r.ReplaceAllString(attr, ""), " ") attrUnit := strings.Trim(r.ReplaceAllString(attr, ""), " ")
//如果单位是W,则要进行除法计算
if strings.ToUpper(attrUnit) == "W" {
//提取值,进行计算
attr = strings.TrimRight(attr, "W")
attr = strings.TrimRight(attr, "w")
attrSlice := strings.Split(attr, "/")
if len(attrSlice) == 2 {
var res float64
a, _ := strconv.ParseFloat(attrSlice[0], 64)
b, _ := strconv.ParseFloat(attrSlice[1], 64)
res = a / b
attr = fmt.Sprintf("%.3f", res) + "W"
}
}
//再找出纯数字 //再找出纯数字
var attrNumber float64
numberR, _ := regexp.Compile(mapping.PureNumberRegular) numberR, _ := regexp.Compile(mapping.PureNumberRegular)
pureNumber := numberR.FindString(attr) pureNumberStr := numberR.FindString(attr)
attrNumber, _ = strconv.ParseFloat(pureNumber, 64) //如果属性是纯数字,则代表是精度
if pureNumberStr == attr {
return "精度" + "€" + attr
}
//找出对应单位需要转换的值 //找出对应单位需要转换的值
var attrName string var attrName, baseUnit string
if attrUnit != "" {
for unit, value := range mapping.UnitValueMapping { if value, exist := mapping.UnitBaseMapping[attrUnit]; exist {
//如果不是标准最小单位,则要进行转换 baseUnit = value
if attrUnit == unit {
value64, _ := strconv.ParseFloat(value, 64)
//还要找到对应的属性,先找到基础单位
var baseUnit string
if value, exist := mapping.UnitBaseMapping[attrUnit]; exist {
baseUnit = value
}
if baseUnit == "" {
return attr
}
//获取重要属性名称
if value, exist := mapping.UnitAttrMapping[baseUnit]; exist {
attrName = value
}
attrNumberStr := common.ToString(attrNumber * value64)
result = attrName + "€" + attrNumberStr
break
}
}
//就算是提取出所谓的单位,也有可能是封装里面乱写的,比如 C0402R
//所以还是要去判断是否是封装
} }
if baseUnit == "" {
return attr
}
//获取重要属性名称
if value, exist := mapping.UnitAttrMapping[baseUnit]; exist {
attrName = value
}
result = attrName + "€" + pureNumberStr
//再去找没有单位的对应属性 //再去找没有单位的对应属性
attrName, _ = redis.String(gredis.HGet("sku_map2", attr)) attrName, _ = redis.String(gredis.HGet("sku_map2", attr))
if attrName != "" { if attrName != "" {
...@@ -243,15 +112,6 @@ func GetStandardEsAttr(attr string) (result string) { ...@@ -243,15 +112,6 @@ func GetStandardEsAttr(attr string) (result string) {
return result return result
} }
//获取需要检查的单位
func getNeedCheckUnit() (unitSlice []string) {
unitMapping := mapping.UnitValueMapping
for unit, _ := range unitMapping {
unitSlice = append(unitSlice, unit)
}
return
}
//转成新约定的形式 //转成新约定的形式
func transformNew(attrs []string) []string { func transformNew(attrs []string) []string {
attrMapping := mapping.AttrBomTransformMapping attrMapping := mapping.AttrBomTransformMapping
......
...@@ -90,7 +90,7 @@ func UpdateBomItem(bomId, bomItemId int) (err error) { ...@@ -90,7 +90,7 @@ func UpdateBomItem(bomId, bomItemId int) (err error) {
} }
defer client.Stop() defer client.Stop()
//匹配之前,去遍历bom_item,把没有型号名称但是有参数的bom_item进行型号补充 //匹配之前,去遍历bom_item,把没有型号名称但是有参数的bom_item进行型号补充
bomItems, err = MatchGoodsNameByAttrs(bomItems) bomItems = GetStandardAttrs(bomItems)
//bomItems, err = MatchGoodsNameByAttrs(bomItems) //bomItems, err = MatchGoodsNameByAttrs(bomItems)
//第一次去精确匹配,没有再去模糊匹配 //第一次去精确匹配,没有再去模糊匹配
var goodsMapList []GoodsMap var goodsMapList []GoodsMap
......
...@@ -20,7 +20,5 @@ func TransformEncap(encap string) (attrValue string) { ...@@ -20,7 +20,5 @@ func TransformEncap(encap string) (attrValue string) {
pureNumber = encapCheck pureNumber = encapCheck
} }
} }
//再去找对应属性 return pureNumber
attrValue = "封装" + "€" + pureNumber
return attrValue
} }
...@@ -101,6 +101,7 @@ func getSearchParams(index string, bomItem model.BomItem, sort int, flag bool) ( ...@@ -101,6 +101,7 @@ func getSearchParams(index string, bomItem model.BomItem, sort int, flag bool) (
} }
source = source.From(0).Size(1) source = source.From(0).Size(1)
searchRequest = es.NewSearchRequest().Source(source) searchRequest = es.NewSearchRequest().Source(source)
fmt.Println(searchRequest.Body())
return searchRequest return searchRequest
} }
......
package mapping package mapping
var KeywordRegular = map[string]string{
//`^(.* )?([\d\.]+)(欧|欧姆|R|r)( .*)?$`: `$1$2Ω$4`,
//`^(.* )?([\d\.]+)(U|u|μ)(.?)( .*)?$`: `$1$2Μ$4$5`,
"COG": "C0G",
"NPO|NP0|nPO|npO|npo|nP0|np0": "C0G",
"华科|华新科技|华新科": "华新",
`(欧姆|欧|O|o|R|r)$`: "Ω",
`(Uf|uf|μf|uF|UF)$`: "μF",
`(Nf|NF|nf)$`: "nF",
`(Pf|PF|pf)$`: "pF",
`(Mf|MF|mf)$`: "mF",
`(Uh|uh|μh|uH|UH)$`: "μH",
`(Mh|mh|mH|MH)$`: "mH",
`(Ma|ma|mA|MA)$`: "mA",
`(K)$`: "nF",
`(V|v)`: "V",
}
var PureNumberRegular = `(\d+(\.\d+)?)` var PureNumberRegular = `(\d+(\.\d+)?)`
var GetAttrUnitRegular = `[\d.]|±|\+_|\+-|/|\(.*\)|\+/-|` var GetAttrUnitRegular = `[\d.]|±|\+_|\+-|/|\(.*\)|\+/-|`
...@@ -26,8 +8,8 @@ var UnitAttrMapping = map[string]string{ ...@@ -26,8 +8,8 @@ var UnitAttrMapping = map[string]string{
"r": "阻值(欧姆)|直流电阻(内阻)", "r": "阻值(欧姆)|直流电阻(内阻)",
"Ω": "阻值(欧姆)", "Ω": "阻值(欧姆)",
"OHM": "阻值(欧姆)|直流电阻(内阻)", "OHM": "阻值(欧姆)|直流电阻(内阻)",
"mh": "电感", "mh": "电感",
"H": "电感", "H": "电感",
"F": "容值", "F": "容值",
"w": "功率", "w": "功率",
"W": "功率", "W": "功率",
...@@ -39,65 +21,11 @@ var UnitAttrMapping = map[string]string{ ...@@ -39,65 +21,11 @@ var UnitAttrMapping = map[string]string{
//属性对应的基础属性 //属性对应的基础属性
var UnitBaseMapping = map[string]string{ var UnitBaseMapping = map[string]string{
"μΩ": "Ω",
"mΩ": "Ω",
"Ω": "Ω", "Ω": "Ω",
"kΩ": "Ω",
"KΩ": "Ω",
"MΩ": "Ω",
"pF": "F", "pF": "F",
"nF": "F", "pH": "H",
"μF": "F",
"μf": "F",
"mF": "F",
"F": "F",
"μH": "H",
"mH": "H",
"H": "H",
"V": "V", "V": "V",
"kV": "V",
"KV": "V",
"mA": "A", "mA": "A",
"A": "A",
"W": "W", "W": "W",
"kW": "W",
"KW": "W",
"%": "%", "%": "%",
} }
//根据单位对值的转换,比如1kΩ=>1000Ω
var UnitValueMapping = map[string]string{
"μΩ": "0.000001",
"mΩ": "0.001",
"Ω": "1",
"kΩ": "1000",
"KΩ": "1000",
"MΩ": "1000000",
"pF": "1",
"nF": "1000",
"μF": "1000000",
"mF": "1000000000",
"F": "1000000000000",
"μH": "1",
"mH": "1000",
"H": "1000000",
"V": "1",
"kV": "1000",
"KV": "1000",
"mA": "1",
"A": "1000",
"W": "1",
"kW": "1000",
"%": "0.01",
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment