[英]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!
let res = reviews.filter { sentence in keywords.contains { sentence.lowercaseString.rangeOfString($0) != nil } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.