-
Notifications
You must be signed in to change notification settings - Fork 0
/
variable.go
160 lines (140 loc) · 4.68 KB
/
variable.go
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package search
import (
"bufio"
"fmt"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"github.com/pkg/errors"
"gopkg.in/olivere/elastic.v6"
"github.com/Bnei-Baruch/archive-backend/consts"
)
type VariableValue struct {
Name string // Variable name.
Value string // Variable value.
Tokenized []string // Tokenized phrase.
Origin string // Original phrase.
OriginFull string // Original phrase with prefix and suffix.
}
// Map from Original Full Phrase => $Var => values
type VariablesByPhrase = map[string]map[string][]string
type Variable interface {
Name() string
Match(token *TokenNode, variableToken *TokenNode, matchPrefixes bool, variables map[string]*Variable) (bool, []VariableValue, []*TokenNode, error)
VariableToPhrases(prefix, suffix string, variables map[string]*Variable) []*PhrasesWithOrigin
}
// Map from language => name => variable.
type VariablesByName = map[string]*Variable
type VariablesByLang = map[string]VariablesByName
// Map from variable => language => value => Tokens
type Translations = map[string]map[string]map[string][][]*TokenNode
func MakeVariables(variablesDir string, esc *elastic.Client, tc *TokensCache) (VariablesByLang, error) {
translations, err := LoadVariablesTranslations(variablesDir, esc, tc)
if err != nil {
return nil, err
}
variables := make(VariablesByLang)
yearVariable := MakeYearVariable()
for _, lang := range consts.ALL_KNOWN_LANGS {
variables[lang] = make(map[string]*Variable)
// Year
variables[lang][yearVariable.Name()] = &yearVariable
// Holiday
//holidayVariable, err := MakeHolidayVariable(lang, translations)
//if err != nil {
// return nil, err
//}
//if holidayVariable != nil {
// variables[lang][holidayVariable.Name()] = holidayVariable
//}
// Convention Location
conventionLocationVariable := MakeFileVariable("$ConventionLocation", lang, translations)
if conventionLocationVariable != nil {
variables[lang][conventionLocationVariable.Name()] = &conventionLocationVariable
}
}
return variables, nil
}
func snakeCaseToCamelCase(snakeCase string) string {
camelCase := ""
isToUpper := false
for k, v := range snakeCase {
if k == 0 {
camelCase = strings.ToUpper(string(snakeCase[0]))
} else {
if isToUpper {
camelCase += strings.ToUpper(string(v))
isToUpper = false
} else {
if v == '_' {
isToUpper = true
} else {
camelCase += string(v)
}
}
}
}
return camelCase
}
func LoadVariablesTranslations(variablesDir string, esc *elastic.Client, tc *TokensCache) (Translations, error) {
suffix := "variable"
matches, err := filepath.Glob(filepath.Join(variablesDir, fmt.Sprintf("*.%s", suffix)))
if err != nil {
return nil, err
}
translations := make(Translations)
for _, variableFile := range matches {
basename := path.Base(variableFile)
variable := fmt.Sprintf("$%s", snakeCaseToCamelCase(basename[:len(basename)-len(suffix)-1]))
variableTranslations, err := LoadVariableTranslations(variableFile, variable, esc, tc)
if err != nil {
return nil, err
}
translations[variable] = variableTranslations
}
return translations, nil
}
func LoadVariableTranslations(variableFile string, variableName string, esc *elastic.Client, tc *TokensCache) (map[string]map[string][][]*TokenNode, error) {
file, err := os.Open(variableFile)
if err != nil {
return nil, errors.Wrapf(err, "Error reading translation file: %s", variableFile)
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
lineNum := 1
translations := make(map[string]map[string][][]*TokenNode)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
// Ignore comments and empty lines.
if line == "" || strings.HasPrefix(line, "#") {
continue
}
re := regexp.MustCompile(`^(.*),(.*) => (.*)$`)
matches := re.FindStringSubmatch(line)
if len(matches) != 4 {
return nil, errors.New(fmt.Sprintf("[%s:%d] Error reading pattern: [%s]", variableFile, lineNum, line))
}
lang := matches[1]
value := matches[2]
translation := matches[3]
if lang == "" || value == "" || translation == "" {
return nil, errors.New(fmt.Sprintf("[%s:%d] Error reading pattern: [%s]", variableFile, lineNum, line))
}
if _, ok := translations[lang]; !ok {
translations[lang] = make(map[string][][]*TokenNode)
}
tokens, err := MakeTokensFromPhrase(translation, lang, esc, tc)
if err != nil {
return nil, errors.Wrapf(err, "Error generating tokens from translation: [%s] in %s.", translation, lang)
}
translations[lang][value] = append(translations[lang][value], tokens)
lineNum++
}
if err := scanner.Err(); err != nil {
return nil, errors.Wrapf(err, "Error reading translation file: %s", variableFile)
}
return translations, nil
}