-
Notifications
You must be signed in to change notification settings - Fork 32
/
statvar_hierarchy_util.go
161 lines (151 loc) · 5.3 KB
/
statvar_hierarchy_util.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
161
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package statvar
import (
"context"
"encoding/json"
"io/ioutil"
"path"
"runtime"
"sort"
"strings"
"time"
"github.com/datacommonsorg/mixer/internal/server/resource"
"github.com/datacommonsorg/mixer/internal/store"
pb "github.com/datacommonsorg/mixer/internal/proto"
"github.com/datacommonsorg/mixer/internal/util"
)
// This should be synced with the list of blocklisted SVGs in the website repo
var ignoredSvgIds = []string{"dc/g/Establishment_Industry", "eia/g/Root", "dc/g/Uncategorized"}
// GetRawSvg gets the raw svg mapping.
func GetRawSvg(ctx context.Context, store *store.Store) (
map[string]*pb.StatVarGroupNode, error) {
svg, err := GetStatVarGroup(ctx, &pb.GetStatVarGroupRequest{}, store, nil)
if err != nil {
return nil, err
}
return svg.StatVarGroups, nil
}
// BuildParentSvgMap gets the mapping of svg/sv id to the parent svg for that
// svg/sv. Only gets the parent svg that have a path to the root node.
func BuildParentSvgMap(rawSvg map[string]*pb.StatVarGroupNode) map[string][]string {
parentSvgMap := map[string][]string{}
// Do breadth first search starting at the root to find all the svg that have
// a path to the root. Only add those as parents.
seenSvg := map[string]struct{}{SvgRoot: {}}
svgToVisit := []string{SvgRoot}
for len(svgToVisit) > 0 {
svgID := svgToVisit[0]
svgToVisit = svgToVisit[1:]
if svgData, ok := rawSvg[svgID]; ok {
for _, childSvg := range svgData.ChildStatVarGroups {
// Add the current svg to the list of parents for this child svg.
if _, ok := parentSvgMap[childSvg.Id]; !ok {
parentSvgMap[childSvg.Id] = []string{}
}
parentSvgMap[childSvg.Id] = append(parentSvgMap[childSvg.Id], svgID)
// If this child svg hasn't been seen yet, add it to the list of svg
// left to visit.
if _, ok := seenSvg[childSvg.Id]; !ok {
seenSvg[childSvg.Id] = struct{}{}
svgToVisit = append(svgToVisit, childSvg.Id)
}
}
// Add the current svg to the list of parents for each child sv.
for _, childSv := range svgData.ChildStatVars {
if _, ok := parentSvgMap[childSv.Id]; !ok {
parentSvgMap[childSv.Id] = []string{}
}
parentSvgMap[childSv.Id] = append(parentSvgMap[childSv.Id], svgID)
}
}
}
for _, parentSvgList := range parentSvgMap {
sort.Strings(parentSvgList)
}
return parentSvgMap
}
// Helper to build a set of ignored SVGs.
func getIgnoredSVGHelper(
ignoredSvg map[string]string,
rawSvg map[string]*pb.StatVarGroupNode,
svgID string) {
ignoredSvg[svgID] = ""
if svgData, ok := rawSvg[svgID]; ok {
for _, svData := range svgData.ChildStatVarGroups {
getIgnoredSVGHelper(ignoredSvg, rawSvg, svData.Id)
}
}
}
func getSynonymMap() map[string][]string {
synonymMap := map[string][]string{}
_, filename, _, _ := runtime.Caller(0)
bytes, err := ioutil.ReadFile(path.Join(path.Dir(filename), "../resource/synonyms.json"))
if err == nil {
var synonyms [][]string
err = json.Unmarshal(bytes, &synonyms)
if err == nil {
// for each synonymList (list of words that are all synonyms of each other),
// append that list to the synonymMap value for each word in the list.
for _, synonymList := range synonyms {
for _, word := range synonymList {
if _, ok := synonymMap[word]; !ok {
synonymMap[word] = synonymList
} else {
synonymMap[word] = append(synonymMap[word], synonymList...)
}
}
}
}
}
return synonymMap
}
// BuildStatVarSearchIndex builds the search index for the stat var hierarchy.
func BuildStatVarSearchIndex(
rawSvg map[string]*pb.StatVarGroupNode,
parentSvg map[string][]string) *resource.SearchIndex {
defer util.TimeTrack(time.Now(), "BuildStatVarSearchIndex")
// map of token to map of sv/svg id to ranking information.
searchIndex := &resource.SearchIndex{
RootTrieNode: &resource.TrieNode{},
Ranking: map[string]*resource.RankingInfo{},
}
ignoredSVG := map[string]string{}
// Exclude svg and sv under miscellaneous from the search index
for _, svgID := range ignoredSvgIds {
getIgnoredSVGHelper(ignoredSVG, rawSvg, svgID)
}
synonymMap := getSynonymMap()
seenSV := map[string]struct{}{}
for svgID, svgData := range rawSvg {
if _, ok := ignoredSVG[svgID]; ok {
continue
}
// Ignore svg that don't have any parents with a path to the root node.
if _, ok := parentSvg[svgID]; !ok {
continue
}
tokenString := svgData.AbsoluteName
searchIndex.Update(svgID, tokenString, tokenString, true /* isSvg */, synonymMap, "")
for _, svData := range svgData.ChildStatVars {
if _, ok := seenSV[svData.Id]; ok {
continue
}
seenSV[svData.Id] = struct{}{}
svTokenString := strings.Join(svData.SearchNames, " ")
searchIndex.Update(svData.Id, svTokenString, svData.DisplayName, false /* isSvg */, synonymMap, svData.Definition)
}
}
return searchIndex
}