├── utils.go ├── .gitignore ├── acronymcreator_test.go ├── LICENSE └── acronymcreator.go /utils.go: -------------------------------------------------------------------------------- 1 | package actor 2 | 3 | func stripCtlFromBytes(str string) string { 4 | b := make([]byte, len(str)) 5 | var bl int 6 | for i := 0; i < len(str); i++ { 7 | c := str[i] 8 | if c >= 32 && c != 127 { 9 | b[bl] = c 10 | bl++ 11 | } 12 | } 13 | return string(b[:bl]) 14 | } 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | -------------------------------------------------------------------------------- /acronymcreator_test.go: -------------------------------------------------------------------------------- 1 | package actor 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | 7 | . "github.com/smartystreets/goconvey/convey" 8 | ) 9 | 10 | func TestCreator_CreateAcronyms(t *testing.T) { 11 | Convey("Testing Creator.CreateAcronyms()", t, func() { 12 | columns := [][]string{ 13 | {"acronym"}, 14 | {"creator"}, 15 | } 16 | creator := New(columns) 17 | 18 | acronyms, err := creator.CreateAcronyms() 19 | So(err, ShouldBeNil) 20 | So(len(acronyms) > 0, ShouldBeTrue) 21 | So(len(acronyms["ac"]) > 0, ShouldBeTrue) 22 | So(len(creator.getCombinations()), ShouldEqual, 1) 23 | So(acronyms["ac"][0].Combination, ShouldResemble, "acronym creator") 24 | for key, acronyms := range acronyms { 25 | fmt.Println(key) 26 | for _, acronym := range acronyms { 27 | fmt.Println(key, acronym) 28 | } 29 | } 30 | }) 31 | } 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Manfred Touron 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /acronymcreator.go: -------------------------------------------------------------------------------- 1 | package actor 2 | 3 | // ACTOR - Acronym CreaTOR :) 4 | 5 | import ( 6 | "bufio" 7 | "fmt" 8 | "os" 9 | "regexp" 10 | "strings" 11 | ) 12 | 13 | type Creator struct { 14 | columns [][]string 15 | } 16 | 17 | type Acronym struct { 18 | Acronym string 19 | Combination string 20 | Highlighted string 21 | Score float64 22 | } 23 | 24 | func New(columns [][]string) Creator { 25 | return Creator{ 26 | columns: columns, 27 | } 28 | } 29 | 30 | func newAcronym(acronym, combination string) Acronym { 31 | return Acronym{ 32 | Acronym: acronym, 33 | Combination: combination, 34 | } 35 | } 36 | 37 | func (c *Creator) getCombinations() [][]string { 38 | // FIXME: create real list of combinations 39 | combination := []string{} 40 | for _, column := range c.columns { 41 | combination = append(combination, column[0]) 42 | } 43 | return [][]string{combination} 44 | } 45 | 46 | func (c *Creator) getDictionary() ([]string, error) { 47 | dictionary, err := os.Open("/usr/share/dict/words") 48 | if err != nil { 49 | return nil, err 50 | } 51 | defer dictionary.Close() 52 | var lines []string 53 | scanner := bufio.NewScanner(dictionary) 54 | for scanner.Scan() { 55 | lines = append(lines, stripCtlFromBytes(scanner.Text())) 56 | } 57 | return lines, nil 58 | } 59 | 60 | func getMatchingScore(a, b string) (int, string) { 61 | score := 0 62 | c := string(a) 63 | minJ := 0 64 | for i := 0; i < len(c); i++ { 65 | found := false 66 | for j := minJ; j < len(b); j++ { 67 | if c[i] == b[j] { 68 | c = c[:i] + strings.ToUpper(fmt.Sprintf("%c", c[i])) + c[i+1:] 69 | found = true 70 | minJ = j 71 | break 72 | } 73 | } 74 | if !found { 75 | score-- 76 | } 77 | } 78 | return score, c 79 | } 80 | 81 | func (c *Creator) CreateAcronyms() (map[string][]Acronym, error) { 82 | acronyms := make(map[string][]Acronym, 0) 83 | 84 | for _, combination := range c.getCombinations() { 85 | acronym := "" 86 | query := "" 87 | for _, word := range combination { 88 | acronym += string(word[0]) 89 | query += string(word[0]) + ".*" 90 | } 91 | if _, found := acronyms[acronym]; !found { 92 | acronyms[acronym] = make([]Acronym, 0) 93 | } 94 | acronyms[acronym] = append(acronyms[acronym], newAcronym(acronym, strings.Join(combination, " "))) 95 | 96 | dict, err := c.getDictionary() 97 | if err != nil { 98 | return nil, err 99 | } 100 | 101 | joinedCombination := strings.Join(combination, " ") 102 | re := regexp.MustCompile("^" + query + "$") 103 | for _, line := range dict { 104 | if !re.MatchString(line) { 105 | continue 106 | } 107 | score, highlighted := getMatchingScore(line, joinedCombination) 108 | if score > -3 { 109 | acr := newAcronym(acronym, strings.Join(combination, " ")) 110 | acr.Highlighted = highlighted 111 | acr.Score = float64(score) 112 | acronyms[acronym] = append(acronyms[acronym], acr) 113 | } 114 | 115 | // fmt.Printf("%s\r\t\t\t%s\t\t%s\r\t\t\t\t\t\t\t\t\t%d\n", line, joinedCombination, highlighted, score) 116 | // FIXME: iterate over letters, check the index in the combination, put in uppercase, and count the amount of lowercase, if the percent is enough, add the word ! 117 | } 118 | } 119 | 120 | return acronyms, nil 121 | } 122 | --------------------------------------------------------------------------------