关键词搜索与负面关键词

huangapple go评论114阅读模式
英文:

Keyword search with negative keywords

问题

我对Go语言中关键字搜索的一个简单问题有疑问。

我想使用正向和负向关键字来搜索一个字符串。

以下是我的代码:

  1. func keyword(itemTitle string, keywords string) bool {
  2. splits := strings.Split(keywords, ",")
  3. for _, item := range splits {
  4. item = strings.TrimSpace(item)
  5. fmt.Println(strings.ToUpper(itemTitle))
  6. fmt.Println(strings.ToUpper(item))
  7. if strings.Contains(item, "-") {
  8. item = item[1:]
  9. if strings.Contains(strings.ToUpper(itemTitle), strings.ToUpper(item)) {
  10. return false
  11. }
  12. }
  13. item = item[1:]
  14. fmt.Println(strings.ToUpper(item))
  15. if strings.Contains(strings.ToUpper(itemTitle), strings.ToUpper(item)) {
  16. return true
  17. }
  18. }
  19. return false
  20. }

这是我的搜索方法:

  1. func TestKeyword(t *testing.T) {
  2. test1 := "Pokemon Nintendo Switch Cool Thing"
  3. keywordTest1 := "+pokemon,-nintendo"
  4. if keyword(test1, keywordTest1) {
  5. fmt.Println("matched")
  6. } else {
  7. fmt.Println("test")
  8. }
  9. test2 := "Pokemon Cards Cool"
  10. if keyword(test2, keywordTest1) {
  11. fmt.Println("matched")
  12. } else {
  13. fmt.Println("test")
  14. }
  15. }

我的测试用例是:

我明白为什么它不起作用,因为"+amd"是切片中的第一个元素,当然会返回true,而不会测试其他的"-radeon",但我有点困惑该怎么办。

给出的输出是:

  1. matched
  2. matched

期望的输出是:

  1. test
  2. matched
英文:

I have a simple question about keywords searching in a Go.

I want to search a string using positive and negative keywords

  1. func keyword(itemTitle string, keywords string) bool {
  2. splits := strings.Split(keywords, ",")
  3. for _, item := range splits {
  4. item = strings.TrimSpace(item)
  5. fmt.Println(strings.ToUpper(itemTitle))
  6. fmt.Println(strings.ToUpper(item))
  7. if strings.Contains(item,"-") {
  8. item = item[1:]
  9. if strings.Contains(strings.ToUpper(itemTitle), strings.ToUpper(item)) {
  10. return false
  11. }
  12. }
  13. item = item[1:]
  14. fmt.Println(strings.ToUpper(item))
  15. if strings.Contains(strings.ToUpper(itemTitle), strings.ToUpper(item)) {
  16. return true
  17. }
  18. }
  19. return false
  20. }

heres my searcher method

  1. func TestKeyword(t *testing.T) {
  2. test1 := "Pokemon Nintendo Switch Cool Thing"
  3. keywordTest1 := "+pokemon,-nintendo"
  4. if keyword(test1, keywordTest1) {
  5. fmt.Println("matched")
  6. } else {
  7. fmt.Println("test")
  8. }
  9. test2 := "Pokemon Cards Cool"
  10. if keyword(test2, keywordTest1) {
  11. fmt.Println("matched")
  12. } else {
  13. fmt.Println("test")
  14. }
  15. }

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

  1. matched
  2. matched

Expected Output

  1. test
  2. matched

答案1

得分: 0

我更新了你的搜索功能,但保留了签名。

  1. func keyword(itemTitle string, keywords string) bool {
  2. a := strings.ToUpper(itemTitle)
  3. b := strings.ToUpper(keywords)
  4. keys := strings.Split(strings.Replace(b, "-", " ", -1), ",")
  5. for _, key := range keys {
  6. key = strings.TrimSpace(key)
  7. if strings.Contains(a, key) {
  8. return true
  9. }
  10. }
  11. return false
  12. }

我还更新了你的测试函数,添加了一个通过的测试和一个失败的测试,以查看它的工作原理。

  1. func TestKeyword(t *testing.T) {
  2. test1 := "Pokemon Nintendo Switch Cool Thing"
  3. keywordTest1 := "+pokemon,-nintendo"
  4. if keyword(test1, keywordTest1) {
  5. t.Log("matched")
  6. } else {
  7. t.Fail()
  8. }
  9. test2 := "Pokemon Cards Cool"
  10. if keyword(test2, keywordTest1) {
  11. t.Log("matched")
  12. } else {
  13. t.Fail()
  14. }
  15. }

关于第二个测试失败的问题,如果需要,你可以通过正则表达式将带有+的关键字传递,以获取只包含字母数字字符的关键字。

英文:

I updated your search function but kept the signature

  1. func keyword(itemTitle string, keywords string) bool {
  2. a := strings.ToUpper(itemTitle)
  3. b := strings.ToUpper(keywords)
  4. keys := strings.Split(strings.Replace(b, "-", " ", -1), ",")
  5. for _, key := range keys {
  6. key = strings.TrimSpace(key)
  7. if strings.Contains(a, key) {
  8. return true
  9. }
  10. }
  11. return false
  12. }

And updated your test function with a passing test and a failed one to see how it works.

  1. func TestKeyword(t *testing.T) {
  2. test1 := "Pokemon Nintendo Switch Cool Thing"
  3. keywordTest1 := "+pokemon,-nintendo"
  4. if keyword(test1, keywordTest1) {
  5. t.Log("matched")
  6. } else {
  7. t.Fail()
  8. }
  9. test2 := "Pokemon Cards Cool"
  10. if keyword(test2, keywordTest1) {
  11. t.Log("matched")
  12. } else {
  13. t.Fail()
  14. }
  15. }

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.

huangapple
  • 本文由 发表于 2021年12月10日 04:29:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/70296328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定