Skip to content

Commit

Permalink
fx cache bugs and add block methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kimziv committed Oct 8, 2013
1 parent 443c31c commit a0258a5
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 6.1;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SDKROOT = iphoneos6.1;
};
name = Debug;
};
Expand All @@ -434,7 +434,7 @@
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 6.1;
OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1";
SDKROOT = iphoneos;
SDKROOT = iphoneos6.1;
VALIDATE_PRODUCT = YES;
};
name = Release;
Expand All @@ -446,6 +446,7 @@
GCC_PREFIX_HEADER = "PinYin4ObjcExample/PinYin4ObjcExample-Prefix.pch";
INFOPLIST_FILE = "PinYin4ObjcExample/PinYin4ObjcExample-Info.plist";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos6.1;
WRAPPER_EXTENSION = app;
};
name = Debug;
Expand All @@ -457,6 +458,7 @@
GCC_PREFIX_HEADER = "PinYin4ObjcExample/PinYin4ObjcExample-Prefix.pch";
INFOPLIST_FILE = "PinYin4ObjcExample/PinYin4ObjcExample-Info.plist";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = iphoneos6.1;
WRAPPER_EXTENSION = app;
};
name = Release;
Expand Down Expand Up @@ -514,6 +516,7 @@
1D40375517E740210061FF18 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
1D40375617E740210061FF18 /* Build configuration list for PBXNativeTarget "PinYin4ObjcExampleTests" */ = {
isa = XCConfigurationList;
Expand All @@ -522,6 +525,7 @@
1D40375817E740210061FF18 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
23 changes: 15 additions & 8 deletions Example/PinYin4ObjcExample/PinYin4ObjcExample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,21 @@ -(IBAction)doClick:(id)sender
[outputFormat setVCharType:VCharTypeWithV];
[outputFormat setCaseType:CaseTypeLowercase];
NSTimeInterval startTime=[[NSDate date] timeIntervalSince1970];
NSString *outputPinyin=[PinyinHelper toHanyuPinyinStringWithNSString:sourceText withHanyuPinyinOutputFormat:outputFormat withNSString:@" "];
NSTimeInterval endTime=[[NSDate date] timeIntervalSince1970];
NSTimeInterval totalTime=endTime-startTime;
_timeLb.text=[NSString stringWithFormat:@"Total Time:%fs",totalTime];
_wordsLb.text=[NSString stringWithFormat:@"Total Words:%i characters",sourceText.length];
// NSLog(@"%@:%@, seconds:%f",sourceText,outputPinyin,(end - begin)/(float)CLOCKS_PER_SEC);
_outputTv.text=outputPinyin;
//[NSString stringWithFormat:@"%@:%@, seconds:%fs",sourceText,outputPinyin,totalTime];

[PinyinHelper toHanyuPinyinStringWithNSString:sourceText withHanyuPinyinOutputFormat:outputFormat withNSString:@"" outputBlock:^(NSString *pinYin) {
NSTimeInterval endTime=[[NSDate date] timeIntervalSince1970];
NSTimeInterval totalTime=endTime-startTime;
_timeLb.text=[NSString stringWithFormat:@"Total Time:%fs",totalTime];
_wordsLb.text=[NSString stringWithFormat:@"Total Words:%i characters",sourceText.length];
_outputTv.text=pinYin;

}];
// NSString *outputPinyin=[PinyinHelper toHanyuPinyinStringWithNSString:sourceText withHanyuPinyinOutputFormat:outputFormat withNSString:@" "];
// NSTimeInterval endTime=[[NSDate date] timeIntervalSince1970];
// NSTimeInterval totalTime=endTime-startTime;
// _timeLb.text=[NSString stringWithFormat:@"Total Time:%fs",totalTime];
// _wordsLb.text=[NSString stringWithFormat:@"Total Words:%i characters",sourceText.length];
// _outputTv.text=outputPinyin;
}

