Skip to content

Commit 0e61969

Browse files
authored
Merge pull request #52 from tmck-code/embedded-name-metadata
Embedded name metadata
2 parents 33e5afc + fc97ea3 commit 0e61969

File tree

13 files changed

+371
-106
lines changed

13 files changed

+371
-106
lines changed

build/read_assets.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package main
2+
3+
import (
4+
"embed"
5+
"flag"
6+
"fmt"
7+
8+
"github.com/tmck-code/pokesay/src/pokedex"
9+
"github.com/tmck-code/pokesay/src/timer"
10+
)
11+
12+
var (
13+
//go:embed assets/metadata/*metadata
14+
GOBCowNames embed.FS
15+
//go:embed assets/cows/*cow
16+
GOBCowFiles embed.FS
17+
//go:embed assets/pokedex.gob
18+
GOBCategory []byte
19+
20+
MetadataRoot string = "assets/metadata"
21+
CowfileRoot string = "assets/cows"
22+
)
23+
24+
type Args struct {
25+
Index int
26+
}
27+
28+
func parseFlags() Args {
29+
index := flag.Int("index", 80, "the metadata file index")
30+
flag.Parse()
31+
32+
return Args{
33+
Index: *index,
34+
}
35+
}
36+
func MetadataFpath(idx int) string {
37+
return pokedex.MetadataFpath(MetadataRoot, idx)
38+
}
39+
40+
func EntryFpath(idx int) string {
41+
return pokedex.EntryFpath(CowfileRoot, idx)
42+
}
43+
44+
func main() {
45+
args := parseFlags()
46+
t := timer.NewTimer("read_assets", true)
47+
pokedex.NewTrieFromBytes(GOBCategory)
48+
t.Mark("trie")
49+
50+
metadata := pokedex.ReadMetadataFromEmbedded(GOBCowNames, MetadataFpath(args.Index))
51+
t.Mark("metadata")
52+
53+
fmt.Println(pokedex.StructToJSON(metadata, 2))
54+
t.Mark("toJSON")
55+
56+
for i, entry := range metadata.Entries {
57+
data := string(pokedex.ReadPokemonCow(GOBCowFiles, EntryFpath(entry.EntryIndex)))
58+
t.Mark(fmt.Sprintf("read-cow-%d", i))
59+
60+
fmt.Printf("%s\n%s\n", entry.Categories, data)
61+
t.Mark(fmt.Sprintf("print-cow-%d", i))
62+
}
63+
t.Stop()
64+
t.PrintJson()
65+
}

pokesay.go

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/tmck-code/pokesay/src/pokedex"
1010
"github.com/tmck-code/pokesay/src/pokesay"
11+
"github.com/tmck-code/pokesay/src/timer"
1112
)
1213

