Created
January 23, 2018 14:42
-
-
Save bretsko/563da4c511baefeca2922b896222e44f to your computer and use it in GitHub Desktop.
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 Foundation | |
let options = NSLinguisticTagger.Options.omitWhitespace.rawValue | NSLinguisticTagger.Options.joinNames.rawValue | |
let tagger = NSLinguisticTagger(tagSchemes: NSLinguisticTagger.availableTagSchemes(forLanguage: "en"), options: Int(options)) | |
//let str = "Can a cat eat?" | |
//[Can:Other, a:Other, cat:Other, eat:Other, ?:SentenceTerminator] | |
//let str = "Can a human swim?" | |
//[Can:Verb, a:Determiner, human:Adjective, swim:Noun, ?:SentenceTerminator] | |
//let str = "Cat food tastes great" | |
//[Cat:Noun, food:Noun, tastes:Noun, great:Adjective] | |
let str = "Meet men today!" | |
//[Meet:Noun, men:Noun, today:Noun, !:SentenceTerminator] | |
tagger.string = str | |
struct TaggedString: CustomStringConvertible { | |
var str: String | |
var pos: String | |
var description: String { | |
return "\(str):\(pos)" | |
} | |
} | |
var taggedStrings = [TaggedString]() | |
let range = NSRange(location: 0, length: str.utf16.count) | |
tagger.enumerateTags(in: range, scheme: .nameTypeOrLexicalClass, options: NSLinguisticTagger.Options(rawValue: options)) { weakPOS, tokenRange, _, _ in | |
guard let range = Range(tokenRange, in: str), | |
let pos = weakPOS else { | |
return | |
} | |
let token = String(str[range]) | |
taggedStrings.append(TaggedString(str:token, pos:pos.rawValue)) | |
} | |
print(taggedStrings) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment