Retry dict lookups with singular version of search term

This commit is contained in:
Ray Miller 2024-05-06 11:45:27 +01:00
parent e7e9cd6cce
commit 5ae1be220b
3 changed files with 16 additions and 0 deletions

2
go.mod
View file

@ -3,3 +3,5 @@ module github.com/ray1729/dict-web-simple
go 1.22.2
require golang.org/x/net v0.24.0
require github.com/gertd/go-pluralize v0.2.1

2
go.sum
View file

@ -1,2 +1,4 @@
github.com/gertd/go-pluralize v0.2.1 h1:M3uASbVjMnTsPb0PNqg+E/24Vwigyo/tvyMTtAlLgiA=
github.com/gertd/go-pluralize v0.2.1/go.mod h1:rbYaKDbsXxmRfr8uygAEKhOWsjyrrqrkHVpZvoOp8zk=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=

12
main.go
View file

@ -10,6 +10,7 @@ import (
"strings"
"golang.org/x/net/dict"
pluralize "github.com/gertd/go-pluralize"
)
func main() {
@ -73,6 +74,13 @@ func renderTemplate(w http.ResponseWriter, t *template.Template, params TmplPara
}
}
var pl *pluralize.Client
func init() {
pl = pluralize.NewClient()
pl.AddSingularRule("(rect)a", "$1um")
}
func getDefinitions(serverAddr string, word string) ([]*dict.Defn, error) {
cli, err := dict.Dial("tcp", serverAddr)
if err != nil {
@ -82,6 +90,10 @@ func getDefinitions(serverAddr string, word string) ([]*dict.Defn, error) {
defs, err := cli.Define("*", word)
if err != nil {
if strings.Contains(err.Error(), "552 no match") {
word_singular := pl.Singular(word)
if word_singular != word {
return getDefinitions(serverAddr, word_singular)
}
return nil, nil
}
return nil, fmt.Errorf("error from dict server: %v", err)