Skip to content

Commit

Permalink
adding acronyms in a separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
paganotoni committed Nov 3, 2018
1 parent e6d128e commit bd71682
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 82 deletions.
81 changes: 81 additions & 0 deletions custom_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package flect

import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
)

func init() {
loadCustomData("inflections.json", "INFLECT_PATH", "could not read inflection file", LoadInflections)
loadCustomData("acronyms.json", "ACRONYMS_PATH", "could not read acronyms file", LoadAcronyms)
}

//CustomDataParser are functions that parse data like acronyms or
//plurals in the shape of a io.Reader it receives.
type CustomDataParser func(io.Reader) error

func loadCustomData(defaultFile, env, readErrorMessage string, parser CustomDataParser) {
pwd, _ := os.Getwd()
path := filepath.Join(pwd, defaultFile)

if p := os.Getenv(env); p != "" {
path = p
}

if _, err := os.Stat(path); err != nil {
return
}

b, err := ioutil.ReadFile(path)
if err != nil {
fmt.Printf("%s %s (%s)\n", readErrorMessage, path, err)
return
}

if err = LoadInflections(bytes.NewReader(b)); err != nil {
fmt.Println(err)
}
}

//LoadAcronyms loads rules from io.Reader param
func LoadAcronyms(r io.Reader) error {
m := []string{}
err := json.NewDecoder(r).Decode(&m)

if err != nil {
return fmt.Errorf("could not decode acronyms JSON from reader: %s", err)
}

for _, acronym := range m {
baseAcronyms[acronym] = true
}

return nil
}

//LoadInflections loads rules from io.Reader param
func LoadInflections(r io.Reader) error {
m := map[string]string{}

err := json.NewDecoder(r).Decode(&m)
if err != nil {
return fmt.Errorf("could not decode inflection JSON from reader: %s", err)
}

pluralMoot.Lock()
defer pluralMoot.Unlock()
singularMoot.Lock()
defer singularMoot.Unlock()

for s, p := range m {
singleToPlural[s] = p
pluralToSingle[p] = s
}

return nil
}
67 changes: 0 additions & 67 deletions flect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,77 +4,10 @@ Package flect is a new inflection engine to replace [https://github.com/markbate
package flect

import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"unicode"
)

const (
//acronymsKey is the key we use to get acronyms from the inflections file
//p.e:
// #inflections.json
// {
// "house":"houses",
// "_acronyms": ["TSA", "LSA"]
// }
acronymsKey = "_acronyms"
)

func init() {
pwd, _ := os.Getwd()
cfg := filepath.Join(pwd, "inflections.json")
if p := os.Getenv("INFLECT_PATH"); p != "" {
cfg = p
}
if _, err := os.Stat(cfg); err == nil {
b, err := ioutil.ReadFile(cfg)
if err != nil {
fmt.Printf("could not read inflection file %s (%s)\n", cfg, err)
return
}
if err = LoadReader(bytes.NewReader(b)); err != nil {
fmt.Println(err)
}
}
}

//LoadReader loads rules from io.Reader param
func LoadReader(r io.Reader) error {
m := map[string]interface{}{}

err := json.NewDecoder(r).Decode(&m)
if err != nil {
return fmt.Errorf("could not decode inflection JSON from reader: %s", err)
}
pluralMoot.Lock()
defer pluralMoot.Unlock()
singularMoot.Lock()
defer singularMoot.Unlock()

for s, p := range m {
if ps, ok := p.(string); ok {
singleToPlural[s] = ps
pluralToSingle[ps] = s
}

if pa, ok := p.([]interface{}); ok && s == acronymsKey {
for _, acronym := range pa {
key := (acronym).(string)
baseAcronyms[key] = true
}
}

}

return nil
}

var spaces = []rune{'_', ' ', ':', '-', '/'}

func isSpace(c rune) bool {
Expand Down
38 changes: 23 additions & 15 deletions flect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,42 @@ type tt struct {
exp string
}

func Test_LoadReader(t *testing.T) {
func Test_LoadInflections(t *testing.T) {
r := require.New(t)
// in := `{"beatle": "the beatles", "xyz": "zyx"}`
m := map[string]interface{}{
"beatle": "the beatles",
"xyz": "zyx",
"_acronyms": []string{"TLC", "LSA"},
m := map[string]string{
"beatle": "the beatles",
"xyz": "zyx",
}

b, err := json.Marshal(m)
r.NoError(err)

r.NoError(LoadReader(bytes.NewReader(b)))

tcases := map[string]string{
"beatle": "the beatles",
"xyz": "zyx",
}
r.NoError(LoadInflections(bytes.NewReader(b)))

for k, v := range tcases {
for k, v := range m {
r.Equal(v, Pluralize(k))
r.Equal(v, Pluralize(v))
r.Equal(k, Singularize(k))
r.Equal(k, Singularize(v))
}
}

r.True(baseAcronyms["TLC"])
r.True(baseAcronyms["LSA"])
func Test_LoadAcronyms(t *testing.T) {
r := require.New(t)
m := []string{
"ACC",
"TLC",
"LSA",
}

b, err := json.Marshal(m)
r.NoError(err)

r.NoError(LoadAcronyms(bytes.NewReader(b)))

for _, acronym := range m {
r.True(baseAcronyms[acronym])
}
}

var singlePluralAssertions = []tt{
Expand Down

0 comments on commit bd71682

Please sign in to comment.