20

The app I'm currently working on requires me to determine the part of speech of a word in NSString.

So basically is there a library/database/class which you can access in Objective C which allows one to check if a single word (in the form of a NSString) is a noun, an adjective, an adverb or a verb?

Something along the lines of:

NSString *foo="cat";

if ([foo wordIsNoun]) {
    //do something
};

On a similar but slightly unrelated note, is it possible to check if two NSString containing verbs of the same stem but different tense (ask, asking, asked, etc) have the same stem? It would be very useful as well.

iHunter
  • 6,205
  • 3
  • 38
  • 56
Charles
  • 493
  • 7
  • 14
  • 2
    um... woa. Is the database already there? Or are you writing the language analysis tool yourself? That's a monstrous task, BTW. – Almo Feb 06 '12 at 21:34
  • 4
    @Almo definitely a monstrous task... unless someone has done it for you already! :) – Dave DeLong Feb 06 '12 at 21:59

1 Answers1

61

You can do this with an NSLinguisticTagger! I've never used one before, but I hacked this together:

NSString *str = @"i have a cat";

NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc] initWithTagSchemes:[NSArray arrayWithObject:NSLinguisticTagSchemeLexicalClass] options:~NSLinguisticTaggerOmitWords];
[tagger setString:str];
[tagger enumerateTagsInRange:NSMakeRange(0, [str length]) 
                      scheme:NSLinguisticTagSchemeLexicalClass 
                     options:~NSLinguisticTaggerOmitWords 
                  usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) {
                               NSLog(@"found: %@ (%@)", [str substringWithRange:tokenRange], tag);
                              }];
[tagger release];

When you run this, it logs:

found: i (Pronoun)
found: have (Verb)
found: a (Determiner)
found: cat (Noun)

Note, however, that NSLinguisticTagger is only available on iOS 5+ (and Mac OS X 10.7+).

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • 1
    Woah. Thanks. I would have bet that such a thing didn't exist in iOS. I'm surprised. – arnaud del. Feb 06 '12 at 21:48
  • @arnauddel. yeah, it's pretty neat! I haven't found a good reason to use it yet, but maybe eventually. – Dave DeLong Feb 06 '12 at 21:56
  • 3
    @DaveDeLong - I wrote the chapter on NSLinguisticTagger in the "iOS5 by Tutorials" book, let me just warn you that in iOS5 it just works for English (with German and French planned). What is more annoying is that for sentences like "i have a cat" it returns pretty ok results, but with the input I used it failed miserably - so don't count on it if you need 100% accurate results – Marin Todorov Feb 10 '12 at 10:12
  • You can even use `NSLinguisticTagger` to _syntax highlight_ English-language text. I've made an [open source example that you can use](http://cutecoder.org/programming/introduction-to-cocoa-nslinguistictagger-in-nsbrief-podcast-72/). – adib Feb 28 '13 at 12:25
  • As with any POS-tagger, results depend a lot on the input text. If you try to tag recipe instructions, for example, any tagger will have difficulties with the sentence initial verbs ("Cook until brown") and will tag them as nouns instead. Expected performance on general texts should be about 95% or so. That's about the maximum that can be achieved these days. – Oliver Mason Aug 30 '13 at 10:10
  • I have a similar issue http://stackoverflow.com/questions/24402415/word-stemming-in-ios-not-working-for-single-word, It would be great if you can help with – Ab'initio Jun 25 '14 at 07:25
  • Any completion block? I'm trying to check if it's done enumerating by looking at the range but if input sentence has trailing-spaces my conditional fails. – Albert Renshaw Feb 03 '18 at 20:49
  • @DaveDeLong thoughts? https://stackoverflow.com/questions/48768919/device-vs-simulator-linguistic-schemes physical devices not working the same dunno why the lexical class can't be found... – Will Von Ullrich Feb 18 '18 at 19:02