Use the language recognition system in iOS 11 to recognize a language.
- [Instructor] There's a lot you can do with language easily in iOS and watchOS. The entire Apple ecosystem has a natural language processing, or NLP system built in and easy to use. Let's take a look at language recognition. Download the starter file. It's a playground with a few languages set up for you in a dictionary. I used Google Translate on them. I'm trusting that Google Translate here is correct, but I do know that at least one of the non-Latin-script languages messed up in copying, so I deleted that.
I'm not 100% sure the others did, but it will give us what we need. The class we'll use is the NSLinguisticTagger class as a class method to find the dominant language in text. Go below all the languages here, and just below the language equals English tag, give yourself some space and add the following, var language = NSLinguisticTagger.dominantLanguage for a string, and we've got the string up above, which is called str, and that's a dictionary of languages.
And I'm just gonna use lang, which is that constant I had before, and I'm gonna unwrap it and go ahead and run that. I've set this to manually run for my playground. And you'll see here it tells you it's English. You can try a few more languages, too. Let me try Hindi for a different script, or I can try Spanish, and it gives me the code for that language here.
Now one of the interesting languages to look at is Chinese, which I had up here, and click that, and you'll see it's got the code for Chinese, but it's also got a modifier code, and some of these actually will have modifier codes to tell you which dialect or script that you're using. Now that's fun and a great way to start automatic localization. You could just look for whatever the dominant language is and then start working from that. But there's more you can do with tagging. I'll give you one more example, lexical tagging.
This finds parts of speech, paragraph, and sentence structure for you. You'll need an instance of the lexical tagger to do this, so let's put in let tagger = NSLinguisticTagger. And you'll find it has an initializer with tagSchemes and options. In the tagSchemes, it's a collection of the schemes that you're gonna use. I'm gonna use just one for this right now, and under tagSchemes, you'll see it's a collection, and there's a whole bunch of them that you can put in here.
I'm just gonna do a period so you can see all of the ones here. Now we can use lexicalClass or nameType. You'll notice under here there's one that says nameTypeOrLexicalClass, so you can do both of those at the same time, and that's the one I'm gonna pick. And then you go to options, and we're gonna put zero for there. We don't have any options. Now you assign a string to the tagger once you have it made, so I'm gonna do tagger.string = str, bracket, lang, and again I'm gonna unwrap that for the dictionary, and then we're gonna, tagger has a method, enumerateTags, which finds all those tags within a range in a string.
So to check this entire string, I'm first gonna need the full range, so I'm gonna make an NSRange here, which is what it uses, so I'm gonna do let fullRange = NSRange and then I'm gonna use the location, length one here. Location is zero and the length will be the length of my string, which I'll use this version, str lang ?.utf16.count.
So you gotta count characters here, so we wanna use the utf one to count it, and then that has to be unwrapped, as well. So we got several parentheses levels here. I'm gonna put this, unwrapped, and close off my parentheses. And with our full range, we can use the tagger and use the enumerateTags. And we're gonna use the enumerateTags in NSRange and we're gonna use this one here, the one in the middle, which, when you click it all out, it's in, unit, scheme, options, using.
The range we already know is the full range, 'cause I wanna do this whole text. The unit is what I'm gonna break this into. I can do paragraphs, sentences, words, the full document. I'm gonna use words here, so I'm gonna .word. Then we get to the scheme, and that's gonna match the scheme for the tagger up above. And so I'm gonna do .nameTypeOrLexicalClass. And the options is a collection again, so I'll make a collection sign there.
I'm gonna omit punctuation. I'm also gonna omit white space. So we can see here some different options you have so that you can figure out what you're doing and clean stuff up easily. And then the last thing you have in here is using, and the way enumerateTags works, it's a lot like a for statement, but in its loop, we're gonna use a closure with three parts to it. The first part, I'm gonna call this tag, which will be the tag that we get. Range will be the range for that particular word, in our case, or our unit, and so I'm gonna call this range.
And the last one is called stop, which I'm not gonna go too deep into an explanation of, but it's a way of stopping the enumeration if you need to. Now in the code here, I can now go ahead and just say for every single word what I'm gonna do for each one of these words, just like I would with a for statement. So what I'm gonna do is gonna say, if tag == .noun. And I have all different parts of speech for my tags, so I'm gonna say, I'm gonna find all my nouns.
I'm gonna say let word = tagger.string! as NSString. And I wanna use an NSString because I'm gonna use a substring statement, which only works on NSStrings here. So once I get that, I'm gonna do substring with, which takes a NSRange, and that will be the range I have up above, and then I can print that word.
Now I'm gonna set this back to English for my first one here and then I'm gonna go ahead and run this. And let me scoot this up a little bit, and you see we've gotten all of our words, nearest, which I don't know if that's considered a noun, but it did pick that one up, but the rest of these are obviously nouns. Okay, I can do this for verbs, too. So I can just change this to verb, run again, and I get my verbs.
I can change this to Spanish for language, run this, I get Spanish as my language. I get my Spanish verbs or, let's say, nouns. And of course we have to do Italian for pizza. Now here's the one thing I do wanna try is what if I try Chinese or Hawaiian? Either one of those will work.
Notice we get nothing. Not all the languages are available, and I've not seen a list of which languages work with tags and which don't. For the languages that do work, there's a lot more you can tag and learn about a sentence. Take a look at the documentation and the WWDC 2017 video for more.
Note: Because this is an ongoing series, viewers will not receive a certificate of completion.