0

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)")
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
ThatGuy
  • 31
  • 6

3 Answers3

1

easier:

let keywords  = ["and", "but", "etc"]
let sentences = ["The owl and the butterfly.", "Fine words butter no parsnips.", "And yet more sentences, etc."]

sentences.map({ sentence in 
    (sentence: sentence, tags: keywords.filter({ sentence.containsString($0) }))
})

result:

[("The owl and the butterfly.", ["and", "but"]), 
 ("Fine words butter no parsnips.", ["but"]), 
 ("And yet more sentences, etc.", ["etc"])]
zepar
  • 155
  • 7
  • 1
    No need to use `flatMap(_:)`, as the transform you're applying doesn't return an optional or a sequence. Just use `map(_:)`. – Hamish Nov 24 '16 at 00:30
0

This quick (version 0) solution will match "but" to "butterfly" (I'll solving that to you) but it still illustrates the basic principle. Iterate through the keywords and the sentences and record the matches found as a pair of numbers indicating the keyword and the sentence.

let keywords  = ["and", "but", "etc"]
let sentences = ["The owl and the butterfly.", "Fine words butter no parsnips.", "And yet more sentences, etc."]

var matches = [(Int, Int)]()
for keyIndex in 0..<keywords.count {
    for sentenceIndex in 0..<sentences.count {
        if sentences[sentenceIndex].lowercased().contains(keywords[keyIndex].lowercased()) {
            matches.append((keyIndex, sentenceIndex))
        }
    }
}
print(matches)
Vince O'Sullivan
  • 2,611
  • 32
  • 45
0

Maybe create an object for each Sentence? With properties for the sentence String and an array of Strings that have been matched to the sentence. So when you're appending each sentence to its' corresponding array you create an object instead.

class Sentence {
  var sentence: String?
  var stringArray: [String] = []
}

Use this method https://stackoverflow.com/a/25523578/3410964 for checking if the sentence String contains the String you're after.

func checkForString(stringToFind: String, sentenceObjects: [Sentence]) -> [Sentence] {
  for sentenceObject in sentenceObjects {
     if (sentenceObject.sentence.contains(stringToFind) {
        sentenceObject.stringArray.append(stringToFind)
     }
  }
  return sentenceObjects
}

This would then return an array of sentence objects that would each have an array of strings that had been matched.

Hope I've understood your question right!

Community
  • 1
  • 1
Niamh
  • 56
  • 1
  • 7