|
| 1 | +// |
| 2 | +// ABBrailleInterpreter.m |
| 3 | +// AccessBraille |
| 4 | +// |
| 5 | +// Created by Michael Timbrook on 8/8/13. |
| 6 | +// Copyright (c) 2013 RIT. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "ABBrailleInterpreter.h" |
| 10 | + |
| 11 | +@implementation ABBrailleInterpreter |
| 12 | +{ |
| 13 | + // Look ups |
| 14 | + NSDictionary *grade2Lookup; |
| 15 | + NSDictionary *numberLookup; |
| 16 | + NSDictionary *prefixLevelTwo; |
| 17 | + NSDictionary *prefixLevelThree; |
| 18 | + NSDictionary *prefixLevelFour; |
| 19 | + NSDictionary *prefixLevelFive; |
| 20 | + NSDictionary *prefixLevelSix; |
| 21 | + NSDictionary *prefixLevelSeven; |
| 22 | + NSDictionary *shortHandlookup; |
| 23 | +} |
| 24 | + |
| 25 | +- (instancetype)init |
| 26 | +{ |
| 27 | + self = [super init]; |
| 28 | + if (self) { |
| 29 | + // Load in dictionaries |
| 30 | + NSString *path = [[NSBundle mainBundle] bundlePath]; |
| 31 | + grade2Lookup = [[NSDictionary alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:@"grade2lookup.plist"]]; |
| 32 | + numberLookup = [[NSDictionary alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:@"numberLookup.plist"]]; |
| 33 | + prefixLevelTwo = [[NSDictionary alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:@"prefixLevelTwo.plist"]]; |
| 34 | + prefixLevelThree = [[NSDictionary alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:@"prefixLevelThree.plist"]]; |
| 35 | + prefixLevelFour = [[NSDictionary alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:@"prefixLevelFour.plist"]]; |
| 36 | + prefixLevelFive = [[NSDictionary alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:@"prefixLevelFive.plist"]]; |
| 37 | + prefixLevelSix = [[NSDictionary alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:@"prefixLevelSix.plist"]]; |
| 38 | + prefixLevelSeven = [[NSDictionary alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:@"prefixLevelSeven.plist"]]; |
| 39 | + shortHandlookup = [[NSDictionary alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:@"shortHandLookup.plist"]]; |
| 40 | + // Init wordgraph |
| 41 | + _wordGraph = [NSMutableArray new]; |
| 42 | + // Set default grade |
| 43 | + _grade = ABGradeTwo; |
| 44 | + } |
| 45 | + return self; |
| 46 | +} |
| 47 | + |
| 48 | +#pragma mark Processing |
| 49 | + |
| 50 | +- (void)processBrailleString:(NSString *)braille |
| 51 | +{ |
| 52 | + [self processBrailleString:braille isShift:NO]; |
| 53 | +} |
| 54 | +- (void)processBrailleString:(NSString *)braille isShift:(BOOL)shift |
| 55 | +{ |
| 56 | + if (_grade == ABGradeOne) |
| 57 | + goto gradeLookup; |
| 58 | + |
| 59 | + if ([ABBrailleInterpreter isValidPrefix:braille]) { |
| 60 | + [_wordGraph addObject:braille]; |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + if (_wordGraph.count == 0) { |
| 65 | + if ([[grade2Lookup allKeys] containsObject:braille]) { |
| 66 | + NSString *ch = grade2Lookup[braille]; |
| 67 | + ch = shift ? ch.uppercaseString : ch.lowercaseString; |
| 68 | + [_wordGraph addObject:ch]; |
| 69 | + [self passToResponder:ch]; |
| 70 | + } |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + if ([ABBrailleInterpreter isValidPrefix:_wordGraph.lastObject]) { |
| 75 | + NSString *ch = [self getPostfixCharacter:braille withPrefix:_wordGraph.lastObject]; |
| 76 | + ch = shift ? ch.uppercaseString : ch.lowercaseString; |
| 77 | + [_wordGraph addObject:ch]; |
| 78 | + [self passToResponder:ch]; |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | +gradeLookup: |
| 83 | + |
| 84 | + if ([[grade2Lookup allKeys] containsObject:braille]) { |
| 85 | + NSString *ch = grade2Lookup[braille]; |
| 86 | + ch = shift ? ch.uppercaseString : ch.lowercaseString; |
| 87 | + [_wordGraph addObject:ch]; |
| 88 | + if (_grade == ABGradeOne && ch.length == 1) { |
| 89 | + [self passToResponder:ch]; |
| 90 | + } else { |
| 91 | + [self passToResponder:ch]; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | +} |
| 96 | + |
| 97 | +- (NSString *)getPostfixCharacter:(NSString *)brailleString withPrefix:(NSString *)prefix |
| 98 | +{ |
| 99 | + NSString *postfixChar = @""; |
| 100 | + if ([prefix isEqualToString:ABPrefixNumber] && [[numberLookup allKeys] containsObject:brailleString]) { |
| 101 | + postfixChar = numberLookup[brailleString]; |
| 102 | + } else if ([prefix isEqualToString:ABPrefixLevelTwo] && [[prefixLevelTwo allKeys] containsObject:brailleString]) { |
| 103 | + postfixChar = prefixLevelTwo[brailleString]; |
| 104 | + } else if ([prefix isEqualToString:ABPrefixLevelThree] && [[prefixLevelThree allKeys] containsObject:brailleString]) { |
| 105 | + postfixChar = prefixLevelThree[brailleString]; |
| 106 | + } else if ([prefix isEqualToString:ABPrefixLevelFour] && [[prefixLevelFour allKeys] containsObject:brailleString]) { |
| 107 | + postfixChar = prefixLevelFour[brailleString]; |
| 108 | + } else if ([prefix isEqualToString:ABPrefixLevelFive] && [[prefixLevelFive allKeys] containsObject:brailleString]) { |
| 109 | + postfixChar = prefixLevelFive[brailleString]; |
| 110 | + } else if ([prefix isEqualToString:ABPrefixLevelSix] && [[prefixLevelSix allKeys] containsObject:brailleString]) { |
| 111 | + postfixChar = prefixLevelSix[brailleString]; |
| 112 | + } else if ([prefix isEqualToString:ABPrefixLevelSeven] && [[prefixLevelSeven allKeys] containsObject:brailleString]) { |
| 113 | + postfixChar = prefixLevelSeven[brailleString]; |
| 114 | + } |
| 115 | + return postfixChar; |
| 116 | +} |
| 117 | + |
| 118 | +- (void)passToResponder:(NSString *)character |
| 119 | +{ |
| 120 | + if ([_responder respondsToSelector:@selector(newCharacterFromInterpreter:)]) { |
| 121 | + [_responder newCharacterFromInterpreter:character]; |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +- (void)reset |
| 126 | +{ |
| 127 | + [_wordGraph removeAllObjects]; |
| 128 | +} |
| 129 | + |
| 130 | +- (void)dropEndOffGraph |
| 131 | +{ |
| 132 | + if (_wordGraph.count > 0) { |
| 133 | + [_wordGraph removeLastObject]; |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +- (NSString *)getCurrentWord; |
| 138 | +{ |
| 139 | + NSString *word = @""; |
| 140 | + for (NSString *str in _wordGraph) { |
| 141 | + if (![ABBrailleInterpreter isValidPrefix:str]) { |
| 142 | + word = [word stringByAppendingString:str]; |
| 143 | + } |
| 144 | + } |
| 145 | + return word; |
| 146 | +} |
| 147 | + |
| 148 | +#pragma mark Helpers |
| 149 | + |
| 150 | +/** |
| 151 | + * Checks if a braille string is a prefix |
| 152 | + */ |
| 153 | ++ (BOOL)isValidPrefix:(NSString *)braille |
| 154 | +{ |
| 155 | + return ([braille isEqualToString:ABPrefixLevelOne] || |
| 156 | + [braille isEqualToString:ABPrefixLevelTwo] || |
| 157 | + [braille isEqualToString:ABPrefixLevelThree] || |
| 158 | + [braille isEqualToString:ABPrefixLevelFour] || |
| 159 | + [braille isEqualToString:ABPrefixLevelFive] || |
| 160 | + [braille isEqualToString:ABPrefixLevelSix] || |
| 161 | + [braille isEqualToString:ABPrefixLevelSeven]); |
| 162 | +} |
| 163 | + |
| 164 | +@end |
0 commit comments