Created
December 31, 2013 14:18
-
-
Save polachok/8197415 to your computer and use it in GitHub Desktop.
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 | |
// #include "cmarisa.h" | |
// #include <marisa/base.h> | |
// #cgo LDFLAGS: -lcmarisa -L/usr/local/lib | |
import "C" | |
import "os" | |
import "io" | |
import "log" | |
import "bufio" | |
import "strings" | |
type KeySet struct { | |
ptr *C.KeySet | |
} | |
type Trie struct { | |
ptr *C.Trie | |
} | |
type Key struct { | |
ptr *C.Key | |
} | |
type Agent struct { | |
ptr *C.Agent | |
} | |
func (key *Key) get_value() string { | |
return C.GoString(C.get_value(key.ptr)) | |
} | |
func NewAgent(query string) (*Agent) { | |
agent := new(Agent) | |
agent.ptr = C.agent_create() | |
C.set_query(agent.ptr, C.CString(query)) | |
return agent | |
} | |
func (agent *Agent) get_key() (*Key) { | |
key := new(Key) | |
key.ptr = C.get_key(agent.ptr) | |
return key | |
} | |
func NewKeySet() (*KeySet) { | |
keyset := new(KeySet) | |
keyset.ptr = C.keyset_create() | |
return keyset | |
} | |
func (keyset *KeySet) push(s string, prio int) { | |
cs := C.CString(s) | |
l := len([]byte(s)) | |
C.keyset_push(keyset.ptr, cs, C.int(l), C.int(prio)) | |
} | |
func NewTrie(options int) (*Trie) { | |
trie := new(Trie) | |
trie.ptr = C.trie_create() | |
return trie | |
} | |
func NewTrieFromKeyset(ks *KeySet, options int) (*Trie) { | |
trie := NewTrie(options) | |
C.trie_build(trie.ptr, ks.ptr, C.int(options)) | |
return trie | |
} | |
func (trie *Trie) load(path string) { | |
C.trie_load(trie.ptr, C.CString(path)) | |
} | |
func (trie *Trie) save(path string) { | |
C.trie_save(trie.ptr, C.CString(path)) | |
} | |
func (trie *Trie) predictive_search(agent *Agent) int { | |
return int(C.predictive_search(trie.ptr, agent.ptr)) | |
} | |
func main() { | |
if (false) { | |
file, err := os.Open("log") | |
if err != nil { | |
log.Fatal(err) | |
} | |
reader := bufio.NewReader(file) | |
keyset := NewKeySet() | |
for { | |
line, err := reader.ReadString('\n') | |
if err == io.EOF { | |
break | |
} | |
parts := strings.SplitAfterN(line, " ", 3) | |
keyset.push(parts[2], 0) | |
} | |
trie := NewTrieFromKeyset(keyset, 0) | |
trie.save("log.trie") | |
} else { | |
trie := NewTrie(0) | |
agent := NewAgent("/home/o/oleele") | |
trie.load("log.trie") | |
for { | |
if (trie.predictive_search(agent) != 0) { | |
key := agent.get_key() | |
s := key.get_value() | |
print(s) | |
} else { | |
break | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment