-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNSString+Japanese.m
536 lines (391 loc) · 17 KB
/
NSString+Japanese.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//
// NSString+JapaneseExtras.h
//
// Created by Grace Steven on 3/26/11.
// Copyright 2011 works5.com All rights reserved.
//
//https://github.com/00StevenG/NSString-Japanese
#import "NSString+Japanese.h"
#if TARGET_OS_IPHONE
#import <UIKit/UITextChecker.h>
#else
#endif
@implementation NSString (NSString_Japanese)
/*
+ Character unicode memo
+
+ Hiragana:
+ Hiragana: 3040-309F
+
+ Katakana:
+ Katakana: 30A0-30FF (except 30FB: KATAKANA MIDDLE DOT)
+ Halfwidth and Fullwidth Forms (FF00-FFEF): FF66-FF9F
+ * Katakana Phonetic Extensions: 31F0-31FF
+
+ Kanji:
+ CJK Radicals Supplement: 2E80-2EFF
+ Kangxi Radicals: 2F00-2FDF
+ CJK Unified Ideographs: 4E00-9FAF
+ * Ideographic Description Characters: 2FF0-2FFF
+ * CJK Unified Ideographs Extension A: 3400-4DBF
+ * CJK Compatibility Ideographs: F900-FAFF
+
+ Kigou:
+ * Arrows: 2190-21FF
+ * Mathematical Operators: 2200-22FF
+ * Box Drawing: 2500-257F
+ * Geometric Shapes: 25A0-25FF
+ * Miscellaneous Symbols: 2600-26FF
+ * CJK Symbols and Punctuation (3000-303F): 3003-303F
+ * Enclosed CJK Letters and Months: 3200-32FF
+ * CJK Compatibility: 3300-33FF
+ * CJK Compatibility Forms: FE30-FE4F
+ * Halfwidth and Fullwidth Forms (FF00-FFEF): FF5F-FF60, FF62-FF63, FFE0-FFF6, FFE8-FFEE
+ * and others?
+
+ Space:
+ C0 Controls and Basic Latin (0000-007F): 0020
+ CJK Symbols and Punctuation (3000-303F): 3000
+
+ Kuten:
+ CJK Symbols and Punctuation (3000-303F): 3001
+ Halfwidth and Fullwidth Forms (FF00-FFEF): FF0E, FF64
+
+ Touten:
+ CJK Symbols and Punctuation (3000-303F): 3002
+ Halfwidth and Fullwidth Forms (FF00-FFEF): FF0C, FF61
+
+ Fullwidth characters of latain:
+ Halfwidth and Fullwidth Forms (FF00-FFEF): FF01-FF5E (correspond to 0021-007E)
+
+ Fullwidth numeral:
+ Halfwidth and Fullwidth Forms (FF00-FFEF): FF10-FF19
+
+ see http://www.unicode.org/charts/
+
*/
/*
The folllowing code was extracted from:
*
* http://www.tjhsst.edu/~jcranmer/c-ccov-old/src/trunk/mailnews/extensions/bayesian-spam-filter/src/nsBayesianFilter.cpp.gcov.html
*
*
*
*/
#pragma mark - UNICODE Ranges
#define IN_RANGE(x, low, high) ((low<=(x))&&((x)<=high))
#define IS_JA_HIRAGANA(x) IN_RANGE(x, 0x3040, 0x309F)
#define IS_JA_KATAKANA(x) ((IN_RANGE(x, 0x30A0, 0x30FF)&&((x)!=0x30FB))||IN_RANGE(x, 0xFF66, 0xFF9F))
#define IS_JA_KANJI(x) (IN_RANGE(x, 0x2E80, 0x2EFF)||IN_RANGE(x, 0x2F00, 0x2FDF)||IN_RANGE(x, 0x4E00, 0x9FAF))
#define IS_JA_KUTEN(x) (((x)==0x3001)||((x)==0xFF64)||((x)==0xFF0E))
#define IS_JA_TOUTEN(x) (((x)==0x3002)||((x)==0xFF61)||((x)==0xFF0C))
#define IS_JA_SPACE(x) ((x)==0x3000)
#define IS_JA_FWLATAIN(x) IN_RANGE(x, 0xFF01, 0xFF5E)
#define IS_JA_FWNUMERAL(x) IN_RANGE(x, 0xFF10, 0xFF19)
#define IS_JAPANESE_SPECIFIC(x) (IN_RANGE(x, 0x3040, 0x30FF)||IN_RANGE(x, 0xFF01, 0xFF9F))
#pragma mark - Char class Helpers
typedef enum {
char_class_space,
char_class_others,
char_class_hiragana,
char_class_katakana,
char_class_kanji,
char_class_kuten,
char_class_touten,
char_class_kigou,
char_class_fwlatain,
char_class_ascii,
}char_class;
//
// Returns the char_class for a given unicode char
char_class getCharClass(unichar c){
if(IS_JA_HIRAGANA(c)){
return char_class_hiragana;
}else if(IS_JA_KATAKANA(c)){
return char_class_katakana;
}else if(IS_JA_KANJI(c)){
return char_class_kanji;
}else if(IS_JA_KUTEN(c)){
return char_class_kuten;
}else if(IS_JA_TOUTEN(c)){
return char_class_touten;
}else if(IS_JA_SPACE(c)){
return char_class_space;
}else if(IS_JA_FWLATAIN(c)){
return char_class_fwlatain;
}else{
return char_class_others;
}
}
#pragma mark - Public
//
//
//
+(SLGJapaneseStringType)japaneseStringTypeForString:(NSString*)string{
SLGJapaneseStringType aType = SLGJapaneseStringTypeOther;
char_class lastCharClass = char_class_others;
// loop through the string char by char
for(int i = 0; i<[string length];i++){
unichar aChar = [string characterAtIndex:i];
char_class aCharClass = getCharClass(aChar);
// first char set the type to a current type
if(i==0){
lastCharClass = aCharClass;
}
else{
if(lastCharClass != aCharClass){
if(aCharClass == char_class_katakana ||
aCharClass == char_class_hiragana ||
aCharClass == char_class_kanji)
return SLGJapaneseStringTypeCompound;
return SLGJapaneseStringTypeOther;
}
}
}
if(lastCharClass == char_class_hiragana){
aType = SLGJapaneseStringTypeHiragana;
}
else if(lastCharClass == char_class_katakana){
aType = SLGJapaneseStringTypeKatakana;
}
else if(lastCharClass == char_class_kanji){
aType = SLGJapaneseStringTypeKanji;
}
return aType;
}
//
//
//
+(BOOL)stringContainsJapaneseText:(NSString*)aString{
for(int i = 0; i<[aString length];i++){
BOOL isHiraganaOrKatakana = IS_JAPANESE_SPECIFIC([aString characterAtIndex:i]);
BOOL isKanji = IS_JA_KANJI([aString characterAtIndex:i]);
if(isHiraganaOrKatakana == YES || isKanji == YES)
return YES;
}
return NO;
}
#pragma mark - Japanese String Transiliteration
//
//
//
+(NSString*)_transliterateString:(NSString*)source
withTransform:(CFStringRef)aTransform{
CFMutableStringRef mString = CFStringCreateMutableCopy(kCFAllocatorDefault,0,(CFStringRef)source);
BOOL result = CFStringTransform(mString,NULL,aTransform,NO);
if(result){
NSString* resultString = [NSString stringWithFormat:@"%@",mString];
CFRelease(mString);
return resultString;
}
CFRelease(mString);
return nil;
}
//
//
//
-(NSString*)stringByTransliteratingJapaneseToRomajiWithWordSeperator:(NSString*)separator{
NSMutableString* aLatinString = [[NSMutableString alloc]init];
// create a tokenizer
CFStringTokenizerRef tok = CFStringTokenizerCreate(NULL,
(CFStringRef)self,
CFRangeMake(0,self.length),
kCFStringTokenizerUnitWord,
NULL);
// goto the first token in the string
CFStringTokenizerTokenType result =CFStringTokenizerAdvanceToNextToken(tok);
// enumerate until the end
while(result !=kCFStringTokenizerTokenNone){
CFTypeRef cTypeRef = CFStringTokenizerCopyCurrentTokenAttribute(tok,kCFStringTokenizerAttributeLatinTranscription);
if(separator){
[aLatinString appendFormat:@"%@%@",separator,cTypeRef];
}
else{
[aLatinString appendFormat:@"%@",cTypeRef];
}
CFRelease(cTypeRef);
result =CFStringTokenizerAdvanceToNextToken(tok);
}
CFRelease(tok);
return [NSString stringWithString:aLatinString];
}
//
//
//
-(NSString*)stringByTransliteratingJapaneseToRomaji{
return [self stringByTransliteratingJapaneseToRomajiWithWordSeperator:nil];
}
//
//
//
-(NSString*)stringByTransliteratingJapaneseToHiragana{
NSString* romaji = [self stringByTransliteratingJapaneseToRomaji];
return [NSString _transliterateString:romaji
withTransform:kCFStringTransformLatinHiragana];
}
//
//
//
-(NSString*)stringByTransliteratingRomajiToKatakana{
return [NSString _transliterateString:self
withTransform:kCFStringTransformLatinKatakana];
}
//
//
//
-(NSString*)stringByTransliteratingRomajiToHiragana{
return [NSString _transliterateString:self
withTransform:kCFStringTransformLatinHiragana];
}
//
//
//
-(NSString*)stringByReplacingJapaneseKanjiWithHiragana{
NSMutableString* kanjiFree = [[NSMutableString alloc]init];
// create a tokenizer to enumerate by WORD
NSLocale *locale= [[NSLocale alloc]initWithLocaleIdentifier:@"ja_JP"];
CFLocaleRef loc=(__bridge CFLocaleRef)(locale);
CFStringTokenizerRef tok = CFStringTokenizerCreate(NULL,
(CFStringRef)self,
CFRangeMake(0,self.length),
kCFStringTokenizerUnitWordBoundary,
loc);
// goto the first token in the string
CFStringTokenizerTokenType result =CFStringTokenizerAdvanceToNextToken(tok);
// enumerate the string
while(result !=kCFStringTokenizerTokenNone){
CFRange currentRange = CFStringTokenizerGetCurrentTokenRange(tok);
NSString* subString = [self substringWithRange:NSMakeRange(currentRange.location, currentRange.length)];
// check the type of the substring
SLGJapaneseStringType type = [[self class]japaneseStringTypeForString:subString];
// for kanji or strings with a combination(kanij+hiragana) of characters convert to hirgana
if(type == SLGJapaneseStringTypeKanji || type == SLGJapaneseStringTypeCompound){
NSString* hiragana = [subString stringByTransliteratingJapaneseToHiragana];
[kanjiFree appendFormat:@"%@",hiragana];
}
else{
// othewise just append the substring as is
[kanjiFree appendFormat:@"%@",subString];
}
result =CFStringTokenizerAdvanceToNextToken(tok);
}
CFRelease(tok);
return [kanjiFree copy];
}
-(NSDictionary*)hiraganaReplacementsForString{
NSMutableDictionary *dict=[NSMutableDictionary dictionary];
// create a tokenizer to enumerate by WORD
NSLocale *locale= [[NSLocale alloc]initWithLocaleIdentifier:@"ja_JP"];
CFLocaleRef loc=(__bridge CFLocaleRef)(locale);
CFStringTokenizerRef tok = CFStringTokenizerCreate(NULL,
(CFStringRef)self,
CFRangeMake(0,self.length),
kCFStringTokenizerUnitWordBoundary,
loc);
// goto the first token in the string
CFStringTokenizerTokenType result =CFStringTokenizerAdvanceToNextToken(tok);
// enumerate the string
while(result !=kCFStringTokenizerTokenNone){
CFRange currentRange = CFStringTokenizerGetCurrentTokenRange(tok);
NSString* subString = [self substringWithRange:NSMakeRange(currentRange.location, currentRange.length)];
// check the type of the substring
SLGJapaneseStringType type = [[self class]japaneseStringTypeForString:subString];
// for kanji or strings with a combination(kanij+hiragana) of characters convert to hirgana
if(type == SLGJapaneseStringTypeKanji || type == SLGJapaneseStringTypeCompound){
CFTypeRef cTypeRef = CFStringTokenizerCopyCurrentTokenAttribute(tok,kCFStringTokenizerAttributeLatinTranscription);
NSString *latin=[NSString stringWithFormat:@"%@",cTypeRef];
NSString *hiragana=[NSString _transliterateString:latin withTransform:kCFStringTransformLatinHiragana];
// NSString* hiragana = [subString stringByTransliteratingJapaneseToHiragana];
NSRange range=NSMakeRange(currentRange.location, currentRange.length);
[dict setObject:hiragana forKey:[NSValue valueWithRange:range]];
CFRelease(cTypeRef);
}
else{
}
result =CFStringTokenizerAdvanceToNextToken(tok);
}
CFRelease(tok);
return [dict copy];
}
-(NSDictionary*)romajiReplacementsForString{
NSMutableDictionary *dict=[NSMutableDictionary dictionary];
// create a tokenizer to enumerate by WORD
NSLocale *locale= [[NSLocale alloc]initWithLocaleIdentifier:@"ja_JP"];
CFLocaleRef loc=(__bridge CFLocaleRef)(locale);
CFStringTokenizerRef tok = CFStringTokenizerCreate(NULL,
(CFStringRef)self,
CFRangeMake(0,self.length),
kCFStringTokenizerUnitWordBoundary,
loc);
// goto the first token in the string
CFStringTokenizerTokenType result =CFStringTokenizerAdvanceToNextToken(tok);
// enumerate the string
while(result !=kCFStringTokenizerTokenNone){
CFRange currentRange = CFStringTokenizerGetCurrentTokenRange(tok);
NSString* subString = [self substringWithRange:NSMakeRange(currentRange.location, currentRange.length)];
// check the type of the substring
SLGJapaneseStringType type = [[self class]japaneseStringTypeForString:subString];
// for kanji or strings with a combination(kanij+hiragana) of characters convert to hirgana
if(type == SLGJapaneseStringTypeKanji || type == SLGJapaneseStringTypeCompound || type==SLGJapaneseStringTypeKatakana || type==SLGJapaneseStringTypeHiragana){
CFTypeRef cTypeRef = CFStringTokenizerCopyCurrentTokenAttribute(tok,kCFStringTokenizerAttributeLatinTranscription);
NSString *latin=[NSString stringWithFormat:@"%@",cTypeRef];
NSRange range=NSMakeRange(currentRange.location, currentRange.length);
[dict setObject:latin forKey:[NSValue valueWithRange:range]];
CFRelease(cTypeRef);
}
else{
}
result =CFStringTokenizerAdvanceToNextToken(tok);
}
CFRelease(tok);
return [dict copy];
}
#pragma mark - Phonetic Methods
//
//
//
-(CGFloat)phoneticSimilarityToString:(NSString*)string{
return [self phoneticSimilarityToString:string isLatinScript:NO];
}
//
//
//
-(CGFloat)phoneticSimilarityToString:(NSString*)string isLatinScript:(BOOL)isLatinScript{
NSString* sourceString= self;
if(isLatinScript==NO)
sourceString = [self stringByTransliteratingJapaneseToHiragana];
__block NSMutableSet* sourceSet = [[NSMutableSet alloc]init];
[sourceString enumerateSubstringsInRange:NSMakeRange(0,[sourceString length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
if(isLatinScript==NO){
[sourceSet addObject:[substring stringByTransliteratingJapaneseToRomaji]];
}
else{
[sourceSet addObject:substring];
}
}];
NSString* targetString = string;
if(isLatinScript==NO)
[string stringByTransliteratingJapaneseToHiragana];
__block NSMutableSet* targetSet = [[NSMutableSet alloc]init];
[targetString enumerateSubstringsInRange:NSMakeRange(0,[targetString length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
if(isLatinScript==NO){
[targetSet addObject:[substring stringByTransliteratingJapaneseToRomaji]];
}
else{
[targetSet addObject:substring];
}
}];
CGFloat sourceTotal = [sourceSet count];
CGFloat targetTotal = [targetSet count];
CGFloat lengthRatio = fabsf(sourceTotal-targetTotal)/(MAX(sourceTotal,targetTotal));
// remove the source sounds from the target
// the result are the target sounds not contained in the source
[targetSet minusSet:sourceSet];
CGFloat adjustedCount = [targetSet count];
return (1- (adjustedCount/targetTotal)*(1-lengthRatio));
}
@end