Skip to content

Commit

Permalink
Merge pull request #52 from tmck-code/embedded-name-metadata
Browse files Browse the repository at this point in the history
Embedded name metadata
  • Loading branch information
tmck-code authored Feb 8, 2023
2 parents 33e5afc + fc97ea3 commit 0e61969
Show file tree
Hide file tree
Showing 13 changed files with 371 additions and 106 deletions.
65 changes: 65 additions & 0 deletions build/read_assets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"embed"
"flag"
"fmt"

"github.com/tmck-code/pokesay/src/pokedex"
"github.com/tmck-code/pokesay/src/timer"
)

var (
//go:embed assets/metadata/*metadata
GOBCowNames embed.FS
//go:embed assets/cows/*cow
GOBCowFiles embed.FS
//go:embed assets/pokedex.gob
GOBCategory []byte

MetadataRoot string = "assets/metadata"
CowfileRoot string = "assets/cows"
)

type Args struct {
Index int
}

func parseFlags() Args {
index := flag.Int("index", 80, "the metadata file index")
flag.Parse()

return Args{
Index: *index,
}
}
func MetadataFpath(idx int) string {
return pokedex.MetadataFpath(MetadataRoot, idx)
}

func EntryFpath(idx int) string {
return pokedex.EntryFpath(CowfileRoot, idx)
}

func main() {
args := parseFlags()
t := timer.NewTimer("read_assets", true)
pokedex.NewTrieFromBytes(GOBCategory)
t.Mark("trie")

metadata := pokedex.ReadMetadataFromEmbedded(GOBCowNames, MetadataFpath(args.Index))
t.Mark("metadata")

fmt.Println(pokedex.StructToJSON(metadata, 2))
t.Mark("toJSON")

for i, entry := range metadata.Entries {
data := string(pokedex.ReadPokemonCow(GOBCowFiles, EntryFpath(entry.EntryIndex)))
t.Mark(fmt.Sprintf("read-cow-%d", i))

fmt.Printf("%s\n%s\n", entry.Categories, data)
t.Mark(fmt.Sprintf("print-cow-%d", i))
}
t.Stop()
t.PrintJson()
}
56 changes: 50 additions & 6 deletions pokesay.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/tmck-code/pokesay/src/pokedex"
"github.com/tmck-code/pokesay/src/pokesay"
"github.com/tmck-code/pokesay/src/timer"
)

