Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add an option to allow words which consists of unique characters. #19

Merged
merged 2 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ var queryCmd = &cobra.Command{
}
fmt.Println("port:", port)

key, err := cmd.Flags().GetString("key")
if err != nil {
panic(err)
}
fmt.Println("key:", key)

include, err := cmd.Flags().GetString("include")
if err != nil {
panic(err)
Expand All @@ -46,14 +52,14 @@ var queryCmd = &cobra.Command{
}
fmt.Println("exclude:", exclude)

key, err := cmd.Flags().GetString("key")
uniq, err := cmd.Flags().GetBool("uniq")
if err != nil {
panic(err)
}
fmt.Println("key:", key)
fmt.Println("uniq:", uniq)

words := service.ClientQuery(address+":"+strconv.Itoa(port),
include, exclude, key)
key, include, exclude, uniq)
for _, word := range words {
fmt.Println(word)
}
Expand All @@ -74,7 +80,8 @@ func init() {
// queryCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
queryCmd.Flags().StringP("address", "a", "localhost", "The IP address to connect")
queryCmd.Flags().IntP("port", "p", 8080, "The port number to connect")
queryCmd.Flags().StringP("include", "i", "", "Included characters (can contain duplicated characters)")
queryCmd.Flags().StringP("exclude", "e", "", "Excluded characters (can contain duplicated characters)")
queryCmd.Flags().StringP("key", "k", "", "Key of the query (eg. \"sp...\" can match strings like \"spawn\", \"speak\", \"spray\", and so on)")
queryCmd.Flags().StringP("include", "i", "", "Included characters (can contain duplicated characters)")
queryCmd.Flags().StringP("exclude", "e", "", "Excluded characters")
queryCmd.Flags().BoolP("uniq", "u", false, "Allow words only consisting of unique characters")
}
4 changes: 2 additions & 2 deletions dictionary/dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,6 @@ func (dict *Dictionary) Load(inputFile string) {
}
}

func (dict *Dictionary) Query(key string, include string, exclude string) []string {
return dict.tr.Query(key, include, exclude)
func (dict *Dictionary) Query(key string, include string, exclude string, uniq bool) []string {
return dict.tr.Query(key, include, exclude, uniq)
}
29 changes: 19 additions & 10 deletions pb/wolper.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions proto/wolper.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ message SearchRequest {
string key = 1;
string include = 2;
string exclude = 3;
bool uniq = 4;
}

message SearchResponse {
Expand Down
3 changes: 2 additions & 1 deletion service/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"google.golang.org/grpc"
)

func ClientQuery(addrAndPort, include, exclude, key string) []string {
func ClientQuery(addrAndPort, key, include, exclude string, uniq bool) []string {
conn, err := grpc.Dial(
addrAndPort,
grpc.WithInsecure(),
Expand All @@ -34,6 +34,7 @@ func ClientQuery(addrAndPort, include, exclude, key string) []string {
Key: key,
Include: include,
Exclude: exclude,
Uniq: uniq,
}

result, err := client.Query(ctx, &searchRequest)
Expand Down
5 changes: 3 additions & 2 deletions service/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ func (wssi *WolperServiceServerImpl) Init(input string) {
}

func (wssi *WolperServiceServerImpl) Query(ctx context.Context, sreq *pb.SearchRequest) (*pb.SearchResponse, error) {
fmt.Printf("Query requested. (key = \"%v\", include = \"%v\", exclude = \"%v\")\n", sreq.Key, sreq.Include, sreq.Exclude)
fmt.Printf("Query requested. (key = \"%v\", include = \"%v\", exclude = \"%v\", uniq = %v)\n",
sreq.Key, sreq.Include, sreq.Exclude, sreq.Uniq)
var result pb.SearchResponse
result.Words = wssi.dict.Query(sreq.Key, sreq.Include, sreq.Exclude)
result.Words = wssi.dict.Query(sreq.Key, sreq.Include, sreq.Exclude, sreq.Uniq)
return &result, nil
}
8 changes: 4 additions & 4 deletions service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ var _ = Describe("Service", func() {

// Start client
words := service.ClientQuery("localhost:"+strconv.Itoa(port),
"", "", "crane")
"crane", "", "", false)
Expect(findWord("crane", words)).To(BeTrue())
Expect(findWord("hello", words)).To(BeFalse())

words = service.ClientQuery("localhost:"+strconv.Itoa(port),
"a", "x", ".r..e")
".r..e", "a", "x", false)
Expect(findWord("crane", words)).To(BeTrue())

words = service.ClientQuery("localhost:"+strconv.Itoa(port),
"", "", ".oda.")
".oda.", "", "", false)
Expect(findWord("today", words)).To(BeTrue())
})
})
Expand Down Expand Up @@ -98,7 +98,7 @@ var _ = Describe("Service", func() {

// Start client
words := service.ClientQuery("localhost:"+strconv.Itoa(clientPort),
"", "", "crane")
"crane", "", "", false)

Expect(words).To(BeNil())
})
Expand Down
13 changes: 7 additions & 6 deletions trie/tree_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func deleteChar(s []byte, c byte) []byte {
}
}

func (treeTrie *TreeTrie) query(key string, include string, exclude string, current string) []string {
func (treeTrie *TreeTrie) query(key string, include string, exclude string, uniq bool, current string) []string {
if key == "" {
if treeTrie.terminal && include == "" {
return []string{current}
Expand All @@ -39,21 +39,22 @@ func (treeTrie *TreeTrie) query(key string, include string, exclude string, curr
currentNode := treeTrie.child
result := make([]string, 0)
for currentNode != nil {
if !strings.Contains(exclude, string(currentNode.c)) {
if !strings.Contains(exclude, string(currentNode.c)) &&
!(uniq && strings.Contains(current, string(currentNode.c))) {
newInclude := include
if firstChar == '.' {
if strings.Contains(include, string(currentNode.c)) {
newInclude = string(deleteChar([]byte(include), currentNode.c))
}
current += string(currentNode.c)
result = append(result, currentNode.query(key, newInclude, exclude, current)...)
result = append(result, currentNode.query(key, newInclude, exclude, uniq, current)...)
current = current[:len(current)-1]
} else if currentNode.c == firstChar {
if strings.Contains(include, string(currentNode.c)) {
newInclude = string(deleteChar([]byte(include), currentNode.c))
}
current += string(currentNode.c)
result = append(result, currentNode.query(key, newInclude, exclude, current)...)
result = append(result, currentNode.query(key, newInclude, exclude, uniq, current)...)
break
}
}
Expand All @@ -62,8 +63,8 @@ func (treeTrie *TreeTrie) query(key string, include string, exclude string, curr
return result
}

func (treeTrie *TreeTrie) Query(key string, include string, exclude string) []string {
return treeTrie.query(key, include, exclude, "")
func (treeTrie *TreeTrie) Query(key string, include string, exclude string, uniq bool) []string {
return treeTrie.query(key, include, exclude, uniq, "")
}

func (treeTrie *TreeTrie) Add(key string) {
Expand Down
Loading