-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
108 lines (89 loc) · 2.05 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import "os"
import "fmt"
import "bufio"
import "regexp"
import "strings"
var lean bool = false
const longestWord int = 45
func checkError (e error) {
if e != nil {
panic(e)
}
}
func isAlpha(str string) bool {
for i := range str {
if str[i] < 'A' || str[i] > 'z' {
return false
} else if str[i] > 'Z' && str[i] < 'a' {
return false
}
}
return true
}
func main() {
// check command line arguments' count
args := os.Args
argLength := len(args)
if argLength > 4 || argLength < 3 || (argLength == 4 && args[3] != "lean") {
fmt.Println("Usage: $ ./spellcheck text dictionary [lean]")
os.Exit(2)
}
// evaluate args
// decide which dictionary & text will be loaded
// set to lean mode (optional)
var dictionary string = args[2]
var textFile string = args[1]
if argLength == 4{
lean = true
}
// load the dictionary
var loaded bool = load(dictionary)
if !loaded {
fmt.Println("Unable to load", dictionary)
os.Exit(1)
}
fmt.Println("\nMISSPELLED WORDS\n")
// prepare to spellcheck
words, misspellings := 0, 0
text, err := os.Open(textFile)
checkError(err)
scanner := bufio.NewScanner(text)
scanner.Split(bufio.ScanWords)
if lean {
for scanner.Scan() {
line := scanner.Text()
if !check(line){
fmt.Println(line)
misspellings += 1
}
words += 1
}
} else {
for scanner.Scan() {
line := scanner.Text()
// ignore words with numbers like MS Word
match, err := regexp.MatchString("[0-9]+", line)
checkError(err)
// reconstruct the word to take only alphabetic characters and apostrophe
var word string
for i := 0; i < len(line); i++ {
if isAlpha(string(line[i])) || (line[i] == '\'') {
word = word + string(line[i])
}
}
word = strings.TrimSpace(word)
// ignore too long words
if !match && len(word) <= longestWord{
if !check(word) && word != ""{
fmt.Println(word)
misspellings += 1
}
}
words += 1
}
}
fmt.Println("\nWORDS MISSPELLED:", misspellings)
fmt.Println("WORDS IN DICTONARY:", size())
fmt.Println("WORDS IN TEXT:", words)
}