diff options
author | Anthony Wang | 2022-03-28 21:06:22 -0500 |
---|---|---|
committer | Anthony Wang | 2022-03-28 21:06:22 -0500 |
commit | a6647501a6dd322a0f6b2c54d802e5ad76eff393 (patch) | |
tree | 319ca9bb9742e3c370619a64e2266c24a8f8e739 | |
parent | 556149c189ecd57dc6075129c954bf2add3a0147 (diff) |
Add an example script for generating Chinese character flashcards
-rw-r--r-- | examples/gen.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/examples/gen.go b/examples/gen.go new file mode 100644 index 0000000..e25a308 --- /dev/null +++ b/examples/gen.go @@ -0,0 +1,60 @@ +package main + +import ( + "bufio" + "database/sql" + "flag" + "fmt" + "math/bits" + "os" + "strings" + + _ "modernc.org/sqlite" +) + +/* +https://en.wiktionary.org/wiki/Appendix:Mandarin_Frequency_lists/1-1000 +https://lingua.mtsu.edu/chinese-computing/statistics/char/list.php?Which=MO +https://jslearnchinese.wordpress.com/2018/07/24/the-1000-most-used-characters-in-chinese/ +*/ + +var verbose = flag.Bool("v", false, "debug output") +var file = flag.String("f", "cards", "cards file") + +func main() { + flag.Parse() + + db, err := sql.Open("sqlite", *file) + if err != nil { + panic(err) + } + + // Create table + _, err = db.Exec("CREATE TABLE IF NOT EXISTS cards (idx INTEGER PRIMARY KEY, weight INTEGER, key STRING, val STRING)") + if err != nil { + panic(err) + } + + f, err := os.Open("list") + if err != nil { + panic(err) + } + defer f.Close() + + scanner := bufio.NewScanner(f) + + i := 0 + for scanner.Scan() { + line := strings.Split(scanner.Text(), "|") + key := line[2] + val := line[3] + " " + strings.Split(line[4], " {")[0] + + if *verbose { + fmt.Println(key) + fmt.Println(val) + } + + _, err = db.Exec("INSERT INTO cards VALUES(?, ?, ?, ?)", i, 1<<(bits.LeadingZeros(uint(i))-40), key, val) + i++ + } +} |