// Convert the map into a flat list of entries, where each entry is // the key (a string of digits) followed by one or more words. func dictmap_to_slice(dm DictMap) DictSlice { return_value := make(DictSlice, 0) longest_length := 0 for key, val := range dm { e := new(Entry) e.Bag = bag.FromString(key) e.Words = make([]string, 0) for word, _ := range val { e.Words = append(e.Words, word) } if len(val) > longest_length { longest_length = len(val) fmt.Println(val) } return_value = append(return_value, *e) } // TODO -- maybe sort the return value in some interesting way (longest bags first, e.g.) return return_value }
func main() { flag.Parse() dictslice, error := anagrams.SnarfDict(*words_file) if error != nil { log.Fatal(error) } fmt.Printf("Number of somethings in the dictionary: %v\n", len(dictslice)) bag := bag.FromString(*input_string) if *cpuprofile != "" { f, err := os.Create(*cpuprofile) if err != nil { log.Fatal(err) } pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() } result := anagrams.Anagrams(dictslice, bag) log.Printf("%d anagrams of '%s'\n", len(result), *input_string) for _, a := range result { fmt.Println(a) } }
func snarfdict(reader *bufio.Reader) (DictMap, error) { log.Printf("Reading dictionary ... ") accum := make(DictMap, 50000) for { word, err := reader.ReadString('\n') if err != nil { // EOF, presumably break } word = strings.ToLower(strings.TrimSpace(word)) if !word_acceptable(word) { continue } key := bag.FromString(word).AsString() words, ok := accum[string(key)] if !ok { words = make(WordSet) accum[string(key)] = words } words[word] = true } log.Printf("Reading dictionary ... done") return accum, nil }
func BenchmarkOneRun(b *testing.B) { dictslice, error := SnarfDict("/usr/share/dict/words") if error != nil { log.Fatal(error) } Anagrams(dictslice, bag.FromString("Hemingway")) }