Skip to content

Commit 913a8c3

Browse files
committed
feat: add option for user defined dictionary
1 parent d73f40e commit 913a8c3

File tree

2 files changed

+55
-22
lines changed

2 files changed

+55
-22
lines changed

README.md

+14-11
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,28 @@ your.txt:42:10 found "langauge" a misspelling of "language"
4343
$ misspell -help
4444
Usage of misspell:
4545
-debug
46-
Debug matching, very slow
46+
Debug matching, very slow
47+
-dict string
48+
User defined corrections file path (.csv). CSV format: typo,fix
4749
-error
48-
Exit with 2 if misspelling found
50+
Exit with 2 if misspelling found
4951
-f string
50-
'csv', 'sqlite3' or custom Golang template for output
52+
'csv', 'sqlite3' or custom Golang template for output
5153
-i string
52-
ignore the following corrections, comma-separated
54+
ignore the following corrections, comma-separated
5355
-j int
54-
Number of workers, 0 = number of CPUs
56+
Number of workers, 0 = number of CPUs
5557
-legal
56-
Show legal information and exit
58+
Show legal information and exit
5759
-locale string
58-
Correct spellings using locale preferences for US or UK. Default is to use a neutral variety of English. Setting locale to US will correct the British spelling of 'colour' to 'color'
60+
Correct spellings using locale preferences for US or UK. Default is to use a neutral variety of English. Setting locale to US will correct the British spelling of 'colour' to 'color'
5961
-o string
60-
output file or [stderr|stdout|] (default "stdout")
61-
-q Do not emit misspelling output
62+
output file or [stderr|stdout|] (default "stdout")
63+
-q Do not emit misspelling output
6264
-source string
63-
Source mode: auto=guess, go=golang source, text=plain or markdown-like text (default "auto")
64-
-w Overwrite file with corrections (default is just to display)
65+
Source mode: text (default), go (comments only) (default "text")
66+
-v Show version and exit
67+
-w Overwrite file with corrections (default is just to display)
6568
```
6669

6770
## FAQ

cmd/misspell/main.go

+41-11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33

44
import (
55
"bytes"
6+
"encoding/csv"
67
"flag"
78
"fmt"
89
"io"
@@ -105,18 +106,20 @@ func worker(writeit bool, r *misspell.Replacer, mode string, files <-chan string
105106
//nolint:funlen,nestif,gocognit,gocyclo,maintidx // TODO(ldez) must be fixed.
106107
func main() {
107108
t := time.Now()
109+
108110
var (
109-
workers = flag.Int("j", 0, "Number of workers, 0 = number of CPUs")
110-
writeit = flag.Bool("w", false, "Overwrite file with corrections (default is just to display)")
111-
quietFlag = flag.Bool("q", false, "Do not emit misspelling output")
112-
outFlag = flag.String("o", "stdout", "output file or [stderr|stdout|]")
113-
format = flag.String("f", "", "'csv', 'sqlite3' or custom Golang template for output")
114-
ignores = flag.String("i", "", "ignore the following corrections, comma-separated")
115-
locale = flag.String("locale", "", "Correct spellings using locale preferences for US or UK. Default is to use a neutral variety of English. Setting locale to US will correct the British spelling of 'colour' to 'color'")
116-
mode = flag.String("source", "text", "Source mode: text (default), go (comments only)")
117-
debugFlag = flag.Bool("debug", false, "Debug matching, very slow")
118-
exitError = flag.Bool("error", false, "Exit with 2 if misspelling found")
119-
showVersion = flag.Bool("v", false, "Show version and exit")
111+
workers = flag.Int("j", 0, "Number of workers, 0 = number of CPUs")
112+
writeit = flag.Bool("w", false, "Overwrite file with corrections (default is just to display)")
113+
quietFlag = flag.Bool("q", false, "Do not emit misspelling output")
114+
outFlag = flag.String("o", "stdout", "output file or [stderr|stdout|]")
115+
format = flag.String("f", "", "'csv', 'sqlite3' or custom Golang template for output")
116+
ignores = flag.String("i", "", "ignore the following corrections, comma-separated")
117+
userDictPath = flag.String("dict", "", "User defined corrections file path (.csv). CSV format: typo,fix")
118+
locale = flag.String("locale", "", "Correct spellings using locale preferences for US or UK. Default is to use a neutral variety of English. Setting locale to US will correct the British spelling of 'colour' to 'color'")
119+
mode = flag.String("source", "text", "Source mode: text (default), go (comments only)")
120+
debugFlag = flag.Bool("debug", false, "Debug matching, very slow")
121+
exitError = flag.Bool("error", false, "Exit with 2 if misspelling found")
122+
showVersion = flag.Bool("v", false, "Show version and exit")
120123

121124
showLegal = flag.Bool("legal", false, "Show legal information and exit")
122125
)
@@ -140,6 +143,7 @@ func main() {
140143
Replacements: misspell.DictMain,
141144
Debug: *debugFlag,
142145
}
146+
143147
//
144148
// Figure out regional variations
145149
//
@@ -156,6 +160,32 @@ func main() {
156160
log.Fatalf("Unknown locale: %q", *locale)
157161
}
158162

163+
//
164+
// Load user defined words
165+
//
166+
if *userDictPath != "" {
167+
file, err := os.Open(*userDictPath)
168+
if err != nil {
169+
log.Fatalf("Failed to load user defined corrections: %v, err: %v", *userDictPath, err)
170+
}
171+
defer file.Close()
172+
173+
reader := csv.NewReader(file)
174+
reader.FieldsPerRecord = 2
175+
176+
data, err := reader.ReadAll()
177+
if err != nil {
178+
log.Fatalf("reading user defined corrections: %v", err)
179+
}
180+
181+
var userDict []string
182+
for _, row := range data {
183+
userDict = append(userDict, row...)
184+
}
185+
186+
r.AddRuleList(userDict)
187+
}
188+
159189
//
160190
// Stuff to ignore
161191
//

0 commit comments

Comments
 (0)