AppleScript名:文字を集合(CountedSet)に変換して文字列同士の近似度を計算する(単語単位で評価) v4 |
— Created 2017-03-18 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aStr to "This is a pinnapple." set bStr to "This is an apple." set cStr to "Piyomaru San Dayo." set aRes to stringDifferenceWithAppearance(aStr, bStr, "A & B") of me –> {intersect:2, minus:2, dataLabel:"A & B"} set bRes to stringDifferenceWithAppearance(bStr, cStr, "B & C") of me –> {intersect:0, minus:4, dataLabel:"B & C"} set cRes to stringDifferenceWithAppearance(cStr, aStr, "C & A") of me –> {intersect:0, minus:3, dataLabel:"C & A"} –結果をソートする set allRes to current application’s NSArray’s arrayWithArray:{aRes, bRes, cRes} set aSortDesc to current application’s NSSortDescriptor’s alloc()’s initWithKey:"intersect" ascending:false set bSortDesc to current application’s NSSortDescriptor’s alloc()’s initWithKey:"minus" ascending:true set aSortedArray to allRes’s sortedArrayUsingDescriptors:{aSortDesc, bSortDesc} –もっとも似ている傾向を持つデータの組み合わせ set aMostApproximative to aSortedArray’s firstObject() as record –> {minus:2, dataLabel:"A & B", intersect:2} –文字列同士の近似傾向を、相違点と重複箇所の2つの方向から計算する on stringDifferenceWithAppearance(aStr, bStr, aLabel) set aMinus to getApproximationBetweenStrings(aStr, bStr) of me set aIntersect to getApproximationBetweenStringsIntersect(aStr, bStr) of me return {intersect:aIntersect, minus:aMinus, dataLabel:aLabel} end stringDifferenceWithAppearance –与えられた文字列同士の相違点を計算する(結果がより小さい値のデータ同士が似ている) on getApproximationBetweenStrings(aStr, bStr) set aList to current application’s NSMutableArray’s arrayWithArray:(words of aStr) set bList to current application’s NSMutableArray’s arrayWithArray:(words of bStr) set aIndex to current application’s NSCountedSet’s alloc()’s initWithArray:aList set bIndex to current application’s NSCountedSet’s alloc()’s initWithArray:bList aIndex’s minusSet:bIndex set aRes to aIndex’s allObjects()’s |count|() bIndex’s minusSet:aIndex set bRes to bIndex’s allObjects()’s |count|() if aRes ≥ bRes then return bRes else return aRes end if end getApproximationBetweenStrings –与えられた文字列同士の重複箇所を計算する(結果がより大きい値のデータ同士が似ている) on getApproximationBetweenStringsIntersect(aStr, bStr) set aList to current application’s NSMutableArray’s arrayWithArray:(words of aStr) set bList to current application’s NSMutableArray’s arrayWithArray:(words of bStr) set aIndex to current application’s NSCountedSet’s alloc()’s initWithArray:aList set bIndex to current application’s NSCountedSet’s alloc()’s initWithArray:bList aIndex’s intersectSet:bIndex set aRes to aIndex’s allObjects()’s |count|() return aRes end getApproximationBetweenStringsIntersect |
More from my site
(Visited 21 times, 1 visits today)