Skip to content

Commit c4d5406

Browse files
committed
Separated exact words finding to a new query
1 parent e06791d commit c4d5406

File tree

2 files changed

+50
-26
lines changed

2 files changed

+50
-26
lines changed

govarnam/dictionary.go

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,16 @@ func makeDictionary(dictPath string) (*sql.DB, error) {
156156
return conn, nil
157157
}
158158

159+
type searchDictionaryType int32
160+
161+
const (
162+
searchMatches searchDictionaryType = 0 // For checking whether there are words in dictionary starting with something
163+
searchStartingWith searchDictionaryType = 1 // Find all words in dictionary starting with something
164+
searchExactWords searchDictionaryType = 2 // Find exact words in dictionary
165+
)
166+
159167
// all - Search for words starting with the word
160-
func (varnam *Varnam) searchDictionary(ctx context.Context, words []string, all bool) []searchDictionaryResult {
168+
func (varnam *Varnam) searchDictionary(ctx context.Context, words []string, searchType searchDictionaryType) []searchDictionaryResult {
161169
likes := ""
162170

163171
var (
@@ -184,11 +192,13 @@ func (varnam *Varnam) searchDictionary(ctx context.Context, words []string, all
184192
// CC BY-SA 4.0 licensed
185193
// https://stackoverflow.com/q/68610241/1372424
186194

187-
if all {
188-
query = "WITH cte(match) AS (VALUES (?) " + likes + ") SELECT c.match AS match, w.* FROM words_fts w INNER JOIN cte c ON w.word MATCH c.match || '*' AND learned_on > 0 ORDER BY weight DESC LIMIT ?"
189-
vals = append(vals, varnam.DictionarySuggestionsLimit)
190-
} else {
195+
if searchType == searchMatches {
191196
query = "WITH cte(match) AS (VALUES (?) " + likes + ") SELECT c.match AS match, w.word AS word, MAX(w.weight), MAX(w.learned_on) FROM words_fts w INNER JOIN cte c ON w.word MATCH c.match || '*' GROUP BY c.match"
197+
} else if searchType == searchStartingWith {
198+
query = "WITH cte(match) AS (VALUES (?) " + likes + ") SELECT c.match AS match, w.* FROM words_fts w INNER JOIN cte c ON w.word MATCH c.match || '*' AND w.word != c.match ORDER BY weight DESC LIMIT ?"
199+
vals = append(vals, varnam.DictionarySuggestionsLimit)
200+
} else if searchType == searchExactWords {
201+
query = "SELECT * FROM words WHERE word IN ((?) " + likes + ")"
192202
}
193203

194204
rows, err := varnam.dictConn.QueryContext(ctx, query, vals...)
@@ -245,13 +255,21 @@ func (varnam *Varnam) getFromDictionary(ctx context.Context, tokensPointer *[]To
245255
toSearch = append(toSearch, getSymbolValue(t.symbols[j], 0))
246256
}
247257

248-
searchResults := varnam.searchDictionary(ctx, toSearch, false)
258+
searchResults := varnam.searchDictionary(
259+
ctx,
260+
toSearch,
261+
searchMatches,
262+
)
249263

250264
tempFoundDictWords = searchResults
251265
tokenizedWords = searchResults
252266

253267
if LOG_TIME_TAKEN {
254-
log.Printf("%s took %v\n", "getFromDictionaryToken0", time.Since(start))
268+
log.Printf(
269+
"%s took %v\n",
270+
"getFromDictionaryToken0",
271+
time.Since(start),
272+
)
255273
}
256274
} else {
257275
start := time.Now()
@@ -269,7 +287,11 @@ func (varnam *Varnam) getFromDictionary(ctx context.Context, tokensPointer *[]To
269287
toSearch = append(toSearch, newTill)
270288
}
271289

272-
searchResults := varnam.searchDictionary(ctx, toSearch, false)
290+
searchResults := varnam.searchDictionary(
291+
ctx,
292+
toSearch,
293+
searchMatches,
294+
)
273295

274296
if len(searchResults) > 0 {
275297
tempFoundDictWords = append(tempFoundDictWords, searchResults...)
@@ -324,27 +346,26 @@ func (varnam *Varnam) getMoreFromDictionary(ctx context.Context, words []Suggest
324346
case <-ctx.Done():
325347
return result
326348
default:
349+
wordsToSearch := []string{}
350+
327351
for i := range words {
328-
var moreSugs []Suggestion
352+
wordsToSearch = append(wordsToSearch, words[i].Word)
329353

330354
search := []string{words[i].Word}
331-
searchResults := varnam.searchDictionary(ctx, search, true)
355+
result.moreSuggestions = append(
356+
result.moreSuggestions,
357+
convertSearchDictResultToSuggestion(
358+
varnam.searchDictionary(ctx, search, searchStartingWith),
359+
true,
360+
),
361+
)
362+
}
332363

333-
for i := range searchResults {
334-
sug := Suggestion{
335-
searchResults[i].word,
336-
searchResults[i].weight,
337-
searchResults[i].learnedOn,
338-
}
339-
if searchResults[i].match == searchResults[i].word {
340-
result.exactWords = append(result.exactWords, sug)
341-
} else {
342-
moreSugs = append(moreSugs, sug)
343-
}
344-
}
364+
result.exactWords = convertSearchDictResultToSuggestion(
365+
varnam.searchDictionary(ctx, wordsToSearch, searchExactWords),
366+
true,
367+
)
345368

346-
result.moreSuggestions = append(result.moreSuggestions, moreSugs)
347-
}
348369
return result
349370
}
350371
}
@@ -458,7 +479,10 @@ func (varnam *Varnam) GetSuggestions(ctx context.Context, word string) []Suggest
458479
case <-ctx.Done():
459480
return sugs
460481
default:
461-
return convertSearchDictResultToSuggestion(varnam.searchDictionary(ctx, []string{word}, true), true)
482+
return convertSearchDictResultToSuggestion(
483+
varnam.searchDictionary(ctx, []string{word}, searchStartingWith),
484+
true,
485+
)
462486
}
463487
}
464488

govarnam/govarnam_ml_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ func TestMLExportAndImport(t *testing.T) {
373373
varnam.Import(exportFilePath)
374374

375375
for _, wordInfo := range words {
376-
results := varnam.searchDictionary(context.Background(), []string{wordInfo.word}, false)
376+
results := varnam.searchDictionary(context.Background(), []string{wordInfo.word}, searchMatches)
377377

378378
assertEqual(t, len(results) > 0, true)
379379
}

0 commit comments

Comments
 (0)