正确答案 - 比较字符串

时间:2014-09-04 06:45:02

标签: ios objective-c xcode nsstring compare

我建立了自己的Levenshtein距离替代方案来训练自己的目标-c。这个课将得到几个句子,在初始测试期间只有2个句子,并返回判断句子的百分之几。多少百分比符合"正确的句子"。

基本上它的作用是得到2个句子,(在初始测试中只有2个单词)并计算。但是我得到了一些奇怪的NSLog。

如果我进入1989年和199年,它将返回75%正确 - 这是正确的答案。但是,如果我输入" orange"和" oange" - 当正确率为83%时,它返回50%正确(对吗??)

这是调用方法的代码:

    -(void)compare {
// Take each and every sentence from the users source and check it against the other sources. If it contains 2 or more words/numbers that are equal, i'll get "investigated" further

NSMutableArray *userSentences = [[MyManager sharedManager] contentSentencesList];
NSMutableArray *serverSentences = [NSMutableArray arrayWithArray:getSentencesFromText(serverText)];

// Sample sentences
[userSentences insertObject:@"oange" atIndex:userSentences.count];
[serverSentences insertObject:@"orange" atIndex:serverSentences.count];

// For-statement integers
int i = 0;
int b = 0;

for (i = 0; i < userSentences.count; i++) {
    // Check first sentence

    NSString *userSentence;
    NSString *serverSentence;

    // Check similarity of the two sentences, get percent and add to centerPercent
    for (b = 0; b < serverSentences.count; b++) {
        // Compare sentences
        userSentence = userSentences[i];
        serverSentence = serverSentences[b];
        // Compare sentences with custom class

        // Initialize Distance class
        SourcerDistance *wordDistance = [[SourcerDistance alloc] init];

        // Create resultPercent integer and calculate it
        int resultPercent = [wordDistance distanceBetween:userSentence serverSentence:serverSentence];

        NSLog(@"%@%d", @"FinishViewController result: ", resultPercent);

        // Add resultPercent to averagePrecent and increase averagPercentToDivide by 1
        centerPercent = centerPercent + resultPercent;
        centerPercentToDivide++;
    }


    // Set and display resultoppositeSpelling
    // averagePercent = centerPercent / centerPercentToDivide;

    // Use an integer to remove decimals
    [self presentResult];
}
}

这是另一个类:

#import "SourcerDistance.h"

@implementation SourcerDistance

-(int)distanceBetween:(NSString *)userSentence serverSentence:(NSString *)serverSentence {

// Declare the 2 arrays containing all the words from the user's source and the developer's source
NSArray *developerSourceSentence = [self getWords:serverSentence];
NSArray *userSourceSentence = [self getWords:userSentence];

// Declare variables that'll be use for for-statements
int developerWordsLoop = developerSourceSentence.count;
int userWordsLoop = userSourceSentence.count;

// Declare variables required for matching (average of all words)
float centerPercent = 0; // This is for ALL words in total
float centerPercentToDivide = 0; // This is for all words in total

// Single-word variables
float totalCharacters = 0;
float matchingCharacters = 0;
float percentMatchingSingleWord = 0;

NSLog(@"%@%d", @"userSourceSentenceCount: ", userSourceSentence.count);
NSLog(@"%@%d", @"developerSourceSentenceCount: ", developerSourceSentence.count);

// Loop through all of the user words
for (userWordsLoop = 0; userWordsLoop < userSourceSentence.count; userWordsLoop++) {

    // Loop through all of the developer words
    for (developerWordsLoop = 0; developerWordsLoop < developerSourceSentence.count; developerWordsLoop++) {

        // Declare variables that contain all the characters of the user- and developer-words
        NSMutableArray *userCharacters = [self getCharacters:userSourceSentence[userWordsLoop]];
        NSMutableArray *developerCharacters = [self getCharacters:developerSourceSentence[developerWordsLoop]];

        // Compare characters
        matchingCharacters = [self compareCharacters:userCharacters developerCharacters:developerCharacters];


        // Set the variables
        totalCharacters = developerCharacters.count;
        percentMatchingSingleWord = matchingCharacters / totalCharacters * 100;

        NSLog(@"%@%f", @"totalCharacters", totalCharacters);
        NSLog(@"%@%f", @"matchingCharacters", matchingCharacters);
        NSLog(@"%@%f", @"iterate", percentMatchingSingleWord);
        centerPercent = centerPercent + percentMatchingSingleWord;
        centerPercentToDivide++;
    }
}

// Declare variables used with final result
float finalPercentFloat = 0;
int finalPercent = 0;
NSLog(@"%@%f", @"centerPercent: ", centerPercent);
NSLog(@"%@%f", @"centerPercentToDivide: ", centerPercentToDivide);
finalPercentFloat = centerPercent/centerPercentToDivide;
NSLog(@"%@%f", @"finalPercent: ", finalPercentFloat);
finalPercent = (int)finalPercentFloat;
return finalPercent;

}

