I am trying to search for keywords in sentences that are in an array. The array's data is coming from a user's input so there is no way to know what they will type. How can I do this and remember what keywords were saved for which sentence? Keywords could be any word such as (to, the, apache, koala, supercalifragilisticexpialidocious). I would like the computer to separate each sentence and examine them individually if possible.
func separateAllSentences() {
userInput.enumerateSubstrings(in: userInput.startIndex ..< userInput.endIndex, options: .bySentences) { userInput, _, _, _ in
if let sentence = userInput?.trimmingCharacters(in: .whitespacesAndNewlines), let lastCharacter = sentence.characters.last {
switch lastCharacter {
case ".":
self.statementsArray.append(sentence)
case "?":
self.questionsArray.append(sentence)
default:
self.unknownArray.append(sentence)
}
}
}
print("questions: \(questionsArray)")
print("statements: \(statementsArray)")
print("unknown: \(unknownArray)")
}