-
Notifications
You must be signed in to change notification settings - Fork 201
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
batyadmx
wants to merge
5
commits into
kontur-courses:master
Choose a base branch
from
batyadmx:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Andronov Alexander #178
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
public class AppOptions | ||
{ | ||
public DomainOptions DomainOptions { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
.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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
![small](Images/small.png) | ||
![big](Images/big.png) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Лайк за Fluent-интерфейс