Skip to content

Instantly share code, notes, and snippets.

@ygit
Last active January 23, 2016 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ygit/533854aff04ad5d02b30 to your computer and use it in GitHub Desktop.
Save ygit/533854aff04ad5d02b30 to your computer and use it in GitHub Desktop.
NSString *fileURL = @"https://s3-ap-southeast-1.amazonaws.com/haptikdev/test/Conversation.txt";
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
delegate:self
delegateQueue:nil];
NSURLSessionDownloadTask *getFile = [session downloadTaskWithURL:[NSURL URLWithString:fileURL]
completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSError *extractErr = nil;
NSString *fetchedStr = [NSString stringWithContentsOfURL:location
encoding:NSUTF8StringEncoding error:&extractErr];
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"<>"];
NSScanner *scanner = [[NSScanner alloc] initWithString:fetchedStr];
scanner.charactersToBeSkipped = charSet;
BOOL isName = YES;
NSString *name;
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
while ([scanner scanUpToCharactersFromSet:charSet intoString:&name]) {
if (isName) {
NSInteger senderCount = [[dict objectForKey:name] integerValue];
[dict setObject:[NSNumber numberWithInteger:++senderCount] forKey:name];
}
isName = !isName;
}
NSLog(@"dict : %@", dict);
NSNumber *maxCount = [NSNumber numberWithInteger:0];
NSNumber *secondMaxCount = [NSNumber numberWithInteger:0];
for (NSString *senderName in dict) {
NSNumber *senderTotalMsgCount = [dict objectForKey:senderName];
if (maxCount < senderTotalMsgCount) {
maxCount = senderTotalMsgCount;
}
else if (secondMaxCount < senderTotalMsgCount) {
secondMaxCount = senderTotalMsgCount;
}
}
NSArray *maxSenders = [dict allKeysForObject:maxCount];
NSArray *secondMaxSenders = [dict allKeysForObject:secondMaxCount];
NSLog(@"Top message senders : %@", maxSenders);
NSLog(@"Second top message senders : %@", secondMaxSenders);
}];
[getFile resume];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment