0

我需要更改在 NSTextField 的每一行中重复的单词的颜色。我正在使用字典来分隔每一行,并且字典具有将单词分隔为数据的数组,然后我正在检查单词是否重复并且我想更改颜色或突出显示重复的单词..不知道如何做。

该程序是获取词频并显示类型标记、平均频率和重复的词(文本颜色/突出显示)

任何帮助都会很棒。现在我正在从导入的 .txt 文件中将文本分配给文本字段。得到了重复的话和一切..只需要显示哪个。

当前代码:

-(NSMutableDictionary*) computeWords:(NSMutableDictionary *)linesDic{
    int count = (int)[linesDic count];
    int numberOfwords = 0;
    int numberOfDiffWords = 0;
    NSMutableDictionary* wordComputation = [[NSMutableDictionary alloc]init];
    NSMutableDictionary* duplicates = [[NSMutableDictionary alloc]init];
    for (int x = 1; x<=count; x++) {
        NSMutableArray* duplicateWords = [[NSMutableArray alloc]init];
        NSMutableArray* counter = [[NSMutableArray alloc]init];
        NSString* key = [NSString stringWithFormat:@"%d", x];
        NSArray* data = [linesDic objectForKey:key];
        numberOfwords = (int)[data count];
        numberOfDiffWords = numberOfwords;
        for (int j = 0; j<numberOfwords; j++) {
            for (int k = j+1; k<numberOfwords; k++) {
                if([[data objectAtIndex:k]isEqualToString:[data objectAtIndex:j]])
                {
                    numberOfDiffWords--;
                    [duplicateWords addObject:[data objectAtIndex:k]];
                    //                    NSLog(@"%@", [data objectAtIndex:k]);
                    //                    NSLog(@"%@", [data objectAtIndex:j]);
                }else{
                    NSString* a = [data objectAtIndex:k];
                    NSString* b = [data objectAtIndex:j];
                    if(a.length<b.length){
                        if((b.length - a.length)<=4){
                            if([[b substringToIndex:a.length]isEqualToString:a])
                            {
                                //                                NSLog(@"KEY: %@ comapare b:%d:%@ and a:%d:%@", key,(int)b.length, b, (int)a.length, a );
                                //                                NSLog(@"Key: %@, %@", key, [b substringFromIndex:(a.length-1)]);
                                if([[b substringFromIndex:(a.length)]isEqualToString:@"s"] || [[b substringFromIndex:(a.length)]isEqualToString:@"es"]){
                                    if (([a isEqualToString:@"a"] && [b isEqualToString:@"as"]))  {
                                        //                                        NSLog(@"break at key: %@, words %@:%@",key, a, b);
                                        break;
                                    }
                                    numberOfDiffWords--;
                                    [duplicateWords addObject:[data objectAtIndex:k]];
                                    [duplicateWords addObject:[data objectAtIndex:j]];
                                    //                                    NSLog(@"%@", [data objectAtIndex:k]);
                                    //                                    NSLog(@"%@", [data objectAtIndex:j]);
                                }
                            }
                        }
                    }else{
                        if((a.length - b.length)<=4){
                            if([[a substringToIndex:b.length]isEqualToString:b])
                            {
                                //                                NSLog(@"KEY: %@ compare a:%d: %@ and b:%d: %@", key,(int)a.length, a, (int)b.length, b );
                                //                                NSLog(@"Key: %@, %@", key, [a substringFromIndex:(b.length-1)]);
                                if([[a substringFromIndex:(b.length)]isEqualToString:@"s"] || [[a substringFromIndex:(b.length)]isEqualToString:@"es"]){
                                    if (([a isEqualToString:@"as"] && [b isEqualToString:@"a"])) {
                                        //                                        NSLog(@"break at key: %@, words %@:%@",key, a, b);
                                        break;
                                    }
                                    numberOfDiffWords--;
                                    [duplicateWords addObject:[data objectAtIndex:k]];
                                    [duplicateWords addObject:[data objectAtIndex:j]];
                                    //                                    NSLog(@"%@", [data objectAtIndex:k]);
                                    //                                    NSLog(@"%@", [data objectAtIndex:j]);
                                }
                            }
                        }
                    }
                }
            }
        }

        NSNumber* nw = [NSNumber numberWithInt:numberOfwords];
        NSNumber* ndw = [NSNumber numberWithInt:numberOfDiffWords];
        [counter addObject:nw];
        [counter addObject:ndw];
        NSLog(@"Key: %@, Data: %@", key, duplicateWords);
        [duplicates setObject:duplicateWords forKey:key];
        [wordComputation setObject:counter forKey:key];
    }
    _duplicates = duplicates;
    NSLog(@"%@", _duplicates);
    _wordComputation = wordComputation;
    return wordComputation;
}

