NSLinguisticTaggerの後継であるNaturalLanguage(NLTokenizer)を用いて、日本語テキストの形態素解析を行なってみました。
この手の頭を使わないプログラミングだとChatGPTに丸投げすれば、(デバッグ作業が必要な)AppleScriptのコードが出てくるので、走らせて直して……で、15分ほどでしょうか。
NLTokenizerの処理結果は、間違ってはいないものの、相変わらず固有名詞を無視しまくるので、実用レベルにあるとはいえません。自分でAppleScriptで書いた簡易日本語パーサー「easyJParse」と処理結果が変わらないので、頑張ってNLTokenizerを使う意義はほとんどないといえます。
AppleScript名:NaturalLanguage.frameworkを用いて日本語テキストの形態素解析を行う.scptd |
— – Created by: Takaaki Naganoya – Created on: 2025/04/10 — – Copyright © 2025 Piyomaru Software, All Rights Reserved — use AppleScript use framework "Foundation" use framework "NaturalLanguage" use scripting additions set inputText to current application’s NSMutableString’s stringWithString:"私の名前は長野谷です。" — NLTokenizerのインスタンスを作成し、単語単位で設定 set tokenizer to current application’s NLTokenizer’s alloc()’s initWithUnit:(current application’s NLTokenUnitWord) tokenizer’s setString:inputText tokenizer’s setLanguage:(current application’s NLLanguageJapanese) — 解析範囲の取得 set textLength to (inputText’s |length|()) as integer set theRange to current application’s NSMakeRange(0, textLength) — トークンを格納するリスト set tokenList to {} — トークン範囲を1つずつ取得して、文字列を抽出 set currentIndex to tokenizer’s tokenRangeAtIndex:0 repeat while (currentIndex’s location < (textLength)) set subStr to (inputText’s substringWithRange:currentIndex) copy (subStr as string) to end of tokenList — 次のトークン範囲へ進む set nextIndex to (currentIndex’s location) + (currentIndex’s |length|()) if nextIndex ≥ textLength then exit repeat set currentIndex to tokenizer’s tokenRangeAtIndex:nextIndex end repeat return tokenList –> {"私", "の", "名前", "は", "長野", "谷", "です"} |