Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c iOS-查找字符串中单词出现次数的最有效方法_Objective C_Ios_Nsstring_Nsset - Fatal编程技术网

Objective c iOS-查找字符串中单词出现次数的最有效方法

Objective c iOS-查找字符串中单词出现次数的最有效方法,objective-c,ios,nsstring,nsset,Objective C,Ios,Nsstring,Nsset,给定一个字符串,我需要获得该字符串中出现的每个单词的计数。为此,我按单词将字符串提取到一个数组中,并以这种方式进行搜索,但我觉得直接搜索字符串更为理想。下面是我最初为解决这个问题而编写的代码。不过,我想听听关于更好解决方案的建议 NSMutableDictionary *sets = [[NSMutableDictionary alloc] init]; NSString *paragraph = [[NSString alloc] initWithContentsOfFile:[[NSBun

给定一个字符串,我需要获得该字符串中出现的每个单词的计数。为此,我按单词将字符串提取到一个数组中,并以这种方式进行搜索,但我觉得直接搜索字符串更为理想。下面是我最初为解决这个问题而编写的代码。不过,我想听听关于更好解决方案的建议

NSMutableDictionary *sets = [[NSMutableDictionary alloc] init];

NSString *paragraph = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"text" ofType:@"txt"] encoding:NSUTF8StringEncoding error:NULL];

NSMutableArray *words = [[[paragraph lowercaseString] componentsSeparatedByString:@" "] mutableCopy];

while (words.count) {
    NSMutableIndexSet *indexSet = [[NSMutableIndexSet alloc] init];
    NSString *search = [words objectAtIndex:0];
    for (unsigned i = 0; i < words.count; i++) {
        if ([[words objectAtIndex:i] isEqualToString:search]) {
            [indexSet addIndex:i];
        }
    }
    [sets setObject:[NSNumber numberWithInt:indexSet.count] forKey:search];
    [words removeObjectsAtIndexes:indexSet];
}

NSLog(@"%@", sets);
NSMutableDictionary*set=[[NSMutableDictionary alloc]init];
NSString*段落=[[NSString alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@“txt”类型的“text”编码:NSUTF8StringEncoding错误:NULL];
NSMUTABLEARRY*字=[[[段落小写]由字符串分隔的组件:@“]mutableCopy];
while(words.count){
NSMutableIndexSet*indexSet=[[NSMutableIndexSet alloc]init];
NSString*search=[words objectAtIndex:0];
for(无符号i=0;i
示例:

起始字符串:
“这是一个测试。这只是一个测试。”

结果:

  • “这个”-2
  • “是”-2
  • “a”-2
  • “测试”-2
  • “仅”-1

如果要我猜的话,我会说
NSRegularExpression
。像这样:

NSUInteger numberOfMatches = [regex numberOfMatchesInString:string
                                                    options:0
                                                      range:NSMakeRange(0, [string length])];
这段话是从我的书里抄来的


编辑1.0:

根据蒂尔爵士所说:

NSString *string = @"This is a test, so it is a test";

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
NSArray *arrayOfWords = [string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
for (NSString *word in arrayOfWords)
{
    if ([dictionary objectForKey:word])
    {
        NSNumber *numberOfOccurences = [dictionary objectForKey:word];
        NSNumber *increment = [NSNumber numberWithInt:(1 + [numberOfOccurences intValue])];
        [dictionary setValue:increment forKey:word];
    }
    else
    {
        [dictionary setValue:[NSNumber numberWithInt:1] forKey:word];
    }
}
您应注意以下事项:

  • 标点符号。(接近其他词语)
  • 大写字母与小写字母

我认为你试图在长段落中用循环搜索单词,这真是个坏主意。您应该使用正则表达式来实现这一点!我知道第一次学它并不容易,但知道它真的很值得!看看这个案例

这正是
NSCountedSet
的作用

您需要将字符串拆分为单词(iOS很好,为我们提供了一个函数,这样我们就不必担心标点),然后将每个单词添加到计数集中,这样可以跟踪每个对象在集合中出现的次数:

NSString     *string     = @"This is a test. This is only a test.";
NSCountedSet *countedSet = [NSCountedSet new];

[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
                           options:NSStringEnumerationByWords | NSStringEnumerationLocalized
                        usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop){

                            // This block is called once for each word in the string.
                            [countedSet addObject:substring];

                            // If you want to ignore case, so that "this" and "This" 
                            // are counted the same, use this line instead to convert
                            // each word to lowercase first:
                            // [countedSet addObject:[substring lowercaseString]];
                        }];

NSLog(@"%@", countedSet);

// Results:  2012-11-13 14:01:10.567 Testing App[35767:fb03] 
// <NSCountedSet: 0x885df70> (a [2], only [1], test [2], This [2], is [2])
NSString*string=@“这是一个测试。这只是一个测试。”;
NSCountedSet*countedSet=[NSCountedSet new];
[string EnumerateSubstringsRange:NSMakeRange(0,[字符串长度])
选项:NSStringEnumerationByWords | NSStringEnumerationLocalized
使用block:^(NSString*子字符串、NSRange substringRange、NSRange enclosuringrange、BOOL*停止){
//该块对于字符串中的每个单词调用一次。
[countedSet addObject:子字符串];
//如果你想忽略这个案例,那么“这个”和“这个”
//如果计数相同,则使用此行转换
//每个单词先小写:
//[countedSet addObject:[子字符串小写字符串]];
}];
NSLog(@“%@”,计数集);
//结果:2012-11-13 14:01:10.567测试应用[35767:fb03]
//(a[2],仅[1],测试[2],此[2]为[2])

我需要对每个单词进行计数。所以在字符串中,“这是一个测试。这只是一个测试。”“测试”的计数为2,“这”的计数为2,“仅”的计数为1,等等!这比我想象的还要容易!谢谢大家!@但这里有一个小问题,当我想计算NSString中包含一些html标记的单词数时,它会跳过它们。但我也要数一数。有什么想法吗?你的方法行吗?你对点符号没有问题吗?我想你应该有“测试”而不是“测试”。