Created
July 25, 2020 22:12
-
-
Save rr-codes/522ac5727754335e3b39068860d3789e to your computer and use it in GitHub Desktop.
A String extension to filter substrings based on parts of speech
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import NaturalLanguage | |
| extension String { | |
| /// Extracts the substrings of this string which match any of the specified `partsOfSpeech` | |
| /// | |
| /// For example, | |
| /// | |
| /// let string = "John's anniversary in Greece" | |
| /// let filtered = string.filter(by: [.noun, .placeName]) // ["anniversary", "Greece"] | |
| /// | |
| /// - Parameter partsOfSpeech: an array of `NLTags` specifying which parts of speech should be extracted | |
| /// | |
| /// - Returns: An array of `Substring`s which match any of the specified parts of speech | |
| func filter(by partsOfSpeech: [NLTag]) -> [Substring] { | |
| let tagger = NLTagger(tagSchemes: [.nameTypeOrLexicalClass]) | |
| tagger.string = self | |
| let tags = tagger.tags( | |
| in: self.startIndex ..< self.endIndex, | |
| unit: .word, | |
| scheme: .nameTypeOrLexicalClass, | |
| options: [.omitPunctuation, .omitWhitespace, .omitOther] | |
| ) | |
| let filtered = tags.filter { (tag, _) in | |
| if let tag = tag { | |
| return partsOfSpeech.contains(tag) | |
| } | |
| return false | |
| } | |
| return filtered.map { (_, range) in self[range] } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment