add strategy-recent

pull/13/head
Simon Let 6 years ago
parent ccaab4c4c7
commit 9d0a641381
  1. 4
      Makefile
  2. 42
      evaluate/resh-evaluate.go
  3. 72
      evaluate/statistics.go
  4. 2
      evaluate/strategy-dummy.go
  5. 31
      evaluate/strategy-recent.go
  6. 2
      sanitizer_data/copyright_information.md

@ -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 ...

@ -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
}

@ -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)
}
}

@ -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 {

@ -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
}

@ -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.
This content was used with permission from FileInfo.com.

Loading…
Cancel
Save