AppleScript名:文字種別にカウントする v2 |
— Created 2018-1-11 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" –http://piyocast.com/as/archives/5104 property NSString : a reference to current application’s NSString property NSNumber : a reference to current application’s NSNumber property NSDictionary : a reference to current application’s NSDictionary property NSCountedSet : a reference to current application’s NSCountedSet property NSMutableArray : a reference to current application’s NSMutableArray property NSNumberFormatter : a reference to current application’s NSNumberFormatter property NSRegularExpressionSearch : a reference to current application’s NSRegularExpressionSearch property NSNumberFormatterRoundUp : a reference to current application’s NSNumberFormatterRoundUp set sourcePath to choose file of type {"public.plain-text"} tell current application set aStr to (read sourcePath as «class utf8») end tell set aRes to detectCharKindRating(aStr) of me –> {kanjiNum:563289, kanjiRating:22.0, hiraganaNum:1311933, hiraganaRating:51.2, katakanaNum:210161, katakanaRating:8.2, otherNum:478690, otherRating:18.7, totalCount:2564073}–Light Novel –> {kanjiNum:24960, kanjiRating:28.0, hiraganaNum:56080, hiraganaRating:62.9, katakanaNum:1063, katakanaRating:1.2, otherNum:7136, otherRating:8.0} –文学(坊ちゃん) on detectCharKindRating(aStr as string) set theCountedSet to NSCountedSet’s alloc()’s initWithArray:(characters of aStr) set theEnumerator to theCountedSet’s objectEnumerator() set cCount to 0 set hCount to 0 set kCount to 0 set oCount to 0 set totalC to length of aStr repeat set aValue to theEnumerator’s nextObject() if aValue is missing value then exit repeat set aStr to aValue as string set tmpCount to (theCountedSet’s countForObject:aValue) set s1Res to chkKanji(aStr) of me set s2Res to chkKatakana(aStr) of me set s3Res to chkHiragana(aStr) of me if s1Res = true then set cCount to cCount + tmpCount else if s2Res = true then set kCount to kCount + tmpCount else if s3Res = true then set hCount to hCount + tmpCount else set oCount to oCount + tmpCount end if end repeat set ckRes to roundingUp((cCount / totalC) * 100, 1) of me set kkRes to roundingUp((kCount / totalC) * 100, 1) of me set hgRes to roundingUp((hCount / totalC) * 100, 1) of me set otRes to roundingUp((oCount / totalC) * 100, 1) of me return {kanjiNum:cCount, kanjiRating:ckRes, hiraganaNum:hCount, hiraganaRating:hgRes, katakanaNum:kCount, katakanaRating:kkRes, otherNum:oCount, otherRating:otRes} end detectCharKindRating on chkKanji(aChar) return detectCharKind(aChar, "[一-龠]") of me end chkKanji on chkHiragana(aChar) return detectCharKind(aChar, "[ぁ-ん]") of me end chkHiragana on chkKatakana(aChar) return detectCharKind(aChar, "[ァ-ヶ]") of me end chkKatakana on detectCharKind(aChar, aPattern) set aChar to NSString’s stringWithString:aChar set searchStr to NSString’s stringWithString:aPattern set matchRes to aChar’s rangeOfString:searchStr options:(NSRegularExpressionSearch) if matchRes’s location() = (current application’s NSNotFound) or (matchRes’s location() as number) > 9.99999999E+8 then return false else return true end if end detectCharKind on roundingUp(aNum, aDigit as integer) set a to aNum as real set aFormatter to NSNumberFormatter’s alloc()’s init() aFormatter’s setMaximumFractionDigits:aDigit aFormatter’s setRoundingMode:(NSNumberFormatterRoundUp) set aStr to aFormatter’s stringFromNumber:(NSNumber’s numberWithFloat:a) return (aStr as text) as real end roundingUp |
More from my site
(Visited 30 times, 1 visits today)