Add an example script for generating Chinese character flashcards
This commit is contained in:
parent
556149c189
commit
a6647501a6
60
examples/gen.go
Normal file
60
examples/gen.go
Normal file
|
@ -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++
|
||||
}
|
||||
}
|
Reference in a new issue