我一直在使用NSDataDetector
从字符串中解析地址,并且在大多数情况下它做得很好。但是,在与此类似的地址上,它不会检测到它。
6200 North Evan Blvd Suit 487 Highland UT 84043
目前我正在使用这段代码:
NSError *error = nil;
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeAddress error:&error];
NSArray *matches = [detector matchesInString:output options:0 range:NSMakeRange(0, [output length])];
for (NSTextCheckingResult *match in matches) {
if ([match resultType] == NSTextCheckingTypeAddress) {
_address = [_tesseractData substringWithRange:[match range]];
NSDictionary *data = [match addressComponents];
_zip = [data objectForKey:@"ZIP"];
if (_zip) {
NSRange zipRange = [_tesseractData rangeOfString:_zip];
if (zipRange.location != NSNotFound) {
[_tesseractData deleteCharactersInRange:zipRange];
}
}
_city = [data objectForKey:@"City"];
if (_city) {
NSRange cityRange = [_tesseractData rangeOfString:[_city uppercaseString]];
if (cityRange.location != NSNotFound) {
[_tesseractData deleteCharactersInRange:cityRange];
}
}
_city = [_city capitalizedString];
_state = [data objectForKey:@"State"];
_street = [data objectForKey:@"Street"];
if (_street) {
NSRange streetRange = [_tesseractData rangeOfString:[_street uppercaseString]];
if (streetRange.location != NSNotFound) {
[_tesseractData deleteCharactersInRange:streetRange];
}
}
_street = [_street capitalizedString];
}
}
任何人都可以建议一种更强大的方法来解析字符串中的物理地址吗?我需要能够获得邮编、街道、州和城市。