Assign numbers to words.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
xorWords/create_db.go

95 lines
2.1 KiB

package main
import (
"database/sql"
"fmt"
"io/ioutil"
"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() {
// Open SQLite database file
db, err := sql.Open("sqlite3", "./english_words.db")
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 american-english file
filePath := "/usr/share/dict/american-english"
content, err := ioutil.ReadFile(filePath)
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.")
}