Last active
January 23, 2016 02:59
-
-
Save ygit/533854aff04ad5d02b30 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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