fast-replace/go-fast-replace/cmd/replace/main.go

108 lines
2.1 KiB
Go
Raw Normal View History

2023-12-30 21:35:09 +00:00
package main
import (
"encoding/csv"
"errors"
"fmt"
"log"
"os"
"time"
"internal/replace"
)
func main() {
run, err := getArgs()
if err != nil {
log.Fatal(err)
return
}
corpusBytes, err := os.ReadFile("./csv/corpus.csv")
if err != nil {
log.Fatal(err)
return
}
// Read the CSV file containing replacement key-value pairs
pairsHandle, err := os.Open("./csv/pairs.csv")
if err != nil {
fmt.Println("Error opening pairs:", err)
return
}
defer pairsHandle.Close()
replacements, err := csv.NewReader(pairsHandle).ReadAll()
if err != nil {
fmt.Println("Error reading pairs:", err)
return
}
corpus := string(corpusBytes)
var pairs []string
var replaced string
var startTime time.Time
var elapsedTime time.Duration
for _, algo := range run {
switch algo {
case "cw":
// Prepare
cw := replace.NewCommentzWalter()
for _, record := range replacements {
cw.AddPattern(record[0], record[1])
}
cw.BuildMatchingMachine()
// Replace
startTime = time.Now()
replaced = cw.CommentzWalterReplace(corpus)
elapsedTime = time.Since(startTime)
case "bm":
// Prepare...
for _, record := range replacements {
pairs = append(pairs, record[0], record[1])
}
// Replace
startTime = time.Now()
replaced = replace.BoyerMooreReplace(corpus, pairs)
elapsedTime = time.Since(startTime)
}
fmt.Printf("\n%s: %s\n", algo, elapsedTime)
}
// Write replaced text to a file
outputHandle, err := os.OpenFile("./csv/replaced.csv", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
fmt.Println("Error creating output file:", err)
return
}
defer outputHandle.Close()
// Write replaced text to the file
_, err = outputHandle.WriteString(replaced)
if err != nil {
fmt.Println("Error writing to file:", err)
return
}
}
func getArgs() ([]string, error) {
var run = []string{"bm", "cw"}
if len(os.Args) > 1 {
switch os.Args[1] {
case "all":
run = []string{"bm", "cw"}
case "cw":
run = []string{"cw"}
case "bm":
run = []string{"bm"}
default:
return []string{}, errors.New("Invalid argument.")
}
}
return run, nil
}