diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..89ab5d8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +The MIT License + +Copyright (c) 2024 Bob + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/create_db.go b/create_db.go index fc395ed..915a133 100644 --- a/create_db.go +++ b/create_db.go @@ -1,6 +1,8 @@ package main import ( + "path/filepath" + "os" "database/sql" "fmt" "flag" @@ -12,8 +14,9 @@ import ( _ "github.com/mattn/go-sqlite3" ) +var version = "0.0.1" + 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", @@ -21,53 +24,66 @@ var topEnglishWords = []string{ "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", + "here", "me", "make", "day", "where", "give", "now", "just", } -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() +var commonSymbols = []string{ + ".", ",", "!", ";", "?", "(", ")", "'", "\"", "...", ":", "*", "$", "@", "#", "%", +} - // Create table - _, err = db.Exec(`CREATE TABLE IF NOT EXISTS words (id INTEGER PRIMARY KEY, word TEXT);`) - if err != nil { - log.Fatal(err) - } +func insertSymbols(db *sql.DB) { + // Insert symbols into the database + tx, err := db.Begin() + 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() - stmt, err := tx.Prepare("INSERT INTO words(word) VALUES(?)") - if err != nil { - log.Fatal(err) - } - defer stmt.Close() + for _, word := range commonSymbols { + _, err = stmt.Exec(word) + if err != nil { + log.Fatal(err) + } + } - for _, word := range topEnglishWords { - _, err = stmt.Exec(word) + // Commit the transaction + tx.Commit() + + fmt.Println("Common symbols inserted into the database.") +} + +func insertCommonEnglishWords(db *sql.DB) { + // Insert top English words into the database + tx, err := db.Begin() if err != nil { log.Fatal(err) } - } - // Commit the transaction - tx.Commit() + 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) + } + } - fmt.Println("Top English words inserted into the database.") + // Commit the transaction + tx.Commit() + fmt.Println("Top English words inserted into the database.") +} + +func insertEnglishWords(db *sql.DB, dictFilePtr *string) { // Read the contents of the shuf american-english file content, err := ioutil.ReadFile(*dictFilePtr) if err != nil { @@ -78,12 +94,12 @@ func main() { words := strings.Fields(string(content)) // Insert american-english words into the database - tx, err = db.Begin() + tx, err := db.Begin() if err != nil { log.Fatal(err) } - stmt, err = tx.Prepare("INSERT INTO words(word) VALUES(?)") + stmt, err := tx.Prepare("INSERT INTO words(word) VALUES(?)") if err != nil { log.Fatal(err) } @@ -102,6 +118,62 @@ func main() { fmt.Println("American-English words inserted into the database.") } +func main() { + help := flag.Bool("help", false, "print a short usage message") + dbPtr := flag.String("db", "./english_words.db", "SQLite3 DB file to use") + dictFilePtr := flag.String("dict", "./rnd-english", "Dictionary file to use") + bDbUpdatePtr := flag.Bool("db-update", false, "Only update the database") + bSkipShufflePtr := flag.Bool("skip-shuffle", false, "Skip shuffle") + bCompressionPtr := flag.Bool("compression", false, "Use High Freq Words to compress") + bSymbolsLastPtr := flag.Bool("symbols-last", false, "Use symbols last") + flag.Parse() + + if *help { + usage() + return + } + + // 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) + } + + if *bDbUpdatePtr { + insertEnglishWords(db, dictFilePtr) + return + } + + if ! *bSkipShufflePtr { + fmt.Println("Shuffling.....") + // Randomize high Freq. words + shuffleArray(topEnglishWords) + shuffleArray(commonSymbols) + } + + if ! *bSymbolsLastPtr { + insertSymbols(db) + } + + if *bCompressionPtr { + insertCommonEnglishWords(db) + } + + insertEnglishWords(db, dictFilePtr) + + if *bSymbolsLastPtr { + insertSymbols(db) + } + +} + func shuffleArray(arr []string) { n := len(arr) for i := n - 1; i > 0; i-- { @@ -112,3 +184,26 @@ func shuffleArray(arr []string) { arr[i], arr[j] = arr[j], arr[i] } } + +func usage() { + progName := filepath.Base(os.Args[0]) + fmt.Printf(`%s version %s, (c) 2024 Bob +PS: Don't forget to shuffle the Dict file $ shuf /usr/share/dict/american-english > rnd-english +Usage: +%s [-help] [-db English.db] [-dict rnd-english] [-shuffle] [-compression] [-later] + + -help Print this help message. + + -db SQLite3 Database to save to + + -dict Dictionary file to use + + -db-update Only update the database, skip adding high-freq words and symbols + + -skip-shuffle Skip shuffling High-Freq. words around + + -compression Compress using high Frequency Words List, less secure + + -symbols-last Use symbols last, gives away size of dict! +`, progName, version, progName) +} diff --git a/readData.go b/readData.go index 20d0cdc..eac3795 100644 --- a/readData.go +++ b/readData.go @@ -1,6 +1,8 @@ package main import ( + "path/filepath" + "os" "database/sql" "flag" "bufio" @@ -8,11 +10,12 @@ import ( "log" "strings" "strconv" - "os" _ "github.com/mattn/go-sqlite3" ) +var version = "0.0.1" + func containsSymbol(input string, symbols []string) bool { for _, symbol := range symbols { if strings.Contains(input, symbol) { @@ -24,10 +27,16 @@ func containsSymbol(input string, symbols []string) bool { func main() { // Define a command-line flags + help := flag.Bool("help", false, "print a short usage message") dbPtr := flag.String("db", "./english_words.db", "SQLite3 DB file to use") - filenamePtr := flag.String("file", "results.txt", "Name of the file to write results to") + filenamePtr := flag.String("file", "results.txt", "Name of the file to read results from") pwdPtr := flag.String("pwd", "", "Enter a password") - flag.Parse() + flag.Parse() + + if *help { + usage() + return + } // Open the SQLite3 database file db, err := sql.Open("sqlite3", *dbPtr) @@ -146,3 +155,17 @@ func xorBy13(input string) string { return result.String() } + +func usage() { + progName := filepath.Base(os.Args[0]) + fmt.Printf(`%s version %s, (c) 2024 Bob + +Usage: +%s [-help] [-db english_words.db] [-file results.txt] [-pwd MyPassword] + + -db SQLite3 DB file to use + -file Name of the file to read results from + -pwd Enter a password + +`, progName, version, progName) +} diff --git a/writeData.go b/writeData.go index 24e522c..f36a5ff 100644 --- a/writeData.go +++ b/writeData.go @@ -1,23 +1,32 @@ package main import ( + "path/filepath" + "os" "database/sql" "flag" "fmt" "log" "strings" - "os" "strconv" _ "github.com/mattn/go-sqlite3" ) +var version = "0.0.1" + func main() { // Define a command-line flags + help := flag.Bool("help", false, "print a short usage message") dbPtr := flag.String("db", "./english_words.db", "SQLite3 DB file to use") filenamePtr := flag.String("file", "results.txt", "Name of the file to write results to") pwdPtr := flag.String("pwd", "", "Enter a password") - flag.Parse() + flag.Parse() + + if *help { + usage() + return + } // Open the SQLite3 database file db, err := sql.Open("sqlite3", *dbPtr) @@ -165,3 +174,17 @@ func compressRLE(input string) string { return result } + +func usage() { + progName := filepath.Base(os.Args[0]) + fmt.Printf(`%s version %s, (c) 2024 Bob + +Usage: +%s [-help] [-db english_words.db] [-file results.txt] [-pwd MyPassword] + + -db SQLite3 DB file to use + -file Name of the file to write results to + -pwd Enter a password + +`, progName, version, progName) +}