简体   繁体   English

如何检查数组中的句子在Swift中是否包含确切的单词?

[英]How can I check if sentences in array contains exact words in Swift?

For example, I have the following arrays: 例如,我有以下数组:

var reviews = ["Skating is good in Austria", 
"I loved the Spanish food, it had so many varieties and 
it was super super delicious. The price was a little bit high but 
it was worth it. People who don't like spicy food might need 
to think twice as it could be a little bit problematic for them.", 
"I didn’t like the Indian food!", 
"People were really friendly, I enjoyed being there."]

var words = ["Skating", "Food", "Climbing"]

Now I want to check, if these reviews contain one of my words. 现在,我想检查一下这些评论是否包含我的一句话。

The challenge is: One or more reviews can contain these words and I want to find these reviews, add them into an array and print. 面临的挑战是:一个或多个评论可以包含这些单词,我想找到这些评论,并将它们添加到数组中并打印。

For this I do: 为此,我这样做:

var arr = [String]()

for review in reviews {
    for word in words {
        if review.containsString(word) {
            arr.append(review)
        }
    }
    print(arr)
}

but that prints only the first sentence: 但这仅显示第一句话:

["Skating is good in Austria"]

So, I want to check, if one or more reviews, contains on of these words from the array, then to put ALL these matched reviews into an arr array. 因此,我想检查一个或多个评论是否包含数组中的这些单词,然后将所有这些匹配的评论放入arr数组中。

Can anyone help me with it? 有人可以帮我吗? I'm confused why it take just one review and stop after 我很困惑为什么只需要进行一次审核并在之后停止

You need to move the print sentence out of your loop. 您需要将print语句移出循环。

Like this: 像这样:

var arr = [String]()

for review in reviews {
    for word in words {
        if review.containsString(word) {
            arr.append(review)
        }
    }
}
print(arr)

Also, if you don't want to get duplicate reviews, I would use a set instead of an array: var arr = Set<String>() 另外,如果您不想得到重复的评论,我将使用set而不是数组: var arr = Set<String>()

Also, it's probably the case that you need a case insensitive string comparison. 另外,可能是需要区分大小写的字符串比较的情况。 Change Food by food in your words array and try again. 在您的单词数组中按food更改Food ,然后重试。

To get a full case insensitive loop working, try this: 要使完整的不区分大小写的循环正常工作,请尝试以下操作:

for review in reviews {
    for word in words {
        if review.lowercaseString.containsString(word.lowercaseString) {
            arr.append(review)
        }
    }
}
print(arr)

If you want ignore case with string just try like this way using filter method of array , it will reduce your code of looping. 如果您想忽略字符串的大小写,只需使用array filter方法以这种方式尝试,它将减少循环代码。

var predicateString = [String]()
for word in words {
    predicateString(String(format:"SELF CONTAINS[cd] %@", word)
}
let predicate = NSPredicate(format: "%@",predicateString.joined(separator: " || "));
let filteredArray = reviews.filter { predicate.evaluateWithObject($0) };
print(filteredArray)

You can use Functional Programming 您可以使用函数式编程

First of all lets create an array of lowercase keywords (I assume you want case insensitive matching). 首先,让我们创建一个小写关键字数组(我假设您要区分大小写)。

let keywords = words.map { $0.lowercaseString }

Next 下一个

let matchingSentences = reviews.filter { sentence  in
    let lowerCaseSentence = sentence.lowercaseString
    return keywords.contains { keyword in sentence.rangeOfString(keyword) != nil }
}

Result 结果

matchingSentences.forEach { print($0) }

Skating is good in Austria
I loved the Spanish food, it had so many varieties and it was super super delicious. The price was a little bit high but it was worth it. People who don't like spicy food might need to think twice as it could be a little bit problematic for them.
I didn’t like the Indian food!

Shorter version 短版

let res = reviews.filter { sentence in keywords.contains { sentence.lowercaseString.rangeOfString($0) != nil } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在数组中创建数组,然后检查它是否包含字符串。 (迅速) - How to create an array in an array, then check if it contains a string. (SWIFT) iOS Swift:符合协议的对象数组:如何检查数组是否包含 - iOS Swift: Array of Objects conforming to a Protocol: How to check if array contains 我将如何从一系列不同的单词或句子中淡入淡出? - How would i go about fading in and out from an array of different words or sentences? 如何检查Parse中的PFRelation是否包含与Swift中完全相同的数组 - How to check if PFRelation in Parse contains exactly same array in Swift 检查一个数组是否包含 swift 中另一个数组的元素 - Check if an array contains elements of another array in swift 如何快速检查 url 字符串是否包含单词? - How do i check if url string contains a word in swift? iOS,Swift:如何保存包含自定义对象的数组,以便可以加载以进行重用? - iOS, Swift: How to save an array that contains custom objects so that I can be loaded for reuse? 检查数组是否包含Swift中的索引值 - Check if array contains an index value in Swift 检查数组是否包含Swift中另一个的所有元素 - Check if an array contains all elements of another in Swift 如何制作一系列句子? - How do I make an array of sentences?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM