示例#1
0
func TestMarks(tt *testing.T) {
	t := trie.NewTrie()
	trie.AddWord(t, "hello")
	trie.AddWord(t, "goodbye")
	trie.AddWord(t, "goodday")

	m := trie.DescendPath(t, "hello")
	m.Mark(10)
	if m.GetMark() != 10 {
		tt.Fail()
	}
}
示例#2
0
func TestReverseLookup(tt *testing.T) {
	t := trie.NewTrie()
	trie.AddWord(t, "hello")
	trie.AddWord(t, "goodbye")
	trie.AddWord(t, "goodday")

	m := trie.DescendPath(t, "hello")
	rev := trie.ReverseLookup(t, m)
	if rev != "hello" {
		tt.Fail()
	}
}
示例#3
0
func TestStartsWordDescend(tt *testing.T) {
	t := trie.NewTrie()
	trie.AddWord(t, "hello")
	trie.AddWord(t, "goodbye")
	trie.AddWord(t, "goodday")

	if !t.StartsWord('h' - 'a') {
		tt.Fail()
	}
	if !t.StartsWord('g' - 'a') {
		tt.Fail()
	}
	if t.StartsWord('x' - 'a') {
		tt.Fail()
	}

	th := t.Descend('h' - 'a')
	if th == nil {
		tt.Fail()
	}
	if th.IsWord() {
		tt.Fail()
	}
	to := th.Descend('e' - 'a').Descend('l' - 'a').Descend('l' - 'a').Descend('o' - 'a')
	if to == nil {
		tt.Fail()
	}
	if !to.IsWord() {
		tt.Fail()
	}

	t_hell := trie.DescendPath(t, "hell")
	if t_hell == nil {
		tt.Fail()
	}
	if t_hell.IsWord() {
		tt.Fail()
	}

	// this test fails, oddly, even though fmt.Println(t_hell.Descend('h' - 'a')) prints '<nil>'
	if t_hell.Descend('h'-'a') != nil {
		tt.Fail()
	}

	t_hello := t_hell.Descend('o' - 'a')
	if !t_hello.IsWord() {
		tt.Fail()
	}
}
示例#4
0
func main() {
	var err error

	T = trie.NewTrie()

	err = importWords(T, FILENAME)
	if err != nil {
		log.Fatalln(err.Error())
	} else {
		log.Printf("服务正在启动,监听端口: %d ...\n", PORT)
		http.HandleFunc("/", mainHandler)
		err = http.ListenAndServe(fmt.Sprintf(":%d", PORT), nil)
		if err != nil {
			log.Fatalln("启动失败: ", err.Error())
		}
	}
}
示例#5
0
func parseDict(dictfile string) *Trie.Trie { //the dict file shoud have this format: word-单词\n
	dict, err := ioutil.ReadFile(dictfile)
	// fmt.Println(string(dict))
	t := Trie.NewTrie()
	handleError(err)
	word := strings.Split(string(dict), "\n")
	word = word[:len(word)-1]
	// fmt.Println(len(word), word)
	fmt.Println("The dictionary is :")
	for _, v := range word {
		fmt.Println(v)
		k := strings.Split(v, "-")
		t.Add(k[0], k[1])
	}

	return t
}
示例#6
0
func boggleTrieFromFilename(filename string) *trie.Trie {
	f, err := os.Open(filename)
	if err != nil {
		fmt.Println(err)
		return nil
	}
	defer f.Close()

	r, err := bufio.NewReaderSize(f, 4*1024)
	if err != nil {
		fmt.Println(err)
		return nil
	}

	t := trie.NewTrie()
	for {
		line, isPrefix, err := r.ReadLine()
		if err == os.EOF {
			break
		}
		if isPrefix {
			fmt.Println("need more buffering!")
			return nil
		}
		if err != nil {
			fmt.Println("error! ", err)
			return nil
		}

		word := string(line)
		ok, boggleword := BogglifyWord(word)
		if ok {
			trie.AddWord(t, boggleword)
		}
	}

	return t
}
示例#7
0
func noTestAddWordIsWord(tt *testing.T) {
	t := trie.NewTrie()
	trie.AddWord(t, "hello")
	trie.AddWord(t, "goodbye")
	trie.AddWord(t, "goodday")

	if !trie.IsWord(t, "hello") {
		tt.Fail()
	}
	if !trie.IsWord(t, "goodbye") {
		tt.Fail()
	}
	if !trie.IsWord(t, "goodday") {
		tt.Fail()
	}
	if trie.IsWord(t, "h") {
		tt.Fail()
	}
	if trie.IsWord(t, "he") {
		tt.Fail()
	}
	if trie.IsWord(t, "hel") {
		tt.Fail()
	}
	if trie.IsWord(t, "hell") {
		tt.Fail()
	}
	if trie.IsWord(t, "goodby") {
		tt.Fail()
	}
	if trie.IsWord(t, "goodda") {
		tt.Fail()
	}
	if trie.IsWord(t, "z") {
		tt.Fail()
	}
}