Archive for the 'NSSpellChecker' Category

2017/05/12 システムスペルチェック辞書への学習と削除

システムのスペルチェック辞書への学習と削除を行うAppleScriptです。

Shane StanleyがMLに投稿したScriptと、それを元に辞書の言語指定をしてみたものです。

AppleScript名:システムスペルチェック辞書への学習と削除
– Created 2017-01-22 by Shane Stanley
use AppleScript version "2.4"
use framework "Foundation"
use framework "AppKit"
use scripting additions
–http://piyocast.com/as/archives/4635

–スペルチェック辞書への学習
set theWord to "piyomaru"
set theChecker to current application’s NSSpellChecker’s sharedSpellChecker()
theChecker’s learnWord:theWord
set knewIt to theChecker’s hasLearnedWord:theWord
–>  true

–スペルチェック辞書からの削除
theChecker’s unlearnWord:theWord
set knowsItNow to theChecker’s hasLearnedWord:theWord
return {knewIt, knowsItNow}
–>  {true, false}

★Click Here to Open This Script 

システムスペルチェック辞書で、辞書の言語を指定して学習させてみました。

AppleScript名:ロシア語(ru)のシステムスペルチェック辞書を指定して学習
– Created 2017-05-12 by Takaaki Naganoya
– 2017 Piyomaru Software
use AppleScript version "2.4"
use framework "Foundation"
use framework "AppKit"
use scripting additions
–http://piyocast.com/as/archives/4635

set theChecker to current application’s NSSpellChecker’s sharedSpellChecker()

set langList to (theChecker’s availableLanguages()) as list
–>  {"en", "en_GB", "en_CA", "en_IN", "en_SG", "en_AU", "en_JP", "fr", "da", "de", "es", "it", "nl", "nb", "pl", "pt_BR", "pt_PT", "fi", "sv", "tr", "ru", "ko"}

set curCheckerLang to (theChecker’s |language|()) as string
–> "en_GB"

set aWord to "Su-35"
theChecker’s setLanguage:"ru"
theChecker’s learnWord:aWord
set knewIt to theChecker’s hasLearnedWord:aWord
–>  true

set curCheckerLang to (theChecker’s |language|()) as string
–>  "ru"

★Click Here to Open This Script 

2015/12/18 指定言語のスペルチェックを行い、候補文を返す

Cocoaの機能を用いて、NSSpellCheckerで指定言語(Englishとか)でスペルチェックを行い、スペルミスがあれば候補文を返すAppleScriptです。

日本語(Japanese)はスペルチェック対象の言語として指定できませんが、よくよく考えると日本語はキーボードからのかな漢字変換入力時にスペルチェックされているようなはずのものなので、本来はスペルチェックよりも入力時の水際チェックに力を入れるべきものなのかもしれません。

日本語はスペルミスを簡単には検出しにくい言語なので、仕方のないところでしょう。

AppleScript名:指定言語のスペルチェックを行い、候補文を返す
– Created 2015-12-18 by Takaaki Naganoya
– 2015 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “AppKit”

set aText to “This is a penz.”
set spRes to spellCheckingInSpecifiedLanguage(aText, “English”) of me
–>  {missed:”penz”, suggestionList:{”This Is A Pens”, “This Is A Pend”, “This Is A Penh”, “This Is A Penn”, “This Is A Pent”, “This is a pen”}}

set aText to “Applele”
set spRes to spellCheckingInSpecifiedLanguage(aText, “English”) of me
–> {missed:”Applele”, suggestionList:{”Apple”, “Apples”, “Appeal”, “Appalled”, “Appellate”}}

set aText to “Applele”
set spRes to spellCheckingInSpecifiedLanguage(aText, “French”) of me
–>  {missed:”Applele”, suggestionList:{”Appelle”, “Appelée”, “Appelles”}}

set spRes to spellCheckingInSpecifiedLanguage(aText, “fr”) of me
–>  {missed:”Applele”, suggestionList:{”Appelle”, “Appelée”, “Appelles”}}

on spellCheckingInSpecifiedLanguage(aText, aLang)

  set aStr to current application’s NSString’s stringWithString:aText
  
set aChecker to current application’s NSSpellChecker’s sharedSpellChecker()
  
aChecker’s setLanguage:aLang
  
  set aMisspelledRange to aChecker’s checkSpellingOfString:aStr startingAt:0
  
