This commit is contained in:
Florian Hoss 2022-11-14 18:29:26 +01:00
parent 256aef2457
commit d52df940fd
2 changed files with 24 additions and 13 deletions

View file

@ -1,14 +1,15 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"github.com/dariubs/percent" "github.com/dariubs/percent"
"gonum.org/v1/plot" "gonum.org/v1/plot"
"gonum.org/v1/plot/plotter" "gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/plotutil" "gonum.org/v1/plot/plotutil"
"gonum.org/v1/plot/vg" "gonum.org/v1/plot/vg"
"io/ioutil"
"log" "log"
"os"
"sort" "sort"
"strings" "strings"
) )
@ -26,27 +27,39 @@ func (l letterList) Len() int {
return len(l) return len(l)
} }
func (l letterList) Less(i, j int) bool { func (l letterList) Less(firstIndex, secondIndex int) bool {
return l[i].frequency > l[j].frequency return l[firstIndex].frequency > l[secondIndex].frequency
} }
func (l letterList) Swap(i, j int) { func (l letterList) Swap(firstIndex, secondIndex int) {
l[i], l[j] = l[j], l[i] l[firstIndex], l[secondIndex] = l[secondIndex], l[firstIndex]
} }
const lettersInTheAlphabet = 'Z' - 'A' + 1 const lettersInTheAlphabet = 'Z' - 'A' + 1
var letters = make([]letter, lettersInTheAlphabet) var letters = make([]letter, lettersInTheAlphabet)
func initLetterStruct() { func init() {
for i := 0; i < lettersInTheAlphabet; i++ { for i := 0; i < lettersInTheAlphabet; i++ {
letters[i].upperCase = string(rune('A' + i)) letters[i].upperCase = string(rune('A' + i))
letters[i].lowerCase = string(rune('A' + i + 32)) letters[i].lowerCase = string(rune('A' + i + 32))
} }
} }
func parseArguments() string {
var inputFile string
flag.StringVar(&inputFile, "f", "", "Specify file to analyse.")
flag.Parse()
if inputFile == "" {
log.Print("No flag provided\n\nHelp:\n")
flag.PrintDefaults()
os.Exit(1)
}
return inputFile
}
func readFile(relativePath string) string { func readFile(relativePath string) string {
content, err := ioutil.ReadFile(relativePath) content, err := os.ReadFile(relativePath)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
@ -100,15 +113,14 @@ func plotResult() {
xAxisNaming = append(xAxisNaming, letters[index].upperCase) xAxisNaming = append(xAxisNaming, letters[index].upperCase)
} }
p.NominalX(xAxisNaming...) p.NominalX(xAxisNaming...)
if err := p.Save(25*vg.Centimeter, 15*vg.Centimeter, "barChart.png"); err != nil { if err = p.Save(25*vg.Centimeter, 15*vg.Centimeter, "barChart.png"); err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
} }
func main() { func main() {
initLetterStruct() inputFile := parseArguments()
//totalCount := countLetters(readFile("plaintext.txt")) totalCount := countLetters(readFile(inputFile))
totalCount := countLetters(readFile("../encrypt/output.txt"))
calculateFrequency(totalCount) calculateFrequency(totalCount)
plotResult() plotResult()
sort.Sort(letterList(letters)) sort.Sort(letterList(letters))

View file

@ -2,7 +2,6 @@ package main
import ( import (
"flag" "flag"
"io/ioutil"
"log" "log"
"os" "os"
"regexp" "regexp"
@ -27,7 +26,7 @@ func parseArguments() (string, string) {
} }
func readFile(relativePath string) string { func readFile(relativePath string) string {
content, err := ioutil.ReadFile(relativePath) content, err := os.ReadFile(relativePath)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }