Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Andronov Alexander #178

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions TagCloud.ConsoleClient/AppOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public class AppOptions
{
public DomainOptions DomainOptions { get; set; }
}
83 changes: 83 additions & 0 deletions TagCloud.ConsoleClient/AppOptionsCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Microsoft.Extensions.Configuration;
using System.Drawing;

public static class AppOptionsCreator
{
public static AppOptions CreateOptions(Options clOptions, IConfiguration defaultConfig)
{
var tagCloudOptions = CreateTagCloudOptions(clOptions, defaultConfig);
var renderOptions = CreateRenderOptions(clOptions, defaultConfig);
var wordExtractorOptions = CreateWordExtractionOptions(clOptions, defaultConfig);
var serviceOptions = CreateServiceOptions(clOptions, defaultConfig);

return new AppOptions()
{
DomainOptions = new DomainOptions()
{
WordExtractionOptions = wordExtractorOptions,
RenderOptions = renderOptions,
TagCloudOptions = tagCloudOptions,
ServiceOptions = serviceOptions
}
};
}

private static TagCloudOptions CreateTagCloudOptions(Options options, IConfiguration defaultConfig)
{
return new TagCloudOptions
{
Center = ParsePoint(options.TagCloudCenter ?? defaultConfig["CenterPoint"]),
MaxTagsCount = -1
};
}

private static RenderOptions CreateRenderOptions(Options options, IConfiguration defaultConfig)
{
var fontSizeSpan = ParseFontSize(options.FontSize ?? defaultConfig["FontSettings:FontSize"]);

return new RenderOptions()
{
ColorScheme = ColorScheme.Schemes[defaultConfig["ColorScheme"]],
FontBase = CreateFontBase(options.FontFamily ?? defaultConfig["FontSettings:FontFamily"]),
ImageSize = ParseSize(options.ImageSize ?? defaultConfig["ImageSize"]),
MinFontSize = fontSizeSpan.min,
MaxFontSize = fontSizeSpan.max,
};
}

private static Font CreateFontBase(string fontFamily)
{
return new Font(fontFamily, 32, FontStyle.Regular, GraphicsUnit.Pixel);
}

private static WordExtractionOptions CreateWordExtractionOptions(Options clOptions, IConfiguration defaultConfig)
{
return new WordExtractionOptions() { MinWordLength = 4, PartsSpeech = PartSpeech.Noun | PartSpeech.Verb };
}

private static Size ParseSize(string str)
{
var coords = str.Split("x").Select(int.Parse).ToArray();
return new Size(coords[0], coords[1]);
}

private static Point ParsePoint(string str)
{
var coords = str.Split("x").Select(int.Parse).ToArray();
return new Point(coords[0], coords[1]);
}

private static (int min, int max) ParseFontSize(string str)
{
var sizes = str.Split(":").Select(int.Parse).ToArray();
return (sizes[0], sizes[1]);
}

public static ServiceOptions CreateServiceOptions(Options clOptions, IConfiguration defaultConfig)
{
return new ServiceOptions()
{
FilterType = FilterType.MorphologicalFilter
};
}
}
9 changes: 9 additions & 0 deletions TagCloud.ConsoleClient/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.Extensions.DependencyInjection;

public static class DependencyInjection
{
public static IServiceCollection AddClient(this IServiceCollection services)
{
return services;
}
}
Binary file added TagCloud.ConsoleClient/Images/big.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added TagCloud.ConsoleClient/Images/small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions TagCloud.ConsoleClient/Options.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using CommandLine;

public class Options
{
[Option('i', "input", Required = true, HelpText = "Path to input text")]
public string InputTextPath{ get; set; }

[Option('o', "output", Required = true, HelpText = "Output image path")]
public string OutputImagePath { get; set; }

[Option('c', "center", Required = false, HelpText = "Center of tag cloud (f.e. 200x200)")]
public string? TagCloudCenter { get; set; }

[Option('s', "size", Required = false, HelpText = "Size of a result image (f.e. 200x200)")]
public string? ImageSize { get; set; }

[Option('f', "fsize", Required = false, HelpText = "Fontsize span (f.e 12:32)")]
public string? FontSize { get; set; }
[Option('t', "tcount", Required = false, HelpText = "Number of tags to take")]
public string? TagsCount { get; set; }
[Option('F', "family", Required = false, HelpText = "Font family (Arial default)")]
public string? FontFamily { get; set; }
}
51 changes: 51 additions & 0 deletions TagCloud.ConsoleClient/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using CommandLine;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

internal class Program
{
private static void Main(string[] args)
{
var options = Parser.Default.ParseArguments<Options>(args);

if (options.Errors.Any())
return;

IConfiguration config =
Result.Of(() => GetConfiguration(PathFinderHelper.GetPathToFile("defaultSettings.json")))
.ReplaceError(errorMessage => "Can't load default settings \n" + errorMessage)
.GetValueOrThrow();

var appOptions = AppOptionsCreator.CreateOptions(options.Value, config);

var provider = GetServiceProvider(appOptions);

var inp = options.Value.InputTextPath;
var outp = options.Value.OutputImagePath;

var textLoader = provider.GetService<ITextLoader>();
var tagCloud = provider.GetService<ITagCloud>();
var imageStorage = provider.GetService<IImageStorage>();

var result = textLoader.Load(inp)
.Then(text => tagCloud.CreateCloud(text))
.Then(image => imageStorage.Save(image, outp))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лайк за Fluent-интерфейс

.OnFail(Console.WriteLine);
}

private static IConfiguration GetConfiguration(string pathToFile)
{
return new ConfigurationBuilder()
.AddJsonFile(pathToFile)
.Build();
}

private static ServiceProvider GetServiceProvider(AppOptions options)
{
var builder = new ServiceCollection();
builder.AddClient();
builder.AddDomain(options.DomainOptions);
builder.AddInfrastructure();
return builder.BuildServiceProvider();
}
}
8 changes: 8 additions & 0 deletions TagCloud.ConsoleClient/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"TagCloud.ConsoleClient": {
"commandName": "Project",
"commandLineArgs": "-i text.txt\r\n-o image.jpeg"
}
}
}
2 changes: 2 additions & 0 deletions TagCloud.ConsoleClient/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
![small](Images/small.png)
![big](Images/big.png)
23 changes: 23 additions & 0 deletions TagCloud.ConsoleClient/TagCloud.ConsoleClient.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="System.Drawing.Common" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TagCloud.Domain\TagCloud.Domain.csproj" />
<ProjectReference Include="..\TagCloud.Infrastructure\TagCloud.Infrastructure.csproj" />
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions TagCloud.ConsoleClient/defaultSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"InputFile": "text.txt",
"OutputFile": "tagcloud.jpeg",
"ImageSize": "8000x8000",
"CenterPoint": "4000x4000",
"FontSettings": {
"FontSize": "24:145",
"FontFamily": "Arial"
},
"ColorScheme" : "Warm"
}
6 changes: 6 additions & 0 deletions TagCloud.Domain/ColorProviders/IColorProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System.Drawing;

public interface IColorProvider
{
Result<Color> GetColor(WordLayout layout);
}
31 changes: 31 additions & 0 deletions TagCloud.Domain/ColorProviders/OpacityColorProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Drawing;


// Changes opacity of color based on font size
public class OpacityColorProvider : IColorProvider
{
private readonly RenderOptions options;

public OpacityColorProvider(RenderOptions options)
{
this.options = options;
}

public Result<Color> GetColor(WordLayout layout)
{
var baseC = options.ColorScheme.FontColor;
var baseOpacity = 0.4;
var addOpacity = 1 - baseOpacity;

var opacity = TagCloudHelpers.GetMultiplier(layout.FontSize, options.MinFontSize, options.MaxFontSize)
.Then(mul => 255 * baseOpacity + 255 * addOpacity * mul);

if (!opacity.IsSuccess)
return Result.Fail<Color>($"Can't calculate color for word {layout}");

var newColor = Color.FromArgb((int)opacity.Value, baseC.R, baseC.G, baseC.B);

return newColor;
}
}

31 changes: 31 additions & 0 deletions TagCloud.Domain/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.Extensions.DependencyInjection;

public static class DependencyInjection
{
public static IServiceCollection AddDomain(this IServiceCollection services, DomainOptions options)
{
services.AddScoped<ITagCloud, TagCloud>();
services.AddScoped<IWordExtractor, WordExtractor>();
services.AddScoped<ITagCloudRenderer, TagCloudRenderer>();
services.AddScoped<ITagCloudLayouter, TagCloudLayouter>();
services.AddScoped<IColorProvider, OpacityColorProvider>();
services.AddScoped<IOptionsValidator, OptionsValidator>();

switch (options.ServiceOptions.FilterType)
{
case FilterType.MorphologicalFilter:
services.AddScoped<IWordsFilter, MorphologicalFilter>();
break;
case FilterType.LengthFilter:
services.AddScoped<IWordsFilter, LengthFilter>();
break;
}

services.AddSingleton(options.TagCloudOptions);
services.AddSingleton(options.RenderOptions);
services.AddSingleton(options.WordExtractionOptions);
services.AddSingleton(options);

return services;
}
}
16 changes: 16 additions & 0 deletions TagCloud.Domain/Helpers/PathFinderHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public static class PathFinderHelper
{
public static string GetPathToFile(string fileName)
{
#if DEBUG
var projectPath = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.Parent.FullName;

return Directory.GetFiles(
Directory.GetParent(projectPath).FullName,
fileName, SearchOption.AllDirectories)[0];
#else
return fileName;
#endif
}
}

7 changes: 7 additions & 0 deletions TagCloud.Domain/Helpers/TagCloudHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public static class TagCloudHelpers
{
public static Result<double> GetMultiplier(int value, int min, int max)
{
return Result.Of(() => ((double)value - min) / (max - min));
}
}
6 changes: 6 additions & 0 deletions TagCloud.Domain/Layouters/ITagCloudLayouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System.Drawing;

public interface ITagCloudLayouter
{
Result<Rectangle> PutNextRectangle(Size size);
}
Loading