首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift:如何在句子中搜索关键字

Swift:如何在句子中搜索关键字
EN

Stack Overflow用户
提问于 2020-08-16 18:18:23
回答 1查看 276关注 0票数 0

我试图在一个句子中做一个关键字搜索与迅速。

例如,给定

关键词=“黑”、“袋”、“爱”、“填充”

Sentence1 =“满是爱的房子里有一个黑色的袋子”

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

Sentence3 =“今天的海洋是美丽而可爱的”

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

Sentence1 :4关键词Sentence2 :3关键词Sentence3 :无

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

代码语言:javascript
复制
 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)")
        
       
    }

以最快的方式实现这一目标的最佳方式是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-16 20:51:20

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

代码语言:javascript
复制
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)
        }
    }
}

代码语言:javascript
复制
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"]]

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

代码语言:javascript
复制
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))]]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63440468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档