-
Notifications
You must be signed in to change notification settings - Fork 2
/
analyzer.go
368 lines (334 loc) · 8.52 KB
/
analyzer.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
Project Apario is the World's Truth Repository that was invented and started by Andrei Merlescu in 2020.
Copyright (C) 2023 Andrei Merlescu
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"bufio"
"context"
"fmt"
"log"
"os"
"strings"
`sync`
"time"
cwg `github.com/andreimerlescu/go-countable-waitgroup`
)
func analyze_StartOnFullText(ctx context.Context, pp PendingPage) {
defer func() {
pp_save(pp)
wg_active_tasks.Done()
if ch_AnalyzeCryptonyms.CanWrite() {
err := ch_AnalyzeCryptonyms.Write(pp)
if err != nil {
log.Printf("cant write to the ch_AnalyzeCryptonyms channel due to error %v", err)
return
}
}
}()
file, fileErr := os.ReadFile(pp.OCRTextPath)
if fileErr != nil {
log.Printf("Error opening file %q: %v\n", pp.OCRTextPath, fileErr)
return
}
pp.Dates = extractDates(string(file))
}
func analyzeCryptonyms(ctx context.Context, pp PendingPage) {
defer func() {
pp_save(pp)
wg_active_tasks.Done()
if ch_AnalyzeLocations.CanWrite() {
err := ch_AnalyzeLocations.Write(pp)
if err != nil {
log.Printf("cannot write to the ch_AnalyzeLocations channel due to error %v", err)
return
}
}
}()
var result []string
file, fileErr := os.ReadFile(pp.OCRTextPath)
if fileErr != nil {
log.Printf("Error opening file %q: %v\n", pp.OCRTextPath, fileErr)
return
}
for key := range m_cryptonyms {
if strings.Contains(string(file), key) {
result = append(result, key)
}
}
pp.Cryptonyms = result
}
func analyzeLocations(ctx context.Context, pp PendingPage) {
defer func() {
pp_save(pp)
wg_active_tasks.Done()
if ch_AnalyzeGematria.CanWrite() {
err := ch_AnalyzeGematria.Write(pp)
if err != nil {
log.Printf("cant write to the ch_AnalyzeGematria channel due to error %v", err)
return
}
}
}()
for {
if a_b_locations_loaded.Load() {
break
}
select {
case <-time.After(9 * time.Second):
log.Printf("waiting for locations to finish loading before running analyzeLocations(%v)", pp.OCRTextPath)
continue
case <-ctx.Done():
return
}
}
done := make(chan Geography)
go func() {
var geography = Geography{
Countries: []CountableLocation{},
States: []CountableLocation{},
Cities: []CountableLocation{},
}
var mu_geography = sync.Mutex{}
defer func() {
done <- geography
close(done)
}()
b_fullText, fileErr := os.ReadFile(pp.OCRTextPath)
if fileErr != nil {
log.Printf("Error opening file %q: %v\n", pp.OCRTextPath, fileErr)
return
}
fullText := strings.ToLower(string(b_fullText))
wg := cwg.CountableWaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
log.Printf("checking pp PDF %v location countries inside a total of %d countries", pp.OCRTextPath, len(m_location_countries))
var e_countries = make(map[string]*Location)
for _, country := range m_location_countries {
c := strings.ToLower(country.Country)
if len(c) == 0 {
continue
}
if strings.Contains(fullText, c) {
if _, ok := e_countries[c]; !ok {
mu_geography.Lock()
geography.Countries = append(geography.Countries, CountableLocation{
Location: country,
Quantity: strings.Count(fullText, c),
})
e_countries[c] = country
mu_geography.Unlock()
}
}
}
}()
wg.Add(1)
go func() {
defer wg.Done()
log.Printf("checking pp PDF %v location states inside a total of %d states", pp.OCRTextPath, len(m_location_states))
var e_states = make(map[string]*Location)
for _, state := range m_location_states {
s := strings.ToLower(state.State)
if len(s) == 0 {
continue
}
if strings.Contains(fullText, s) {
if _, ok := e_states[s]; !ok {
mu_geography.Lock()
geography.States = append(geography.States, CountableLocation{
Location: state,
Quantity: strings.Count(fullText, s),
})
e_states[s] = state
mu_geography.Unlock()
}
}
}
}()
wg.Add(1)
go func() {
defer wg.Done()
log.Printf("checking pp PDF %v location cities inside a total of %d cities", pp.OCRTextPath, len(m_location_cities))
var e_cities = make(map[string]*Location)
for _, city := range m_location_cities {
c := strings.ToLower(city.City)
if len(c) == 0 {
continue
}
if strings.Contains(fullText, c) {
if _, ok := e_cities[c]; !ok {
mu_geography.Lock()
geography.Cities = append(geography.Cities, CountableLocation{
Location: city,
Quantity: strings.Count(fullText, c),
})
e_cities[c] = city
mu_geography.Unlock()
}
}
}
}()
wg.PreventAdd()
wg.Wait()
return
}()
for {
select {
case <-ctx.Done():
return
case geography, opened := <-done:
if opened {
pp.Geography = geography
}
return
}
}
}
func analyzeGematria(ctx context.Context, pp PendingPage) {
defer func() {
pp_save(pp)
wg_active_tasks.Done()
if ch_AnalyzeDictionary.CanWrite() {
err := ch_AnalyzeDictionary.Write(pp)
if err != nil {
log.Printf("cant write to the ch_AnalyzeDictionary channel due to error %v", err)
return
}
}
}()
for {
if a_b_dictionary_loaded.Load() {
break
}
select {
case <-time.After(9 * time.Second):
log.Printf("waiting for word dictionary to finish loading before running analyzeGematria(%v)", pp.OCRTextPath)
continue
case <-ctx.Done():
return
}
}
done := make(chan struct{})
var fileResults = map[string][]WordResult{}
go func() {
defer func() {
done <- struct{}{}
close(done)
}()
file, fileErr := os.Open(pp.OCRTextPath)
if fileErr != nil {
log.Printf("Error opening file %q: %v\n", pp.OCRTextPath, fileErr)
return
}
defer func() {
file.Close()
}()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
select {
case <-ctx.Done():
return
default:
line := scanner.Text()
words := strings.Fields(line)
for _, word := range words {
for language, dictionary := range m_language_dictionary {
if _, ok := dictionary[word]; ok {
wr := WordResult{
Gematria: Gematria{word, NewGemScore(word)},
}
_, found := fileResults[language]
if !found {
fileResults[language] = []WordResult{}
}
fileResults[language] = append(fileResults[language], wr)
}
}
}
}
}
if err := scanner.Err(); err != nil {
log.Println(err)
}
return
}()
for {
select {
case <-ctx.Done():
return
case <-done:
output := fmt.Sprintf("Words in OCR file %v", pp.OCRTextPath)
var languages = map[string]int{}
var selectedLanguage string
var totalWords int
var twMu = sync.Mutex{}
for language, results := range fileResults {
languages[language] = len(results)
}
for language, count := range languages {
if len(selectedLanguage) == 0 || count > totalWords {
twMu.Lock()
selectedLanguage = language
totalWords = count
twMu.Unlock()
}
}
pp.Language = selectedLanguage
pp.Words = fileResults[selectedLanguage]
var deDupWords = map[string]WordResult{}
for _, wr := range pp.Words {
if _, exists := deDupWords[wr.Word]; !exists {
deDupWords[wr.Word] = wr
}
}
var cleanedWords []WordResult
for _, wr := range deDupWords {
cleanedWords = append(cleanedWords, wr)
}
pp.Words = cleanedWords
for _, wr := range pp.Words {
output += fmt.Sprintf("-> %v (%v) = %v", wr.Word, wr.Language, wr.Gematria)
}
log.Println(output)
return
}
}
}
func analyzeWordIndexer(ctx context.Context, pp PendingPage) {
defer func() {
pp_save(pp)
wg_active_tasks.Done()
if ch_CompletedPage.CanWrite() {
err := ch_CompletedPage.Write(pp)
if err != nil {
log.Printf("cant write to the ch_CompletedPage channel due to error %v", err)
return
}
}
}()
for {
if a_b_dictionary_loaded.Load() {
break
}
select {
case <-time.After(9 * time.Second):
log.Printf("waiting for word dictionary to finish loading before running analyzeWordIndexer(%v)", pp.OCRTextPath)
continue
case <-ctx.Done():
return
}
}
return
}