【问题标题】:dictionary without plural words for game on iOSiOS上没有复数单词的字典
【发布时间】:2013-06-13 12:29:44
【问题描述】:

我正在为 iOS 创建一个文字游戏。我想防止玩家制作复数词。是否有任何字典可以用来编写类似的函数

isPluralWord(@"tables")

这将返回 true 和

isPluralWord(@"table")

将返回 false。

谢谢!

【问题讨论】:

  • 我问的不是 NSDictionary,而是英语词典 :)

标签: ios dictionary words plural


【解决方案1】:

幼稚且不正确的解决方案:

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

正确的解决方案是将其与检测不规则单词(例如“formula”和“formulae”)以及非复数但无论如何以“s”结尾的单词(例如“括号”和“括弧”)。为此,您可能想要获取某种具有一些语法注释的英语单词数据库。

【讨论】:

  • 谢谢,我想知道是否有人知道我可以在游戏中使用的数据库或字典。检查 's' 作为最后一个字符是不够的。例如,我想允许使用“群众”和“混乱”之类的词。
【解决方案2】:

而不是检查字符串末尾的字符's',您应该使用 Localizable.stringsdict 来表示这样的复数单词。在这个 plist 中,您可以提及您的键与其复数映射。

请看下面这个字符串的例子

<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>

在您的 plist 中定义上述复数后,您可以通过将整数与字符串一起传递来直接调用它

NSInteger x = 1 传递 1,其他任何数字传递

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.

您也可以查看链接以获取更多参考:http://macoscope.com/blog/effective-localization-when-working-with-language-plural-rules/

【讨论】:

    【解决方案3】:

    您可以这样使用NSLinguisticTagger

    #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);
        }
    }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-19
      • 2021-07-30
      • 1970-01-01
      • 2019-06-06
      • 1970-01-01
      • 2014-10-19
      • 2022-12-09
      • 2011-07-25
      相关资源
      最近更新 更多