Created
July 10, 2016 10:26
-
-
Save sneznaovca/6d2f1ff126247d0fc98998cd50dd2376 to your computer and use it in GitHub Desktop.
Script print histogram first chars in wordlist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"log" | |
"os" | |
"strings" | |
"sort" | |
) | |
func rankByWordCount(wordFrequencies map[string]int) PairList{ | |
pl := make(PairList, len(wordFrequencies)) | |
i := 0 | |
for k, v := range wordFrequencies { | |
pl[i] = Pair{k, v} | |
i++ | |
} | |
sort.Sort(sort.Reverse(pl)) | |
return pl | |
} | |
type Pair struct { | |
Key string | |
Value int | |
} | |
type PairList []Pair | |
func (p PairList) Len() int { return len(p) } | |
func (p PairList) Less(i, j int) bool { return p[i].Value < p[j].Value } | |
func (p PairList) Swap(i, j int){ p[i], p[j] = p[j], p[i] } | |
func main() { | |
file, err := os.Open("sk.txt") | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer file.Close() | |
countChars := make(map[string]int) | |
// read file by line | |
scanner := bufio.NewScanner(file) | |
for scanner.Scan() { | |
countChars[strings.ToLower(string([]rune(scanner.Text())[0]))]++ | |
} | |
if err := scanner.Err(); err != nil { | |
log.Fatal(err) | |
} | |
// sort by value | |
sorted := rankByWordCount(countChars) | |
// sum all values | |
count := 0 | |
for _, v := range sorted { | |
count += v.Value | |
} | |
// print result | |
onePercent := float64(count) * 0.01 | |
for _, v := range sorted { | |
fmt.Printf("%s :\t %.1f%%\n", v.Key, float64(v.Value) / onePercent) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment