1
0
mirror of https://github.com/ru-de/faq.git synced 2024-11-23 02:42:19 +00:00
faq-de/pages/files/spell-checker.go
Evgeniy Sokolov 5ef8f27e0f gh-pages
2018-01-02 19:12:55 +01:00

70 lines
1.4 KiB
Go

package main
import (
"fmt"
"os"
"bufio"
"regexp"
"strings"
)
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
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"))
}
}
os.Exit(exitCode)
}
}
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]
typeResult, ok := types[resultType]
if !ok {
typeResult = &TypeResult{}
types[resultType] = typeResult
}
typeResult.results = append(typeResult.results, Result{line: line, word: strings.Trim(text[1:], " ")})
}
}
return types
}