-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
317 lines (246 loc) · 11.4 KB
/
Program.cs
File metadata and controls
317 lines (246 loc) · 11.4 KB
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
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
using Tesseract;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Media;
#pragma warning disable CS0219 // Warning for unused variables, as we're commenting out things.
#pragma warning disable CS1998 // Warning for async method without await, as we're commenting out things.
#pragma warning disable CA1416 // Warning for reachable on all platforms, as we're commenting out things.
#pragma warning disable CS8602 // Warning for dereferencing a possibly null reference.
public class Program
{
[DllImport("user32.dll")]
public static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
static IntPtr windowHandle = IntPtr.Zero;
const int maxChunkSizeKb = 10;
const int chunksAtATimeToKeepToRateLimit = 5;
static OpenAiChatBookService? chatService;
public static async Task Main()
{
// --- Set below values accordingly for each book you want to shorten. ---
const string bookName = "SomeBookName";
const int maxPages = 64;
// keyPath should point to a text file containing your OpenAI API key.
// Note that this will make API requests that cost money, use at your own risk.
chatService = new OpenAiChatBookService(keyPath: @"D:\_misc\openai-key.txt");
// chatService.summaryLanguage = "German";
// chatService.translationLanguage = "German";
// chatService.additionalSummaryInstructions = "Write in the style of Douglas Adams.";
// chatService.addSummaryHeadlines = true;
// chatService.addSummaryImages = true;
const string projectFolder = "D:\\Shortbook";
const string dataFolder = projectFolder + "\\Data\\" + bookName;
const string screenshotsFolder = dataFolder + "\\Screenshots";
const string textFile = dataFolder + "\\Text.txt";
const string tessdataFolder = projectFolder + "\\tessdata";
const string summariesFolder = dataFolder + "\\Summaries";
const string summariesFolderGerman = dataFolder + "\\SummariesGerman";
const string originalsFolder = dataFolder + "\\Originals";
const string allFile = dataFolder + "\\Summary.txt";
const string allFileGerman = dataFolder + "\\SummaryGerman.txt";
System.IO.Directory.CreateDirectory(screenshotsFolder);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// --- Use below one by one, as this still requires some hand-holding and checks. ---
// SavePagesAsImages(screenshotsFolder, maxPages);
// SaveTextOfImages(tessdataFolder, screenshotsFolder, textFile, maxPages, TesseractLanguage.Languages.English);
// await SaveSummariesOfText(textFile, originalsFolder, summariesFolder);
// CombineSummariesIntoSingleFile(summariesFolder, allFile);
// Note instead of translating the summaries (as defined by chatService.translationLanguage),
// you can also directly have the summaries be generated in your target language (as defined
// by chatService.summaryLanguage).
// CombineSummariesIntoSingleFile(summariesFolderGerman, allFileGerman);
}
Console.WriteLine("Done.");
}
static async Task TranslateSummaries(string summariesFolder, string summariesFolderGerman)
{
Directory.CreateDirectory(summariesFolderGerman);
var files = Directory.GetFiles(summariesFolder).OrderBy(f => int.Parse(Path.GetFileNameWithoutExtension(f))).ToArray();
for (int chunkFrom = 0; chunkFrom < files.Length; chunkFrom += chunksAtATimeToKeepToRateLimit)
{
var tasks = new List<Task>();
int chunkTo = chunkFrom + chunksAtATimeToKeepToRateLimit;
for (int i = chunkFrom; i < chunkTo && i < files.Length; i++)
{
var file = files[i];
string fileName = Path.GetFileName(file);
string translationPath = Path.Combine(summariesFolderGerman, fileName);
if (!File.Exists(translationPath))
{
tasks.Add(TranslateFile(file, translationPath));
}
}
await Task.WhenAll(tasks);
}
}
static async Task TranslateFile(string filePath, string translationPath)
{
Console.WriteLine($"Translating \"{filePath}\" into \"{translationPath}\"");
string summary = await File.ReadAllTextAsync(filePath);
string translation = await chatService.GetTranslation(summary);
await File.WriteAllTextAsync(translationPath, translation);
}
static void CombineSummariesIntoSingleFile(string summariesFolder, string allFile)
{
Console.WriteLine("Starting CombineSummaries...");
var files = Directory.GetFiles(summariesFolder).OrderBy(f => int.Parse(Path.GetFileNameWithoutExtension(f)));
var summaries = new List<string>();
foreach (var file in files)
{
string fileName = Path.GetFileNameWithoutExtension(file);
if (int.TryParse(fileName, out int _))
{
string summary = File.ReadAllText(file);
summary = summary.Replace("[...]", "...");
summary = summary.Replace("(...)", "...");
summaries.Add(summary);
}
}
File.WriteAllText(allFile, string.Join("\n\n", summaries));
}
static async Task SaveSummariesOfText(string textFile, string originalsFolder, string summariesFolder)
{
Console.WriteLine("Starting SaveSummariesOfText...");
string text = await File.ReadAllTextAsync(textFile);
Console.WriteLine("Got text...");
string[] chunks = SplitBookIntoChunks(text);
Console.WriteLine("Got chunks...");
await SummarizeBookChunks(originalsFolder, summariesFolder, chunks);
}
static async Task SummarizeBookChunks(string originalsFolder, string summariesFolder, string[] bookChunks)
{
// bookChunks = new string[] { bookChunks[0] };
for (int chunkFrom = 0; chunkFrom < bookChunks.Length; chunkFrom += chunksAtATimeToKeepToRateLimit)
{
var tasks = new List<Task>();
int chunkTo = chunkFrom + chunksAtATimeToKeepToRateLimit;
for (int i = chunkFrom; i < chunkTo && i < bookChunks.Length; i++)
{
string chunk = bookChunks[i];
string fileName = (i + 1) + ".txt";
if (!File.Exists(summariesFolder + "\\" + fileName))
{
var task = SummarizeAndWriteFile(chunk, originalsFolder, summariesFolder, fileName, i + 1, bookChunks.Length);
tasks.Add(task);
}
}
await Task.WhenAll(tasks);
}
Console.WriteLine("SummarizeBookChunks done!");
PlaySuccessSound();
}
static void PlaySuccessSound()
{
const string soundPath = ".\\sounds\\success.wav";
using (var soundPlayer = new SoundPlayer(soundPath))
{
Console.WriteLine($"Playing success sound: {soundPath}");
soundPlayer.PlaySync();
}
}
static async Task SummarizeAndWriteFile(string chunk, string originalsFolder, string summariesFolder, string fileName, int chunkNumber, int totalChunks)
{
Directory.CreateDirectory(originalsFolder);
Directory.CreateDirectory(summariesFolder);
Console.WriteLine($"Summarizing {chunkNumber} of {totalChunks}");
await File.WriteAllTextAsync(originalsFolder + "\\" + fileName, chunk);
string summary = await chatService.GetSummary(chunk);
await File.WriteAllTextAsync(summariesFolder + "\\" + fileName, summary);
}
static string[] SplitBookIntoChunks(string text)
{
string[] sections = text.Split(new string[] { "\n\n" }, StringSplitOptions.None);
List<string> chunks = new List<string>();
StringBuilder currentChunk = new StringBuilder();
foreach (string section in sections)
{
if (Encoding.UTF8.GetByteCount(currentChunk.ToString() + "\n\n" + section) / 1024.0 > maxChunkSizeKb)
{
chunks.Add(currentChunk.ToString());
currentChunk.Clear();
}
if (currentChunk.Length > 0)
{
currentChunk.Append("\n\n");
}
currentChunk.Append(section);
}
if (currentChunk.Length > 0)
{
chunks.Add(currentChunk.ToString());
}
return chunks.ToArray();
}
static void SavePagesAsImages(string folder, int maxPages)
{
const bool overwriteOld = false;
EnumWindows(FindAndFrontKindleWindow, IntPtr.Zero);
var sim = new WindowsInput.InputSimulator();
for (int i = 1; i <= maxPages; i++)
{
if (i > 1)
{
sim.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.RIGHT);
System.Threading.Thread.Sleep(100);
}
string filePath = folder + "\\" + i + ".png";
if (overwriteOld || !System.IO.File.Exists(filePath))
{
var image = ScreenCapture.CaptureDesktop(56, 102, 1905, 1030);
image.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
}
}
}
static void SaveTextOfImages(string tessdataFolder, string screenshotsFolder, string textFile, int maxPages, TesseractLanguage.Languages language = TesseractLanguage.Languages.English)
{
var ocr = new Tesseract.TesseractEngine(
tessdataFolder, TesseractLanguage.GetLanguageCode(language), Tesseract.EngineMode.Default
);
var sb = new StringBuilder();
for (int i = 1; i <= maxPages; i++)
{
string filePath = screenshotsFolder + "\\" + i + ".png";
var img = Pix.LoadFromFile(filePath);
using (var page = ocr.Process(img))
{
sb.AppendLine(page.GetText());
}
Console.WriteLine("OCR page " + i + "/ " + maxPages + " done.");
}
System.IO.File.WriteAllText(textFile, sb.ToString());
}
public static bool FindAndFrontKindleWindow(IntPtr hWnd, IntPtr lParam)
{
const string titlePartToMatch = "Kindle for PC";
int size = GetWindowTextLength(hWnd);
if (size++ > 0 && IsWindowVisible(hWnd))
{
StringBuilder sb = new StringBuilder(size);
GetWindowText(hWnd, sb, size);
if (sb.ToString().Contains(titlePartToMatch))
{
SetForegroundWindow(hWnd);
windowHandle = hWnd;
SetForegroundWindow(hWnd);
return false;
}
}
return true;
}
}