-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding acronyms in a separate package
- Loading branch information
1 parent
e6d128e
commit bd71682
Showing
3 changed files
with
104 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters