1
0
mirror of https://github.com/ru-de/faq.git synced 2024-11-23 19:02:19 +00:00
faq-de/files/spell-checker.go

70 lines
1.4 KiB
Go
Raw Normal View History

2016-01-18 23:38:49 +00:00
package main
import (
"fmt"
"os"
"bufio"
"regexp"
"strings"
2016-01-18 23:38:49 +00:00
)
type TypeResult struct {
results []Result
}
type Result struct {
line int
word string
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
types := parseHunspellOutput(scanner);
_, ok := types["&"]
if ok {
var dropCol = regexp.MustCompile(`^([^ ]+) \d+ \d+:(.*)$`)
var minimumWord = regexp.MustCompile(`^[^ ]{3}`)
exitCode := 0
2016-01-18 23:38:49 +00:00
for _, result := range types["&"].results {
if minimumWord.MatchString(result.word) {
exitCode = 1
fmt.Println("Строка " + fmt.Sprintf("%v", result.line) + ": " + dropCol.ReplaceAllString(result.word, "$1 >$2"))
}
2016-01-18 23:38:49 +00:00
}
os.Exit(exitCode)
2016-01-18 23:38:49 +00:00
}
}
func parseHunspellOutput(scanner *bufio.Scanner) map[string]*TypeResult {
line := 1;
types := make(map[string]*TypeResult)
scanner.Scan()
for scanner.Scan() {
text := scanner.Text()
if text == "" {
line++;
} else {
resultType := text[0:1]
2016-01-18 23:38:49 +00:00
typeResult, ok := types[resultType]
if !ok {
typeResult = &TypeResult{}
types[resultType] = typeResult
}
typeResult.results = append(typeResult.results, Result{line: line, word: strings.Trim(text[1:], " ")})
2016-01-18 23:38:49 +00:00
}
}
return types
2016-01-19 08:52:01 +00:00
}