2
0
Fork 0
FLIK/list/fix-em.go

77 lines
1.5 KiB
Go
Raw Normal View History

2023-05-29 19:05:56 -04:00
package main
import (
"bufio"
"encoding/csv"
2023-05-29 19:05:56 -04:00
"fmt"
"os"
"regexp"
"strings"
)
func main() {
2023-05-30 16:52:08 -04:00
fmt.Println("Enter the name of the input file:")
var inputFileName string
_, err := fmt.Scanln(&inputFileName)
if err != nil {
panic(err)
}
2023-05-29 19:05:56 -04:00
// Open input file
2023-05-30 16:52:08 -04:00
file, err := os.Open(inputFileName)
2023-05-29 19:05:56 -04:00
if err != nil {
panic(err)
}
defer file.Close()
// Create output file
output, err := os.Create("movies.csv")
2023-05-29 19:05:56 -04:00
if err != nil {
panic(err)
}
defer output.Close()
// Initialize CSV writer
writer := csv.NewWriter(output)
defer writer.Flush()
2023-05-29 19:05:56 -04:00
// Regex to match movie title, year, and director
r := regexp.MustCompile(`(.*?)(?:,\s*(The|An|A))?\s+(\d{4})`)
2023-05-29 19:05:56 -04:00
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
// Parse line using regex
matches := r.FindStringSubmatch(line)
if len(matches) != 4 {
// Skip lines that don't match the expected format
2023-05-29 19:05:56 -04:00
continue
}
// Extract movie information
2023-05-29 19:05:56 -04:00
movieTitle := strings.TrimSpace(matches[1])
article := strings.TrimSpace(matches[2])
year := matches[3]
// Adjust movie title format
if article != "" {
2023-05-30 16:52:08 -04:00
movieTitle = fmt.Sprintf("%s %s", article, movieTitle) // Removed the comma here
}
2023-05-29 19:05:56 -04:00
// Output fixed movie title and year to terminal
fmt.Printf("Fixed: %s (%s)\n", movieTitle, year)
// Write movie title and year to CSV file
err = writer.Write([]string{movieTitle, year})
2023-05-29 19:05:56 -04:00
if err != nil {
panic(err)
}
}
// Check for errors from scanner
if err := scanner.Err(); err != nil {
panic(err)
}
}