ChatGPTに日本語形態素解析のAppleScriptを書かせてみました。そのままでは使い物にならず、ずいぶん書き直しましたが、ゼロから書くよりは時間の節約になっています。
ただし、実際に書いて動かしてみるとNLTaggerでは日本語の品詞情報が取得できないので、ほぼ役に立ちません。MeCab経由で取得したほうが実用的です。
この状態で「なんちゃらIntelligence」とか言われても、「はぁ?」としか、、、基礎ができていないのに応用はできませんよ。
AppleScript名:NLTaggerで品詞つきの日本語形態素解析(ただし、品詞はほとんど役に立たない).scptd |
— – Created by: Takaaki Naganoya – Created on: 2025/06/30 — – Copyright © 2025 Piyomaru Software, All Rights Reserved — use AppleScript version "2.8" — macOS 12 or later use framework "Foundation" use framework "NaturalLanguage" use scripting additions set inputText to current application’s NSString’s stringWithString:"私は昨日東京に行きました。" — NLTaggerの作成(品詞情報を取得したいのでLexicalClassを指定) set tagger to current application’s NLTagger’s alloc()’s initWithTagSchemes:{current application’s NLTagSchemeLexicalClass} tagger’s setString:inputText tagger’s setLanguage:(current application’s NLLanguageJapanese) range:{location:0, |length|:(inputText’s |length|())} — 結果を格納するリスト set tokenList to {} — 解析処理 set inputLength to (inputText’s |length|()) as integer set currentIndex to 0 repeat while currentIndex < inputLength set tokenRangeRef to reference set {tagRes, theRange} to tagger’s tagAtIndex:(currentIndex) unit:(current application’s NLTokenUnitWord) |scheme|:(current application’s NLTagSchemeLexicalClass) tokenRange:(tokenRangeRef) –return theRange set theRange to theRange as record set startLoc to theRange’s location set rangeLen to theRange’s |length|() if (rangeLen > 0) then set tokenText to inputText’s substringWithRange:(current application’s NSMakeRange(startLoc, rangeLen)) if tagRes is not missing value then set posStr to tagRes as string else set posStr to "Unknown" end if set endIndex to startLoc + rangeLen set end of tokenList to {(tokenText as string), posStr} set currentIndex to endIndex else exit repeat end if end repeat return tokenList –> {{"私", "OtherWord"}, {"は", "OtherWord"}, {"昨日", "OtherWord"}, {"東京", "OtherWord"}, {"に", "OtherWord"}, {"行き", "OtherWord"}, {"まし", "OtherWord"}, {"た", "OtherWord"}, {"。", "SentenceTerminator"}} |