-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabbrev.go
More file actions
57 lines (52 loc) · 1.27 KB
/
abbrev.go
File metadata and controls
57 lines (52 loc) · 1.27 KB
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
// abbrev.go - generate abbreviations from a wordlist
//
// (c) 2014 Sudhi Herle <sw-at-herle.net>
//
// Placed in the Public Domain
// This software does not come with any express or implied
// warranty; it is provided "as is". No claim is made to its
// suitability for any purpose.
package utils
// Given a wordlist in 'words', generate unique abbreviations of it
// and return as a map[abbrev]word.
// e.g.,
//
// given a wordlist ["hello", "help", "sync"],
// Abbrev() returns:
// {
// "hello": "hello",
// "hell": "hell"
// "help": "help",
// "sync": "sync",
// "syn": "sync",
// "sy": "sync",
// "s": "sync"
// }
func Abbrev(words []string) map[string]string {
seen := make(map[string]int)
table := make(map[string]string)
for _, w := range words {
for n := len(w) - 1; n > 0; n -= 1 {
ab := w[:n]
seen[ab] += 1
switch seen[ab] {
case 1:
table[ab] = w
case 2:
delete(table, ab)
default:
goto next
}
}
next:
}
// non abbreviations always get entered
// This has to be done _after_ the loop above; because
// if there are words that are prefixes of other words in
// the argument list, we need to ensure we capture them
// intact.
for _, w := range words {
table[w] = w
}
return table
}