Skip to content

Commit 5dc4d58

Browse files
committed
- minor bug fix
1 parent 864b42e commit 5dc4d58

File tree

3 files changed

+66
-32
lines changed

3 files changed

+66
-32
lines changed

controllers/crud.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package controllers
22

33
import (
4-
"github.com/Darklabel91/API_Names/database"
54
"github.com/Darklabel91/API_Names/models"
65
"github.com/gin-gonic/gin"
76
"net/http"
@@ -81,26 +80,29 @@ func GetName(c *gin.Context) {
8180

8281
//GetMetaphoneMatch read name by metaphone
8382
func GetMetaphoneMatch(c *gin.Context) {
83+
var nameType models.NameType
84+
8485
//Check the cache
8586
var preloadTable []models.NameType
8687
cache, existKey := c.Get("nameTypes")
8788
if existKey {
8889
preloadTable = cache.([]models.NameType)
8990
} else {
90-
if err := database.DB.Find(&preloadTable).Error; err != nil {
91-
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to preload nameTypes"})
91+
allNames, err := nameType.GetAllNames()
92+
if err != nil {
93+
c.JSON(http.StatusInternalServerError, gin.H{"Message": "Error on caching all name types"})
9294
return
9395
}
96+
preloadTable = allNames
9497
}
9598

9699
//name to be searched
97100
name := c.Params.ByName("name")
98101

99102
//search similar names
100-
var nameType models.NameType
101103
canonicalEntity, err := nameType.GetSimilarMatch(name, preloadTable)
102104
if err != nil {
103-
c.JSON(http.StatusInternalServerError, gin.H{"Message": err})
105+
c.JSON(http.StatusNotFound, gin.H{"Message": "Couldn't find canonical entity"})
104106
return
105107
}
106108

models/name.go

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package models
33
import (
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
175173
func (*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

226241
func (*NameType) SearchCacheName(name string, cache []NameType) (*NameType, bool) {

routes/routes.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import (
55
"github.com/Darklabel91/API_Names/middlewares"
66
"github.com/Darklabel91/API_Names/models"
77
"github.com/gin-gonic/gin"
8+
"net/http"
89
"os"
910
"time"
1011
)
1112

1213
const DOOR = ":8080"
1314
const FILENAME = "Logs.txt"
14-
const SECONDS = 10
15+
const MICROSECONDS = 300
1516

1617
var nameTypesCache []models.NameType
1718

@@ -34,7 +35,7 @@ func HandleRequests() {
3435

3536
//upload the log file from time to time
3637
var log models.Log
37-
ticker := time.NewTicker(SECONDS * time.Second)
38+
ticker := time.NewTicker(MICROSECONDS * time.Microsecond)
3839
defer ticker.Stop()
3940
log.UploadLog(ticker, FILENAME)
4041

@@ -49,16 +50,32 @@ func HandleRequests() {
4950
r.POST("/name", middlewares.ValidateName(), middlewares.ValidateNameJSON(), controllers.CreateName)
5051
r.GET("/:id", middlewares.ValidateID(), controllers.GetID)
5152
r.GET("/name/:name", middlewares.ValidateName(), controllers.GetName)
53+
r.GET("/metaphone/:name", middlewares.ValidateName(), cachingNameTypes(nameTypesCache), controllers.GetMetaphoneMatch)
5254
r.PATCH("/:id", middlewares.ValidateID(), middlewares.ValidateNameJSON(), controllers.UpdateName)
5355
r.DELETE("/:id", middlewares.ValidateID(), controllers.DeleteName)
5456

55-
//special route to search with metaphone
56-
var nameType models.NameType
57-
r.GET("/metaphone/:name", middlewares.ValidateName(), nameType.CachingNameTypes(nameTypesCache), controllers.GetMetaphoneMatch)
58-
5957
// run
6058
err = r.Run(DOOR)
6159
if err != nil {
6260
return
6361
}
6462
}
63+
64+
func cachingNameTypes(nameTypesCache []models.NameType) gin.HandlerFunc {
65+
var name models.NameType
66+
67+
if nameTypesCache == nil {
68+
nameTypes, err := name.GetAllNames()
69+
if err != nil {
70+
return func(c *gin.Context) {
71+
c.JSON(http.StatusInternalServerError, gin.H{"Message": "Error on caching all name types"})
72+
}
73+
}
74+
nameTypesCache = nameTypes
75+
}
76+
77+
return func(c *gin.Context) {
78+
c.Set("nameTypes", nameTypesCache)
79+
c.Next()
80+
}
81+
}

0 commit comments

Comments
 (0)