Add tags

Add tags to the following comment
Javaはすでに出ていますが,以下のように改良してあります。
・コメント内の文字は集計に含めるとおかしくなりそうなので含めない
・JavaのソースコードはZIPファイルで提供されることが多いので,指定されたZIPファイル内のすべてのJavaソースファイルに対して集計を行う

JDK 1.6に付属のsrc.zipを集計してみました。
' ' 9328666
'e' 2074804
't' 1744299
'n' 1284657
'i' 1246055
'r' 1217281
'a' 1167409
'o' 1041531
'l' 873609
's' 851094
'c' 728897
'u' 631196
'p' 571200
'(' 514249
')' 514079
'.' 493002
'd' 466367
'm' 422504
';' 418324
'g' 417286
\u0009 403663
'f' 339805
'b' 311888
'h' 272053
'S' 271704
'=' 271260
'E' 267436
',' 224844
'C' 214651
'I' 210502
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class CharCounter {
    public static void main(String[] args) throws Exception {
        Hashtable<Character, Count> counts = new Hashtable<Character, Count>();
        ZipFile file = new ZipFile(args[0]);
        Enumeration<? extends ZipEntry> entries = file.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (!entry.getName().endsWith(".java")) continue;
            Reader src = new InputStreamReader(file.getInputStream(entry));
            StreamTokenizer tokenizer = new StreamTokenizer(src);
            tokenizer.resetSyntax();
            tokenizer.slashSlashComments(true);
            tokenizer.slashStarComments(true);
            while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
                if (tokenizer.ttype == StreamTokenizer.TT_EOL) continue;
                char c = (char)tokenizer.ttype;
                Count count = counts.get(c);
                if (count == null) {
                    count = new Count(c);
                    counts.put(c, count);
                }
                count.n++;
            }
        }
        Vector<Count> ranking = new Vector<Count>();
        ranking.addAll(counts.values());
        Collections.sort(ranking);
        for (int i = 0; i < ranking.size(); i++) {
            System.out.println(ranking.get(i));
        }
    }
    static class Count implements Comparable<Count> {
        char c;
        int n = 0;
        Count(char c) {
            this.c = c;
        }
        public int compareTo(Count o) {
            return (o.n == this.n)? 0 : (o.n < this.n)? -1 : 1;
        }
        public String toString() {
            if (c < ' ') {
                return String.format("\\u%04x", (int)c) + "\t" + n;
            }
            return "'" + c + "'\t" + n;
        }
    }
}

Add tags

The input will be splited to tags with space.

Index

Feed

Other

Link

Pathtraq

loading...