我从Ryan Nystrom 的 github下载了这段代码;它是Dave Child 的 github上 PHP 文本统计项目的 Objective C 端口。有些东西我不认为是Objective C,但我是一个新手程序员,所以我想通过stackoverflow运行它,看看在我与程序员联系之前我是否犯了一些明显的错误。
我的问题是音节计数方法中有一个单词的 NSDictionary 是异常的,但是当我使用它来计算包含这些单词的文本中的音节时,它们不会被视为异常。例如,字典包含“十二”这个词,并指出它应该算作一个音节,但是当我分析“十二”这个词时,它会出现两个音节。
现在还有一个结尾/模式列表,需要计为一个音节,否则可能会计为两个音节(-cious、-cial 等)。当我在该列表中添加“十二”时,它被计为一个音节。因此,该列表似乎运行良好。这只是似乎不起作用的异常字典。
我错过了一些非常明显的东西吗?或者这是与编码员联系并让他知道的情况?
在此先感谢您的帮助。
- (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;
}