【问题标题】:Swift: How to search for keywords in a sentenceSwift:如何在句子中搜索关键字
【发布时间】:2020-12-05 23:26:35
【问题描述】:

我正在尝试用 swift 在句子中进行关键字搜索。

例如给定

关键字 = [“黑色”、“包”、“爱”、“填充”]

Sentence1 = "一个装满爱的房子里有一个黑色的袋子"

Sentence2 = "我们在一家商店。柜台上有一个黑色的袋子"

Sentence3 = "今天的海洋美丽而可爱"

我想在每个句子中搜索所有关键字并返回包含所有关键字和不包含关键字的句子。所以输出应该

Sentence1 : 4 个关键字 Sentence2 : 3 个关键词 第三句:无

这是我解决这个问题的尝试

 var RawSentences = ["There is a black bag in a house filled with love", "We are in a shop. There is a black bag on the counter", " The ocean is beautiful and lovely today"]

 var keywords = ["black", "bag", "love", "filled"]

 for item in RawSentences {
        var matchkeywords: [String] = []
        
        for kword in keywords{
            
            if item.range(of:kword) != nil {
                print("Yes!!!! \(kword) is in \(generatedString)")
                matchkeywords.append(kword)
            }
        }
         
        print("There are \(String(matchkeywords.count)) keyword in \(item)")
        
       
    }

在 swift 中实现此功能的最佳方法是什么?

【问题讨论】:

  • 添加您的代码。向我们展示您迄今为止的尝试。
  • @Frankenstein 看我的代码
  • 什么是RawSearchDict?添加代码,使其可以在 Xcode-playgrounds 中运行,其他人可以轻松找到解决方案。
  • @Frankenstein 我已经简化它以捕获我的代码

标签: swift algorithm string-matching


【解决方案1】:

如果您只想匹配整个单词,则需要使用正则表达式并为关键字添加边界。您还可以使您的搜索大小写和变音符号不敏感:

let sentences = ["There is a black bag in a house filled with love",
                 "We are in a shop. There is a black bag on the counter",
                 "The ocean is beautiful and lovely today"]
let keywords = ["black", "bag", "love", "filled"]

var results: [String: [String]] = [:]
for sentence in sentences {
    for keyword in keywords {
        let escapedPattern = NSRegularExpression.escapedPattern(for: keyword)
        let pattern = "\\b\(escapedPattern)\\b"
        if sentence.range(of: pattern, options: [.regularExpression, .caseInsensitive, .diacriticInsensitive]) != nil {
            results[sentence, default: []].append(keyword)
        }
    }
}

print(results)  // ["There is a black bag in a house filled with love": ["black", "bag", "love", "filled"], "We are in a shop. There is a black bag on the counter": ["black", "bag"]]

如果您想知道句子中关键字的位置,您只需附加找到的范围而不是关键字:

var results: [String:[Range<String.Index>]] = [:]
for sentence in sentences {
   for keyword in keywords {
       let escapedPattern = NSRegularExpression.escapedPattern(for: keyword)
       let pattern = "\\b\(escapedPattern)\\b"
       if let range = sentence.range(of: pattern, options: [.regularExpression, .caseInsensitive, .diacriticInsensitive]) {
           results[sentence, default: []].append(range)
       }
   }
}

print(results)  // ["We are in a shop. There is a black bag on the counter": [Range(Swift.String.Index(_rawBits: 1900544)..<Swift.String.Index(_rawBits: 2228224)), Range(Swift.String.Index(_rawBits: 2293760)..<Swift.String.Index(_rawBits: 2490368))], "There is a black bag in a house filled with love": [Range(Swift.String.Index(_rawBits: 720896)..<Swift.String.Index(_rawBits: 1048576)), Range(Swift.String.Index(_rawBits: 1114112)..<Swift.String.Index(_rawBits: 1310720)), Range(Swift.String.Index(_rawBits: 2883584)..<Swift.String.Index(_rawBits: 3145728)), Range(Swift.String.Index(_rawBits: 2097152)..<Swift.String.Index(_rawBits: 2490368))]]

【讨论】:

  • 这是一个很好的解决方案!你能帮忙解释一下你的代码,尤其是你在用 NSRegularExpression 和模式做什么
  • @learner101 如果关键字没有在正则表达式中使用的特殊字符,则不需要这样做 "通过根据需要添加反斜杠转义符来返回字符串,以保护任何将匹配为模式元字符的字符。”
猜你喜欢
  • 1970-01-01
  • 2021-07-26
  • 2017-02-18
  • 1970-01-01
  • 1970-01-01
  • 2014-12-08
  • 1970-01-01
  • 2016-04-19
  • 2010-10-20
相关资源
最近更新 更多