Code#
package main
import (
"fmt"
"github.com/dariubs/percent"
"io/ioutil"
"log"
"sort"
"strings"
)
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]
}
const lettersInTheAlphabet = 'Z' - 'A' + 1
var letters = make([]letter, lettersInTheAlphabet)
func initLetterStruct() {
for i := 0; i < lettersInTheAlphabet; i++ {
letters[i].upperCase = string(rune('A' + i))
letters[i].lowerCase = string(rune('A' + i + 32))
}
}
func readFile(relativePath string) string {
content, err := ioutil.ReadFile(relativePath)
if err != nil {
log.Fatalln(err)
}
return string(content)
}
func countLetters(inputText string) int {
totalCount := 0
for i := 0; i < lettersInTheAlphabet; i++ {
letters[i].count +=
strings.Count(inputText, letters[i].lowerCase) +
strings.Count(inputText, letters[i].upperCase)
totalCount += letters[i].count
}
return totalCount
}
func calculateFrequency(totalCount int) {
for i := 0; i < lettersInTheAlphabet; i++ {
letters[i].frequency = percent.PercentOf(letters[i].count, totalCount)
}
}
func printResult() {
for i := 0; i < lettersInTheAlphabet; i++ {
l := letters[i]
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,
)
}
}
func main() {
initLetterStruct()
//totalCount := countLetters(readFile("plaintext.txt"))
totalCount := countLetters(readFile("../encrypt/output.txt"))
calculateFrequency(totalCount)
sort.Sort(letterList(letters))
printResult()
}
Makefile#
# Go parameters
GoCMD=go
GoBUILD=$(GoCMD) build
GoCLEAN=$(GoCMD) clean
GoTEST=$(GoCMD) test
GoMOD=$(GoCMD) mod
BINARY_NAME=tool
# To install GoLang please follow the installation instructions on https://go.dev/
all: build
build:
$(GoBUILD) -o $(BINARY_NAME) -v
clean:
$(GOCLEAN)
rm -f $(BINARY_NAME)
deps:
$(GoMOD) tidy