2022-05-30 09:36:08 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-05-30 10:07:48 +02:00
|
|
|
"fmt"
|
|
|
|
"github.com/dariubs/percent"
|
2022-05-30 11:42:59 +02:00
|
|
|
"gonum.org/v1/plot"
|
|
|
|
"gonum.org/v1/plot/plotter"
|
|
|
|
"gonum.org/v1/plot/plotutil"
|
|
|
|
"gonum.org/v1/plot/vg"
|
2022-05-30 11:02:34 +02:00
|
|
|
"io/ioutil"
|
2022-05-30 11:42:59 +02:00
|
|
|
"log"
|
2022-05-30 11:02:34 +02:00
|
|
|
"sort"
|
2022-05-30 09:36:08 +02:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-05-30 11:02:34 +02:00
|
|
|
type letter struct {
|
|
|
|
upperCase string
|
|
|
|
lowerCase string
|
|
|
|
count int
|
|
|
|
frequency float64
|
|
|
|
}
|
|
|
|
|
|
|
|
type letterList []letter
|
|
|
|
|
|
|
|
func (l letterList) Len() int {
|
|
|
|
return len(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l letterList) Less(i, j int) bool {
|
|
|
|
return l[i].frequency > l[j].frequency
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l letterList) Swap(i, j int) {
|
|
|
|
l[i], l[j] = l[j], l[i]
|
|
|
|
}
|
|
|
|
|
2022-06-08 16:26:04 +02:00
|
|
|
const lettersInTheAlphabet = 'Z' - 'A' + 1
|
2022-05-30 11:42:59 +02:00
|
|
|
|
2022-06-08 16:26:04 +02:00
|
|
|
var letters = make([]letter, lettersInTheAlphabet)
|
2022-05-30 11:02:34 +02:00
|
|
|
|
|
|
|
func initLetterStruct() {
|
2022-06-08 16:26:04 +02:00
|
|
|
for i := 0; i < lettersInTheAlphabet; i++ {
|
|
|
|
letters[i].upperCase = string(rune('A' + i))
|
|
|
|
letters[i].lowerCase = string(rune('A' + i + 32))
|
2022-05-30 11:02:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readFile(relativePath string) string {
|
|
|
|
content, err := ioutil.ReadFile(relativePath)
|
|
|
|
if err != nil {
|
2022-05-30 11:42:59 +02:00
|
|
|
log.Fatalln(err)
|
2022-05-30 11:02:34 +02:00
|
|
|
}
|
|
|
|
return string(content)
|
|
|
|
}
|
2022-05-30 10:07:48 +02:00
|
|
|
|
2022-05-30 11:02:34 +02:00
|
|
|
func countLetters(inputText string) int {
|
2022-05-30 10:07:48 +02:00
|
|
|
totalCount := 0
|
2022-06-08 16:26:04 +02:00
|
|
|
for i := 0; i < lettersInTheAlphabet; i++ {
|
2022-05-30 13:41:45 +02:00
|
|
|
letters[i].count +=
|
|
|
|
strings.Count(inputText, letters[i].lowerCase) +
|
|
|
|
strings.Count(inputText, letters[i].upperCase)
|
|
|
|
totalCount += letters[i].count
|
2022-05-30 10:07:48 +02:00
|
|
|
}
|
2022-05-30 11:02:34 +02:00
|
|
|
return totalCount
|
|
|
|
}
|
|
|
|
|
|
|
|
func calculateFrequency(totalCount int) {
|
2022-06-08 16:26:04 +02:00
|
|
|
for i := 0; i < lettersInTheAlphabet; i++ {
|
2022-05-30 13:41:45 +02:00
|
|
|
letters[i].frequency = percent.PercentOf(letters[i].count, totalCount)
|
2022-05-30 10:07:48 +02:00
|
|
|
}
|
2022-05-30 11:02:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func printResult() {
|
2022-06-08 16:26:04 +02:00
|
|
|
for i := 0; i < lettersInTheAlphabet; i++ {
|
2022-05-30 13:41:45 +02:00
|
|
|
l := letters[i]
|
2022-05-30 11:02:34 +02:00
|
|
|
fmt.Printf(
|
|
|
|
"The letter %s (%s) occurs %d times in the text and the frequencyArray in percent is %0.2f\n",
|
|
|
|
l.upperCase, l.lowerCase, l.count, l.frequency,
|
|
|
|
)
|
2022-05-30 10:07:48 +02:00
|
|
|
}
|
2022-05-30 09:36:08 +02:00
|
|
|
}
|
|
|
|
|
2022-05-30 11:42:59 +02:00
|
|
|
func plotResult() {
|
|
|
|
var count plotter.Values
|
2022-06-08 16:26:04 +02:00
|
|
|
for i := 0; i < lettersInTheAlphabet; i++ {
|
2022-05-30 13:41:45 +02:00
|
|
|
count = append(count, float64(letters[i].count))
|
2022-05-30 11:42:59 +02:00
|
|
|
}
|
|
|
|
p := plot.New()
|
|
|
|
p.Title.Text = "Letter count"
|
|
|
|
p.X.Label.Text = "letter"
|
|
|
|
p.Y.Label.Text = "count"
|
2022-06-08 16:26:04 +02:00
|
|
|
countBars, err := plotter.NewBarChart(count, vg.Points(lettersInTheAlphabet/2))
|
2022-05-30 11:42:59 +02:00
|
|
|
countBars.Color = plotutil.Color(2)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
p.Add(countBars)
|
|
|
|
var xAxisNaming []string
|
|
|
|
for index := range letters {
|
|
|
|
xAxisNaming = append(xAxisNaming, letters[index].upperCase)
|
|
|
|
}
|
|
|
|
p.NominalX(xAxisNaming...)
|
|
|
|
if err := p.Save(25*vg.Centimeter, 15*vg.Centimeter, "barChart.png"); err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-30 09:36:08 +02:00
|
|
|
func main() {
|
2022-05-30 11:02:34 +02:00
|
|
|
initLetterStruct()
|
2022-05-30 13:25:07 +02:00
|
|
|
//totalCount := countLetters(readFile("plaintext.txt"))
|
2022-05-30 21:15:55 +02:00
|
|
|
totalCount := countLetters(readFile("../encrypt/output.txt"))
|
2022-05-30 11:02:34 +02:00
|
|
|
calculateFrequency(totalCount)
|
2022-05-30 11:42:59 +02:00
|
|
|
plotResult()
|
2022-05-30 11:02:34 +02:00
|
|
|
sort.Sort(letterList(letters))
|
|
|
|
printResult()
|
2022-05-30 10:07:48 +02:00
|
|
|
}
|