This repository has been archived on 2024-01-02. You can view files and clone it, but cannot push or open issues or pull requests.
SD/examples/gen.go

62 lines
1.1 KiB
Go

package main
import (
"bufio"
"database/sql"
"flag"
"fmt"
"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")
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<<(20-i/50), key, val)
i++
}
}