【问题标题】:Replacing bad words in a string in Objective-C在 Objective-C 中替换字符串中的坏词
【发布时间】:2013-11-29 16:10:40
【问题描述】:

我有一个带有公开高分列表的游戏,我允许层输入他们的名称(或任何不超过 12 个字符的名称)。我正在尝试创建几个函数来从坏词列表中过滤掉坏词

我有一个文本文件。我有两种方法:

一个读入文本文件:

-(void) getTheBadWordsAndSaveForLater {

    badWordsFilePath = [[NSBundle mainBundle] pathForResource:@"badwords" ofType:@"txt"];
    badWordFile = [[NSString alloc] initWithContentsOfFile:badWordsFilePath encoding:NSUTF8StringEncoding error:nil];

    badwords =[[NSArray alloc] initWithContentsOfFile:badWordFile];
    badwords = [badWordFile componentsSeparatedByString:@"\n"];


    NSLog(@"Number Of Words Found in file: %i",[badwords count]);

    for (NSString* words in badwords) {

        NSLog(@"Word in Array----- %@",words);
    }


}

还有一个检查单词(NSString*) 和我读到的列表:

-(NSString *) removeBadWords :(NSString *) string {


    // If I hard code this line below, it works....
    // *****************************************************************************
    //badwords =[[NSMutableArray alloc] initWithObjects:@"shet",@"shat",@"shut",nil];
    // *****************************************************************************


    NSLog(@"checking: %@",string);

    for (NSString* words in badwords) {

       string = [string stringByReplacingOccurrencesOfString:words withString:@"-" options:NSCaseInsensitiveSearch range:NSMakeRange(0, string.length)];

        NSLog(@"Word in Array: %@",words);
    }

     NSLog(@"Cleaned Word Returned: %@",string);
    return string;
}

我遇到的问题是,当我将单词硬编码到一个数组中时(参见上面的注释),它就像一个魅力。但是当我使用第一种方法读入的数组时,它不起作用 - stringByReplacingOccurrencesOfString:words 似乎没有效果。我已经追踪到日志,所以我可以查看是否有单词通过并且它们是……除非我硬核到数组中,否则一行似乎看不到单词。

有什么建议吗?

【问题讨论】:

  • 您的代码没有多大意义。您从 badWordsFilePath 中的文件加载 badWordFile,然后从 badWordsFile 中的文件加载坏词。然后用通过 componentsSeparatedByString 处理的 badWordsFile 覆盖该值(无论它是什么)。
  • 但您的主要问题可能是未能从读取数组的元素中删除回车符(这可能是使用记事本创建的,或者添加回车符的东西)。尝试stringByTrimmingCharactersInSetwhitespaceAndNewlineCharacterSet(在每个单词上)。
  • 顺便说一句,这个计划充满了危险。许多完全合法的词在其中包含“坏”词。甚至(稍微)“坏”的词在不同的上下文中也完全可以:“新落雪胸前的月亮,给下面的物体带来正午的光泽”。
  • 感谢 HOTLICKS 的建议 - 你能告诉我我对 XCODE 有点陌生吗?至于险恶的方案,我同意并欢迎更好的建议——我知道如果我不在那里设置任何保护措施,它就会被滥用。
  • HOTLICKS - 成功了!非常感谢!-

标签: objective-c file nsstring


【解决方案1】:

一些想法:

  1. 你有两行:

    badwords =[[NSArray alloc] initWithContentsOfFile:badWordFile];
    badwords = [badWordFile componentsSeparatedByString:@"\n"];
    

    如果您只是要在下一行用componentsSeparatedByString 替换它,那么这样做initWithContentsOfFile 是没有意义的。另外,initWithContentsOfFile 假定该文件是一个属性列表 (plist),但您的其余代码显然假定它是一个换行符分隔的文本文件。就个人而言,我会使用 plist 格式(它避免了从单个单词中修剪空格的需要),但您可以使用任何您喜欢的格式。但请使用其中一种,但不能同时使用。

    如果您仍然使用换行符分隔的坏词列表,那么只需删除显示initWithContentsOfFile 的那一行,无论如何您都会忽略其结果。因此:

    - (void)getTheBadWordsAndSaveForLater {
    
        // these should be local variables, so get rid of your instance variables of the same name
    
        NSString *badWordsFilePath = [[NSBundle mainBundle] pathForResource:@"badwords" ofType:@"txt"];
        NSString *badWordFile = [[NSString alloc] initWithContentsOfFile:badWordsFilePath encoding:NSUTF8StringEncoding error:nil];
    
        // calculate `badwords` solely from `componentsSeparatedByString`, not `initWithContentsOfFile`
    
        badwords = [badWordFile componentsSeparatedByString:@"\n"];
    
        // confirm what we got
    
        NSLog(@"Found %i words: %@", [badwords count], badwords);
    }
    
  2. 您可能只想查找整个单词的出现,而不仅仅是任何地方出现的坏词:

    - (NSString *) removeBadWords:(NSString *) string {
    
        NSLog(@"checking: %@ for occurrences of these bad words: %@", string, badwords);
    
        for (NSString* badword in badwords) {
            NSString *searchString = [NSString stringWithFormat:@"\\b%@\\b", badword];
            string = [string stringByReplacingOccurrencesOfString:searchString
                                                       withString:@"-"
                                                          options:NSCaseInsensitiveSearch | NSRegularExpressionSearch
                                                            range:NSMakeRange(0, string.length)];
        }
    
        NSLog(@"resulted in: %@", string);
    
        return string;
    }
    

    这使用“正则表达式”搜索,其中\b 代表“单词之间的边界”。因此,\bhell\b(或者,因为反斜杠必须在 NSString 文字中引用,即 @"\\bhell\\b")将搜索单词“hell”,它是一个单独的单词,但不会匹配“hello”,因为例子。

  3. 注意,在上面,我还记录了badwords 以查看该变量是否以某种方式被重置。鉴于您描述的症状,这是唯一有意义的事情,即从文本文件中加载坏词有效,但替换过程失败。所以在更换之前检查badwords,并确保它仍然设置正确。

【讨论】:

  • 感谢您的指导。效果很好!
猜你喜欢
  • 2010-10-14
  • 2011-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多