Git Product home page Git Product logo

kimziv / pinyin4objc Goto Github PK

View Code? Open in Web Editor NEW
877.0 37.0 166.0 270 KB

PinYin4Objc is a popular objective-c library supporting convertion between Chinese(both Simplified and Tranditional) characters and most popular Pinyin systems, it's performance is very efficient, data cached at first time, and use async method with block, this can avoid ui blocking, and runs smoothly. The output format of pinyin could be customized.(关键字:汉字转拼音,中文转拼音)

Home Page: http://github.kimziv.com/

License: Apache License 2.0

Objective-C 97.22% Ruby 2.78%

pinyin4objc's Introduction

PinYin4Objc

PinYin4Objc is a popular objective-c library supporting convertion between Chinese(both Simplified and Tranditional) characters and most popular Pinyin systems, it's performance is very efficient, data cached at first time, you can use async method with block, this can avoid ui blocking, and runs smoothly. The output format of pinyin could be customized.

*update

2013-10-08, V1.1, add async methods whit block, solve ui blcoking problems and fix some cache bugs; 2013-09-16, V1.0, firt virsion published.

  • Usage

1.use async method with block, this can avoid ui blocking, and runs smoothly:

	NSString *sourceText=@"我爱中文";
	HanyuPinyinOutputFormat *outputFormat=[[HanyuPinyinOutputFormat alloc] init];
	[outputFormat setToneType:ToneTypeWithoutTone];
	[outputFormat setVCharType:VCharTypeWithV];
	[outputFormat setCaseType:CaseTypeLowercase];    
	[PinyinHelper toHanyuPinyinStringWithNSString:sourceText 
					   withHanyuPinyinOutputFormat:outputFormat 
									   withNSString:@" " 
									   outputBlock:^(NSString *pinYin) {
  		_outputTv.text=pinYin; //update ui

	 }];

2.use sync method below:

	NSString *sourceText=@"我爱中文";
	HanyuPinyinOutputFormat *outputFormat=[[HanyuPinyinOutputFormat alloc] init];
	[outputFormat setToneType:ToneTypeWithoutTone];
	[outputFormat setVCharType:VCharTypeWithV];
	[outputFormat setCaseType:CaseTypeLowercase];
	NSString *outputPinyin=[PinyinHelper toHanyuPinyinStringWithNSString:sourceText withHanyuPinyinOutputFormat:outputFormat withNSString:@" "];
  • ScreenShot

    ScreenShot

pinyin4objc's People

Contributors

kimziv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pinyin4objc's Issues

Will PinYin4Objc support arm64 ?

you know, apple will reject any app that doesn't support arm64 afer next Feb. That means all third-party frameworks in my project need support arm64. PinYin4Objc is a good framework, so will it support arm64 proccessor?

kimziv你好

kimziv:你好。感谢你分享你的PinYin4Objc项目,我想给你的这个加到cocoapods管理,你能把PinYin4Objc项目打一个tag吗?

不能转换,不过解决了

试了好多办法,不能转换。
但最后解决了:
ChineseToPinyinResource.m 里面有个读取unicode_to_hanyu_pinyin.txt的代码:

NSDictionary *dataMap=(NSDictionary *)[self cachedObjectForKey:kCacheKeyForUnicode2Pinyin];
if (dataMap) {
self->_unicodeToHanyuPinyinTable=dataMap;
}
else{
NSString *resourceName =[[NSBundle mainBundle] pathForResource:@"unicode_to_hanyu_pinyin" ofType:@"txt"];
NSString *dictionaryText=[NSString stringWithContentsOfFile:resourceName encoding:NSUTF8StringEncoding error:nil];
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]];
[tempMap setObject:lineComponents[1] forKey:lineComponents[0]];
}];
}
self->_unicodeToHanyuPinyinTable=tempMap;
[self cacheObjec:self->_unicodeToHanyuPinyinTable forKey:kCacheKeyForUnicode2Pinyin];
}

不过注意dataMap的值,有时候它可以!=nil,但是里面的 :

[dataMap allKey].count == 0

也就是是一个empty dictionary,但!=nil, 这样它不能初始化unicodeToHanyuPinyinTable了,只需要在if里面加上一下:

if (dataMap && [dataMap allKeys].count != 0)

就可以解决了。

内存问题和线程问题

问题1:这个是在ARC下面的代码,如果没有开启ARC,会出现泄漏
问题2:发现在多线程下面,偶然会发生错误,这边继续跟踪一下,多线程下载处理数据.貌似总是返回nil

 NSString * pinyin = [PinyinHelper getFirstHanyuPinyinStringWithChar:[name characterAtIndex:i] withHanyuPinyinOutputFormat:outputFormat];

但是,很难重现...

THX

it helps me a lot

内存泄露怎么解决呢?

  • (void)toHanyuPinyinStringWithNSString:(NSString *)str
    withHanyuPinyinOutputFormat:(HanyuPinyinOutputFormat *)outputFormat
    withNSString:(NSString *)seperater
    outputBlock:(OutputStringBlock)outputBlock

这方法有内存泄露,instruments检测来的

多线程问题,有截图

多个线程跑的时候,出现错误,不是每次都会出现.
截图如下:
error

方法里面出错.
(NSString *)getHanyuPinyinRecordFromCharWithChar:(unichar)ch {
int codePointOfChar = ch;
NSString *codepointHexStr =[[NSString stringWithFormat:@"%x", codePointOfChar] uppercaseString];
NSString *foundRecord =[self->_unicodeToHanyuPinyinTable objectForKey:codepointHexStr];
return [self isValidRecordWithNSString:foundRecord] ? foundRecord : nil;
}

convertToneNumber2ToneMarkWithNSString [out of bounds exception]

你好!Love your code! But I noticed that when trying to return PinYin with the tone marks I was receiving the out of bounds errors.

I managed to fix it by replacing;
[[lowerCasePinyinStr substringWithRange:NSMakeRange(indexOfUnmarkedVowel, lowerCasePinyinStr.length-indexOfUnmarkedVowel)] stringByReplacingOccurrencesOfString:@"v" withString:@"ü"]];

with;
[resultBuffer appendString:[[lowerCasePinyinStr substringToIndex:indexOfUnmarkedVowel] stringByReplacingOccurrencesOfString:@"v" withString:@"ü"]];
[resultBuffer appendFormat:@"%C",markedVowel];
[resultBuffer appendString:[lowerCasePinyinStr substringWithRange:NSMakeRange(indexOfUnmarkedVowel + 1, (lowerCasePinyinStr.length - indexOfUnmarkedVowel) - 2)]];

Seems to work well for me :) - the PinYin comes out clean and looks to be correct.

Hope it helps - 谢谢你!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.