Debian Codegraph @

suggestions.go

raw symbols SCIP
1 package cliscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/
2
3 import (
4 "mathscip-go gomod github.com/golang/go/src go1.22 math/"
5 )
6
7 const suggestDidYouMeanTemplatescip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/suggestDidYouMeanTemplate. = "Did you mean %q?"
8
9 var (
10 SuggestFlagscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/SuggestFlag. SuggestFlagFuncscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/SuggestFlagFunc# = suggestFlagscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/suggestFlag().
11 SuggestCommandscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/SuggestCommand. SuggestCommandFuncscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/SuggestCommandFunc# = suggestCommandscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/suggestCommand().
12 SuggestDidYouMeanTemplatescip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/SuggestDidYouMeanTemplate. string = suggestDidYouMeanTemplatescip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/suggestDidYouMeanTemplate.
13 )
14
15 type SuggestFlagFuncscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/SuggestFlagFunc# func(flags []Flagscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Flag#, provided string, hideHelp bool) string
16
17 type SuggestCommandFuncscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/SuggestCommandFunc# func(commands []*Commandscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#, provided string) string
18
19 // jaroDistance is the measure of similarity between two strings. It returns a
20 // value between 0 and 1, where 1 indicates identical strings and 0 indicates
21 // completely different strings.
22 //
23 // Adapted from https://github.com/xrash/smetrics/blob/5f08fbb34913bc8ab95bb4f2a89a0637ca922666/jaro.go.
24 func jaroDistancescip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/jaroDistance().(a, b string) float64 {
25 if len(a) == 0 && len(b) == 0 {
26 return 1
27 }
28 if len(a) == 0 || len(b) == 0 {
29 return 0
30 }
31
32 lenA := float64(len(a))
33 lenB := float64(len(b))
34 hashA := make([]bool, len(a))
35 hashB := make([]bool, len(b))
36 maxDistance := int(mathscip-go gomod github.com/golang/go/src go1.22 math/.Maxscip-go gomod github.com/golang/go/src go1.22 math/Max().(0, mathscip-go gomod github.com/golang/go/src go1.22 math/.Floorscip-go gomod github.com/golang/go/src go1.22 math/Floor().(mathscip-go gomod github.com/golang/go/src go1.22 math/.Maxscip-go gomod github.com/golang/go/src go1.22 math/Max().(lenA, lenB)/2.0)-1))
37
38 var matches float64
39 for i := 0; i < len(a); i++ {
40 start := int(mathscip-go gomod github.com/golang/go/src go1.22 math/.Maxscip-go gomod github.com/golang/go/src go1.22 math/Max().(0, float64(i-maxDistance)))
41 end := int(mathscip-go gomod github.com/golang/go/src go1.22 math/.Minscip-go gomod github.com/golang/go/src go1.22 math/Min().(lenB-1, float64(i+maxDistance)))
42
43 for j := start; j <= end; j++ {
44 if hashB[j] {
45 continue
46 }
47 if a[i] == b[j] {
48 hashA[i] = true
49 hashB[j] = true
50 matches++
51 break
52 }
53 }
54 }
55 if matches == 0 {
56 return 0
57 }
58
59 var transpositions float64
60 var j int
61 for i := 0; i < len(a); i++ {
62 if !hashA[i] {
63 continue
64 }
65 for !hashB[j] {
66 j++
67 }
68 if a[i] != b[j] {
69 transpositions++
70 }
71 j++
72 }
73
74 transpositions /= 2
75 return ((matches / lenA) + (matches / lenB) + ((matches - transpositions) / matches)) / 3.0
76 }
77
78 // jaroWinkler is more accurate when strings have a common prefix up to a
79 // defined maximum length.
80 //
81 // Adapted from https://github.com/xrash/smetrics/blob/5f08fbb34913bc8ab95bb4f2a89a0637ca922666/jaro-winkler.go.
82 func jaroWinklerscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/jaroWinkler().(a, b string) float64 {
83 const (
84 boostThreshold = 0.7
85 prefixSize = 4
86 )
87 jaroDist := jaroDistancescip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/jaroDistance().(a, b)
88 if jaroDist <= boostThreshold {
89 return jaroDist
90 }
91
92 prefix := int(mathscip-go gomod github.com/golang/go/src go1.22 math/.Minscip-go gomod github.com/golang/go/src go1.22 math/Min().(float64(len(a)), mathscip-go gomod github.com/golang/go/src go1.22 math/.Minscip-go gomod github.com/golang/go/src go1.22 math/Min().(float64(prefixSize), float64(len(b)))))
93
94 var prefixMatch float64
95 for i := range prefix {
96 if a[i] == b[i] {
97 prefixMatch++
98 } else {
99 break
100 }
101 }
102 return jaroDist + 0.1*prefixMatch*(1.0-jaroDist)
103 }
104
105 func suggestFlagscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/suggestFlag().(flags []Flagscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Flag#, provided string, hideHelp bool) string {
106 distance := 0.0
107 suggestion := ""
108
109 for _, flag := range flags {
110 flagNames := flag.Namesscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Flag#Names.()
111 if !hideHelp && HelpFlagscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/HelpFlag. != nil {
112 flagNames = append(flagNames, HelpFlagscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/HelpFlag..Namesscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Flag#Names.()...)
113 }
114 for _, name := range flagNames {
115 newDistance := jaroWinklerscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/jaroWinkler().(name, provided)
116 if newDistance > distance {
117 distance = newDistance
118 suggestion = name
119 }
120 }
121 }
122
123 if len(suggestion) == 1 {
124 suggestion = "-" + suggestion
125 } else if len(suggestion) > 1 {
126 suggestion = "--" + suggestion
127 }
128
129 return suggestion
130 }
131
132 // suggestCommand takes a list of commands and a provided string to suggest a
133 // command name
134 func suggestCommandscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/suggestCommand().(commands []*Commandscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#, provided string) (suggestion string) {
135 distance := 0.0
136 for _, command := range commands {
137 for _, name := range append(command.Namesscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/Command#Names().(), helpNamescip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/helpName., helpAliasscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/helpAlias.) {
138 newDistance := jaroWinklerscip-go gomod github.com/urfave/cli/v3 . `github.com/urfave/cli/v3`/jaroWinkler().(name, provided)
139 if newDistance > distance {
140 distance = newDistance
141 suggestion = name
142 }
143 }
144 }
145
146 return suggestion
147 }