-(float)compareCharacters:(NSMutableArray *)userCharacters developerCharacters:(NSMutableArray *)developerCharacters {
// Declare result variables and other required variables
float matchingCharacters;
int userCharactersLoop = 0;
int developerCharactersLoop = 0;

// Loop through all of the userCharacters
for (userCharactersLoop = 0; userCharactersLoop < userCharacters.count; userCharactersLoop++) {
    // Loop through all of the developerCharacters
    for (developerCharactersLoop = 0; developerCharactersLoop < developerCharacters.count; developerCharactersLoop++) {
        // Match every character here
        if ([userCharacters[userCharactersLoop] isEqualToString:developerCharacters[developerCharactersLoop]]) {
            // Increase matchingCharacters
            matchingCharacters++;
        } else {
            // Do nothing
        }
    }
}

// Return result variable
return matchingCharacters;
}

-(NSArray *)getWords:(NSString *)sentence {
// Get words of sentence from developer-source
NSArray *sentenceWords;
NSString *serverSentenceToRead = sentence;
sentenceWords = [serverSentenceToRead componentsSeparatedByCharactersInSet:
                           [NSCharacterSet characterSetWithCharactersInString:@":;.!? "]
                           ];

// Return developer words
return  sentenceWords;
}

-(NSMutableArray *)getCharacters:(NSString *)word {
NSMutableArray *wordCharacters = [[NSMutableArray alloc] initWithCapacity:[word length]];
for (int i=0; i < [word length]; i++) {
    NSString *ichar  = [NSString stringWithFormat:@"%c", [word characterAtIndex:i]];
    [wordCharacters addObject:ichar];
}
// Return the characters of the word
return wordCharacters;
}

@end

的NSLog:

2014-09-03 20:22:32.015 Sourcer[27532:60b] userSourceSentenceCount: 1
2014-09-03 20:22:32.017 Sourcer[27532:60b] developerSourceSentenceCount: 1
2014-09-03 20:22:32.018 Sourcer[27532:60b] totalCharacters6.000000
2014-09-03 20:22:32.018 Sourcer[27532:60b] matchingCharacters3.001519
2014-09-03 20:22:32.019 Sourcer[27532:60b] iterate50.025322
2014-09-03 20:22:32.020 Sourcer[27532:60b] centerPercent: 50.025322
2014-09-03 20:22:32.021 Sourcer[27532:60b] centerPercentToDivide: 1.000000
2014-09-03 20:22:32.021 Sourcer[27532:60b] finalPercent: 50.025322
2014-09-03 20:22:32.022 Sourcer[27532:60b] FinishViewController result: 50
2014-09-03 20:22:32.022 Sourcer[27532:60b] averagePercent (float): 50.000000

我在这里做错了什么?任何人都可以理解代码并帮助我找出错误吗?这个算法有些奇怪

非常感谢!

(我知道我有点重新发明轮子,但我想尝试:))

此致 埃里克

0 个答案:

没有答案