-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathConsoleClient.cs
More file actions
44 lines (42 loc) · 1.57 KB
/
ConsoleClient.cs
File metadata and controls
44 lines (42 loc) · 1.57 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
using ResultOf;
using TagsCloudContainer.Core.Infrastructure.Analysis;
using TagsCloudContainer.Core.Infrastructure.Layout;
using TagsCloudContainer.Core.Infrastructure.Preprocessing;
using TagsCloudContainer.Core.Infrastructure.Reading;
using TagsCloudContainer.Core.Infrastructure.Rendering;
using TagsCloudContainer.Core.Models;
namespace TagsCloudContainer.Console;
public class ConsoleClient(
ITextReader textReader,
ITextPreprocessor preprocessor,
IWordFrequencyAnalyzer analyzer,
ITagCloudAlgorithm algorithm,
ITagCloudRenderer renderer)
{
public Result<None> GenerateTagCloud(
string inputFile,
string outputFile,
LayoutOptions options)
{
System.Console.WriteLine($"Reading words from {inputFile}...");
return textReader.ReadWords(inputFile)
.Then(preprocessor.Process)
.Then(EnsureNotEmpty)
.Then(analyzer.Analyze)
.Then(freqs =>
algorithm.Arrange(freqs, options))
.Then(layout =>
{
System.Console.WriteLine($"Rendering image ({options.Width} x{options.Height})...");
return renderer.Render(layout, options);
})
.Then(image => renderer.SaveToFile(image, outputFile))
.RefineError("Tag cloud generation failed");
}
private static Result<IReadOnlyList<string>> EnsureNotEmpty(IReadOnlyList<string> words)
{
return words.Count == 0
? Result.Fail<IReadOnlyList<string>>("No words to build a tag cloud")
: Result.Ok(words);
}
}