- (void)didReceiveMemoryWarning
Expand Down
51 changes: 28 additions & 23 deletions PinYin4Objc/Classes/ChineseToPinyinResource.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
#define RIGHT_BRACKET @")"
#define COMMA @","

#define kCacheKeyForUnicode2Pinyin @"cache.key.for.unicode.to.pinyin"

#define kCacheKeyForUnicode2Pinyin @"UnicodeToPinyin"
static inline NSString* cachePathForKey(NSString* directory, NSString* key) {
return [directory stringByAppendingPathComponent:key];
}
Expand All @@ -22,10 +21,6 @@ -(void)cacheObjec:(id<NSCoding>)obj forKey:(NSString *)key;
@end

@implementation ChineseToPinyinResource
//@synthesize unicodeToHanyuPinyinTable=_unicodeToHanyuPinyinTable;
//- (NSDictionary *)getUnicodeToHanyuPinyinTable {
// return _unicodeToHanyuPinyinTable;
//}

- (id)init {
if (self = [super init]) {
Expand All @@ -37,13 +32,16 @@ - (id)init {

- (void)initializeResource {
NSString* cachesDirectory = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString* oldCachesDirectory = [[[cachesDirectory stringByAppendingPathComponent:[[NSProcessInfo processInfo] processName]] stringByAppendingPathComponent:@"PinYinCache"] copy];

if([[NSFileManager defaultManager] fileExistsAtPath:oldCachesDirectory]) {
[[NSFileManager defaultManager] removeItemAtPath:oldCachesDirectory error:NULL];
}

_directory = [[[cachesDirectory stringByAppendingPathComponent:[[NSBundle mainBundle] bundleIdentifier]] stringByAppendingPathComponent:@"PinYinCache"] copy];
NSFileManager *fileManager=[NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:_directory])
{
NSError *error=nil;
if (![fileManager createDirectoryAtPath:_directory withIntermediateDirectories:YES attributes:nil error:&error]) {
NSLog(@"Error, s is %@, %s, %s, %d",error.description, __FILE__ ,__FUNCTION__, __LINE__);
}

}

NSDictionary *dataMap=(NSDictionary *)[self cachedObjectForKey:kCacheKeyForUnicode2Pinyin];
if (dataMap) {
Expand All @@ -54,22 +52,25 @@ - (void)initializeResource {
NSArray *lines = [dictionaryText componentsSeparatedByString:@"\r\n"];
__block NSMutableDictionary *tempMap=[[NSMutableDictionary alloc] init];
@autoreleasepool {
[lines enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSArray *lineComponents=[obj componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
//NSLog(@"%@, %@",lineComponents[0],lineComponents[1]);
[tempMap setObject:lineComponents[1] forKey:lineComponents[0]];
}];
}
[lines enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSArray *lineComponents=[obj componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
[tempMap setObject:lineComponents[1] forKey:lineComponents[0]];
}];
}
self->_unicodeToHanyuPinyinTable=tempMap;
[self cacheObjec:self->_unicodeToHanyuPinyinTable forKey:kCacheKeyForUnicode2Pinyin];
}
}

- (id<NSCoding>)cachedObjectForKey:(NSString*)key
{
NSData *data = [NSData dataWithContentsOfFile:cachePathForKey(_directory, key) options:0 error:NULL];
if (data) {
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSError *error=nil;
NSData *data = [NSData dataWithContentsOfFile:cachePathForKey(_directory, key) options:0 error:&error];
// NSAssert4((!error), @"Error, s is %@, %s, %s, %d",error.description, __FILE__ ,__FUNCTION__, __LINE__);
if (!error) {
if (data) {
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
}
return nil;
}
Expand All @@ -78,8 +79,12 @@ -(void)cacheObjec:(id<NSCoding>)obj forKey:(NSString *)key
{
NSData* data= [NSKeyedArchiver archivedDataWithRootObject:obj];
NSString* cachePath = cachePathForKey(_directory, key);
dispatch_async(dispatch_get_main_queue(), ^{
[data writeToFile:cachePath atomically:YES];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error=nil;
[data writeToFile:cachePath options:NSDataWritingAtomic error:&error];
if (error){
NSLog(@"Error, s is %@, %s, %s, %d",error.description, __FILE__ ,__FUNCTION__, __LINE__);
}
});
}

Expand Down
42 changes: 41 additions & 1 deletion PinYin4Objc/Classes/PinyinHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,51 @@
#ifndef _PinyinHelper_H_
#define _PinyinHelper_H_

typedef void(^OutputStringBlock)(NSString *pinYin) ;
typedef void(^OutputArrayBlock)(NSArray *array) ;

@class HanyuPinyinOutputFormat;

@interface PinyinHelper : NSObject {
}

/* async methods, "ui blocking" is gone, make ui update smoothly ,recommend use methods below*/

+ (void)toHanyuPinyinStringArrayWithChar:(unichar)ch
outputBlock:(OutputArrayBlock)outputBlock;
+ (void)toHanyuPinyinStringArrayWithChar:(unichar)ch
withHanyuPinyinOutputFormat:(HanyuPinyinOutputFormat *)outputFormat
outputBlock:(OutputArrayBlock)outputBlock;
+ (void)getFormattedHanyuPinyinStringArrayWithChar:(unichar)ch
withHanyuPinyinOutputFormat:(HanyuPinyinOutputFormat *)outputFormat
outputBlock:(OutputArrayBlock)outputBlock;
+ (void)getUnformattedHanyuPinyinStringArrayWithChar:(unichar)ch
outputBlock:(OutputArrayBlock)outputBlock;
+ (void)toTongyongPinyinStringArrayWithChar:(unichar)ch
outputBlock:(OutputArrayBlock)outputBlock;
+ (void)toWadeGilesPinyinStringArrayWithChar:(unichar)ch
outputBlock:(OutputArrayBlock)outputBlock;
+ (void)toMPS2PinyinStringArrayWithChar:(unichar)ch
outputBlock:(OutputArrayBlock)outputBlock;
+ (void)toYalePinyinStringArrayWithChar:(unichar)ch
outputBlock:(OutputArrayBlock)outputBlock;
+ (void)convertToTargetPinyinStringArrayWithChar:(unichar)ch
withPinyinRomanizationType:(NSString *)targetPinyinSystem
outputBlock:(OutputArrayBlock)outputBlock;
+ (void)toGwoyeuRomatzyhStringArrayWithChar:(unichar)ch
outputBlock:(OutputArrayBlock)outputBlock;
+ (void)convertToGwoyeuRomatzyhStringArrayWithChar:(unichar)ch
outputBlock:(OutputArrayBlock)outputBlock;
+ (void)toHanyuPinyinStringWithNSString:(NSString *)str
withHanyuPinyinOutputFormat:(HanyuPinyinOutputFormat *)outputFormat
withNSString:(NSString *)seperater
outputBlock:(OutputStringBlock)outputBlock;
+ (void)getFirstHanyuPinyinStringWithChar:(unichar)ch
withHanyuPinyinOutputFormat:(HanyuPinyinOutputFormat *)outputFormat
outputBlock:(OutputStringBlock)outputBlock;

/* sync methods */

+ (NSArray *)toHanyuPinyinStringArrayWithChar:(unichar)ch;
+ (NSArray *)toHanyuPinyinStringArrayWithChar:(unichar)ch
withHanyuPinyinOutputFormat:(HanyuPinyinOutputFormat *)outputFormat;
Expand All @@ -31,7 +71,7 @@
withNSString:(NSString *)seperater;
+ (NSString *)getFirstHanyuPinyinStringWithChar:(unichar)ch
withHanyuPinyinOutputFormat:(HanyuPinyinOutputFormat *)outputFormat;
- (id)init;

@end

#endif // _PinyinHelper_H_
Loading

0 comments on commit a0258a5

Please sign in to comment.