【问题标题】:Printing the most frequent words in a file(string) Objective-C打印文件中最常见的单词(字符串)Objective-C
【发布时间】:2014-04-15 06:44:44
【问题描述】:

刚接触objective-c,需要帮助解决这个问题:

编写一个带有两个参数的函数:

  • 1 表示文本文档的字符串和

  • 2 一个整数,提供要返回的项目数。实现该函数,使其返回按词频排序的字符串列表,首先出现最频繁的词。使用您的最佳判断来决定如何分隔单词。您的解决方案应该在 O(n) 时间内运行,其中 n 是文档中的字符数。像对生产/商业系统一样实现此功能。您可以使用任何标准数据结构。

到目前为止我尝试了什么(正在进行中):` // 函数正在进行中

// -(NSString *) wordFrequency:(int)itemsToReturn  inDocument:(NSString *)textDocument ;
//  Get the desktop directory (where the text document is)

NSURL *desktopDirectory = [[NSFileManager defaultManager] URLForDirectory:NSDesktopDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];

 //  Create full path to the file
 NSURL *fullPath = [desktopDirectory URLByAppendingPathComponent:@"document.txt"];

 //  Load the string
 NSString *content = [NSString stringWithContentsOfURL:fullPath encoding:NSUTF8StringEncoding error:nil];
 //  Optional code for confirmation - Check that the file is here and print its content to the console
 //  NSLog(@" The string is:%@", content);

 // Create an array with the words contain in the string
  NSArray *myWords = [content componentsSeparatedByString:@" "];

 //  Optional code for confirmation - Print content of the array to the console
 //  NSLog(@"array: %@", myWords);
 //  Take an NSCountedSet of objects in an array and order those objects by their object count then returns a sorted array, sorted in descending order by the count of the objects.

  NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:myWords];
  NSMutableArray *dictArray = [NSMutableArray array];
  [countedSet enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
  [dictArray addObject:@{@"word": obj,
                               @"count": @([countedSet countForObject:obj])}];
    }];

  NSLog(@"Words sorted by count: %@", [dictArray sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"count" ascending:NO]]]);
 }
return 0;
 }

【问题讨论】:

  • 您的问题是什么?你的算法有效吗?如果没有,你会得到什么结果? - 如果您的问题是关于改进工作代码,那么最好在codereview.stackexchange.com提问。
  • 嗨 Martin,我希望有人能帮助我解决这个编码挑战,因为我被困在了 Objective-c 的新手。我在其他讨论中看到了 ruby​​ 中相同问题的编码示例,但我希望一些人在 Objective-c 中看到一些代码示例,以便更好地理解并让我走上正确的道路。
  • 算法工作我得到类似的东西:2014-04-15 02:59:51.387 [6666:303] 按计数排序的单词:( { count = 6; word = and; }, { count = 5; word = a; }, { count = 5; word = the; }, { count = 3; word = of; 但我不知道如何将此代码集成到适当的函数中

标签: objective-c algorithm sorting big-o


【解决方案1】:

这是 ma​​p-reduce 的经典工作。我非常熟悉objective-c,但据我所知-这些概念很容易在其中实现。

第一个 map-reduce 正在计算出现次数。
这一步基本上就是按照单词对元素进行分组,然后进行计数。

map(text):
   for each word in text:
       emit(word,'1')
reduce(word,list<number>):
    emit (word,sum(number))

使用 map-reduce 的另一种方法是使用迭代计算和哈希映射,它是一个统计每个单词出现次数的直方图。

在你得到一个数字和出现的列表之后,你所要做的就是从它们中找出前 k 个。这个线程很好地解释了这一点:Store the largest 5000 numbers from a stream of numbers
在这里,“比较器”是每个单词的#occurrances,如上一步计算的那样。

基本思想是使用最小堆,并在其中存储k 的第一个元素。
现在,迭代剩余的元素,如果新元素大于顶部(堆中的最小元素),则移除顶部并用新元素替换它。

最后,您有一个包含 k 最大元素的堆,并且它们已经在一个堆中 - 所以它们已经排序(虽然顺序相反,但处理起来相当容易)。

复杂度为O(nlogK)

要实现O(n + klogk),你可以使用selection algorithm而不是min-heap方案来获取top-k,然后对检索到的元素进行排序。

【讨论】:

    猜你喜欢
    • 2014-12-10
    • 2014-12-14
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多