短い日本語の文章で、カギカッコ(「」)のペアがそろっているか、順番がきちんとしているか、個数が合っているかなどを検出するテスト用のAppleScriptです。
何回も同じようなScriptを書いてきたような気がします。
AppleScript名:カギカッコのペア検出+エラーチェック.scpt |
— – Created by: Takaaki Naganoya – Created on: 2022/08/22 — – Copyright © 2022 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use scripting additions set aStr to "「モーニング娘。」を表示した。「藤岡弘。」を表示。「藤岡弘。」。「藤岡弘。」" –set aStr to "「モーニング娘。」を表示した。「藤岡弘。」を表示「。「藤岡弘。」。」「藤岡弘。」" set kagikakkoList to {"「", "」"} –カギカッコ 開始文字、終了文字ペア。英語で言うところのダブルクォート(「“」, 「”」) set aRes to pairKagikakkoCheckAndReturnPositionPairts(aStr, kagikakkoList) of me –> {{1, 9}, {16, 21}, {26, 31}, {33, 38}}–正常な場合 –> false –カッコの対応についてエラーがある場合 on pairKagikakkoCheckAndReturnPositionPairts(aStr as string, kagikakkoList as list) set aList to {} –カギカッコの開始文字、終了文字の位置をシーケンシャルにピックアップする repeat with i in kagikakkoList set j to contents of i set aRes to scanStrMultiple(aStr, j) of me set the end of aList to aRes end repeat –> {{1, 16, 26, 33}, {9, 21, 31, 38}} –カギカッコの個数が合っていないかどうかチェック if length of aList is not equal to length of kagikakkoList then error "Separator number error" –ペアリスト作成前に、カギカッコの開始、修了の文字の個数が合っているかをチェック set startLen to length of first item of aList set endLen to length of second item of aList if startLen is not equal to endLen then error "Separator pair number is not same" –ペアリストを作成 set pairList to {} repeat with i from 1 to (length of first item of aList) set item1Dat to contents of item i of (first item of aList) set item2Dat to contents of item i of (second item of aList) set the end of pairList to {item1Dat, item2Dat} end repeat –> {{1, 9}, {16, 21}, {25, 32}, {27, 34}, {35, 40}} –ペアリストのクロスチェック repeat with i from 1 to ((length of pairList) – 1) set {itemA1, itemA2} to contents of item i of pairList set {itemB1, itemB2} to contents of item (i + 1) of pairList if itemA1 > itemA2 then return false if itemA1 > itemB1 then return false if itemA2 > itemB1 then return false end repeat return pairList end pairKagikakkoCheckAndReturnPositionPairts on scanStrMultiple(aStr, targStr) set aStrLen to length of aStr set tLen to (length of targStr) set posOffset to 0 copy aStr to bStr set aList to {} repeat set aRes to offset of targStr in bStr if aRes = 0 then exit repeat if aRes is not in aList then set the end of aList to (aRes + posOffset) set posOffset to posOffset + aRes end if if posOffset ≥ aStrLen then exit repeat set tPos to (aRes + tLen) if tPos > length of bStr then set tPos to length of bStr end if if (length of bStr) ≤ tLen then exit repeat set bStr to text tPos thru -1 of bStr end repeat return aList end scanStrMultiple –offset命令の実行を横取りする on offset of searchStr in str set aRes to getOffset(str, searchStr) of me return aRes end offset on getOffset(str, searchStr) set d to divideBy(str, searchStr) if (count d) is less than 2 then return 0 return (length of item 1 of d) + 1 end getOffset on divideBy(str, separator) set delSave to AppleScript’s text item delimiters set the AppleScript’s text item delimiters to separator set strItems to every text item of str set the AppleScript’s text item delimiters to delSave return strItems end divideBy |
More from my site
(Visited 43 times, 1 visits today)