Skip to content

Commit

Permalink
feat: add option for user defined dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Apr 18, 2024
1 parent d73f40e commit 913a8c3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 22 deletions.
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,28 @@ your.txt:42:10 found "langauge" a misspelling of "language"
$ misspell -help
Usage of misspell:
-debug
Debug matching, very slow
Debug matching, very slow
-dict string
User defined corrections file path (.csv). CSV format: typo,fix
-error
Exit with 2 if misspelling found
Exit with 2 if misspelling found
-f string
'csv', 'sqlite3' or custom Golang template for output
'csv', 'sqlite3' or custom Golang template for output
-i string
ignore the following corrections, comma-separated
ignore the following corrections, comma-separated
-j int
Number of workers, 0 = number of CPUs
Number of workers, 0 = number of CPUs
-legal
Show legal information and exit
Show legal information and exit
-locale string
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'
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'
-o string
output file or [stderr|stdout|] (default "stdout")
-q Do not emit misspelling output
output file or [stderr|stdout|] (default "stdout")
-q Do not emit misspelling output
-source string
Source mode: auto=guess, go=golang source, text=plain or markdown-like text (default "auto")
-w Overwrite file with corrections (default is just to display)
Source mode: text (default), go (comments only) (default "text")
-v Show version and exit
-w Overwrite file with corrections (default is just to display)
```

## FAQ
Expand Down
52 changes: 41 additions & 11 deletions cmd/misspell/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main

import (
"bytes"
"encoding/csv"
"flag"
"fmt"
"io"
Expand Down Expand Up @@ -105,18 +106,20 @@ func worker(writeit bool, r *misspell.Replacer, mode string, files <-chan string
//nolint:funlen,nestif,gocognit,gocyclo,maintidx // TODO(ldez) must be fixed.
func main() {
t := time.Now()

var (
workers = flag.Int("j", 0, "Number of workers, 0 = number of CPUs")
writeit = flag.Bool("w", false, "Overwrite file with corrections (default is just to display)")
quietFlag = flag.Bool("q", false, "Do not emit misspelling output")
outFlag = flag.String("o", "stdout", "output file or [stderr|stdout|]")
format = flag.String("f", "", "'csv', 'sqlite3' or custom Golang template for output")
ignores = flag.String("i", "", "ignore the following corrections, comma-separated")
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'")
mode = flag.String("source", "text", "Source mode: text (default), go (comments only)")
debugFlag = flag.Bool("debug", false, "Debug matching, very slow")
exitError = flag.Bool("error", false, "Exit with 2 if misspelling found")
showVersion = flag.Bool("v", false, "Show version and exit")
workers = flag.Int("j", 0, "Number of workers, 0 = number of CPUs")
writeit = flag.Bool("w", false, "Overwrite file with corrections (default is just to display)")
quietFlag = flag.Bool("q", false, "Do not emit misspelling output")
outFlag = flag.String("o", "stdout", "output file or [stderr|stdout|]")
format = flag.String("f", "", "'csv', 'sqlite3' or custom Golang template for output")
ignores = flag.String("i", "", "ignore the following corrections, comma-separated")
userDictPath = flag.String("dict", "", "User defined corrections file path (.csv). CSV format: typo,fix")
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'")
mode = flag.String("source", "text", "Source mode: text (default), go (comments only)")
debugFlag = flag.Bool("debug", false, "Debug matching, very slow")
exitError = flag.Bool("error", false, "Exit with 2 if misspelling found")
showVersion = flag.Bool("v", false, "Show version and exit")

showLegal = flag.Bool("legal", false, "Show legal information and exit")
)
Expand All @@ -140,6 +143,7 @@ func main() {
Replacements: misspell.DictMain,
Debug: *debugFlag,
}

//
// Figure out regional variations
//
Expand All @@ -156,6 +160,32 @@ func main() {
log.Fatalf("Unknown locale: %q", *locale)
}

//
// Load user defined words
//
if *userDictPath != "" {
file, err := os.Open(*userDictPath)
if err != nil {
log.Fatalf("Failed to load user defined corrections: %v, err: %v", *userDictPath, err)
}
defer file.Close()

reader := csv.NewReader(file)
reader.FieldsPerRecord = 2

data, err := reader.ReadAll()
if err != nil {
log.Fatalf("reading user defined corrections: %v", err)
}

var userDict []string
for _, row := range data {
userDict = append(userDict, row...)
}

r.AddRuleList(userDict)
}

//
// Stuff to ignore
//
Expand Down

0 comments on commit 913a8c3

Please sign in to comment.