英文:
Keyword search with negative keywords
问题
我对Go语言中关键字搜索的一个简单问题有疑问。
我想使用正向和负向关键字来搜索一个字符串。
以下是我的代码:
func keyword(itemTitle string, keywords string) bool {
splits := strings.Split(keywords, ",")
for _, item := range splits {
item = strings.TrimSpace(item)
fmt.Println(strings.ToUpper(itemTitle))
fmt.Println(strings.ToUpper(item))
if strings.Contains(item, "-") {
item = item[1:]
if strings.Contains(strings.ToUpper(itemTitle), strings.ToUpper(item)) {
return false
}
}
item = item[1:]
fmt.Println(strings.ToUpper(item))
if strings.Contains(strings.ToUpper(itemTitle), strings.ToUpper(item)) {
return true
}
}
return false
}
这是我的搜索方法:
func TestKeyword(t *testing.T) {
test1 := "Pokemon Nintendo Switch Cool Thing"
keywordTest1 := "+pokemon,-nintendo"
if keyword(test1, keywordTest1) {
fmt.Println("matched")
} else {
fmt.Println("test")
}
test2 := "Pokemon Cards Cool"
if keyword(test2, keywordTest1) {
fmt.Println("matched")
} else {
fmt.Println("test")
}
}
我的测试用例是:
我明白为什么它不起作用,因为"+amd"是切片中的第一个元素,当然会返回true,而不会测试其他的"-radeon",但我有点困惑该怎么办。
给出的输出是:
matched
matched
期望的输出是:
test
matched
英文:
I have a simple question about keywords searching in a Go.
I want to search a string using positive and negative keywords
func keyword(itemTitle string, keywords string) bool {
splits := strings.Split(keywords, ",")
for _, item := range splits {
item = strings.TrimSpace(item)
fmt.Println(strings.ToUpper(itemTitle))
fmt.Println(strings.ToUpper(item))
if strings.Contains(item,"-") {
item = item[1:]
if strings.Contains(strings.ToUpper(itemTitle), strings.ToUpper(item)) {
return false
}
}
item = item[1:]
fmt.Println(strings.ToUpper(item))
if strings.Contains(strings.ToUpper(itemTitle), strings.ToUpper(item)) {
return true
}
}
return false
}
heres my searcher method
func TestKeyword(t *testing.T) {
test1 := "Pokemon Nintendo Switch Cool Thing"
keywordTest1 := "+pokemon,-nintendo"
if keyword(test1, keywordTest1) {
fmt.Println("matched")
} else {
fmt.Println("test")
}
test2 := "Pokemon Cards Cool"
if keyword(test2, keywordTest1) {
fmt.Println("matched")
} else {
fmt.Println("test")
}
}
my test cases
i understand why its not working because +amd is the first in the slice and its ofc going to return true and not test any of the other like -radeon but im just kinda stumped on what todo.
Output given
matched
matched
Expected Output
test
matched
答案1
得分: 0
我更新了你的搜索功能,但保留了签名。
func keyword(itemTitle string, keywords string) bool {
a := strings.ToUpper(itemTitle)
b := strings.ToUpper(keywords)
keys := strings.Split(strings.Replace(b, "-", " ", -1), ",")
for _, key := range keys {
key = strings.TrimSpace(key)
if strings.Contains(a, key) {
return true
}
}
return false
}
我还更新了你的测试函数,添加了一个通过的测试和一个失败的测试,以查看它的工作原理。
func TestKeyword(t *testing.T) {
test1 := "Pokemon Nintendo Switch Cool Thing"
keywordTest1 := "+pokemon,-nintendo"
if keyword(test1, keywordTest1) {
t.Log("matched")
} else {
t.Fail()
}
test2 := "Pokemon Cards Cool"
if keyword(test2, keywordTest1) {
t.Log("matched")
} else {
t.Fail()
}
}
关于第二个测试失败的问题,如果需要,你可以通过正则表达式将带有+的关键字传递,以获取只包含字母数字字符的关键字。
英文:
I updated your search function but kept the signature
func keyword(itemTitle string, keywords string) bool {
a := strings.ToUpper(itemTitle)
b := strings.ToUpper(keywords)
keys := strings.Split(strings.Replace(b, "-", " ", -1), ",")
for _, key := range keys {
key = strings.TrimSpace(key)
if strings.Contains(a, key) {
return true
}
}
return false
}
And updated your test function with a passing test and a failed one to see how it works.
func TestKeyword(t *testing.T) {
test1 := "Pokemon Nintendo Switch Cool Thing"
keywordTest1 := "+pokemon,-nintendo"
if keyword(test1, keywordTest1) {
t.Log("matched")
} else {
t.Fail()
}
test2 := "Pokemon Cards Cool"
if keyword(test2, keywordTest1) {
t.Log("matched")
} else {
t.Fail()
}
}
Regarding the second test failing with a keyword with + on it, you could pass that through a regex to get only alphanumeric characters, if required.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论