Parsing a arbitrary textual data format - objective-c

I am trying to parse a .rtf file that was created by someone else, and I don't have control over the file contents or format. There are several blocks to the file and each block has a set of information that I need to get. Each block is set up like this:
[Title]
[Type] ([sub type])
Level: [CSV list of levels]
Components: [CSV list of components]
Time: [proprietary time format]
Length: [length value]
Target: [target text]
Dwell: [dwell time in proprietary time format]
Saves: [yes/no]
Additional Information: [additional information]
[notes]
There may be from 50 to 100 blocks like the one above in each file. I have used the NSRegularExpression class to do some other parsing in my app, but I can't even think about how to accomplish this.
As far as I can tell, each block is separated by a double line.

Try using a NSScanner, like this:
NSString *input =
#"[Title]\n"
#"[Type] ([sub type])\n"
#"Level: [CSV list of levels]\n"
#"Components: [CSV list of components]\n"
#"Time: [proprietary time format]\n"
#"Length: [length value]\n"
#"Target: [target text]\n"
#"Dwell: [dwell time in proprietary time format]\n"
#"Saves: [yes/no]\n"
#"Additional Information: [additional information]\n"
#"[notes]\n";
NSString *title, *type, *subType, *level, *components, *time, *length, *target, *dwell, *saves, *additional, *notes;
title = type = subType = level = components = time = length = target = dwell = saves = additional = notes = nil;
NSScanner *scanner = [NSScanner scannerWithString:input];
// read the first line into title...
[scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&title];
[scanner scanCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:nil];
// read the first part of the second line into type
[scanner scanUpToString:#" (" intoString:&type];
[scanner scanString:#"(" intoString:nil];
// read the next part of the second line into subType
[scanner scanUpToString:#")" intoString:&subType];
// read the end of the line
[scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:nil];
[scanner scanCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:nil];
// read in level
[scanner scanString:#"Level: " intoString:nil];
[scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&level];
[scanner scanCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:nil];
// read in components:
[scanner scanString:#"Components: " intoString:nil];
[scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&components];
[scanner scanCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:nil];
// read in time:
[scanner scanString:#"Time: " intoString:nil];
[scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&time];
[scanner scanCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:nil];
// read in length
[scanner scanString:#"Length: " intoString:nil];
[scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&length];
[scanner scanCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:nil];
// complete for all other metadata
NSLog(#"%#", title);
NSLog(#"%# (%#)", type, subType);
NSLog(#"%#", level);
NSLog(#"%#", components);
NSLog(#"%#", time);
NSLog(#"%#", length);
NSLog(#"%#", target);
NSLog(#"%#", dwell);
NSLog(#"%#", saves);
NSLog(#"%#", additional);
NSLog(#"%#", notes);
This works for me, obviously complete the process for all the other fields.

For the time being, I can just convert the .rtf files to regular text files. This allows me to process them more easily.
Thanks for the help! I am going to look into using the NSScanner to do this more elegantly.

Related

How can I modify this SRT file parser?

I found some good code for parsing .srt files on stackoverflow (Parsing SRT file with Objective C) shown below:
NSScanner *scanner = [NSScanner scannerWithString:[theTextView string]];
while (![scanner isAtEnd])
{
#autoreleasepool
{
NSString *indexString;
(void) [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&indexString];
NSString *startString;
(void) [scanner scanUpToString:#" --> " intoString:&startString];
(void) [scanner scanString:#"-->" intoString:NULL];
NSString *endString;
(void) [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&endString];
NSString *textString;
(void) [scanner scanUpToString:#"\r\n\r\n" intoString:&textString];
textString = [textString stringByReplacingOccurrencesOfString:#"\r\n" withString:#" "];
textString = [textString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
indexString, #"index",
startString, #"start",
endString , #"end",
textString , #"text",
nil];
NSLog(#"%#", dictionary);
}
}
I have a number of .srt files from a TV series that contain a lot of ‘credit’ subs which kinda spoil the experience and coded them out, leaving me with non-sequential indexes like this:
// deleted subtitles
3
00:00:11,070 --> 00:00:14,466
Screenwriter: Name here...
4
00:00:14,633 --> 00:00:17,466
Music: Name here...
5
00:00:17,686 --> 00:00:20,680
Narrator: Name here...
// deleted subtitle
7
00:01:17,966 --> 00:01:21,966
Episode 12
which chokes FCPX when I try to import the file. I’m completely new to NSScanner and tried everything I can think of without success. I'd appreciate any help in modifying the above just to skip the sub index line altogether (if possible?). I'm okay with adding them back in sequentially with separate code. Thanks!
UPDATE:
Thanks for your suggestion of indexing through the 'while' loop skaak, but the problem still seems to defy logic as it never increases beyond the very first pass (!!). The logs are shown below - firstly using an NSDictionary and then appending to an NSMutableString (probably more useful for my purposes). Note that in both cases the first sub does get changed to 1, but indices 4,5,7 remain unchanged rather than being renumbered 2,3,4.
2020-07-29 18:35:26.267 SRT Editor[12494:903]
{
end = "00:00:14,466";
index = 1;
start = "00:00:11,070";
text = "Screenwriter: Hashida Sugako\n\n4 00:00:14,633 --> 00:00:17,466 Music: Sakada Koichi\n\n5 00:00:17,686 --> 00:00:20,680 Narrator: Naraoka Tomoko\n\n7 00:01:28,633 --> 00:01:34,233 It was early spring in 1958...
}
2020-07-29 18:51:15.612 SRT Editor[12646:903]
1
00:00:11,07000:00:14,466Screenwriter: Hashida Sugako
4 00:00:14,633 --> 00:00:17,466 Music: Sakada Koichi
5 00:00:17,686 --> 00:00:20,680 Narrator: Naraoka Tomoko
7 00:01:28,633 --> 00:01:34,233 It was early spring in 1958...
Another puzzling observation is that if I put in a loopCounter++ it also suggests the 'while' loop only makes one pass through which baffles me, though I did mention being unfamiliar with NSScanner.
Try this
NSUInteger index = 1;
NSScanner *scanner = [NSScanner scannerWithString:[theTextView string]];
while (![scanner isAtEnd])
{
#autoreleasepool
{
NSString *indexString;
(void) [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&indexString];
NSString *startString;
(void) [scanner scanUpToString:#" --> " intoString:&startString];
(void) [scanner scanString:#"-->" intoString:NULL];
NSString *endString;
(void) [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&endString];
NSString *textString;
(void) [scanner scanUpToString:#"\r\n\r\n" intoString:&textString];
textString = [textString stringByReplacingOccurrencesOfString:#"\r\n" withString:#" "];
textString = [textString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
// Use my own incremental index
#( index ), #"index",
startString, #"start",
endString , #"end",
textString , #"text",
nil];
NSLog(#"%#", dictionary);
// Move to next index
index ++;
}
}

Remove contents between script and style tags in Objective-C

Alright, so I am working on a web crawler that can take webpages and convert them into passages of text. To remove the tags themselves, I found this on Stack Overflow:
- (NSString *) stripTags:(NSString *)str
{
NSMutableString *ms = [NSMutableString stringWithCapacity:[str length]];
NSScanner *scanner = [NSScanner scannerWithString:str];
[scanner setCharactersToBeSkipped:nil];
NSString *s = nil;
while (![scanner isAtEnd])
{
[scanner scanUpToString:#"<" intoString:&s];
if (s != nil)
[ms appendString:s];
[scanner scanUpToString:#">" intoString:NULL];
if (![scanner isAtEnd])
[scanner setScanLocation:[scanner scanLocation]+1];
s = nil;
}
return ms;
}
And it works, however, it only removes the tags, not the contents between script and style tags (as obviously I don't want the contents between all tags to be removed as that would result in an empty string).
Is there any way I can have specifically the script and style tags truncated?
Thanks a lot in advance.
EDIT:
I have tried changing my code to:
- (NSString *) stripTags:(NSString *)str
{
NSMutableString *ms = [NSMutableString stringWithCapacity:[str length]];
NSScanner *scanner = [NSScanner scannerWithString:str];
[scanner setCharactersToBeSkipped:nil];
NSString *s = nil;
while (![scanner isAtEnd])
{
[scanner scanUpToString:#"<script" intoString:&s];
if (s != nil)
[ms appendString:s];
[scanner scanUpToString:#"script>" intoString:NULL];
if (![scanner isAtEnd])
[scanner setScanLocation:[scanner scanLocation]+1];
[scanner scanUpToString:#"<" intoString:&s];
if (s != nil)
[ms appendString:s];
[scanner scanUpToString:#">" intoString:NULL];
if (![scanner isAtEnd])
[scanner setScanLocation:[scanner scanLocation]+1];
s = nil;
}
return ms;
}
but the scripts and css is still being included
You can edit the scanner code so that you can check the tags. If the tag is one you want to remove then you can scan to the closing tag and just discard the string. You not then you can store / append the string.
Read up to the tag start (<)' then read the tag so you can check what it is. Then read to the tag close and either drop it or save it.
Start with something like (typed inline and not tested in any way):
while (![scanner isAtEnd])
{
[scanner scanUpToString:#"<" intoString:&s];
if (s != nil)
[ms appendString:s];
[scanner scanUpToString:#">" intoString:&t];
if ([t isEqualToString:#"tagToIgnore"]) {
[scanner scanUpToString:#"<" intoString:NULL];
[scanner setScanLocation:[scanner scanLocation]-1];
s = nil;
t = nil;
continue;
}
if (![scanner isAtEnd])
[scanner setScanLocation:[scanner scanLocation]+1];
s = nil;
t = nil;
}

Get certain values in a NSString

I have this NSString below. I want to be able to get the name of each application and the location of it. Can someone help me or point me in the right direction?
Thanks in advance.
Applications:
Xcode:
Version: 4.5.2
Last Modified: 11/3/12 11:45 PM
Kind: Intel
64-Bit (Intel): Yes
App Store: Yes
Location: /Applications/Xcode.app
Terminal:
Version: 2.3
Last Modified: 6/21/12 12:01 AM
Kind: Intel
64-Bit (Intel): Yes
App Store: No
Location: /Applications/Utilities/Terminal.app
Google Chrome:
Version: 23.0.1271.64
Last Modified: 10/31/12 7:59 PM
Kind: Intel
64-Bit (Intel): No
App Store: No
Location: /Applications/Google Chrome.app
App Store:
Version: 1.2.1
Last Modified: 4/18/12 7:52 PM
Kind: Intel
64-Bit (Intel): Yes
App Store: No
Location: /Applications/App Store.app
Create an instance of NSScanner using your string. Since instances of NSScanner default to skipping whitespace and newlines by default, you need to disable this behavior before proceeding, like this:
[scanner setCharactersToBeSkipped:[[[NSCharacterSet alloc] init] autorelease]];
From here, you'd do something like this:
NSString *appName = nil;
[scanner scanString:#"Applications:" intoString:NULL];
NSCharacterSet *charset = [NSCharacterSet newlineCharacterSet];
[scanner scanCharactersFromSet:charset intoString:NULL];
while ([scanner isAtEnd] == NO)
{
// Get your application name.
[scanner scanUpToCharactersFromSet:charset intoString:&appName];
[scanner scanCharactersFromSet:charset intoString:NULL];
// You could do something with your application name here.
// Skip over the other stuff.
for (NSUInteger idx = 0; idx < 6; idx++)
{
[scanner scanUpToString:#":" intoString:NULL];
[scanner scanString:#":" intoString:NULL];
[scanner scanUpToCharactersFromSet:charset intoString:NULL];
[scanner scanCharactersFromSet:charset intoString:NULL];
}
}
Note that, despite appearances, I didn't give you a complete solution. Beyond checking for the end of the string, there's no error checking, and it goes without saying that Real Applications check for errors. (There should even have been a check for end-of-string at the very outset.) Also, this snippet relies on a rigid presentation of the data, such as you have provided.
I would use componentsSeparatedByString if there are easy delimiters. If not, try to use a regex with NSRegularExpression to find the value
There are numerous ways to solve this. NSRegularExpression has been mentioned. NSScanner is another. Here's a potential solution (no guarantees about correctness - just a guide.)
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
#autoreleasepool {
NSString *source = #"Applications:\n\nXcode:\n\n Version: 4.5.\n Last Modified: 11/3/12 11:45 PM\n Kind: Intel\n 64-Bit (Intel): Yes\n App Store: Yes\n Location: /Applications/Xcode.app";
NSScanner *scanner = [NSScanner scannerWithString:source];
[scanner scanUpToString:#":" intoString:NULL];
[scanner scanString:#":" intoString:NULL];
while( ![scanner isAtEnd] ) {
NSString *appName = nil;
NSString *appPath = nil;
[scanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:NULL];
[scanner scanCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:&appName];
NSString *junk = nil;
[scanner scanUpToString:#"Location: " intoString:&junk];
[scanner scanString:#"Location: " intoString:NULL];
[scanner scanUpToString:#"\n" intoString:&appPath];
[scanner scanString:#"\n" intoString:NULL];
printf("app name = %s, path = %s\n",[appName UTF8String],[appPath UTF8String]);
}
}
return 0;
}

Objective C: Get Substring between Double Quotes

What would be the best way to get every substring between double quotes and make it into an array?
For example, if the string (NSString) is:
#"abcd \"efgh\" ijklm \"no\" p \"qrst\" uvwx \"y\" z"
I want the result to be:
{#"efgh", #"no", #"qrst", #"y"}
as an NSArray.
This should get you started:
NSString *str = #"abcd \"efgh\" ijklm \"no\" p \"qrst\" uvwx \"y\" z";
NSMutableArray *target = [NSMutableArray array];
NSScanner *scanner = [NSScanner scannerWithString:str];
NSString *tmp;
while ([scanner isAtEnd] == NO)
{
[scanner scanUpToString:#"\"" intoString:NULL];
[scanner scanString:#"\"" intoString:NULL];
[scanner scanUpToString:#"\"" intoString:&tmp];
if ([scanner isAtEnd] == NO)
[target addObject:tmp];
[scanner scanString:#"\"" intoString:NULL];
}
for (NSString *item in target)
{
NSLog(#"%#", item);
}
One way would be to use componentsSeparatedByString: to split them based on ". This should give you an array of words the count of which should be odd. Filter all the even numbered words into an array. This should be your desired array.
Alternatively look at NSPredicate.

How to scan this string?

Suppose I have a string like this: Conditions for Paris, FR at 10:29 pm CET.
I'm wanting to grab the time: 10:29.
I know how to scan a string into another string up to a certain point, like this:
NSString *something;
NSScanner *scanner = [NSScanner scannerWithString:#"Sometest_something"];
[scanner scanUpToString:#"_" intoString:&something];
And the result will be Sometest.
However, this doesn't seem to help me get the time string I'm looking for. Any ideas? I'm sure it's quite simple.
Update: I also need to have am/pm as a separate string. For getting the time, Caleb's answer works perfectly. But I still haven't figured out how to get am/pm. Any ideas?
Yep, here we go, figured it out. :)
NSScanner *scanner = [NSScanner scannerWithString:timestring];
NSString *ampm;
[scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:nil];
[scanner scanUpToCharactersFromSet:[NSCharacterSet letterCharacterSet] intoString:nil];
[scanner scanUpToCharactersFromSet:[NSCharacterSet uppercaseLetterCharacterSet] intoString:&ampm];
It's not clear from your question, but I'm guessing that the "Paris, FR" part of the string might change but the rest of the string (obviously not including the time) is constant? If so, you could:
[scanner scanUpToString:#" at " intoString:nil];
[scanner scanString:#" at " intoString:nil];
[scanner scanUpToCharactersFromSet:[NSCharacterSet letterCharacterSet] intoString:&something];
Edit: Come to think of it, if you know that there are no digits before the time, you could just use -scanUpToCharactersFromSet: for the whole thing:
[scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:nil];
[scanner scanUpToCharactersFromSet:[NSCharacterSet letterCharacterSet] intoString:&something];