@@ -3,9 +3,7 @@ package models
33import (
44 "errors"
55 "github.com/Darklabel91/metaphone-br"
6- "github.com/gin-gonic/gin"
76 "gorm.io/gorm"
8- "net/http"
97 "strings"
108)
119
@@ -80,19 +78,19 @@ func (n *NameType) GetSimilarMatch(name string, allNames []NameType) (*NameType,
8078
8179 //1- get the exact metaphone match
8280 metaphoneNameMatches := n .SearchCacheMetaphone (nameMetaphone , allNames )
83- //case we don't find the exact match of metaphone we search all similar metaphones
81+ //case we don't find the exact match of metaphone we search all similar metaphone
8482 if len (metaphoneNameMatches ) == 0 {
8583 //get all similar metaphone code
8684 metaphoneNameMatches = n .SearchSimilarMetaphone (nameMetaphone , allNames )
8785 if len (metaphoneNameMatches ) == 0 {
88- return nil , err
86+ return nil , errors . New ( "no metaphone matches found" )
8987 }
9088 }
9189
9290 //2- get all similar names by metaphone list
9391 similarNames := n .SearchSimilarNames (name , metaphoneNameMatches , LEVENSHTEIN )
9492 if len (similarNames ) == 0 {
95- return nil , err
93+ return nil , errors . New ( "no similar name matches" )
9694 }
9795 //case similarNames is too small we search for all similar names of all similar names listed so far
9896 if len (similarNames ) < 5 {
@@ -175,16 +173,25 @@ func (*NameType) SearchSimilarNames(paradigmName string, allNames []NameType, th
175173func (* NameType ) SearchCanonicalName (paradigmName string , threshold float32 , allNames []NameType , matchingMetaphoneNames []NameType , nameVariations []string ) (* NameType , error ) {
176174 n := strings .ToUpper (paradigmName )
177175
176+ //transform the nameVariations into a string to be returned
177+ var rNv string
178+ for _ , nv := range nameVariations {
179+ rNv += nv + " | "
180+ }
181+
178182 //search exact match on matchingMetaphoneNames
179183 for _ , similarName := range matchingMetaphoneNames {
180184 if similarName .Name == n {
185+ similarName .NameVariations = rNv
181186 return & similarName , nil
182187 }
183188 }
184189
185190 //search for similar names on matchingMetaphoneNames
186191 for _ , similarName := range matchingMetaphoneNames {
187- if metaphone .SimilarityBetweenWords (n , strings .ToUpper (similarName .Name )) >= threshold {
192+ sn := strings .ToUpper (similarName .NameVariations )
193+ if metaphone .SimilarityBetweenWords (n , sn ) >= threshold {
194+ similarName .NameVariations = rNv
188195 return & similarName , nil
189196 }
190197 }
@@ -195,32 +202,40 @@ func (*NameType) SearchCanonicalName(paradigmName string, threshold float32, all
195202 if sn == n {
196203 for _ , name := range allNames {
197204 if name .Name == n {
205+ name .NameVariations = rNv
198206 return & name , nil
199207 }
200208 }
201209 }
202210 }
203211
204- return & NameType {}, errors .New ("couldn't find canonical name" )
205- }
206-
207- func (* NameType ) CachingNameTypes (nameTypesCache []NameType ) gin.HandlerFunc {
208- var name NameType
209-
210- if nameTypesCache == nil {
211- nameTypes , err := name .GetAllNames ()
212- if err != nil {
213- return func (c * gin.Context ) {
214- c .JSON (http .StatusInternalServerError , gin.H {"Message" : "Error on caching all name types" })
212+ //search for similar names on nameVariations
213+ for _ , similarName := range nameVariations {
214+ sn := strings .ToUpper (similarName )
215+ if metaphone .SimilarityBetweenWords (n , sn ) >= threshold {
216+ for _ , name := range allNames {
217+ if name .Name == sn {
218+ name .NameVariations = rNv
219+ return & name , nil
220+ }
215221 }
216222 }
217- nameTypesCache = nameTypes
218223 }
219224
220- return func (c * gin.Context ) {
221- c .Set ("nameTypes" , nameTypesCache )
222- c .Next ()
225+ //case none are found we establish a return similarity for names 0.1 bellow the original threshold
226+ for _ , similarName := range nameVariations {
227+ sn := strings .ToUpper (similarName )
228+ if metaphone .SimilarityBetweenWords (n , sn ) >= threshold - 0.1 {
229+ for _ , name := range allNames {
230+ if name .Name == sn {
231+ name .NameVariations = rNv
232+ return & name , nil
233+ }
234+ }
235+ }
223236 }
237+
238+ return & NameType {}, errors .New ("couldn't find canonical name" )
224239}
225240
226241func (* NameType ) SearchCacheName (name string , cache []NameType ) (* NameType , bool ) {
0 commit comments