package main import ( "database/sql" "fmt" "flag" "io/ioutil" "math/rand" "log" "strings" _ "github.com/mattn/go-sqlite3" ) var topEnglishWords = []string{ ".", ",", "!", ";", "?", "(", ")", "'", "\"", "the", "and", "of", "to", "a", "in", "is", "it", "you", "that", "he", "was", "for", "on", "are", "with", "as", "I", "his", "they", "be", "at", "one", "have", "this", "from", "or", "had", "by", "hot", "word", "but", "what", "some", "we", "can", "out", "other", "were", "all", "there", "when", "up", "use", "your", "how", "said", "an", "each", "she", "which", "do", "their", "time", "if", "will", "way", "about", "many", "then", "them", "write", "would", "like", "so", "these", } func main() { dbPtr := flag.String("db", "./english_words.db", "SQLite3 DB file to use") dictFilePtr := flag.String("dict", "./rnd-english", "Dictionary file to use") flag.Parse() // Randomize high Freq. words shuffleArray(topEnglishWords) // Open SQLite database file db, err := sql.Open("sqlite3", *dbPtr) if err != nil { log.Fatal(err) } defer db.Close() // Create table _, err = db.Exec(`CREATE TABLE IF NOT EXISTS words (id INTEGER PRIMARY KEY, word TEXT);`) if err != nil { log.Fatal(err) } // Insert top English words into the database tx, err := db.Begin() if err != nil { log.Fatal(err) } stmt, err := tx.Prepare("INSERT INTO words(word) VALUES(?)") if err != nil { log.Fatal(err) } defer stmt.Close() for _, word := range topEnglishWords { _, err = stmt.Exec(word) if err != nil { log.Fatal(err) } } // Commit the transaction tx.Commit() fmt.Println("Top English words inserted into the database.") // Read the contents of the shuf american-english file content, err := ioutil.ReadFile(*dictFilePtr) if err != nil { log.Fatal(err) } // Split content into words words := strings.Fields(string(content)) // Insert american-english words into the database tx, err = db.Begin() if err != nil { log.Fatal(err) } stmt, err = tx.Prepare("INSERT INTO words(word) VALUES(?)") if err != nil { log.Fatal(err) } defer stmt.Close() for _, word := range words { _, err = stmt.Exec(word) if err != nil { log.Fatal(err) } } // Commit the transaction tx.Commit() fmt.Println("American-English words inserted into the database.") } func shuffleArray(arr []string) { n := len(arr) for i := n - 1; i > 0; i-- { // Generate a random index between 0 and i (inclusive) j := rand.Intn(i + 1) // Swap the elements at indices i and j arr[i], arr[j] = arr[j], arr[i] } }