0

このコードはRyan Nystrom の githubからダウンロードしました。Dave Child の githubにある PHP Text Statistics プロジェクトの Objective C ポートです。Objective C として認識されていないものがありますが、私は初心者のプログラマーなので、プログラマーに連絡する前に、明らかな間違いを犯していないかどうかを確認するために、stackoverflow で実行したかったのです。

私の問題は、音節カウント方法の例外である単語の NSDictionary があることですが、それを使用してそれらの単語を含むテキストの音節をカウントすると、例外としてカウントされません。たとえば、辞書には「twelve」という単語があり、1音節として数えるべきであると示されていますが、「twelve」という単語を分析すると、2音節になってしまいます。

現在は、1 音節としてカウントする必要があるエンディング/パターンのリストもあり、そうでなければ 2 音節としてカウントされる可能性があります (-cious、-cial など)。そのリストに「twelve」を追加すると、1 音節としてカウントされます。そのため、そのリストは正常に機能しているようです。機能していないように見えるのは、例外の辞書だけです。

信じられないほど明白な何かが欠けていますか?それとも、これはコーダーと連絡を取り、彼に知らせる状況ですか?

助けてくれてありがとう。

- (NSInteger)syllableCount {
    if ([self isEqualToString:@""]) {
        return 0;
    }

    // remove non-alpha chars
    NSString *strippedString = [self stringByReplacingRegularExpression:@"[^A-Za-z]" withString:@"" options:kNilOptions];
    // use lowercase for brevity w/ options + patterns
    NSString *lowercase = [strippedString lowercaseString];
    // altered in enumerate blocks
    __block NSInteger syllableCount = 0;

//***It's this dictionary whose items seem not to be registering as exceptions:

    // special rules that don't follow syllable matching patterns
    NSDictionary *exceptions = @{
    @"you" : @1,
    @"simile" : @3,
    @"forever" : @3,
    @"shoreline" : @2,
    @"poetry" : @3,
    @"twelve" : @1,
    @"delete" : @2,
    };
    // if one of the preceding words, return special case value
    NSNumber *caught = exceptions[self];
    if (caught) {
        return caught.integerValue;
    }

//***If I put those words in the appropriate places in the following lists, however, they end up being counted correctly.

// These syllables would be counted as two but should be one
NSArray *subSyllables = @[
@"cial",
//...various other things...
@"[aeiouy]rse$",
];

// These syllables would be counted as one but should be two
NSArray *addSyllables = @[
@"ia",
//...various other things...
@"ie(r|st)$"
];

// Single syllable prefixes and suffixes
NSArray *prefixSuffix = @[
@"^un",
//...various other things...
@"ings?$",
];

// remove prefix & suffix, count how many are removed
NSInteger prefixesSuffixesCount = 0;
NSString *strippedPrefixesSuffixes = [NSRegularExpression stringByReplacingOccurenceOfPatterns:prefixSuffix inString:lowercase options:kNilOptions withTemplate:@"" count:&prefixesSuffixesCount];

// removed non-word chars from word
NSString *strippedNonWord = [strippedPrefixesSuffixes stringByReplacingRegularExpression:@"[^a-z]" withString:@"" options:kNilOptions];
NSString *nonVowelPattern = @"[aeiouy]+";
NSError *vowelError = nil;
NSRegularExpression *nonVowelRegex = [[NSRegularExpression alloc] initWithPattern:nonVowelPattern options:kNilOptions error:&vowelError];
NSArray *wordPartsResults = [nonVowelRegex matchesInString:strippedNonWord options:kNilOptions range:NSMakeRange(0, [strippedNonWord length])];

NSMutableArray *wordParts = [NSMutableArray array];
[wordPartsResults enumerateObjectsUsingBlock:^(NSTextCheckingResult *match, NSUInteger idx, BOOL *stop) {
    NSString *substr = [strippedNonWord substringWithRange:match.range];
    if (substr) {
        [wordParts addObject:substr];
    }
}];

__block NSInteger wordPartCount = 0;
[wordParts enumerateObjectsUsingBlock:^(NSString *part, NSUInteger idx, BOOL *stop) {
    if (! [part isEqualToString:@""]) {
        wordPartCount++;
    }
}];

syllableCount = wordPartCount + prefixesSuffixesCount;

// Some syllables do not follow normal rules - check for them
[subSyllables enumerateObjectsUsingBlock:^(NSString *subSyllable, NSUInteger idx, BOOL *stop) {
    NSError *error = nil;
    NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:subSyllable options:kNilOptions error:&error];
    syllableCount -= [regex numberOfMatchesInString:strippedNonWord options:kNilOptions range:NSMakeRange(0, [strippedNonWord length])];
}];

[addSyllables enumerateObjectsUsingBlock:^(NSString *addSyllable, NSUInteger idx, BOOL *stop) {
    NSError *error = nil;
    NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:addSyllable options:kNilOptions error:&error];
    syllableCount += [regex numberOfMatchesInString:strippedNonWord options:kNilOptions range:NSMakeRange(0, [strippedNonWord length])];
}];

syllableCount = syllableCount <= 0 ? 1 : syllableCount;

return syllableCount;

}

4

1 に答える 1

0

メソッドの残りの部分では、文字列の処理された形式 (つまり、空白と小文字を削除した後) を使用していますが、そのexceptions辞書検索では元の形式を使用しているため、文字列が , , ではなく正確に , でない限り、うまくいきません そこにはありません。@"twelve"@"Twelve"@" twelve "@"twelve\t"

修理:

--- a/NSString+RNTextStatistics.m
+++ b/NSString+RNTextStatistics.m
@@ -204,7 +204,7 @@
     @"wandered" : @2
     };
     // if one of the preceding words, return special case value
-    NSNumber *caught = exceptions[self];
+    NSNumber *caught = exceptions[lowercase];
     if (caught) {
         return caught.integerValue;
     }

おそらく、これをバグとして作成者に提出する必要があります。

于 2013-01-24T20:35:34.020 に答える