0

I am creating a words game for iOS. I would like to prevent players from making plural words. Is there any dictionary that I can use to write a function like

isPluralWord(@"tables")

which will return true and

isPluralWord(@"table")

will return false.

Thanks!

dhruv chopra
  • 490
  • 3
  • 11

3 Answers3

0

The naive and incorrect solution:

BOOL isPlural(NSString *s)
{
    return [s characterAtIndex:s.length - 1] == 's';
}

The correct solution would be to combine this with something intelligent that detects irregular words (such as "formula" and "formulae") and words which are not plural but end in "s" anyway (such as "parenthesis" and "parentheses"). For that, you probably want to acquire some kind of English word database which has some grammatical annotations.

  • Thanks, I wanted to know if someone knows of such a database or dictionary which I can use in the game. Checking for 's' as the last character will not suffice. I want to allow words like "mass" and "chaos" for example. – dhruv chopra Jun 13 '13 at 12:40
0

Rather than checking character 's' at the end of string, You should use Localizable.stringsdict for plural words like this. In this plist you can mention your key mapped with its plural.

Please take a look at below example of such string

<key>%d Likes</key>
<dict>
    <key>NSStringLocalizedFormatKey</key>
    <string>%#@likes@</string>
    <key>likes</key>
    <dict>
        <key>NSStringFormatSpecTypeKey</key>
        <string>NSStringPluralRuleType</string>
        <key>NSStringFormatValueTypeKey</key>
        <string>d</string>
        <key>one</key>
        <string>%d Like</string>
        <key>other</key>
        <string>%d Likes</string>
    </dict>
</dict>

After you define above plurals in your plist you can call it directly by passing an integer with the string

NSInteger x = Pass 1 for one and any other number for other

NSString *pluralString = [NSString localizedStringWithFormat:NSLocalizedString(@"%d Likes", @"X number of Likes for a post"), x]

In case of X = 1, you will get 1 Like
And, In case of X = any number other than 1, consider 10, answer will be 10 Likes.

You can also check the link for more reference: http://macoscope.com/blog/effective-localization-when-working-with-language-plural-rules/

Mohnish Hirudkar
  • 303
  • 3
  • 14
0

You can use NSLinguisticTagger as such:

#import <Foundation/Foundation.h>

NSLinguisticTagger *linguisticTagger = [[NSLinguisticTagger alloc] initWithTagSchemes:@[NSLinguisticTagSchemeLemma] options:kNilOptions];

linguisticTagger.string = @"table tables";

[linguisticTagger enumerateTagsInRange:NSMakeRange(0, linguisticTagger.string.length) scheme:NSLinguisticTagSchemeLemma options:NSLinguisticTaggerOmitWhitespace usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) {
    NSString *word = [linguisticTagger.string substringWithRange:tokenRange];

    if ([word isEqualToString:tag]) {
        NSLog(@"word '%@' is singular", word);
    } else {
        NSLog(@"word '%@' is plural", word);
    }
}];
Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113