var (
Expand Down Expand Up @@ -65,7 +66,7 @@ func parseFlags() pokesay.Args {
}

func EntryFpath(idx int) string {
return pokedex.EntryFpath(MetadataRoot, idx)
return pokedex.EntryFpath(CowDataRoot, idx)
}

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

func runPrintByName(args pokesay.Args, categories pokedex.Trie) {
t := timer.NewTimer("runPrintByName", true)
match := pokesay.ChooseByName(args.NameToken, categories)
t.Mark("match")
metadata := pokedex.ReadMetadataFromEmbedded(GOBCowNames, MetadataFpath(match.Entry.Index))
t.Mark("read metadata")
choice := pokesay.RandomInt(len(metadata.Entries))
t.Mark("choice")
final := metadata.Entries[choice]
// fmt.Println(pokedex.StructToJSON(metadata), "\n", choice, "\n", final)

pokesay.Print(args, match.Entry.Index, GenerateNames(metadata, args), match.Keys, GOBCowData)
pokesay.Print(args, final.EntryIndex, GenerateNames(metadata, args), final.Categories, GOBCowData)
t.Mark("print")

t.Stop()
t.PrintJson()
}

func runPrintByCategory(args pokesay.Args, categories pokedex.Trie) {
t := timer.NewTimer("runPrintByCategory", true)
choice, keys := pokesay.ChooseByCategory(args.Category, categories)
t.Mark("choose by category")

metadata := pokedex.ReadMetadataFromEmbedded(GOBCowNames, MetadataFpath(choice.Index))
t.Mark("read metadata")

pokesay.Print(args, choice.Index, GenerateNames(metadata, args), keys, GOBCowData)
t.Mark("print")

t.Stop()
t.PrintJson()
}

func runPrintRandom(args pokesay.Args) {
t := timer.NewTimer("runPrintRandom", true)
_, choice := pokesay.ChooseByRandomIndex(GOBTotal)
t.Mark("choose index")
metadata := pokedex.ReadMetadataFromEmbedded(GOBCowNames, MetadataFpath(choice))
t.Mark("read metadata")

pokesay.Print(args, choice, GenerateNames(metadata, args), strings.Split(metadata.Categories, "/"), GOBCowData)
final := metadata.Entries[pokesay.RandomInt(len(metadata.Entries))]
t.Mark("choose entry")

pokesay.Print(args, final.EntryIndex, GenerateNames(metadata, args), final.Categories, GOBCowData)
t.Mark("print")

t.Stop()
t.PrintJson()
}

func main() {
args := parseFlags()
t := timer.NewTimer("main", true)

if args.ListCategories {
runListCategories(pokedex.NewTrieFromBytes(GOBCategory))
c := pokedex.NewTrieFromBytes(GOBCategory)
t.Mark("trie")
runListCategories(c)
t.Mark("op")
} else if args.ListNames {
runListNames()
} else if args.NameToken != "" {
runPrintByName(args, pokedex.NewTrieFromBytes(GOBCategory))
c := pokedex.NewTrieFromBytes(GOBCategory)
t.Mark("trie")
runPrintByName(args, c)
t.Mark("op")
} else if args.Category != "" {
runPrintByCategory(args, pokedex.NewTrieFromBytes(GOBCategory))
c := pokedex.NewTrieFromBytes(GOBCategory)
t.Mark("trie")
runPrintByCategory(args, c)
t.Mark("op")
} else {
runPrintRandom(args)
t.Mark("op")
}

t.Stop()
t.PrintJson()
}
41 changes: 27 additions & 14 deletions src/bin/pokedex/pokedex.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,29 +100,42 @@ func main() {
pokemonNames := pokedex.ReadNames(args.FromMetadataFname)
fmt.Println("- Read", len(pokemonNames), "pokemon names from", args.FromMetadataFname)

// 1. Create the category struct using the cowfile paths, pokemon names and indexes\
fmt.Println("- Writing entries to file")
pbar := bin.NewProgressBar(len(cowfileFpaths))
for i, fpath := range cowfileFpaths {
data, err := os.ReadFile(fpath)
pokedex.Check(err)

pokedex.WriteBytesToFile(data, pokedex.EntryFpath(entryDirPath, i), true)
pbar.Add(1)
}

// 1. For each pokemon name, write a metadata file, containing the name information, and
// links to all of the matching cowfile indexes
pokemonMetadata := make([]pokedex.PokemonMetadata, 0)
i := 0
for key, name := range pokemonNames {
metadata := pokedex.CreateNameMetadata(i, key, name, args.FromDir, cowfileFpaths)
fmt.Printf("-- %d %+v\n", i, metadata)
pokedex.WriteStructToFile(metadata, pokedex.MetadataFpath(metadataDirPath, i))
pokemonMetadata = append(pokemonMetadata, *metadata)
i++
}
fmt.Println("wrote", i, "name metadata files to", metadataDirPath)

// 2. Create the category struct using the cowfile paths, pokemon names and indexes\
fmt.Println("- Writing categories to file")
pokedex.WriteStructToFile(
pokedex.CreateCategoryStruct(args.FromDir, cowfileFpaths, args.Debug),
pokedex.CreateCategoryStruct(args.FromDir, pokemonMetadata, args.Debug),
categoryFpath,
)

// 2. Create the metadata files, containing name/category/japanese name info for each pokemon
metadata := pokedex.CreateMetadata(args.FromDir, cowfileFpaths, pokemonNames, args.Debug)

fmt.Println("- Writing metadata and entries to file")
pbar := bin.NewProgressBar(len(metadata))
for _, m := range metadata {
pokedex.WriteBytesToFile(m.Data, pokedex.EntryFpath(entryDirPath, m.Index), true)
pokedex.WriteStructToFile(m.Metadata, pokedex.MetadataFpath(metadataDirPath, m.Index))
pbar.Add(1)
}
fmt.Println("- Writing total metadata to file")
pokedex.WriteIntToFile(len(metadata), totalFpath)
pokedex.WriteIntToFile(len(pokemonMetadata), totalFpath)

fmt.Println("✓ Complete! Indexed", len(cowfileFpaths), "total cowfiles")
fmt.Println("✓ Wrote gzipped metadata to", metadataDirPath)
fmt.Println("✓ Wrote gzipped cowfiles to", entryDirPath)
fmt.Println("✓ Wrote 'total' metadata to", totalFpath)
fmt.Println("✓ Wrote 'total' metadata to", totalFpath, len(pokemonMetadata))
fmt.Println("✓ Wrote gzipped category trie to", categoryFpath)
}
2 changes: 2 additions & 0 deletions src/pokedex/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ type PokemonName struct {
English string
Japanese string
JapanesePhonetic string
Slug string
}

func NewPokemonName(entry DataEntry) *PokemonName {
return &PokemonName{
English: entry.Name.Eng,
Japanese: entry.Name.Jpn,
JapanesePhonetic: entry.Slug.Jpn,
Slug: entry.Slug.Eng,
}
}

Expand Down
36 changes: 31 additions & 5 deletions src/pokedex/metadata.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
package pokedex

import "embed"
import (
"embed"

"github.com/tmck-code/pokesay/src/timer"
)

type PokemonEntryMapping struct {
EntryIndex int
Categories []string
}

type PokemonMetadata struct {
Categories string
Name string
JapaneseName string
JapanesePhonetic string
Entries []PokemonEntryMapping
}

func NewMetadata(categories string, name string, japaneseName string, japanesePhonetic string) *PokemonMetadata {
func NewMetadata(name string, japaneseName string, japanesePhonetic string, entryMap map[int][][]string) *PokemonMetadata {

entries := make([]PokemonEntryMapping, 0)

for idx, categories := range entryMap {
for _, category := range categories {
entries = append(entries, PokemonEntryMapping{idx, category})
}
}

return &PokemonMetadata{
Categories: categories,
Name: name,
JapaneseName: japaneseName,
JapanesePhonetic: japanesePhonetic,
Entries: entries,
}
}

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

func ReadMetadataFromEmbedded(embeddedData embed.FS, fpath string) PokemonMetadata {
t := timer.NewTimer("ReadMetadataFromEmbedded")
metadata, err := embeddedData.ReadFile(fpath)
Check(err)
return ReadMetadataFromBytes(metadata)
t.Mark("read file")

data := ReadMetadataFromBytes(metadata)
t.Mark("read metadata")

t.Stop()
t.PrintJson()
return data
}
Loading

0 comments on commit 0e61969

Please sign in to comment.