/* Copyright (C) 2014, 2015 by Alexandru Cojocaru */ /* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package main import ( "bufio" "bytes" "fmt" "log" "os" ) type pair [2]rune var words = make(map[pair]uint) func insertWord(word []byte) { runes := bytes.Runes(word) if len(runes) == 1 { return } for i := 0; i < len(runes)-1; i++ { p := pair{runes[i], runes[i+1]} words[p] += 1 } } func checkSpelling(word []byte) { runes := bytes.Runes(word) for i := 0; i < len(runes)-1; i++ { p := pair{runes[i], runes[i+1]} if words[p] < 100 { fmt.Printf("%s [mispelled]\n", string(runes)) return } } fmt.Printf("%s [correct]\n", string(runes)) } func foreachLine(path string, cb func([]byte)) { f, err := os.Open(path) if err != nil { log.Fatal(err) } defer f.Close() scanner := bufio.NewScanner(f) for scanner.Scan() { cb(scanner.Bytes()) } if scanner.Err() != nil { log.Fatal(scanner.Err()) } } func main() { log.SetFlags(log.Lshortfile) foreachLine("1922_wordlist.txt", insertWord) foreachLine("1922_input.txt", checkSpelling) }