–課題:複数の小数点チェックが未実装 –set a to the clipboard set a to "1垓232京120兆1億822万10" set jRes to decodeJapaneseNumText(a) of japaneseNumberDecodingKit –> "102320120000108220000" –set the clipboard to jRes
script japaneseNumberDecodingKit –日本語数値表現文字列を数字文字列にデコードする on decodeJapaneseNumText(a) set scaleList1 to {"万", "億", "兆", "京", "垓", "丈", "壌", "溝", "砂", "正", "載", "極", "恒河沙", "阿僧梢", "那由他", "不可思議", "無量大数"} set bList to splitByDigitText(a, scaleList1) of me –パースした最終アイテムにメジャー桁の桁リスト内の文字が入っているかをチェック(0〜9999までの桁を処理するため) set lastItem to contents of last item of bList set aRes to chkContainEachListItem(lastItem, scaleList1) of me if aRes = false then –bListの最終アイテムが0〜9999だった場合 set bbList to items 1 thru -2 of bList else –bListの最終アイテムはscaleList1の桁テキストを含んでいた(端数がなかった) set bbList to bList set lastItem to "0" end if –各桁の評価を行う実装試験 set allFormula to {} set scaleList2a to {"千", "百", "十"} set scaleList2b to {"1000", "100", "10"} set firstF to true repeat with i in bbList set anItem to contents of i as string set cList to splitByDigitText(anItem, scaleList2a) of me set tList to {"千", "百", "十"} set rList to {"*1000+", "*100+", "*10+"} set aa to repCharList(anItem, tList, rList) of me set aPart to text 1 thru -2 of aa set bPart to text -1 thru -1 of aa if aPart ends with "+" then set aPart to aPart & "0" end if set scaleList2 to {"10000", "100000000", "1000000000000", "10000000000000000", "100000000000000000000", "1000000000000000000000000", "10000000000000000000000000000", "100000000000000000000000000000000", "1000000000000000000000000000000000000", "10000000000000000000000000000000000000000", "100000000000000000000000000000000000000000000", "1000000000000000000000000000000000000000000000000", "10000000000000000000000000000000000000000000000000000", "100000000000000000000000000000000000000000000000000000000", "1000000000000000000000000000000000000000000000000000000000000", "10000000000000000000000000000000000000000000000000000000000000000", "100000000000000000000000000000000000000000000000000000000000000000000"} set aFormula to "(" & aPart & ")" & "*" & repCharList(bPart, scaleList1, scaleList2) of me if firstF then set allFormula to aFormula set firstF to false else set allFormula to allFormula & " + " & aFormula end if end repeat set allFormula to allFormula & " + " & lastItem –allFormula set cRes to calcByBC(allFormula) of me return cRes end decodeJapaneseNumText on splitByDigitText(aText, scaleList) set aList to words of aText if (count of aList) = 1 then return aText end if set calList to {} set posCount to 1 set startPointer to 1 set iCount to length of aList repeat with i from 1 to iCount set j to contents of item i of aList if j is in scaleList then set tmpItem to items startPointer thru i of aList set the end of calList to (tmpItem as string) set startPointer to i + 1 end if end repeat if (i + 1) is not equal to startPointer then set tmpItem to items startPointer thru -1 of aList set tmpItem to tmpItem as string set the end of calList to tmpItem end if return calList end splitByDigitText –Written By Philip Aker –文字置換ルーチン on repChar(origText, targStr, repStr) set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr} set temp to text items of origText set AppleScript’s text item delimiters to repStr set res to temp as text set AppleScript’s text item delimiters to txdl return res end repChar on calcByBC(aFormula) set yC to ASCII character 92 set aCMD to "echo \" scale=10; " & aFormula & "\" | bc" set aRes to do shell script aCMD return aRes end calcByBC –文字置換ルーチン リストにより複数のテキストを一括で置換 on repCharList(origText, targStrList, repStrList) set {tCount, rCount} to {length of targStrList, length of repStrList} –targStrListとrepStrListの項目数がイコールでなければfalseを返す if tCount is not equal to rCount then return false set origT to origText repeat with i from 1 to tCount set targStr to contents of (item i of targStrList) set repStr to contents of (item i of repStrList) set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr} set temp to text items of origT set AppleScript’s text item delimiters to repStr set res to temp as text set AppleScript’s text item delimiters to txdl set origT to res end repeat return res end repCharList –指定文字列の中に指定リストの各アイテムが含まれているかをチェック on chkContainEachListItem(chkStr, chkList) repeat with i in chkList set j to contents of i if chkStr contains j then return true end if end repeat return false end chkContainEachListItem end script |