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/readData.go

171 lines
3.7 KiB

package main
import (
"path/filepath"
"os"
"database/sql"
"flag"
"bufio"
"fmt"
"log"
"strings"
"strconv"
_ "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) {
return true
}
}
return false
}
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 read results from")
pwdPtr := flag.String("pwd", "", "Enter a password")
flag.Parse()
if *help {
usage()
return
}
// Open the SQLite3 database file
db, err := sql.Open("sqlite3", *dbPtr)
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Open the file
file, err := os.Open(*filenamePtr)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
symbols := []string{
".", ",", "!", ";", "?", "(", ")", "'", "\"", "...", ":", "*", "$", "@", "#", "%",
}
// Create a scanner to read the file line by line
scanner := bufio.NewScanner(file)
// Loop through each line in the file
for scanner.Scan() {
line := scanner.Text()
// Split the line by spaces
parts := strings.Fields(line)
last := ""
input := ""
// Process the parsed values (assuming each line has a specific structure)
for _, part := range parts {
if strings.Contains(part, "_") {
result := strings.Replace(part, "_", "", -1)
if (*pwdPtr != "") {
input = decompressRLE(xorStringWithPassword(result, *pwdPtr))
} else {
input = decompressRLE(xorBy13(result))
}
if containsSymbol(input, symbols) {
last = strings.TrimRight(last, " ") + fmt.Sprintf("%s ", input)
} else {
last = last + fmt.Sprintf("%s ", input)
}
} else {
query := "SELECT word FROM words WHERE id = ?"
rows, err := db.Query(query, part)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
if rows.Next() {
var word string
err = rows.Scan(&word)
if err != nil {
log.Fatal(err)
}
if containsSymbol(word, symbols) {
last = strings.TrimRight(last, " ") + fmt.Sprintf("%s ", word)
} else {
last = last + fmt.Sprintf("%s ", word)
}
}
}
}
fmt.Println(last)
}
// Check for errors during scanning
if err := scanner.Err(); err != nil {
fmt.Println("Error reading file:", err)
}
}
// decompressRLE decompresses a string that has been Run-Length Encoded
func decompressRLE(compressed string) string {
result := ""
for i := 0; i < len(compressed); i += 2 {
char := compressed[i]
count, err := strconv.Atoi(string(compressed[i+1]))
if err != nil {
// Handle error if conversion fails
fmt.Println("Error converting count to integer:", err)
return ""
}
// Repeat the character count times in the result
for j := 0; j < count; j++ {
result += string(char)
}
}
return result
}
func xorStringWithPassword(input string, password string) string {
inputBytes := []byte(input)
passwordBytes := []byte(password)
for i := 0; i < len(inputBytes); i++ {
inputBytes[i] ^= passwordBytes[i%len(passwordBytes)]
}
return string(inputBytes)
}
func xorBy13(input string) string {
var result strings.Builder
for _, char := range input {
xored := char ^ 13
result.WriteRune(xored)
}
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)
}