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

1

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.

Vizor answered 6/2, 2012 at 21:12 Comment(2)
um... woa. Is the database already there? Or are you writing the language analysis tool yourself? That's a monstrous task, BTW.Antonio
@Antonio definitely a monstrous task... unless someone has done it for you already! :)Moriarty
M
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+).

Moriarty answered 6/2, 2012 at 21:39 Comment(8)
Woah. Thanks. I would have bet that such a thing didn't exist in iOS. I'm surprised.Seppala
@arnauddel. yeah, it's pretty neat! I haven't found a good reason to use it yet, but maybe eventually.Moriarty
@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 resultsUnwearied
You can even use NSLinguisticTagger to syntax highlight English-language text. I've made an open source example that you can use.Mudskipper
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.Blueing
I have a similar issue #24402915, It would be great if you can help withTorras
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.Rheinlander
@DaveDeLong thoughts? #48769419 physical devices not working the same dunno why the lexical class can't be found...Gillman

© 2022 - 2024 — McMap. All rights reserved.