Skip to content

Commit c5f649b

Browse files
committed
Add route to fix words using a dictionary
1 parent 1ec9563 commit c5f649b

3 files changed

Lines changed: 72 additions & 6 deletions

File tree

README.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,61 @@ Content-Type: application/json
4343
"weight": 1
4444
},
4545
{
46-
"text": "The knight raised his weapon before charging into battle.",
46+
"text": "the knight raised his weapon before charging into battle",
4747
"weight": 1
4848
}
4949
]
5050
}
5151
```
52+
53+
3) Fix phrase
54+
```
55+
POST /v1/dictionaries/my-dictionary/phrase
56+
Content-Type: application/json
57+
58+
{
59+
"text": "the knight raised his waapon befor charging into battl"
60+
}
61+
```
62+
63+
Response:
64+
65+
```
66+
{
67+
"fixes": [
68+
{
69+
"start": 22,
70+
"end": 28,
71+
"suggestions": [
72+
{
73+
"text": "weapon",
74+
"score": 2.7081884344831684
75+
}
76+
],
77+
"error": "invalid_word"
78+
},
79+
{
80+
"start": 29,
81+
"end": 34,
82+
"suggestions": [
83+
{
84+
"text": "before",
85+
"score": 2.339371734389815
86+
}
87+
],
88+
"error": "invalid_word"
89+
},
90+
{
91+
"start": 49,
92+
"end": 54,
93+
"suggestions": [
94+
{
95+
"text": "battle",
96+
"score": 2.339371734389815
97+
}
98+
],
99+
"error": "invalid_word"
100+
}
101+
]
102+
}
103+
```
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@ package routes
22

33
import (
44
"context"
5+
"errors"
56
"regexp"
67
"unicode/utf8"
78

8-
"github.com/f1monkey/spellchecker"
9+
"github.com/f1monkey/spellchecker-web/internal/spellchecker"
910
"github.com/swaggest/usecase"
1011
"github.com/swaggest/usecase/status"
1112
)
1213

13-
type SpellcheckerFixRequest struct {
14+
type DictionaryFixRequest struct {
15+
Code string `path:"code" minLength:"1"`
16+
1417
Text string `json:"text" description:"Phrase to be checked"`
1518
Limit int `json:"limit" default:"5" desciption:"Max suggestions per word"`
1619
}
1720

18-
type SpellcheckerFixResponse struct {
21+
type DictionaryFixResponse struct {
1922
Fixes []Fix `json:"fixes" description:"List of detected issues."`
2023
}
2124

@@ -33,13 +36,20 @@ type SpellcheckerSuggestion struct {
3336

3437
var wordSymbols = regexp.MustCompile(`[-\pL]+`)
3538

36-
func SpellcheckerFix(sc *spellchecker.Spellchecker) usecase.Interactor {
39+
func dictionaryFix(registry *spellchecker.Registry) usecase.Interactor {
3740
const (
3841
errorUnknownWord = "unknown_word"
3942
errorInvalidWord = "invalid_word"
4043
)
4144

42-
u := usecase.NewInteractor(func(ctx context.Context, input SpellcheckerFixRequest, output *SpellcheckerFixResponse) error {
45+
u := usecase.NewInteractor(func(ctx context.Context, input DictionaryFixRequest, output *DictionaryFixResponse) error {
46+
sc, err := registry.Get(input.Code)
47+
if errors.Is(spellchecker.ErrNotFound, err) {
48+
return status.Wrap(err, status.NotFound)
49+
} else if err != nil {
50+
return status.Wrap(err, status.Internal)
51+
}
52+
4353
if input.Text == "" {
4454
return nil
4555
}

internal/routes/routes.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,9 @@ func dictionaryRoutes(registry *spellchecker.Registry) func(r chi.Router) {
3333
r.Method(http.MethodPost, "/{code}/add", nethttp.NewHandler(
3434
dictionaryItemAdd(registry)),
3535
)
36+
37+
r.Method(http.MethodPost, "/{code}/fix", nethttp.NewHandler(
38+
dictionaryFix(registry)),
39+
)
3640
}
3741
}

0 commit comments

Comments
 (0)