-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.go
93 lines (76 loc) · 1.95 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"errors"
"flag"
"sort"
"github.com/mitchellh/cli"
)
// EksiSozlukCLICommand type holds necessary fields for command
type EksiSozlukCLICommand struct {
UI cli.Ui
}
// Entry defines fields for a single entry
type Entry struct {
Text string
Author string
Date string
ID string
}
// Topic defines fields for a topic
type Topic struct {
Title string
Count int64
Link string
}
// Parameter holds command line parameter resolutions
type Parameter struct {
PageNumber int
Limit int
Sukela bool
Output string
OutputDir string
}
type byCount []Topic
func (a byCount) Len() int {
return len(a)
}
func (a byCount) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a byCount) Less(i, j int) bool {
return a[i].Count > a[j].Count
}
// Mean assumes the list is already sorted.
func (a byCount) Mean() (value int64, index int) {
n := len(a)
if n < 1 {
return
}
for _, t := range a {
value += t.Count
}
value = value / int64(n)
// find the topic that is closest to mean value
index = sort.Search(n, func(i int) bool { return a[i].Count <= value })
if index >= n {
index = n - 1
}
return
}
func parameterFlagHandler(args []string, eksiCLI interface{}, c EksiSozlukCLICommand) (parameter Parameter, err error) {
cmdFlags := flag.NewFlagSet("parameter", flag.ContinueOnError)
var pageNumber, limit int
var sukelaMod bool
var output string
var outputDir string
cmdFlags.IntVar(&pageNumber, "page", 1, "Sayfa numarasi")
cmdFlags.IntVar(&limit, "limit", -1, "Limit")
cmdFlags.BoolVar(&sukelaMod, "sukela", false, "Sukela mod")
cmdFlags.StringVar(&output, "output", "console", "Output mode")
cmdFlags.StringVar(&outputDir, "outputDir", ".", "Output file")
cmdFlags.Usage = func() { c.UI.Output(eksiCLI.(cli.Command).Help()) }
if err := cmdFlags.Parse(args); err != nil {
return parameter, errors.New("Error in parameter handling")
}
return Parameter{pageNumber, limit, sukelaMod, output, outputDir}, nil
}