NSOrthographyクラス
Index>Foundation>NSOrthography
解説
テキストの言語・スクリプトなどを表すクラスです。
継承 | NSObject |
準拠 | NSCoding |
NSCopying | |
NSObject (NSObject) | |
フレームワーク | /System/Library/Frameworks/Foundation.framework |
使用可能 | OS X 10.6以降 |
iOS 4.0以降 | |
定義 | NSOrthography.h |
概要
本クラスについて
テキストの言語・スクリプトなどを表すクラスです。NSLinguisticTaggerクラスなどと組み合わせて文章の構文解析の際に使用します。iOS5.1では、英語のみ品詞の解析ができるようですが、日本語は形態素解析しかできない状態のようです。
NSOrthographyオブジェクトを作成
NSOrthographyオブジェクトを作成するには(+ orthographyWithDominantScript:languageMap)メソッドを使用します。
allocで作成したオブジェクトを初期化するには(– initWithDominantScript:languageMap)メソッドを使用します。
形態素解析を行い、各単語の品詞を調べるサンプル
#pragma mark NSOrthography orthographyWithDominantScript: -(void)method001 { NSString *textToAnalyse = @"That Japanese restaurant has a really authentic atmosphere."; //パースしたときの文字の要素の範囲 NSRange stringRange = NSMakeRange(0, textToAnalyse.length); // 言語マップの辞書 NSArray *language = [NSArray arrayWithObjects:@"en",@"de",@"fr",nil]; NSDictionary* languageMap = [NSDictionary dictionaryWithObject:language forKey:@"Latn"]; NSOrthography *orthography = [NSOrthography orthographyWithDominantScript:@"Latn" languageMap:languageMap]; [textToAnalyse enumerateLinguisticTagsInRange:stringRange scheme:NSLinguisticTagSchemeLexicalClass options:(NSLinguisticTaggerOmitWhitespace | NSLinguisticTaggerOmitPunctuation) orthography:orthography usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) { NSLog(@"\"%@\" is a %@, tokenRange (%d,%d), sentenceRange (%d-%d)",[textToAnalyse substringWithRange:tokenRange] ,tag,tokenRange.location,tokenRange.length, sentenceRange.location, sentenceRange.length); }]; //=>"That" is a Determiner, tokenRange (0,4), sentenceRange (0-59) //=>"Japanese" is a Adjective, tokenRange (5,8), sentenceRange (0-59) //=>"restaurant" is a Noun, tokenRange (14,10), sentenceRange (0-59) // ... }
言語とスクリプトの管理
解析の結果、含まれる言語のコードを取得するには(allLanguages)プロパティを参照します。読み取り専用です。enやjaなどのコードです。
解析の結果、含まれるスクリプトを取得するには(allScripts)プロパティを参照します。読み取り専用です。LatnやJpanなどのコードです。
解析の結果、含まれる言語マップを取得するには(languageMap)プロパティを参照します。読み取り専用です。Jpan = (ja);などスクリプトと言語の関連づけがなされた辞書です。
テキストのなかで大勢を占める言語を取得するには(dominantLanguage)プロパティを参照します。読み取り専用です。
テキストの中で大勢を占めるスクリプトを取得するには(dominantScript)プロパティを参照します。読み取り専用です。
指定したスクリプトの言語を返すには(– languagesForScript)メソッドを使用します。指定したスクリプトの大勢を占める言語を返すには(– dominantLanguageForScript)メソッドを使用します。
テキストを与えて解析させるサンプル
#pragma mark NSOrthography property -(void)method002 { NSString *aString = @"This is iOS code. これは日本語です。"; NSRange aRange = NSMakeRange (1, [aString length]-1); NSString *tagScheme = NSLinguisticTagSchemeNameType; NSLinguisticTaggerOptions opts = NSLinguisticTaggerOmitPunctuation; NSArray *supportedLanguage = [NSLinguisticTagger availableTagSchemesForLanguage:@"en"]; NSLinguisticTagger *t = [[NSLinguisticTagger alloc] initWithTagSchemes:supportedLanguage options: NSLinguisticTaggerOmitPunctuation] ; [t setString:aString]; [t enumerateTagsInRange: aRange scheme: tagScheme options:(NSLinguisticTaggerOptions)opts usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) { NSLog(@"%@", tag); }]; [t enumerateTagsInRange: aRange scheme: NSLinguisticTagSchemeNameTypeOrLexicalClass options:(NSLinguisticTaggerOptions) opts usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) { NSLog(@"%@", tag); }]; NSRange effectiveRange; NSOrthography *orthography = [t orthographyAtIndex:aRange.location effectiveRange:&effectiveRange]; NSLog(@"%u,%u,%@",effectiveRange.location,effectiveRange.length,orthography.allLanguages); NSLog(@"%u,%u,%@",effectiveRange.location,effectiveRange.length,orthography.allScripts); NSLog(@"%u,%u,%@",effectiveRange.location,effectiveRange.length,orthography.dominantLanguage); NSLog(@"%u,%u,%@",effectiveRange.location,effectiveRange.length,orthography.dominantScript); NSLog(@"%u,%u,%@",effectiveRange.location,effectiveRange.length,orthography.languageMap); NSLog(@"%s %@",__FUNCTION__,[orthography languagesForScript:@"Jpan"]); NSLog(@"%s %@",__FUNCTION__,[orthography dominantLanguageForScript:@"Jpan"]); }
メソッド
NSOrthographyのインスタンスの作成
+ orthographyWithDominantScript:languageMap
– initWithDominantScript:languageMap
言語マップの定義
dominantScript property
languageMap property
言語とスクリプトの管理
– languagesForScript
– dominantLanguageForScript
allLanguages property
allScripts property
dominantLanguage property
定数