diff --git a/pkg/strat/directory-sensitive.go b/pkg/strat/directory-sensitive.go index 87edaec..89d030e 100644 --- a/pkg/strat/directory-sensitive.go +++ b/pkg/strat/directory-sensitive.go @@ -2,23 +2,28 @@ package strat import "github.com/curusarn/resh/pkg/records" +// DirectorySensitive prediction/recommendation strategy type DirectorySensitive struct { history map[string][]string lastPwd string } +// Init see name func (s *DirectorySensitive) Init() { s.history = map[string][]string{} } +// GetTitleAndDescription see name func (s *DirectorySensitive) GetTitleAndDescription() (string, string) { return "directory sensitive (recent)", "Use recent commands executed is the same directory" } +// GetCandidates see name func (s *DirectorySensitive) GetCandidates() []string { return s.history[s.lastPwd] } +// AddHistoryRecord see name func (s *DirectorySensitive) AddHistoryRecord(record *records.EnrichedRecord) error { // work on history for PWD pwd := record.Pwd @@ -34,6 +39,7 @@ func (s *DirectorySensitive) AddHistoryRecord(record *records.EnrichedRecord) er return nil } +// ResetHistory see name func (s *DirectorySensitive) ResetHistory() error { s.Init() s.history = map[string][]string{} diff --git a/pkg/strat/dummy.go b/pkg/strat/dummy.go index b21a60d..fc813f2 100644 --- a/pkg/strat/dummy.go +++ b/pkg/strat/dummy.go @@ -2,23 +2,28 @@ package strat import "github.com/curusarn/resh/pkg/records" +// Dummy prediction/recommendation strategy type Dummy struct { history []string } +// GetTitleAndDescription see name func (s *Dummy) GetTitleAndDescription() (string, string) { return "dummy", "Return empty candidate list" } +// GetCandidates see name func (s *Dummy) GetCandidates() []string { return nil } +// AddHistoryRecord see name func (s *Dummy) AddHistoryRecord(record *records.EnrichedRecord) error { s.history = append(s.history, record.CmdLine) return nil } +// ResetHistory see name func (s *Dummy) ResetHistory() error { return nil } diff --git a/pkg/strat/dynamic-record-distance.go b/pkg/strat/dynamic-record-distance.go index 4a3be10..1f779c2 100644 --- a/pkg/strat/dynamic-record-distance.go +++ b/pkg/strat/dynamic-record-distance.go @@ -8,6 +8,7 @@ import ( "github.com/curusarn/resh/pkg/records" ) +// DynamicRecordDistance prediction/recommendation strategy type DynamicRecordDistance struct { history []records.EnrichedRecord DistParams records.DistParams @@ -23,6 +24,7 @@ type strDynDistEntry struct { distance float64 } +// Init see name func (s *DynamicRecordDistance) Init() { s.history = nil s.pwdHistogram = map[string]int{} @@ -30,6 +32,7 @@ func (s *DynamicRecordDistance) Init() { s.gitOriginHistogram = map[string]int{} } +// GetTitleAndDescription see name func (s *DynamicRecordDistance) GetTitleAndDescription() (string, string) { return "dynamic record distance (depth:" + strconv.Itoa(s.MaxDepth) + ";" + s.Label + ")", "Use TF-IDF record distance to recommend commands" } @@ -38,6 +41,7 @@ func (s *DynamicRecordDistance) idf(count int) float64 { return math.Log(float64(len(s.history)) / float64(count)) } +// GetCandidates see name func (s *DynamicRecordDistance) GetCandidates(strippedRecord records.EnrichedRecord) []string { if len(s.history) == 0 { return nil @@ -70,6 +74,7 @@ func (s *DynamicRecordDistance) GetCandidates(strippedRecord records.EnrichedRec return hist } +// AddHistoryRecord see name func (s *DynamicRecordDistance) AddHistoryRecord(record *records.EnrichedRecord) error { // append record to front s.history = append([]records.EnrichedRecord{*record}, s.history...) @@ -79,6 +84,7 @@ func (s *DynamicRecordDistance) AddHistoryRecord(record *records.EnrichedRecord) return nil } +// ResetHistory see name func (s *DynamicRecordDistance) ResetHistory() error { s.Init() return nil diff --git a/pkg/strat/frequent.go b/pkg/strat/frequent.go index 6525b9f..ff3b912 100644 --- a/pkg/strat/frequent.go +++ b/pkg/strat/frequent.go @@ -6,6 +6,7 @@ import ( "github.com/curusarn/resh/pkg/records" ) +// Frequent prediction/recommendation strategy type Frequent struct { history map[string]int } @@ -15,14 +16,17 @@ type strFrqEntry struct { count int } -func (s *Frequent) init() { +// Init see name +func (s *Frequent) Init() { s.history = map[string]int{} } +// GetTitleAndDescription see name func (s *Frequent) GetTitleAndDescription() (string, string) { return "frequent", "Use frequent commands" } +// GetCandidates see name func (s *Frequent) GetCandidates() []string { var mapItems []strFrqEntry for cmdLine, count := range s.history { @@ -36,12 +40,14 @@ func (s *Frequent) GetCandidates() []string { return hist } +// AddHistoryRecord see name func (s *Frequent) AddHistoryRecord(record *records.EnrichedRecord) error { s.history[record.CmdLine]++ return nil } +// ResetHistory see name func (s *Frequent) ResetHistory() error { - s.init() + s.Init() return nil } diff --git a/pkg/strat/markov-chain-cmd.go b/pkg/strat/markov-chain-cmd.go index 1bd1ded..b1fa2f5 100644 --- a/pkg/strat/markov-chain-cmd.go +++ b/pkg/strat/markov-chain-cmd.go @@ -8,6 +8,7 @@ import ( "github.com/mb-14/gomarkov" ) +// MarkovChainCmd prediction/recommendation strategy type MarkovChainCmd struct { Order int history []strMarkCmdHistoryEntry @@ -24,15 +25,18 @@ type strMarkCmdEntry struct { transProb float64 } +// Init see name func (s *MarkovChainCmd) Init() { s.history = nil s.historyCmds = nil } +// GetTitleAndDescription see name func (s *MarkovChainCmd) GetTitleAndDescription() (string, string) { return "command-based markov chain (order " + strconv.Itoa(s.Order) + ")", "Use command-based markov chain to recommend commands" } +// GetCandidates see name func (s *MarkovChainCmd) GetCandidates() []string { if len(s.history) < s.Order { var hist []string @@ -78,6 +82,7 @@ func (s *MarkovChainCmd) GetCandidates() []string { return hist } +// AddHistoryRecord see name func (s *MarkovChainCmd) AddHistoryRecord(record *records.EnrichedRecord) error { s.history = append(s.history, strMarkCmdHistoryEntry{cmdLine: record.CmdLine, cmd: record.Command}) s.historyCmds = append(s.historyCmds, record.Command) @@ -85,6 +90,7 @@ func (s *MarkovChainCmd) AddHistoryRecord(record *records.EnrichedRecord) error return nil } +// ResetHistory see name func (s *MarkovChainCmd) ResetHistory() error { s.Init() return nil diff --git a/pkg/strat/markov-chain.go b/pkg/strat/markov-chain.go index 07006e0..50c7fdc 100644 --- a/pkg/strat/markov-chain.go +++ b/pkg/strat/markov-chain.go @@ -8,6 +8,7 @@ import ( "github.com/mb-14/gomarkov" ) +// MarkovChain prediction/recommendation strategy type MarkovChain struct { Order int history []string @@ -18,14 +19,17 @@ type strMarkEntry struct { transProb float64 } +// Init see name func (s *MarkovChain) Init() { s.history = nil } +// GetTitleAndDescription see name func (s *MarkovChain) GetTitleAndDescription() (string, string) { return "markov chain (order " + strconv.Itoa(s.Order) + ")", "Use markov chain to recommend commands" } +// GetCandidates see name func (s *MarkovChain) GetCandidates() []string { if len(s.history) < s.Order { return s.history @@ -58,12 +62,14 @@ func (s *MarkovChain) GetCandidates() []string { return hist } +// AddHistoryRecord see name func (s *MarkovChain) AddHistoryRecord(record *records.EnrichedRecord) error { s.history = append(s.history, record.CmdLine) // s.historySet[record.CmdLine] = true return nil } +// ResetHistory see name func (s *MarkovChain) ResetHistory() error { s.Init() return nil diff --git a/pkg/strat/random.go b/pkg/strat/random.go index 27e959c..0ff52f1 100644 --- a/pkg/strat/random.go +++ b/pkg/strat/random.go @@ -7,21 +7,25 @@ import ( "github.com/curusarn/resh/pkg/records" ) +// Random prediction/recommendation strategy type Random struct { CandidatesSize int history []string historySet map[string]bool } +// Init see name func (s *Random) Init() { s.history = nil s.historySet = map[string]bool{} } +// GetTitleAndDescription see name func (s *Random) GetTitleAndDescription() (string, string) { return "random", "Use random commands" } +// GetCandidates see name func (s *Random) GetCandidates() []string { seed := time.Now().UnixNano() rand.Seed(seed) @@ -39,12 +43,14 @@ func (s *Random) GetCandidates() []string { return candidates } +// AddHistoryRecord see name func (s *Random) AddHistoryRecord(record *records.EnrichedRecord) error { s.history = append([]string{record.CmdLine}, s.history...) s.historySet[record.CmdLine] = true return nil } +// ResetHistory see name func (s *Random) ResetHistory() error { s.Init() return nil diff --git a/pkg/strat/recent-bash.go b/pkg/strat/recent-bash.go index c5319ce..ace3571 100644 --- a/pkg/strat/recent-bash.go +++ b/pkg/strat/recent-bash.go @@ -2,21 +2,25 @@ package strat import "github.com/curusarn/resh/pkg/records" +// RecentBash prediction/recommendation strategy type RecentBash struct { histfile []string histfileSnapshot map[string][]string history map[string][]string } +// Init see name func (s *RecentBash) Init() { s.histfileSnapshot = map[string][]string{} s.history = map[string][]string{} } +// GetTitleAndDescription see name func (s *RecentBash) GetTitleAndDescription() (string, string) { return "recent (bash-like)", "Behave like bash" } +// GetCandidates see name func (s *RecentBash) GetCandidates(strippedRecord records.EnrichedRecord) []string { // populate the local history from histfile if s.histfileSnapshot[strippedRecord.SessionID] == nil { @@ -25,6 +29,7 @@ func (s *RecentBash) GetCandidates(strippedRecord records.EnrichedRecord) []stri return append(s.history[strippedRecord.SessionID], s.histfileSnapshot[strippedRecord.SessionID]...) } +// AddHistoryRecord see name func (s *RecentBash) AddHistoryRecord(record *records.EnrichedRecord) error { // remove previous occurance of record for i, cmd := range s.history[record.SessionID] { @@ -44,6 +49,7 @@ func (s *RecentBash) AddHistoryRecord(record *records.EnrichedRecord) error { return nil } +// ResetHistory see name func (s *RecentBash) ResetHistory() error { s.Init() return nil diff --git a/pkg/strat/recent.go b/pkg/strat/recent.go index 68c8eff..157b52c 100644 --- a/pkg/strat/recent.go +++ b/pkg/strat/recent.go @@ -2,18 +2,22 @@ package strat import "github.com/curusarn/resh/pkg/records" +// Recent prediction/recommendation strategy type Recent struct { history []string } +// GetTitleAndDescription see name func (s *Recent) GetTitleAndDescription() (string, string) { return "recent", "Use recent commands" } +// GetCandidates see name func (s *Recent) GetCandidates() []string { return s.history } +// AddHistoryRecord see name func (s *Recent) AddHistoryRecord(record *records.EnrichedRecord) error { // remove previous occurance of record for i, cmd := range s.history { @@ -26,6 +30,7 @@ func (s *Recent) AddHistoryRecord(record *records.EnrichedRecord) error { return nil } +// ResetHistory see name func (s *Recent) ResetHistory() error { s.history = nil return nil diff --git a/pkg/strat/record-distance.go b/pkg/strat/record-distance.go index 816922e..e582584 100644 --- a/pkg/strat/record-distance.go +++ b/pkg/strat/record-distance.go @@ -7,6 +7,7 @@ import ( "github.com/curusarn/resh/pkg/records" ) +// RecordDistance prediction/recommendation strategy type RecordDistance struct { history []records.EnrichedRecord DistParams records.DistParams @@ -19,14 +20,17 @@ type strDistEntry struct { distance float64 } +// Init see name func (s *RecordDistance) Init() { s.history = nil } +// GetTitleAndDescription see name func (s *RecordDistance) GetTitleAndDescription() (string, string) { return "record distance (depth:" + strconv.Itoa(s.MaxDepth) + ";" + s.Label + ")", "Use record distance to recommend commands" } +// GetCandidates see name func (s *RecordDistance) GetCandidates(strippedRecord records.EnrichedRecord) []string { if len(s.history) == 0 { return nil @@ -52,12 +56,14 @@ func (s *RecordDistance) GetCandidates(strippedRecord records.EnrichedRecord) [] return hist } +// AddHistoryRecord see name func (s *RecordDistance) AddHistoryRecord(record *records.EnrichedRecord) error { // append record to front s.history = append([]records.EnrichedRecord{*record}, s.history...) return nil } +// ResetHistory see name func (s *RecordDistance) ResetHistory() error { s.Init() return nil diff --git a/pkg/strat/strat.go b/pkg/strat/strat.go index d5b1b2c..28ac015 100644 --- a/pkg/strat/strat.go +++ b/pkg/strat/strat.go @@ -4,6 +4,7 @@ import ( "github.com/curusarn/resh/pkg/records" ) +// ISimpleStrategy interface type ISimpleStrategy interface { GetTitleAndDescription() (string, string) GetCandidates() []string @@ -11,6 +12,7 @@ type ISimpleStrategy interface { ResetHistory() error } +// IStrategy interface type IStrategy interface { GetTitleAndDescription() (string, string) GetCandidates(r records.EnrichedRecord) []string