Skip to content

Commit

Permalink
Fix --speak-diacritics
Browse files Browse the repository at this point in the history
  • Loading branch information
C-Loftus committed Oct 12, 2024
1 parent 5b5a73a commit ba16e7a
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 9 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
github: [c-loftus]
custom: ["https://www.paypal.com/paypalme/coltonloftus"]
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"editor.formatOnType": true,
// hide git ignored files
"explorer.excludeGitIgnore": false
"explorer.excludeGitIgnore": false,
"go.testFlags": ["-count=1"] // disable caching for go tests
}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# docker build -t quickpiperaudiobook .
# docker run quickpiperaudiobook /app/examples/lorem_ipsum.txt

FROM --platform=linux/amd64 golang:latest as build
FROM --platform=linux/amd64 golang:1.22 as build

WORKDIR /app

Expand Down
3 changes: 3 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ func RunCLI() {
if err != nil {
fmt.Printf("Error: %v\n", err)
ctx.FatalIfErrorf(err)
} else if data == nil {
fmt.Println("After converting" + cli.Input + "to txt, no data was generated.")
return
} else {
fmt.Println("Text conversion completed successfully.")
}
Expand Down
8 changes: 8 additions & 0 deletions cli/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@ func TestCLI(t *testing.T) {
os.Args = append(os.Args, "https://example-files.online-convert.com/document/txt/example.txt")
RunCLI()
}

func TestCLIWithDiacritics(t *testing.T) {
// reset all cli args, since the golang testing framework changes them
os.RemoveAll(configDir)
os.Args = os.Args[:1]
os.Args = append(os.Args, "https://example-files.online-convert.com/document/txt/example.txt", "--speak-diacritics")
RunCLI()
}
7 changes: 5 additions & 2 deletions lib/piper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ import (
"github.com/gen2brain/beeep"
)

func RunPiper(filename string, // we need to have the filename here since the file passed in is a tmp file and a dummy name
model string, file *os.File, outdir string) error {
func RunPiper(
filename string, // filename must be specified since the file passed in is a tmp file and a dummy name
model string, // piper model used
file *os.File, // file with text to convert
outdir string) error {

// Debugging: Read file content to check if it's empty
fileContent, err := io.ReadAll(file)
Expand Down
6 changes: 2 additions & 4 deletions lib/textProcessing.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (

func RemoveDiacritics(file *os.File) (*os.File, error) {
if file == nil {
return nil, fmt.Errorf("file is nil")
return nil, fmt.Errorf("file to remove diacritics from is nil")
}

// Create a temporary file for the output
tmpFile, err := os.CreateTemp("", "diacritics-removed-*.txt")
tmpFile, err := os.CreateTemp("/tmp/", "diacritics-removed-*.txt")
if err != nil {
return nil, fmt.Errorf("failed to create temporary file: %v", err)
}
Expand Down Expand Up @@ -106,8 +106,6 @@ func GetConvertedRawText(inputPath string) (*os.File, error) {
if err != nil {
return nil, fmt.Errorf("failed to create temporary file: %v", err)
}
defer tmpFile.Close()

c := color.New(color.Bold, color.FgMagenta)
c.Println("Converting " + filepath.Base(inputPath) + " to the proper intermediary text format...")

Expand Down
38 changes: 37 additions & 1 deletion lib/textProcessing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lib

import (
"os"
"path/filepath"
"testing"
)

Expand Down Expand Up @@ -88,7 +89,42 @@ func TestRemoveDiacritics_FileNotExist(t *testing.T) {
t.Fatal("expected error but got nil")
}

if err.Error() != "file is nil" {
if err.Error() != "file to remove diacritics from is nil" {
t.Errorf("unexpected error: got %v, want %v", err.Error(), "file is nil")
}
}

func TestPiperWithRemovedDiacritics(t *testing.T) {
homeDir, _ := os.UserHomeDir()
path := filepath.Join(homeDir, ".config/QuickPiperAudiobook")

if !PiperIsInstalled(path) {
if err := InstallPiper(path); err != nil {
t.Fatalf("error installing piper: %v", err)
}
}

model := "en_US-hfc_male-medium.onnx"
if err := DownloadModelIfNotExists(model, "."); err != nil {
t.Fatalf("error grabbing model: %v", err)
}

// read in test_diacritics.txt
inputFile, err := os.Open("test_diacritics.txt")
if err != nil {
t.Fatalf("failed to open input file: %v", err)
}
defer inputFile.Close()

// Call RemoveDiacritics function
outputFile, err := RemoveDiacritics(inputFile)
if err != nil {
t.Fatalf("RemoveDiacritics failed: %v", err)
}
defer os.Remove(outputFile.Name())

if err := RunPiper("test_diacritics.txt", model, outputFile, "/tmp/"); err != nil {
t.Fatalf("error running piper: %v", err)
}

}

0 comments on commit ba16e7a

Please sign in to comment.