LoginSignup
1
0

More than 5 years have passed since last update.

メソッドで使われている単語を調べる

Last updated at Posted at 2017-04-05

CompiledMethodにメソッドを追加する

CompiledMethodにメソッド名で使われいる単語を返すメソッドselectorWordsを追加する。メソッドはMyStaticsパッケージに属させたいのでプロトコルは*MyStaticsにしておく。selectorWordsが動くように、Stringにもメソッド、selectorWords、splitAtCapitalを追加する。これらもプロトコルは*MyStaticsにする。

CompiledMethod>>selectorWords(*MyStatics)
selectorWords
    ^ self selector selectorWords .
String>>selectorWords(*MyStatics)
selectorWords
    "
    'acceptDroppingMorph:event:inMorph:' selectorWords 
    => #('accept' 'Dropping' 'Morph' 'event' 'in' 'Morph')
    "
    | words |
    words := self keywords flatCollect: #splitAtCapital.
    ^ words collect: [ :each | each copyWithout: $: ]

String>>splitAtCapital(*MyStatics)

splitAtCapital
    "
    'aCamelCase' splitAtCapital 
    => an OrderedCollection('a' 'Camel' 'Case')
    "
    ^ OrderedCollection
        streamContents: [ :stream | 
            | start doCopy |
            start := 1.
            doCopy := [ :end | (self copyFrom: start to: end) ifNotEmpty: [ :it | stream nextPut: it ] ].
            self
                doWithIndex: [ :each :index | 
                    each isUppercase
                        ifTrue: [ doCopy value: index - 1.
                            start := index ] ].
            doCopy value: self size ]

メソッドで使われている単語をすべて集める

テストメソッドと単語の内一文字だけの物は除外するようにする。以下のコードでsortedCountsに出現頻度でソートされた単語が入っている。

workspace.st

"テストメソッドっぽいのを除外"
methods := CompiledMethod allInstances reject: [ :each | each selector beginsWith: 'test' ].

words := (methods flatCollect: #selectorWords as: Bag) collect: #asLowercase.
"一文字だけの単語を除外"
selectedWords := words reject: [ :each | each size <= 1].
sortedCounts := selectedWords sortedCounts.

上位100位を見てみる。メソッド名なのでやっぱり動詞と前置詞が多くなるようだ。

1   7347->'for'
2   6379->'on'
3   5243->'is'
4   4803->'class'
5   4609->'to'
6   4455->'with'
7   3921->'in'
8   3876->'initialize'
9   3775->'from'
10  3591->'name'
11  3028->'new'
12  2996->'at'
13  2776->'set'
14  2735->'token'
15  2726->'of'
16  2618->'add'
17  2398->'visit'
18  2393->'tokens'
19  2391->'node'
20  2294->'action'
21  2184->'text'
22  2184->'method'
23  2183->'all'
24  2164->'default'
25  2133->'do'
26  2075->'color'
27  2010->'value'
28  1805->'example'
29  1699->'string'
30  1632->'if'
31  1624->'as'
32  1589->'variables'
33  1573->'type'
34  1523->'list'
35  1461->'accept'
36  1456->'size'
37  1399->'label'
38  1386->'get'
39  1368->'up'
40  1359->'variable'
41  1342->'selector'
42  1322->'selected'
43  1312->'file'
44  1307->'block'
45  1302->'remove'
46  1279->'package'
47  1278->'next'
48  1261->'reduce'
49  1250->'has'
50  1243->'button'
51  1232->'element'
52  1218->'version'
53  1199->'render'
54  1192->'and'
55  1169->'index'
56  1156->'named'
57  1130->'morph'
58  1108->'print'
59  1106->'left'
60  1105->'icon'
61  1092->'right'
62  1089->'context'
63  1082->'selection'
64  1075->'style'
65  1030->'update'
66  988->'menu'
67  979->'handle'
68  951->'source'
69  943->'copy'
70  932->'start'
71  888->'put'
72  884->'gt'
73  866->'key'
74  864->'names'
75  856->'stream'
76  853->'event'
77  845->'width'
78  844->'open'
79  842->'title'
80  835->'line'
81  832->'code'
82  828->'create'
83  814->'test'
84  804->'fill'
85  800->'expression'
86  794->'number'
87  779->'model'
88  778->'position'
89  773->'category'
90  747->'load'
91  745->'visitor'
92  743->'absent'
93  739->'end'
94  737->'instance'
95  734->'path'
96  732->'project'
97  721->'font'
98  720->'description'
99  718->'not'
100 704->'parse'

Roassalでグラフにしてみる

単語の上位2000位までをグラフにして見てみる。y軸のスケールは対数

workspace.st

dataSet := RTData new 
    points: (sortedCounts copyFrom: 1 to: 2000);
    y:  [ :each | each key log];
    yourself.

grapher := RTGrapher new
    add: dataSet ;
    open.

rossale.1.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0