Search code examples
iphoneobjective-cioslinguistics

How do I identify the part of speech of a word within a NSString?


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.


Solution

  • 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+).