From 9d0a641381b79826e7137dafc1a63d81f5a6e4db Mon Sep 17 00:00:00 2001 From: Simon Let Date: Mon, 9 Sep 2019 00:00:50 +0200 Subject: [PATCH] add strategy-recent --- Makefile | 4 +- evaluate/resh-evaluate.go | 42 ++++++++++----- evaluate/statistics.go | 72 +++++++++++++++++++++++++ evaluate/strategy-dummy.go | 2 +- evaluate/strategy-recent.go | 31 +++++++++++ sanitizer_data/copyright_information.md | 2 +- 6 files changed, 137 insertions(+), 16 deletions(-) create mode 100644 evaluate/statistics.go create mode 100644 evaluate/strategy-recent.go diff --git a/Makefile b/Makefile index d5b4bb0..d7abc39 100644 --- a/Makefile +++ b/Makefile @@ -115,8 +115,8 @@ resh-collect: collect/resh-collect.go common/resh-common.go version resh-sanitize-history: sanitize-history/resh-sanitize-history.go common/resh-common.go version go build ${GOFLAGS} -o $@ $< -resh-evaluate: evaluate/resh-evaluate.go evaluate/strategy-*.go common/resh-common.go version - go build ${GOFLAGS} -o $@ $< evaluate/strategy-*.go +resh-evaluate: evaluate/resh-evaluate.go evaluate/statistics.go evaluate/strategy-*.go common/resh-common.go version + go build ${GOFLAGS} -o $@ $< evaluate/statistics.go evaluate/strategy-*.go $(HOME)/.resh $(HOME)/.resh/bin $(HOME)/.config: # Creating dirs ... diff --git a/evaluate/resh-evaluate.go b/evaluate/resh-evaluate.go index e6d667b..78532ef 100644 --- a/evaluate/resh-evaluate.go +++ b/evaluate/resh-evaluate.go @@ -67,7 +67,7 @@ func main() { } defer writer.Flush() - evaluator := evaluator{sanitizedInput: *sanitizedInput, writer: writer} + evaluator := evaluator{sanitizedInput: *sanitizedInput, writer: writer, maxCandidates: 42} err := evaluator.init(*inputPath) if err != nil { log.Fatal("Evaluator init() error:", err) @@ -75,9 +75,11 @@ func main() { var strategies []strategy - dummy := strategyDummy{} + // dummy := strategyDummy{} + // strategies = append(strategies, &dummy) - strategies = append(strategies, &dummy) + recent := strategyRecent{} + strategies = append(strategies, &recent) for _, strat := range strategies { err = evaluator.evaluate(strat) @@ -97,6 +99,7 @@ type strategy interface { type evaluator struct { sanitizedInput bool writer *bufio.Writer + maxCandidates int historyRecords []common.Record } @@ -106,28 +109,43 @@ func (e *evaluator) init(inputPath string) error { } func (e *evaluator) evaluate(strat strategy) error { - // init dist buckets ? - // map dist int -> matches int - // map dist int -> charactersRecalled int + stats := statistics{writer: e.writer, size: e.maxCandidates + 1} + stats.init() + for _, record := range e.historyRecords { - _ = strat.GetCandidates() - // evaluate distance and characters recalled + candidates := strat.GetCandidates() + + match := false + for i, candidate := range candidates { + // make an option (--calculate-total) to turn this on/off ? + // if i >= e.maxCandidates { + // break + // } + if candidate == record.CmdLine { + stats.addMatch(i+1, record.CmdLength) + match = true + break + } + } + if match == false { + stats.addMiss() + } err := strat.AddHistoryRecord(&record) if err != nil { log.Println("Error while evauating", err) return err } } - // print results - outLine := "testing testing 123 testing ..." - n, err := e.writer.WriteString(string(outLine) + "\n") + title, description := strat.GetTitleAndDescription() + n, err := e.writer.WriteString(title + " - " + description + "\n") if err != nil { log.Fatal(err) } if n == 0 { log.Fatal("Nothing was written", n) } - e.writer.Flush() + // print results + stats.printCumulative() return nil } diff --git a/evaluate/statistics.go b/evaluate/statistics.go new file mode 100644 index 0000000..98bd2dd --- /dev/null +++ b/evaluate/statistics.go @@ -0,0 +1,72 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "math" + "strconv" +) + +type statistics struct { + writer *bufio.Writer + size int + matches []int + matchesTotal int + charactersRecalled []int + charactersRecalledTotal int + dataPointCount int +} + +func (s *statistics) init() { + s.matches = make([]int, s.size) + s.charactersRecalled = make([]int, s.size) +} + +func (s *statistics) addMatch(distance int, cmdLength int) { + if distance >= s.size { + // --calculate-total + // log.Fatal("Match distance is greater than size of statistics") + s.matchesTotal++ + s.charactersRecalledTotal += cmdLength + return + } + s.matches[distance]++ + s.matchesTotal++ + s.charactersRecalled[distance] += cmdLength + s.charactersRecalledTotal += cmdLength + s.dataPointCount++ +} + +func (s *statistics) addMiss() { + s.dataPointCount++ +} + +func (s *statistics) printCumulative() { + matchesPercent := 0.0 + out := "### Matches ###\n" + for i := 0; i < s.size; i++ { + matchesPercent += 100 * float64(s.matches[i]) / float64(s.dataPointCount) + out += strconv.Itoa(i) + " ->" + out += fmt.Sprintf(" (%.1f %%)\n", matchesPercent) + for j := 0; j < int(math.Round(matchesPercent)); j++ { + out += "#" + } + out += "\n" + } + matchesPercent = 100 * float64(s.matchesTotal) / float64(s.dataPointCount) + out += "TOTAL ->" + out += fmt.Sprintf(" (%.1f %%)\n", matchesPercent) + for j := 0; j < int(math.Round(matchesPercent)); j++ { + out += "#" + } + out += "\n" + + n, err := s.writer.WriteString(string(out) + "\n\n") + if err != nil { + log.Fatal(err) + } + if n == 0 { + log.Fatal("Nothing was written", n) + } +} diff --git a/evaluate/strategy-dummy.go b/evaluate/strategy-dummy.go index c8e1dc8..28ed8ec 100644 --- a/evaluate/strategy-dummy.go +++ b/evaluate/strategy-dummy.go @@ -7,7 +7,7 @@ type strategyDummy struct { } func (s *strategyDummy) GetTitleAndDescription() (string, string) { - return "recent", "Use recent commands" + return "dummy", "Return empty candidate list" } func (s *strategyDummy) GetCandidates() []string { diff --git a/evaluate/strategy-recent.go b/evaluate/strategy-recent.go new file mode 100644 index 0000000..b75adc2 --- /dev/null +++ b/evaluate/strategy-recent.go @@ -0,0 +1,31 @@ +package main + +import "github.com/curusarn/resh/common" + +type strategyRecent struct { + history []string +} + +func (s *strategyRecent) GetTitleAndDescription() (string, string) { + return "recent", "Use recent commands" +} + +func (s *strategyRecent) GetCandidates() []string { + return s.history +} + +func (s *strategyRecent) AddHistoryRecord(record *common.Record) error { + // remove previous occurance of record + for i, cmd := range s.history { + if cmd == record.CmdLine { + s.history = append(s.history[:i], s.history[i+1:]...) + } + } + // append new record + s.history = append([]string{record.CmdLine}, s.history...) + return nil +} + +func (s *strategyRecent) ResetHistory() error { + return nil +} diff --git a/sanitizer_data/copyright_information.md b/sanitizer_data/copyright_information.md index a1b1308..abdbf33 100644 --- a/sanitizer_data/copyright_information.md +++ b/sanitizer_data/copyright_information.md @@ -4,4 +4,4 @@ Whitelist contains content from variety of sources. Part of the whitelist (`./whitelist.txt`) is made of copyrighted content from [FileInfo.com](https://fileinfo.com/filetypes/common). -This content was used with permission from FileInfo.com. \ No newline at end of file +This content was used with permission from FileInfo.com.