if aMisspelledRange’s |length|() = 0 then return missing value
  
  –Get Misspelled String  
  
set aMisString to (aStr’s substringWithRange:aMisspelledRange) as text
  
  –Get Suggestion
  
set aSugList to (aChecker’s guessesForWord:aStr) as list
  
–>  {”This Is A Pens”, “This Is A Pend”, “This Is A Penh”, “This Is A Penn”, “This Is A Pent”, “This is a pen”}
  
  return {missed:aMisString, suggestionList:aSugList}
end spellCheckingInSpecifiedLanguage

★Click Here to Open This Script 

2015/12/18 NSSpellCheckerでスペルチェック可能な言語の一覧リストを取得

Cocoaの機能を用いて、NSSpellCheckerでスペルチェック可能な言語名称の一覧リストを取得するAppleScriptです。

AppleScript名:NSSpellCheckerでスペルチェック可能な言語の一覧リストを取得
– Created 2015-12-18 by Takaaki Naganoya
– 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set aChecker to current application’s NSSpellChecker’s sharedSpellChecker()
set langList to (aChecker’s availableLanguages()) as list
–>  {"en", "en_GB", "en_AU", "en_CA", "en_IN", "en_SG", "fr", "da", "de", "es", "it", "nl", "nb", "pl", "pt_BR", "pt_PT", "fi", "sv", "tr", "ru", "ko"}

set langNameList to {}
repeat with i in langList
  set j to contents of i
  
set aLocale to (current application’s NSLocale’s localeWithLocaleIdentifier:j)
  
set aLocName to (aLocale’s displayNameForKey:(current application’s NSLocaleIdentifier) value:j)
  
set the end of langNameList to aLocName as string
end repeat

return langNameList
–>  {"English", "English (United Kingdom)", "English (Australia)", "English (Canada)", "English (India)", "English (Singapore)", "français", "dansk", "Deutsch", "español", "italiano", "Nederlands", "norsk bokmål", "polski", "português (Brasil)", "português (Portugal)", "suomi", "svenska", "Türkçe", "русский", "한국어"}

★Click Here to Open This Script 

2015/12/18 英文のスペルチェックを行い、候補文を返す

Cocoaの機能を用いて、英文のスペルチェックを行い、スペルミスがあった場合には候補文を返すAppleScriptです。

何も言語を指定していないので英語になっていますが、当然のように他の言語を指定したくなります。ただ、日本語は無理でしょうね、、、

日本語のスペルチェックは無理だとしても、制限したキーワードを逸脱しないかどうか(AppleScriptの命令の予約語程度とか)を、独自のキーワード辞書を与えてスペルチェックするぐらいはしたいところです。

AppleScript名:英文のスペルチェックを行い、候補文を返す
– Created 2015-12-18 by Takaaki Naganoya
– 2015 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “AppKit”

set aText to “This is a penz.”
set spRes to spellChecking(aText) of me
–>  {missed:”penz”, suggestionList:{”This Is A Pens”, “This Is A Pend”, “This Is A Penh”, “This Is A Penn”, “This Is A Pent”, “This is a pen”}}

set aText to “AppleScript”
set spRes to spellChecking(aText) of me
–>  missing value

set aText to “Applele”
set spRes to spellChecking(aText) of me
–> {missed:”Applele”, suggestionList:{”Apple”, “Apples”, “Appeal”, “Appalled”, “Appellate”}}

on spellChecking(aText)
  set aStr to current application’s NSString’s stringWithString:aText
  
set aChecker to current application’s NSSpellChecker’s sharedSpellChecker()
  
set aMisspelledRange to aChecker’s checkSpellingOfString:aStr startingAt:0
  
if aMisspelledRange’s |length|() = 0 then return missing value
  
  
–Get Misspelled String  
  
set aMisString to (aStr’s substringWithRange:aMisspelledRange) as text
  
  
–Get Suggestion
  
set aSugList to (aChecker’s guessesForWord:aStr) as list
  
–>  {”This Is A Pens”, “This Is A Pend”, “This Is A Penh”, “This Is A Penn”, “This Is A Pent”, “This is a pen”}
  
  
return {missed:aMisString, suggestionList:aSugList}
end spellChecking

★Click Here to Open This Script