Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 444
Charles Avatar asked Feb 06 '12 21:02

Charles


1 Answers

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

like image 103
Dave DeLong Avatar answered Nov 05 '22 13:11

Dave DeLong