// // WordContainer.m // Duncan // // Created by Risa Mettaserikul on Sun Feb 16 2003. // Copyright (c) 2003 CSFSS. All rights reserved. // #import #import "WordContainer.h" @implementation WordContainer - (id)init { self = [super init]; isDirty = NO; isChanged = NO; isSearch = NO; isReady = YES; [self setString:[NSString stringWithFormat:@"A Container"]]; wordList = [[NSMutableArray alloc] init]; return self; } - (id)initWithName:(NSString *)newName { self = [self init]; [self setString:newName]; return self; } - (void) byPassQuery:(id)datarep withParentPath:(NSString *)parentpath { [super query:datarep withParentPath:parentpath]; } - (void) query:(id)datarep withParentPath:(NSString *)parentpath { [self byPassQuery:datarep withParentPath:parentpath]; NSEnumerator *objEnum; id word; id pathString = [NSString stringWithFormat:@"%@/%@",parentpath,string]; objEnum = [self objectEnumerator]; while ((word = [objEnum nextObject])) { [word query:datarep withParentPath:pathString]; } } - (void) _addEntriesFromDictionary:(NSDictionary *)dict { NSLog(@"addEntries %@, %d",string,[NSThread currentThread]); NSEnumerator *enumerator = [dict keyEnumerator]; id key; if ((key = [enumerator nextObject])) { isDirty = YES; isChanged = YES; do { WordContainer *container = nil; id def = [dict objectForKey:key]; NSRange range = [def rangeOfString:@"###S. "]; if (range.location != NSNotFound) { id defs = [def substringFromIndex:range.location + 6]; def = [def substringToIndex:range.location]; defs = [defs componentsSeparatedByString:@", "]; container = [[WordContainer alloc] init]; [container setString:key]; [container setDefinition:def]; [wordList addObject:container]; NSEnumerator *en = [defs objectEnumerator]; id word; while ((word = [en nextObject])) { [container addWord:[[Word alloc] initWithString:word definition:[NSString stringWithFormat:@"(S) %@",key]]]; } } range = [def rangeOfString:@"###A. "]; if (range.location != NSNotFound) { id defs = [def substringFromIndex:range.location + 6]; if ([def length] > range.location) def = [def substringToIndex:range.location]; defs = [defs componentsSeparatedByString:@", "]; if (container == nil) { container = [[WordContainer alloc] init]; [container setString:key]; [container setDefinition:def]; [wordList addObject:container]; } NSEnumerator *en = [defs objectEnumerator]; id word; while ((word = [en nextObject])) { [container addWord:[[Word alloc] initWithString:word definition:[NSString stringWithFormat:@"(A) %@",key]]]; } } range = [def rangeOfString:@"###SW. "]; if (range.location != NSNotFound) { id defs = [def substringFromIndex:range.location + 7]; if ([def length] > range.location) def = [def substringToIndex:range.location]; defs = [defs componentsSeparatedByString:@", "]; if (container == nil) { container = [[WordContainer alloc] init]; [container setString:key]; [container setDefinition:def]; [wordList addObject:container]; } NSEnumerator *en = [defs objectEnumerator]; id word; while ((word = [en nextObject])) { [container addWord:[[Word alloc] initWithString:word definition:[NSString stringWithFormat:@"(SW) %@",key]]]; } } if (container == nil) [wordList addObject:[[Word alloc] initWithString:key definition:def]]; } while ((key = [enumerator nextObject])); } NSLog(@"exit addEntries %@, %d",string,[NSThread currentThread]); } - (int)count { NSLog(@"in count %d",[NSThread currentThread]); if (isReady) { int n = [[self sortedIndexes] count]; return n; } return 0; } - (NSString *)counter { return [NSString stringWithFormat:@"%d",[self count]]; } - (BOOL)isReady { return isReady; } - (void)addWord:(Word *)aWord { if ([aWord isKindOfClass:[Word class]]) { isDirty = YES; isChanged = YES; [wordList addObject:aWord]; } } - (NSEnumerator *)objectEnumerator { id obje; obje = [[self sortedIndexes] objectEnumerator]; return obje; } - (NSArray *)findWordEqualToString:(NSString *)aString { if (isSearch) return nil; isSearch = YES; // FIXME: this may raises a threading issue. NSEnumerator *objEnum; NSMutableArray *anArray; anArray = [NSMutableArray array]; id word; objEnum = [self objectEnumerator]; while ((word = [objEnum nextObject])) // wonder if using IMP on nextObject... { /* score = [word scoreWithString:aString]; if (score < 3) // broke, it use cstring, try something else NSLog(@"good %@ %d",[word string],score);*/ NSArray *wp; if ((wp = [word findWordEqualToString:aString]) != nil) // use IMP here too. { NSEnumerator *retEnum = [wp objectEnumerator]; Word *w; while ((w = [retEnum nextObject])) { [w setPath:[NSString stringWithFormat:@"%@/%@",[self string],[w path]]]; } [anArray addObjectsFromArray:wp]; /* do stemming here, need a flag that will force the path to actually return something if it it didn't contain the item, may be a wrapp er over this function */ } } // [anArray addObjectsFromArray:[super findWordEqualToString:aString]]; isSearch = NO; if ([anArray count] == 0) { anArray = [super findWordEqualToString:aString]; } else { [self setPath:string]; [anArray insertObject:self atIndex:0]; } return anArray; } //make this threadsafe /* - (void)sort { [self lock]; if(sortingList == nil) { } [self unlock]; } */ - (void)sort { [wordList sortUsingSelector:@selector(caseInsensitiveCompare:)]; } - (NSMutableArray *)sortedIndexes { id sortedIndexes; // NSLog(@"sortedIndexes %@ %d",string,[NSThread currentThread]); [self lock]; sortedIndexes = wordList; if (isDirty) { [self sort]; isDirty = NO; } [self unlock]; // NSLog(@"exit sortedIndexes %@ %d",string,[NSThread currentThread]); return sortedIndexes; } - (WordContainer *)wordAtIndex:(int)i { return [[self sortedIndexes] objectAtIndex:i]; } - (NSAttributedString *)presentation { NSMutableAttributedString * str; if(isReady) { str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\nThis dictionary has %@ words",[self definition],[self counter]]]; } else { str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\nDictionary is being loaded",[self definition]]]; } [str addAttribute:NSForegroundColorAttributeName value:[NSColor blueColor] range:NSMakeRange(0,[[self definition] length])]; return [str autorelease]; } - (void) dealloc { NSLog(@"Wordcontainer got dealloc"); [wordList release]; return [super dealloc]; } @end