Example #1
0
func (b *Boggler) Hit(x, y, length uint, t *trie.Trie) {
	idx := x*4 + y
	if b.used&(1<<idx) == 0 {
		cc := uint8(b.bd[idx])
		if t.StartsWord(cc) {
			b.DoDFS(idx, length, t.Descend(cc))
		}
	}
}
Example #2
0
//导入过滤词库
func importWords(T *trie.Trie, file string) (err error) {
	rd, err := os.Open(file)
	if err != nil {
		return
	}
	defer rd.Close()
	r := bufio.NewReader(rd)
	for {
		line, isPrefix, e := r.ReadLine()
		if e != nil {
			if e != io.EOF {
				err = e
			}
			break
		}
		if isPrefix {
			continue
		}
		if word := strings.TrimSpace(string(line)); word != "" {
			T.Add(word)
		}
	}
	return
}
Example #3
0
func main() {
	dt := make(map[string]float64)

	for i := 0; i < 20; i++ {
		k := strconv.Itoa(i)
		dt[k] = float64(i)
	}

	t := new(trie.Trie)
	keys := make([]string, 0, 1)
	for k, _ := range dt {
		keys = append(keys, k)
	}

	sort.Strings(keys)
	fmt.Println(keys)
	values := make([]float64, 0, 1)

	for _, m := range keys {
		values = append(values, dt[m])
	}

	fmt.Println(values)

	t.BuildStringFloat(keys, values)
	d := t.GetArray()
	for i, dl := range d {
		fmt.Println(i, dl)
	}
	for _, k := range keys {
		b, e := t.FindString(k)
		if e != nil {
			fmt.Println(e)
		} else {
			fmt.Println(k, b.Value())
		}
	}

}
Example #4
0
func (b *Boggler) DoDFS(i, length uint, t *trie.Trie) {
	//fmt.Printf("i=%d, len=%d, word=%s\n", i, length, trie.ReverseLookup(b.t, t))
	c := b.bd[i]
	b.used ^= (1 << i)
	if c == kQ {
		length += 2
	} else {
		length += 1
	}

	if t.IsWord() {
		// fmt.Println("Found ", trie.ReverseLookup(b.t, t))
		if t.GetMark() != b.counter {
			t.Mark(b.counter)
			b.score += kWordScores[length]
		}
	}

	l := length
	switch i {
	case 0*4 + 0:
		b.Hit(0, 1, l, t)
		b.Hit(1, 0, l, t)
		b.Hit(1, 1, l, t)
	case 0*4 + 1:
		b.Hit(0, 0, l, t)
		b.Hit3y(1, 0, l, t)
		b.Hit(0, 2, l, t)
	case 0*4 + 2:
		b.Hit(0, 1, l, t)
		b.Hit3y(1, 1, l, t)
		b.Hit(0, 3, l, t)
	case 0*4 + 3:
		b.Hit(0, 2, l, t)
		b.Hit(1, 2, l, t)
		b.Hit(1, 3, l, t)

	case 1*4 + 0:
		b.Hit(0, 0, l, t)
		b.Hit(2, 0, l, t)
		b.Hit3x(0, 1, l, t)
	case 1*4 + 1:
		b.Hit8(1, 1, l, t)
	case 1*4 + 2:
		b.Hit8(1, 2, l, t)
	case 1*4 + 3:
		b.Hit3x(0, 2, l, t)
		b.Hit(0, 3, l, t)
		b.Hit(2, 3, l, t)

	case 2*4 + 0:
		b.Hit(1, 0, l, t)
		b.Hit(3, 0, l, t)
		b.Hit3x(1, 1, l, t)
	case 2*4 + 1:
		b.Hit8(2, 1, l, t)
	case 2*4 + 2:
		b.Hit8(2, 2, l, t)
	case 2*4 + 3:
		b.Hit3x(1, 2, l, t)
		b.Hit(1, 3, l, t)
		b.Hit(3, 3, l, t)

	case 3*4 + 0:
		b.Hit(2, 0, l, t)
		b.Hit(2, 1, l, t)
		b.Hit(3, 1, l, t)
	case 3*4 + 1:
		b.Hit3y(2, 0, l, t)
		b.Hit(3, 0, l, t)
		b.Hit(3, 2, l, t)
	case 3*4 + 2:
		b.Hit3y(2, 1, l, t)
		b.Hit(3, 1, l, t)
		b.Hit(3, 3, l, t)
	case 3*4 + 3:
		b.Hit(2, 2, l, t)
		b.Hit(3, 2, l, t)
		b.Hit(2, 3, l, t)
	}

	b.used ^= (1 << i)
}