-(void)showTable:(NSMutableDictionary*)wordComputation{
    int count = (int)[wordComputation count];
    int numberOfwords = 0;
    int numberOfDiffWords = 0;
    //    NSLog(@"\t \t Diff Words  Words  Type Token \t Mean");
    for (int x = 1; x<=count; x++) {
        NSString* key = [NSString stringWithFormat:@"%d", x];
        NSArray* data = [wordComputation objectForKey:key];
        numberOfwords = [[data objectAtIndex:0]intValue];
        numberOfDiffWords = [[data objectAtIndex:1]intValue];
        //print log
        //        NSLog(@"Line: %@ \t %@ \t %@ \t %@ \t\t %@", key, numberOfDiffWords<10?[NSString stringWithFormat:@"0%d", numberOfDiffWords]:[NSString stringWithFormat:@"%d", numberOfDiffWords], numberOfwords<10?[NSString stringWithFormat:@"0%d", numberOfwords]:[NSString stringWithFormat:@"%d", numberOfwords],[NSString stringWithFormat:@"%d/%d", numberOfDiffWords, numberOfwords], [NSString stringWithFormat:@"%d/%d", numberOfwords, numberOfDiffWords]);
    }

    [_frequencyTable reloadData];
}

尝试这样做只是为了首先更改字符串的颜色,但随后显示

{
    NSColor = "NSCalibratedRGBColorSpace 1 0 0 1";
} 

在字符串中的第 5 位


- (IBAction)ViewDuplicatesPressed:(id)sender {
    [_duplicatesTextField setHidden:NO];
    NSLog(@"%@", _duplicates);


    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@", [_contentTextField stringValue]] /*attributes:@{NSForegroundColorAttributeName : [NSColor redColor]}*/];


    [attributedString addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:NSMakeRange(0,5)];



    [_duplicatesTextField setStringValue:[NSString stringWithFormat:@"%@", attributedString]];
}

也试过

int lineCount = (int)[_linesDic count];
NSString* key;
NSMutableDictionary* dupliDic = [[NSMutableDictionary alloc]init];

for (int i = 0; i<lineCount; i++) {
    NSArray *duplicateWords;
    key = [NSString stringWithFormat:@"%d", i];
    duplicateWords = [_duplicates objectForKey:key];
    NSMutableAttributedString *attributedString;
    NSString* stringForKey = [NSString stringWithFormat:@"%@", [_linesDic objectForKey:key]];
    for (int j =0 ; i < (int)[duplicateWords count]; j++) {
        NSString* word = [duplicateWords objectAtIndex:j];
        NSRange range = [stringForKey rangeOfString:word];
       attributedString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@", [_linesDic objectForKey:key]] /*attributes:@{NSForegroundColorAttributeName : [NSColor redColor]}*/];
        [attributedString addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:range];
    }
    [dupliDic setObject:attributedString forKey:key];
}


[_duplicatesTextField setHidden:NO];
NSLog(@"%@", dupliDic);
[_duplicatesTextField setStringValue:[NSString stringWithFormat:@"%@", dupliDic]];
4

0 回答 0