From 5ae1be220b165a3d4fbb2a9779cac3dc25ae90cd Mon Sep 17 00:00:00 2001 From: Ray Miller Date: Mon, 6 May 2024 11:45:27 +0100 Subject: [PATCH] Retry dict lookups with singular version of search term --- go.mod | 2 ++ go.sum | 2 ++ main.go | 12 ++++++++++++ 3 files changed, 16 insertions(+) diff --git a/go.mod b/go.mod index e6b42a5..78a6a80 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 4a8ba20..79ecc45 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index d5fbaa7..9875a78 100644 --- a/main.go +++ b/main.go @@ -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)