-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquotes1.go
119 lines (99 loc) · 3.1 KB
/
quotes1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
)
type Speakers map[string]int
func processChapters(baseDir string) error {
// Read speakers from counts.json
speakersFile := filepath.Join(baseDir, "vaktāsaha", "counts.json")
speakersData, err := ioutil.ReadFile(speakersFile)
if err != nil {
return fmt.Errorf("error reading speakers file: %v", err)
}
var speakers Speakers
if err := json.Unmarshal(speakersData, &speakers); err != nil {
return fmt.Errorf("error parsing speakers JSON: %v", err)
}
// Sort speakers by line count (descending)
speakerKeys := make([]string, 0, len(speakers))
for speaker := range speakers {
speakerKeys = append(speakerKeys, strings.TrimSpace(speaker))
}
sort.Slice(speakerKeys, func(i, j int) bool {
return speakers[speakerKeys[i]] > speakers[speakerKeys[j]]
})
// Process UR text files
byChaptersDir := filepath.Join(baseDir, "decomposed", "by_chapters")
outputDir := filepath.Join(baseDir, "by_speaker")
os.MkdirAll(outputDir, 0755)
return filepath.Walk(byChaptersDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Process only UR text files
if !info.IsDir() && strings.HasSuffix(path, "_ur.txt") {
// Read file content
content, err := ioutil.ReadFile(path)
if err != nil {
return fmt.Errorf("error reading file %s: %v", path, err)
}
// Prepare quotes map
quotes := make(map[int]string)
lines := strings.Split(string(content), "\n")
quoteCounter := 1
var currentQuote strings.Builder
for _, line := range lines {
matchedSpeaker := ""
for _, speaker := range speakerKeys {
if strings.Contains(line, speaker) {
matchedSpeaker = speaker
break
}
}
if matchedSpeaker != "" {
// Save previous quote if exists
if currentQuote.Len() > 0 {
quotes[quoteCounter] = strings.TrimSpace(currentQuote.String())
quoteCounter++
currentQuote.Reset()
}
// Start new quote
currentQuote.WriteString(line + "\n")
} else if currentQuote.Len() > 0 {
currentQuote.WriteString(line + "\n")
}
}
// Save last quote
if currentQuote.Len() > 0 {
quotes[quoteCounter] = strings.TrimSpace(currentQuote.String())
}
// Determine output path
relPath, _ := filepath.Rel(byChaptersDir, path)
bookDir := filepath.Dir(filepath.Dir(relPath))
outputPath := filepath.Join(outputDir, bookDir, "quotes.json")
// Ensure directory exists
os.MkdirAll(filepath.Dir(outputPath), 0755)
// Write quotes to JSON
if len(quotes) > 0 {
quotesJSON, _ := json.MarshalIndent(quotes, "", " ")
if err := ioutil.WriteFile(outputPath, quotesJSON, 0644); err != nil {
return fmt.Errorf("error writing quotes file: %v", err)
}
fmt.Printf("Created quotes file: %s\n", outputPath)
}
}
return nil
})
}
func main() {
baseDir := `D:\Projects\oper`
if err := processChapters(baseDir); err != nil {
fmt.Printf("Error: %v\n", err)
}
}