Skip to content

Commit 454dce2

Browse files
committed
Return correct words list in /fix route
1 parent 18e7311 commit 454dce2

3 files changed

Lines changed: 68 additions & 15 deletions

File tree

README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Response:
7272
"suggestions": [
7373
{
7474
"text": "weapon",
75-
"score": 2.7081884344831684
75+
"score": 0.8239592165010822
7676
}
7777
],
7878
"error": "invalid_word"
@@ -83,7 +83,7 @@ Response:
8383
"suggestions": [
8484
{
8585
"text": "before",
86-
"score": 2.339371734389815
86+
"score": 0.7797905781299383
8787
}
8888
],
8989
"error": "invalid_word"
@@ -94,11 +94,37 @@ Response:
9494
"suggestions": [
9595
{
9696
"text": "battle",
97-
"score": 2.339371734389815
97+
"score": 0.7797905781299383
9898
}
9999
],
100100
"error": "invalid_word"
101101
}
102+
],
103+
"correct": [
104+
{
105+
"start": 0,
106+
"end": 3
107+
},
108+
{
109+
"start": 4,
110+
"end": 10
111+
},
112+
{
113+
"start": 11,
114+
"end": 17
115+
},
116+
{
117+
"start": 18,
118+
"end": 21
119+
},
120+
{
121+
"start": 35,
122+
"end": 43
123+
},
124+
{
125+
"start": 44,
126+
"end": 48
127+
}
102128
]
103129
}
104130
```

internal/routes/dictionary_fix.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ type DictionaryFixRequest struct {
2424
}
2525

2626
type DictionaryFixResponse struct {
27-
Fixes []Fix `json:"fixes" description:"List of detected issues."`
27+
Fixes []Fix `json:"fixes" description:"List of detected issues."`
28+
Correct []Correct `json:"correct" description:"List of correct words."`
2829
}
2930

3031
type Fix struct {
@@ -34,6 +35,11 @@ type Fix struct {
3435
Error string `json:"error" enum:"unknown_word,invalid_word" description:"Type of detected error. unknown_word - no possible corrections found; invalid_word - the word can be corrected using one of the provided suggestions"`
3536
}
3637

38+
type Correct struct {
39+
Start int `json:"start" description:"Starting character index of the word in the input."`
40+
End int `json:"end" description:"Ending character index."`
41+
}
42+
3743
type SpellcheckerSuggestion struct {
3844
Text string `json:"text" descrption:"Suggested corrected word."`
3945
Score float64 `json:"score" description:"Confidence score of the suggestion."`
@@ -60,6 +66,7 @@ func dictionaryFix(registry dictionaryGetter, splitter *regexp.Regexp) usecase.I
6066

6167
matches := splitter.FindAllStringIndex(input.Text, -1)
6268
fixes := make([]Fix, 0, len(matches))
69+
correct := make([]Correct, 0, len(matches))
6370

6471
for _, match := range matches {
6572
startByte, endByte := match[0], match[1]
@@ -76,6 +83,11 @@ func dictionaryFix(registry dictionaryGetter, splitter *regexp.Regexp) usecase.I
7683
suggestions := sc.SuggestScore(word, input.Limit)
7784

7885
if suggestions.ExactMatch {
86+
correct = append(correct, Correct{
87+
Start: startRune,
88+
End: endRune,
89+
})
90+
7991
continue
8092
}
8193

@@ -97,6 +109,7 @@ func dictionaryFix(registry dictionaryGetter, splitter *regexp.Regexp) usecase.I
97109
}
98110

99111
output.Fixes = fixes
112+
output.Correct = correct
100113

101114
return nil
102115
})

internal/routes/dictionary_fix_test.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,21 @@ func Test_DictionaryFix(t *testing.T) {
3737
sc.Add("hello")
3838

3939
tests := []struct {
40-
name string
41-
getter dictionaryGetter
42-
input DictionaryFixRequest
43-
wantErr bool
44-
wantCode status.Code
45-
wantFixes []Fix
40+
name string
41+
getter dictionaryGetter
42+
input DictionaryFixRequest
43+
wantErr bool
44+
wantCode status.Code
45+
wantFixes []Fix
46+
wantCorrect []Correct
4647
}{
4748
{
48-
name: "empty text",
49-
getter: &testDictionaryGetter{sc: &f1mspellchecker.Spellchecker{}},
50-
input: DictionaryFixRequest{Code: "en", Text: "", Limit: 5},
51-
wantErr: false,
52-
wantFixes: []Fix{},
49+
name: "empty text",
50+
getter: &testDictionaryGetter{sc: &f1mspellchecker.Spellchecker{}},
51+
input: DictionaryFixRequest{Code: "en", Text: "", Limit: 5},
52+
wantErr: false,
53+
wantFixes: []Fix{},
54+
wantCorrect: []Correct{},
5355
},
5456
{
5557
name: "exact match word",
@@ -59,6 +61,9 @@ func Test_DictionaryFix(t *testing.T) {
5961
input: DictionaryFixRequest{Code: "en", Text: "hello", Limit: 5},
6062
wantErr: false,
6163
wantFixes: []Fix{},
64+
wantCorrect: []Correct{
65+
{Start: 0, End: 5},
66+
},
6267
},
6368
{
6469
name: "word with suggestions",
@@ -76,6 +81,7 @@ func Test_DictionaryFix(t *testing.T) {
7681
},
7782
},
7883
},
84+
wantCorrect: []Correct{},
7985
},
8086
{
8187
name: "word without suggestions",
@@ -90,6 +96,7 @@ func Test_DictionaryFix(t *testing.T) {
9096
Error: "unknown_word",
9197
},
9298
},
99+
wantCorrect: []Correct{},
93100
},
94101
{
95102
name: "dictionary not found",
@@ -136,6 +143,13 @@ func Test_DictionaryFix(t *testing.T) {
136143
assert.Equal(t, f.End, out.Fixes[i].End)
137144
assert.Equal(t, f.Error, out.Fixes[i].Error)
138145
}
146+
147+
require.Len(t, out.Correct, len(tt.wantCorrect))
148+
149+
for i, f := range tt.wantCorrect {
150+
assert.Equal(t, f.Start, out.Correct[i].Start)
151+
assert.Equal(t, f.End, out.Correct[i].End)
152+
}
139153
}
140154
})
141155
}

0 commit comments

Comments
 (0)