1314
var (
@@ -65,7 +66,7 @@ func parseFlags() pokesay.Args {
6566
}
6667

6768
func EntryFpath(idx int) string {
68-
return pokedex.EntryFpath(MetadataRoot, idx)
69+
return pokedex.EntryFpath(CowDataRoot, idx)
6970
}
7071

7172
func MetadataFpath(idx int) string {
@@ -101,38 +102,81 @@ func GenerateNames(metadata pokedex.PokemonMetadata, args pokesay.Args) []string
101102
}
102103

103104
func runPrintByName(args pokesay.Args, categories pokedex.Trie) {
105+
t := timer.NewTimer("runPrintByName", true)
104106
match := pokesay.ChooseByName(args.NameToken, categories)
107+
t.Mark("match")
105108
metadata := pokedex.ReadMetadataFromEmbedded(GOBCowNames, MetadataFpath(match.Entry.Index))
109+
t.Mark("read metadata")
110+
choice := pokesay.RandomInt(len(metadata.Entries))
111+
t.Mark("choice")
112+
final := metadata.Entries[choice]
113+
// fmt.Println(pokedex.StructToJSON(metadata), "\n", choice, "\n", final)
106114

107-
pokesay.Print(args, match.Entry.Index, GenerateNames(metadata, args), match.Keys, GOBCowData)
115+
pokesay.Print(args, final.EntryIndex, GenerateNames(metadata, args), final.Categories, GOBCowData)
116+
t.Mark("print")
117+
118+
t.Stop()
119+
t.PrintJson()
108120
}
109121

110122
func runPrintByCategory(args pokesay.Args, categories pokedex.Trie) {
123+
t := timer.NewTimer("runPrintByCategory", true)
111124
choice, keys := pokesay.ChooseByCategory(args.Category, categories)
125+
t.Mark("choose by category")
126+
112127
metadata := pokedex.ReadMetadataFromEmbedded(GOBCowNames, MetadataFpath(choice.Index))
128+
t.Mark("read metadata")
113129

114130
pokesay.Print(args, choice.Index, GenerateNames(metadata, args), keys, GOBCowData)
131+
t.Mark("print")
132+
133+
t.Stop()
134+
t.PrintJson()
115135
}
116136

117137
func runPrintRandom(args pokesay.Args) {
138+
t := timer.NewTimer("runPrintRandom", true)
118139
_, choice := pokesay.ChooseByRandomIndex(GOBTotal)
140+
t.Mark("choose index")
119141
metadata := pokedex.ReadMetadataFromEmbedded(GOBCowNames, MetadataFpath(choice))
142+
t.Mark("read metadata")
120143

121-
pokesay.Print(args, choice, GenerateNames(metadata, args), strings.Split(metadata.Categories, "/"), GOBCowData)
144+
final := metadata.Entries[pokesay.RandomInt(len(metadata.Entries))]
145+
t.Mark("choose entry")
146+
147+
pokesay.Print(args, final.EntryIndex, GenerateNames(metadata, args), final.Categories, GOBCowData)
148+
t.Mark("print")
149+
150+
t.Stop()
151+
t.PrintJson()
122152
}
123153

124154
func main() {
125155
args := parseFlags()
156+
t := timer.NewTimer("main", true)
126157

127158
if args.ListCategories {
128-
runListCategories(pokedex.NewTrieFromBytes(GOBCategory))
159+
c := pokedex.NewTrieFromBytes(GOBCategory)
160+
t.Mark("trie")
161+
runListCategories(c)
162+
t.Mark("op")
129163
} else if args.ListNames {
130164
runListNames()
131165
} else if args.NameToken != "" {
132-
runPrintByName(args, pokedex.NewTrieFromBytes(GOBCategory))
166+
c := pokedex.NewTrieFromBytes(GOBCategory)
167+
t.Mark("trie")
168+
runPrintByName(args, c)
169+
t.Mark("op")
133170
} else if args.Category != "" {
134-
runPrintByCategory(args, pokedex.NewTrieFromBytes(GOBCategory))
171+
c := pokedex.NewTrieFromBytes(GOBCategory)
172+
t.Mark("trie")
173+
runPrintByCategory(args, c)
174+
t.Mark("op")
135175
} else {
136176
runPrintRandom(args)
177+
t.Mark("op")
137178
}
179+
180+
t.Stop()
181+
t.PrintJson()
138182
}

src/bin/pokedex/pokedex.go

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,29 +100,42 @@ func main() {
100100
pokemonNames := pokedex.ReadNames(args.FromMetadataFname)
101101
fmt.Println("- Read", len(pokemonNames), "pokemon names from", args.FromMetadataFname)
102102

103-
// 1. Create the category struct using the cowfile paths, pokemon names and indexes\
103+
fmt.Println("- Writing entries to file")
104+
pbar := bin.NewProgressBar(len(cowfileFpaths))
105+
for i, fpath := range cowfileFpaths {
106+
data, err := os.ReadFile(fpath)
107+
pokedex.Check(err)
108+
109+
pokedex.WriteBytesToFile(data, pokedex.EntryFpath(entryDirPath, i), true)
110+
pbar.Add(1)
111+
}
112+
113+
// 1. For each pokemon name, write a metadata file, containing the name information, and
114+
// links to all of the matching cowfile indexes
115+
pokemonMetadata := make([]pokedex.PokemonMetadata, 0)
116+
i := 0
117+
for key, name := range pokemonNames {
118+
metadata := pokedex.CreateNameMetadata(i, key, name, args.FromDir, cowfileFpaths)
119+
fmt.Printf("-- %d %+v\n", i, metadata)
120+
pokedex.WriteStructToFile(metadata, pokedex.MetadataFpath(metadataDirPath, i))
121+
pokemonMetadata = append(pokemonMetadata, *metadata)
122+
i++
123+
}
124+
fmt.Println("wrote", i, "name metadata files to", metadataDirPath)
125+
126+
// 2. Create the category struct using the cowfile paths, pokemon names and indexes\
104127
fmt.Println("- Writing categories to file")
105128
pokedex.WriteStructToFile(
106-
pokedex.CreateCategoryStruct(args.FromDir, cowfileFpaths, args.Debug),
129+
pokedex.CreateCategoryStruct(args.FromDir, pokemonMetadata, args.Debug),
107130
categoryFpath,
108131
)
109132

110-
// 2. Create the metadata files, containing name/category/japanese name info for each pokemon
111-
metadata := pokedex.CreateMetadata(args.FromDir, cowfileFpaths, pokemonNames, args.Debug)
112-
113-
fmt.Println("- Writing metadata and entries to file")
114-
pbar := bin.NewProgressBar(len(metadata))
115-
for _, m := range metadata {
116-
pokedex.WriteBytesToFile(m.Data, pokedex.EntryFpath(entryDirPath, m.Index), true)
117-
pokedex.WriteStructToFile(m.Metadata, pokedex.MetadataFpath(metadataDirPath, m.Index))
118-
pbar.Add(1)
119-
}
120133
fmt.Println("- Writing total metadata to file")
121-
pokedex.WriteIntToFile(len(metadata), totalFpath)
134+
pokedex.WriteIntToFile(len(pokemonMetadata), totalFpath)
122135

123136
fmt.Println("✓ Complete! Indexed", len(cowfileFpaths), "total cowfiles")
124137
fmt.Println("✓ Wrote gzipped metadata to", metadataDirPath)
125138
fmt.Println("✓ Wrote gzipped cowfiles to", entryDirPath)
126-
fmt.Println("✓ Wrote 'total' metadata to", totalFpath)
139+
fmt.Println("✓ Wrote 'total' metadata to", totalFpath, len(pokemonMetadata))
127140
fmt.Println("✓ Wrote gzipped category trie to", categoryFpath)
128141
}

src/pokedex/data.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ type PokemonName struct {
3232
English string
3333
Japanese string
3434
JapanesePhonetic string
35+
Slug string
3536
}
3637

3738
func NewPokemonName(entry DataEntry) *PokemonName {
3839
return &PokemonName{
3940
English: entry.Name.Eng,
4041
Japanese: entry.Name.Jpn,
4142
JapanesePhonetic: entry.Slug.Jpn,
43+
Slug: entry.Slug.Eng,
4244
}
4345
}
4446

src/pokedex/metadata.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,38 @@
11
package pokedex
22

3-
import "embed"
3+
import (
4+
"embed"
5+
6+
"github.com/tmck-code/pokesay/src/timer"
7+
)
8+
9+
type PokemonEntryMapping struct {
10+
EntryIndex int
11+
Categories []string
12+
}
413

514
type PokemonMetadata struct {
6-
Categories string
715
Name string
816
JapaneseName string
917
JapanesePhonetic string
18+
Entries []PokemonEntryMapping
1019
}
1120

12-
func NewMetadata(categories string, name string, japaneseName string, japanesePhonetic string) *PokemonMetadata {
21+
func NewMetadata(name string, japaneseName string, japanesePhonetic string, entryMap map[int][][]string) *PokemonMetadata {
22+
23+
entries := make([]PokemonEntryMapping, 0)
24+
25+
for idx, categories := range entryMap {
26+
for _, category := range categories {
27+
entries = append(entries, PokemonEntryMapping{idx, category})
28+
}
29+
}
30+
1331
return &PokemonMetadata{
14-
Categories: categories,
1532
Name: name,
1633
JapaneseName: japaneseName,
1734
JapanesePhonetic: japanesePhonetic,
35+
Entries: entries,
1836
}
1937
}
2038

@@ -23,7 +41,15 @@ func ReadMetadataFromBytes(data []byte) PokemonMetadata {
2341
}
2442

2543
func ReadMetadataFromEmbedded(embeddedData embed.FS, fpath string) PokemonMetadata {
44+
t := timer.NewTimer("ReadMetadataFromEmbedded")
2645
metadata, err := embeddedData.ReadFile(fpath)
2746
Check(err)
28-
return ReadMetadataFromBytes(metadata)
47+
t.Mark("read file")
48+
49+
data := ReadMetadataFromBytes(metadata)
50+
t.Mark("read metadata")
51+
52+
t.Stop()
53+
t.PrintJson()
54+
return data
2955
}

0 commit comments

Comments
 (0)