「words of」によるAppleScriptビルトインの超簡易形態素解析を実行して、日本語の文を単語(形態素)ごとに分解し、macOS標準搭載の辞書.appの「スーパー大辞林」で全単語を検索するAppleScriptです。
実行にあたっては、上記Frameworkを~/Library/Frameworksにインストールしたうえで、macOS 10.14以降ではScript Debuggerを用いるか、お使いのMacをSIP解除してScript Editor上で呼び出して実行する必要があります。
処理対象:”大きな栗の木の下で”
実行結果:
–> {{dictName:”スーパー大辞林”, keywordName:”で”, dictContents:”で 「て」の濁音の仮名。歯茎破裂音の有声子音と前舌の半狭母音とから成る音節。”}….}}
AppleScript名:簡易形態素解析してスーパー大辞林で辞書検索 |
— Created 2015-10-25 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "dictKit" –https://github.com/mattt/DictionaryKit set aText to "大きな栗の木の下で" set wList to words of aText repeat with i in wList set dRes to getWordDifinitionInJapaneseDictionary(i as string) of me end repeat on getWordDifinitionInJapaneseDictionary(aTerm as string) set localNameList to getNameOfLocalDictionaries() of me set dNameList to {"スーパー大辞林"} set aSet to current application’s NSMutableSet’s setWithArray:localNameList set bSet to current application’s NSMutableSet’s setWithArray:dNameList aSet’s intersectSet:bSet set dList to aSet’s allObjects() as list if dList = {} then return false set aResList to {} repeat with i in dNameList set aDictionary to (current application’s TTTDictionary’s dictionaryNamed:i) set hitEntryList to (aDictionary’s entriesForSearchTerm:aTerm) as list if hitEntryList is not equal to {missing value} then repeat with ii in hitEntryList set j to contents of ii set headW to (j’s headword) set headW to headW as text try set aText to (j’s |text|) set aText to aText as text on error set aText to (j’s HTML) set aText to decodeCharacterReference(aText) of me end try set the end of aResList to {dictName:(i as text), keywordName:headW, dictContents:aText} end repeat end if end repeat aResList end getWordDifinitionInJapaneseDictionary on decodeCharacterReference(aStr) set anNSString to current application’s NSString’s stringWithString:aStr set theData to anNSString’s dataUsingEncoding:(current application’s NSUTF16StringEncoding) set styledString to current application’s NSAttributedString’s alloc()’s initWithHTML:theData documentAttributes:(missing value) set plainText to (styledString’s |string|()) as string return plainText end decodeCharacterReference on getNameOfLocalDictionaries() set dSet to current application’s TTTDictionary’s availableDictionaries() set dList to dSet’s allObjects() set dNameList to {} repeat with i in dList set the end of dNameList to (i’s |name|()) as text end repeat return dNameList end getNameOfLocalDictionaries |