From 72c4522db57b9699e1dc324f62dbeb036405f2c6 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Tue, 16 Feb 2010 23:11:38 +0000 Subject: =?UTF-8?q?=E2=80=A2=20csv=20field=20mapper=20-=20added=20chance?= =?UTF-8?q?=20to=20align=20the=20csv=20field=20and=20table=20field=20names?= =?UTF-8?q?=20via=20'matching=20names'=20which=20uses=20the=20Levenshtein?= =?UTF-8?q?=20distance=20[first=20approach]=20if=20csv=20file's=20first=20?= =?UTF-8?q?line=20is=20an=20header?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Interfaces/English.lproj/DataMigrationDialog.xib | 71 +++++++++++++++++------- Source/SPFieldMapperController.h | 10 +++- Source/SPFieldMapperController.m | 48 +++++++++++++++- 3 files changed, 104 insertions(+), 25 deletions(-) diff --git a/Interfaces/English.lproj/DataMigrationDialog.xib b/Interfaces/English.lproj/DataMigrationDialog.xib index f908ed52..d0afc212 100644 --- a/Interfaces/English.lproj/DataMigrationDialog.xib +++ b/Interfaces/English.lproj/DataMigrationDialog.xib @@ -12,7 +12,7 @@ YES - + YES @@ -991,7 +991,7 @@ cyAKCQluZXcgcm93cyBSRVBMQUNFIElOVE8g4oCmA MC45Mjk0MTE4Mjg1IDAuOTI5NDExODI4NSAwLjkyOTQxMTgyODUAA - {4, 4} + {1, -1} 0 6 @@ -1043,9 +1043,10 @@ cyAKCQluZXcgcm93cyBSRVBMQUNFIElOVE8g4oCmA 400 75 - + - file order + YES + matching names 1048576 2147483647 @@ -1053,6 +1054,7 @@ cyAKCQluZXcgcm93cyBSRVBMQUNFIElOVE8g4oCmA _popUpItemAction: + 2 YES @@ -1060,35 +1062,34 @@ cyAKCQluZXcgcm93cyBSRVBMQUNFIElOVE8g4oCmA OtherViews YES - - + - reversed file order + file order + 1048576 2147483647 _popUpItemAction: - 1 - + - YES - matching names + reversed file order - 1048576 2147483647 _popUpItemAction: - 2 + 1 + YES + 2 1 YES YES @@ -1335,6 +1336,30 @@ cyAKCQluZXcgcm93cyBSRVBMQUNFIElOVE8g4oCmA 152 + + + matchingNameMenuItem + + + + 154 + + + + changeHasHeaderCheckbox: + + + + 155 + + + + addGlobalSourceVariable: + + + + 156 + @@ -1974,9 +1999,9 @@ cyAKCQluZXcgcm93cyBSRVBMQUNFIElOVE8g4oCmA YES com.apple.InterfaceBuilder.CocoaPlugin - {{164, 144}, {465, 405}} + {{120, 351}, {465, 405}} com.apple.InterfaceBuilder.CocoaPlugin - {{164, 144}, {465, 405}} + {{120, 351}, {465, 405}} {{387, 725}, {432, 282}} @@ -1995,7 +2020,7 @@ cyAKCQluZXcgcm93cyBSRVBMQUNFIElOVE8g4oCmA com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - {{170, 196}, {161, 54}} + {{126, 403}, {161, 54}} com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -2138,7 +2163,7 @@ cyAKCQluZXcgcm93cyBSRVBMQUNFIElOVE8g4oCmA - 152 + 156 @@ -2174,7 +2199,9 @@ cyAKCQluZXcgcm93cyBSRVBMQUNFIElOVE8g4oCmA YES YES + addGlobalSourceVariable: changeFieldAlignment: + changeHasHeaderCheckbox: changeImportMethod: changeTableTarget: closeSheet: @@ -2187,6 +2214,8 @@ cyAKCQluZXcgcm93cyBSRVBMQUNFIElOVE8g4oCmA id id id + id + id @@ -2200,6 +2229,7 @@ cyAKCQluZXcgcm93cyBSRVBMQUNFIElOVE8g4oCmA importButton importFieldNamesHeaderSwitch importMethodPopup + matchingNameMenuItem recordCountLabel rowDownButton rowUpButton @@ -2208,17 +2238,18 @@ cyAKCQluZXcgcm93cyBSRVBMQUNFIElOVE8g4oCmA YES - id + NSPopUpButton NSTableView id NSPathControl id id + NSPopUpButton + NSMenuItem id id id - id - id + NSPopUpButton id diff --git a/Source/SPFieldMapperController.h b/Source/SPFieldMapperController.h index 62139b7d..4b9a3178 100644 --- a/Source/SPFieldMapperController.h +++ b/Source/SPFieldMapperController.h @@ -37,10 +37,11 @@ IBOutlet id importFieldNamesHeaderSwitch; IBOutlet id importButton; IBOutlet NSPopUpButton *alignByPopup; - + IBOutlet NSMenuItem *matchingNameMenuItem; + id theDelegate; id fieldMappingImportArray; - + NSInteger fieldMappingCurrentRow; NSMutableArray *fieldMappingArray; NSMutableArray *fieldMappingTableColumnNames; @@ -49,7 +50,7 @@ NSMutableArray *fieldMappingButtonOptions; NSMutableArray *fieldMappingOperatorOptions; NSMutableArray *fieldMappingOperatorArray; - + NSNumber *doImport; NSNumber *doNotImport; NSNumber *isEqual; @@ -87,10 +88,13 @@ - (IBAction)changeTableTarget:(id)sender; - (IBAction)changeImportMethod:(id)sender; - (IBAction)changeFieldAlignment:(id)sender; +- (IBAction)changeHasHeaderCheckbox:(id)sender; - (IBAction)stepRow:(id)sender; +- (IBAction)addGlobalSourceVariable:(id)sender; - (IBAction)closeSheet:(id)sender; // Others +- (void)matchHeaderNames; - (void)setupFieldMappingArray; - (void)updateFieldMappingButtonCell; - (void)updateFieldMappingOperatorOptions; diff --git a/Source/SPFieldMapperController.m b/Source/SPFieldMapperController.m index 84dfc4c8..42ac3b0d 100644 --- a/Source/SPFieldMapperController.m +++ b/Source/SPFieldMapperController.m @@ -27,6 +27,7 @@ #import "SPTableData.h" #import "TablesList.h" #import "SPArrayAdditions.h" +#import "SPStringAdditions.h" #import "SPConstants.h" @implementation SPFieldMapperController @@ -95,6 +96,7 @@ [importFieldNamesHeaderSwitch setState:importFieldNamesHeader]; + [self changeHasHeaderCheckbox:self]; [self changeTableTarget:self]; [[self window] makeFirstResponder:fieldMapperTableView]; if([fieldMappingTableColumnNames count]) @@ -162,7 +164,7 @@ - (BOOL)importFieldNamesHeader { - return importFieldNamesHeader; + return ([importFieldNamesHeaderSwitch state] == NSOnState)?YES:NO; } #pragma mark - @@ -279,7 +281,9 @@ for(i=possibleImports; i>=0; i--) [fieldMappingArray replaceObjectAtIndex:possibleImports-i withObject:[NSNumber numberWithInteger:i]]; break; - + case 2: // try to align header and table target field names via Levenshtein distance + [self matchHeaderNames]; + break; } [fieldMapperTableView reloadData]; } @@ -304,6 +308,46 @@ [rowUpButton setEnabled:(fieldMappingCurrentRow != ([fieldMappingImportArray count]-1))]; } +- (IBAction)changeHasHeaderCheckbox:(id)sender +{ + [matchingNameMenuItem setEnabled:([importFieldNamesHeaderSwitch state] == NSOnState)?YES:NO]; +} + +- (IBAction)addGlobalSourceVariable:(id)sender +{ + +} + +#pragma mark - +#pragma mark Others + +- (void)matchHeaderNames +{ + if(![fieldMappingImportArray count]) return; + + NSMutableArray *fileHeaderNames = [NSMutableArray array]; + [fileHeaderNames setArray:NSArrayObjectAtIndex(fieldMappingImportArray, 0)]; + NSMutableArray *tableHeaderNames = [NSMutableArray array]; + [tableHeaderNames setArray:fieldMappingTableColumnNames]; + + NSInteger i,j; + NSMutableArray *matchedHeaderNames = [NSMutableArray array]; + for(i=0; i < [tableHeaderNames count]; i++) { + CGFloat minDist = 1e6; + NSInteger minIndex = 0; + for(j=0; j < [fileHeaderNames count]; j++) { + NSString *headerName = [NSArrayObjectAtIndex(fileHeaderNames,j) lowercaseString]; + CGFloat dist = [[NSArrayObjectAtIndex(tableHeaderNames,i) lowercaseString] levenshteinDistanceWithWord:headerName]; + if(dist < minDist && ![matchedHeaderNames containsObject:headerName]) { + minDist = dist; + minIndex = j; + } + if(dist == 0.0f) [matchedHeaderNames addObject:headerName]; + } + [fieldMappingArray replaceObjectAtIndex:i withObject:[NSNumber numberWithInteger:minIndex]]; + } +} + /* * Sets up the fieldMapping array to be shown in the tableView */ -- cgit v1.2.3