Created
January 5, 2016 11:39
-
-
Save dragosperca/2d5e2f04816c639c4e7d to your computer and use it in GitHub Desktop.
Trie in Go test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/MathieuTurcotte/go-trie/gtrie" | |
"log" | |
"os" | |
"strings" | |
"bufio" | |
"io" | |
"math/rand" | |
"time" | |
) | |
func main() { | |
log.Println("starting procedure") | |
words_dictionary := readWords("words.txt") // 610000 words https://www.dropbox.com/s/5uiduyiddhk4vr1/words.txt?dl=0 | |
trie, err := gtrie.Create(words_dictionary) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("\nthe entire dictionary is", len(words_dictionary), "words\n") | |
shuffled_dictionary := words_dictionary | |
shuffle(shuffled_dictionary) | |
var slice_dictionary []string = shuffled_dictionary[1:25000] | |
// fmt.Println(slice_dictionary, "\n") | |
for _, word := range slice_dictionary { | |
if !trie.Accepts(word) { | |
log.Println(word, "was not found") | |
} | |
} | |
log.Println("\nprocedure completed") | |
} | |
func readWords(filename string) (words []string) { | |
file, err := os.Open(filename) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer file.Close() | |
reader := bufio.NewReader(file) | |
for { | |
word, rerr := reader.ReadString('\n') | |
if rerr != nil { | |
if rerr == io.EOF { | |
break | |
} else { | |
log.Fatal(err) | |
} | |
} | |
words = append(words, strings.TrimSpace(word)) | |
} | |
return | |
} | |
func shuffle(arr []string) { | |
t := time.Now() | |
rand.Seed(int64(t.Nanosecond())) // no shuffling without this line | |
for i := len(arr) - 1; i > 0; i-- { | |
j := rand.Intn(i) | |
arr[i], arr[j] = arr[j], arr[i] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment