-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
- Using json.RawMessage makes the code a bit more robust - Getting rid of baseURL makes the task independent from corpora. Now the only assumption is that the URL returns a JSON with an array of strings at a given key - Namespacing cat under animal so that we follow the existing convention - Renamed emojis to emoji.go as they're both valid plurals but the latter is shorter At this point I'm not sure the naming `importcorpora` and `make corpora` is right anymore but, as we're going to work on this soon, it's safe to delay the decision
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
// Usage: go run cmd/importcorpora/main.go | ||
// | ||
// Updates the at the bottom of this file specified data from dariusk/corpora. | ||
package main | ||
|
||
import ( | ||
|
@@ -14,31 +11,58 @@ import ( | |
"strings" | ||
) | ||
|
||
// Base URL to download JSON files from | ||
const baseURL = "https://raw.githubusercontent.com/dariusk/corpora/master/data/" | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
// Path to write Go files to | ||
// relative package data dir | ||
const targetDir = "pkg/data" | ||
|
||
// Content of a Go file | ||
const fileTemplate = `package data | ||
// %s is an array of %s | ||
var %s = %s` | ||
var %s = []string{%s} | ||
` | ||
|
||
var tasks = []struct { | ||
URL, Key, Var, File string | ||
}{ | ||
{ | ||
URL: "https://raw.githubusercontent.com/dariusk/corpora/master/data/words/nouns.json", | ||
Key: "nouns", | ||
Var: "Nouns", | ||
File: "nouns.go", | ||
}, | ||
{ | ||
URL: "https://raw.githubusercontent.com/dariusk/corpora/master/data/animals/common.json", | ||
Key: "animals", | ||
Var: "Animals", | ||
File: "animals.go", | ||
}, | ||
{ | ||
URL: "https://raw.githubusercontent.com/dariusk/corpora/master/data/animals/cats.json", | ||
Key: "cats", | ||
Var: "Cats", | ||
File: "cats.go", | ||
}, | ||
{ | ||
URL: "https://raw.githubusercontent.com/dariusk/corpora/master/data/words/emoji/emoji.json", | ||
Key: "emoji", | ||
Var: "Emoji", | ||
File: "emoji.go", | ||
}, | ||
} | ||
|
||
func main() { | ||
// Check if running in repo directory | ||
// Check if running in repository directory | ||
_, err := os.Stat(targetDir) | ||
if err != nil && !os.IsNotExist(err) { | ||
log.Fatal(err) | ||
} | ||
if err != nil { | ||
log.Fatalf("The data directory cannot be found at %s. Ensure the importer is running in the correct location.", targetDir) | ||
log.Fatalf("the data directory cannot be found at %s. Ensure the importer is running in the correct location: %v", targetDir, err) | ||
} | ||
|
||
for _, d := range data { | ||
for _, task := range tasks { | ||
// Get JSON from URL | ||
resp, err := http.Get(baseURL + d.From) | ||
resp, err := http.Get(task.URL) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
@@ -48,62 +72,31 @@ func main() { | |
} | ||
}() | ||
|
||
// Get data from JSON | ||
var jsonData map[string]interface{} | ||
var jsonData map[string]json.RawMessage | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
lucapette
Author
Owner
|
||
if err := json.NewDecoder(resp.Body).Decode(&jsonData); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Fix formatting | ||
value := strings.Replace(fmt.Sprintf("%#v\n", jsonData[d.Key]), "interface {}", "string", 1) | ||
content := fmt.Sprintf(fileTemplate, d.Var, strings.ToLower(d.Var), d.Var, value) | ||
var data []string | ||
if err := json.Unmarshal(jsonData[task.Key], &data); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Create required directories | ||
to := filepath.Join(targetDir, d.To) | ||
if err := os.MkdirAll(filepath.Dir(to), 0777); err != nil { | ||
file := filepath.Join(targetDir, task.File) | ||
if err := os.MkdirAll(filepath.Dir(file), 0777); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
array := make([]string, len(data)) | ||
for i, val := range data { | ||
array[i] = fmt.Sprintf("\"%s\"", val) | ||
} | ||
This comment has been minimized.
Sorry, something went wrong.
jorinvo
Contributor
|
||
|
||
content := fmt.Sprintf(fileTemplate, task.Var, task.Key, task.Var, strings.Join(array, ", ")) | ||
|
||
// Write to Go file | ||
if err := ioutil.WriteFile(to, []byte(content), 0644); err != nil { | ||
if err := ioutil.WriteFile(file, []byte(content), 0644); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
} | ||
|
||
// Data to import is specified here | ||
var data = []struct { | ||
// JSON file to read from | ||
From string | ||
// Key containing data in JSON file | ||
Key string | ||
// Go File to write to | ||
To string | ||
// Variable name in the Go File | ||
Var string | ||
}{ | ||
{ | ||
From: "words/nouns.json", | ||
Key: "nouns", | ||
To: "nouns.go", | ||
Var: "Nouns", | ||
}, | ||
{ | ||
From: "animals/common.json", | ||
Key: "animals", | ||
To: "animals.go", | ||
Var: "Animals", | ||
}, | ||
{ | ||
From: "animals/cats.json", | ||
Key: "cats", | ||
To: "cats.go", | ||
Var: "Cats", | ||
}, | ||
{ | ||
From: "words/emoji/emoji.json", | ||
Key: "emoji", | ||
To: "emojis.go", | ||
Var: "Emojis", | ||
}, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package data | ||
|
||
// Emoji is an array of emoji | ||
var Emoji = []string{"๐", "๐", "๐ ฐ", "๐ ฑ", "๐ พ", "๐ ฟ", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐ฆ", "๐ง", "๐จ", "๐ฉ", "๐ช", "๐ซ", "๐ฌ", "๐ญ", "๐ฎ", "๐ฏ", "๐ฐ", "๐ฑ", "๐ฒ", "๐ณ", "๐ด", "๐ต", "๐ถ", "๐ท", "๐ธ", "๐น", "๐บ", "๐ป", "๐ผ", "๐ฝ", "๐พ", "๐ฟ", "๐", "๐", "๐", "๐ฏ", "๐ฒ", "๐ณ", "๐ด", "๐ต", "๐ถ", "๐ท", "๐ธ", "๐น", "๐บ", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐ฐ", "๐ฑ", "๐ฒ", "๐ณ", "๐ด", "๐ต", "๐ท", "๐ธ", "๐น", "๐บ", "๐ป", "๐ผ", "๐ฝ", "๐พ", "๐ฟ", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐ก", "๐ข", "๐ฃ", "๐ค", "๐ฅ", "๐ฆ", "๐ง", "๐จ", "๐ฉ", "๐ช", "๐ซ", "๐ฌ", "๐ญ", "๐ฎ", "๐ฏ", "๐ฐ", "๐ฑ", "๐ฒ", "๐ณ", "๐ด", "๐ต", "๐ถ", "๐ท", "๐ธ", "๐น", "๐บ", "๐ป", "๐ผ", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐ก", "๐ข", "๐ฃ", "๐ค", "๐ฅ", "๐ฆ", "๐ง", "๐จ", "๐ฉ", "๐ช", "๐ซ", "๐ฌ", "๐ญ", "๐ฎ", "๐ฏ", "๐ฐ", "๐ฑ", "๐ฒ", "๐ณ", "๐ด", "๐ต", "๐ถ", "๐ท", "๐ธ", "๐น", "๐บ", "๐ป", "๐ผ", "๐ฝ", "๐พ", "๐ฟ", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐ก", "๐ข", "๐ฃ", "๐ค", "๐ฅ", "๐ฆ", "๐ง", "๐จ", "๐ฉ", "๐ช", "๐ซ", "๐ฌ", "๐ญ", "๐ฎ", "๐ฏ", "๐ฐ", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐ก", "๐ข", "๐ฃ", "๐ค", "๐ฅ", "๐ฆ", "๐ง", "๐จ", "๐ฉ", "๐ช", "๐ซ", "๐ฌ", "๐ญ", "๐ฎ", "๐ฏ", "๐ฐ", "๐ฑ", "๐ฒ", "๐ณ", "๐ด", "๐ต", "๐ถ", "๐ท", "๐ธ", "๐น", "๐บ", "๐ป", "๐ผ", "๐ฝ", "๐พ", "๐", "๐", "๐", "๐", "๐ ", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐ก", "๐ข", "๐ฃ", "๐ค", "๐ฅ", "๐ฆ", "๐ง", "๐จ", "๐ฉ", "๐ช", "๐ซ", "๐ฌ", "๐ญ", "๐ฎ", "๐ฏ", "๐ฐ", "๐ฑ", "๐ฒ", "๐ณ", "๐ด", "๐ต", "๐ถ", "๐ท", "๐ธ", "๐น", "๐บ", "๐ป", "๐ผ", "๐ฝ", "๐พ", "๐ฟ", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐ก", "๐ข", "๐ฃ", "๐ค", "๐ฅ", "๐ฆ", "๐ง", "๐จ", "๐ฉ", "๐ช", "๐ซ", "๐ฌ", "๐ญ", "๐ฎ", "๐ฏ", "๐ฐ", "๐ฑ", "๐ฒ", "๐ณ", "๐ด", "๐ต", "๐ถ", "๐ท", "๐ธ", "๐น", "๐บ", "๐ป", "๐ผ", "๐ฝ", "๐พ", "๐ฟ", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐ก", "๐ข", "๐ฃ", "๐ค", "๐ฅ", "๐ฆ", "๐ง", "๐จ", "๐ฉ", "๐ช", "๐ซ", "๐ฌ", "๐ญ", "๐ฎ", "๐ฏ", "๐ฐ", "๐ฑ", "๐ฒ", "๐ณ", "๐ด", "๐ต", "๐ถ", "๐ท", "๐น", "๐บ", "๐ป", "๐ผ", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐ก", "๐ข", "๐ฃ", "๐ค", "๐ฅ", "๐ฆ", "๐ง", "๐จ", "๐ฉ", "๐ช", "๐ซ", "๐ฌ", "๐ญ", "๐ฎ", "๐ฏ", "๐ฐ", "๐ฑ", "๐ฒ", "๐ณ", "๐ด", "๐ต", "๐ถ", "๐ท", "๐ธ", "๐น", "๐บ", "๐ป", "๐ผ", "๐ฝ", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐ก", "๐ข", "๐ฃ", "๐ค", "๐ฅ", "๐ฆ", "๐ง", "๐ป", "๐ผ", "๐ฝ", "๐พ", "๐ฟ", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐ก", "๐ข", "๐ฃ", "๐ค", "๐ฅ", "๐ฆ", "๐ง", "๐จ", "๐ฉ", "๐ช", "๐ซ", "๐ฌ", "๐ญ", "๐ฎ", "๐ฏ", "๐ฐ", "๐ฑ", "๐ฒ", "๐ณ", "๐ด", "๐ต", "๐ถ", "๐ท", "๐ธ", "๐น", "๐บ", "๐ป", "๐ผ", "๐ฝ", "๐พ", "๐ฟ", "๐", "๐", "๐", "๐ ", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐", "๐ ", "๐ก", "๐ข", "๐ฃ", "๐ค", "๐ฅ", "๐ฆ", "๐ง", "๐จ", "๐ฉ", "๐ช", "๐ซ", "๐ฌ", "๐ญ", "๐ฎ", "๐ฏ", "๐ฐ", "๐ฑ", "๐ฒ", "๐ณ", "๐ด", "๐ต", "๐ถ", "๐ท", "๐ธ", "๐น", "๐บ", "๐ป", "๐ผ", "๐ฝ", "๐พ", "๐ฟ", "๐", "๐", "๐", "๐", "๐", "๐ ", "โผ", "โ", "โข", "โน", "โ", "โ", "โ", "โ", "โ", "โ", "โฉ", "โช", "#", "โ", "โ", "โฉ", "โช", "โซ", "โฌ", "โฐ", "โณ", "โ", "โช", "โซ", "โถ", "โ", "โป", "โผ", "โฝ", "โพ", "โ", "โ", "โ", "โ", "โ", "โ", "โ", "โบ", "โ", "โ", "โ", "โ", "โ", "โ", "โ", "โ", "โ", "โ", "โ", "โ", "โ ", "โฃ", "โฅ", "โฆ", "โจ", "โป", "โฟ", "โ", "โ ", "โก", "โช", "โซ", "โฝ", "โพ", "โ", "โ ", "โ", "โ", "โช", "โฒ", "โณ", "โต", "โบ", "โฝ", "โ", "โ ", "โ", "โ", "โ", "โ", "โ", "โ", "โ", "โ", "โ", "โจ", "โณ", "โด", "โ", "โ", "โ", "โ", "โ", "โ", "โ", "โ", "โค", "โ", "โ", "โ", "โก", "โฐ", "โฟ", "โคด", "โคต", "โฌ ", "โฌ", "โฌ", "โฌ", "โฌ", "โญ", "โญ", "0", "ใฐ", "ใฝ", "1", "2", "ใ", "ใ", "3", "4", "5", "6", "7", "8", "9", "ยฉ", "ยฎ", "๎"} |
This file was deleted.
Good idea to make it more general ๐