Comment detail

コード中の文字の頻度分析 (Nested Flatten)
 scalaパッケージ直下のソースファイルを対象に処理した結果の上位10件は以下の様になりました。

SPACE 127418 28.10
e 27909 6.15
t 21232 4.68
a 15490 3.42
n 14061 3.10
r 13917 3.07
o 13648 3.01
CR 12956 2.86
i 12905 2.85
s 11936 2.63
 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
59
60
61
62
63
64
65
66
import    java.io.File
import    java.io.FileNotFoundException
import    scala.collection.mutable.HashMap
import    scala.io.Source

class FrequencyAnalyzer(ps:List[String]) {
    var    l:Int = 0
    var    a:HashMap[Char,Int] = null
    
    def getTotal:Int = l
    def getResult:HashMap[Char,Int] = a
    
    private def analyzeFile(s:Source):Unit = {
        s.hasNext match {
            case true => {
                val    c:Char = s.next
                a.get(c) match {
                    case Some(i) => a.update(c,i + 1)
                    case _ => a.update(c, 1)
                }
                l = l + 1
                analyzeFile(s)
            }
            case _ => ()
        }
    }
    
    def analyze:FrequencyAnalyzer = {
        a = new HashMap[Char,Int]
        l = 0
        ps.foreach { f =>
            try {
                analyzeFile(Source.fromFile(new File(f)))
            } catch {
                case ex:FileNotFoundException => printf("%s not found.\n",f)
                case ex:Exception => {
                    ex.printStackTrace
                    System.exit(1)
                }
            }
        }
        this
    }
    
    def print:Unit = {
        getResult.elements.toList.sort { (a,b) => a._2 > b._2 }.foreach { e =>
            val    d:String = e._1 match {
                case ' ' => "SPACE"
                case '\t' => "TAB"
                case 0x0a => "CR"
                case 0x0d => "LF"
                case c => c.toString
            }
            printf("%s\t%d\t%2.2f\n",d,e._2,e._2.asInstanceOf[Float] * 100 / getTotal)
        }
    }
}

object FrequencyAnalyzerMain {
    def main(args:Array[String]):Unit = {
        args.size match {
            case 0 => println("usage: scala FrequencyAnalyzerMain source")
            case _ => (new FrequencyAnalyzer(args.toList)).analyze.print
        }
    }
}

Index

Feed

Other

Link

Pathtraq

loading...