Created
September 18, 2020 18:27
-
-
Save tkausch/fef57a06c8c0e171b3fdbcf46ef8a6ea to your computer and use it in GitHub Desktop.
How to parse a sentence and distinguish word types
This file contains 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
let options = NSLinguisticTagger.Options.omitWhitespace.rawValue | NSLinguisticTagger.Options.joinNames.rawValue | |
let tagger = NSLinguisticTagger(tagSchemes: NSLinguisticTagger.availableTagSchemes(forLanguage: "en"), options: Int(options)) | |
let inputString = "This is a very long test for you to try" | |
tagger.string = inputString | |
let range = NSRange(location: 0, length: inputString.utf16.count) | |
tagger.enumerateTags(in: range, scheme: .nameTypeOrLexicalClass, options: NSLinguisticTagger.Options(rawValue: options)) { tag, tokenRange, sentenceRange, stop in | |
guard let range = Range(tokenRange, in: inputString) else { return } | |
let token = inputString[range] | |
print("\(tag): \(token)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment