-
Notifications
You must be signed in to change notification settings - Fork 239
Савицких Антон #210
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
Open
Xineev
wants to merge
13
commits into
kontur-courses:master
Choose a base branch
from
Xineev: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
Савицких Антон #210
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
eed1d8a
Import solution from previous task
Xineev bda3418
Implemented Result<T> pattern
Xineev 0a21c47
Updated tests with new exceptions handling pattern
Xineev 52f7aed
Implemented Result<T> pattern for more classes and added fixes
Xineev 57591ca
Made critical adjustments for classes
Xineev a1fdfe0
Updated tag cloud tests
Xineev 0ba2794
Updated CloudGenerator and Infrastructure
Xineev db85b57
Moved renderer to console client
Xineev a28b2b6
Moved renderer to UI client
Xineev ca24a76
Updated tests
Xineev 264f1a5
Updated generator, docxReader, renderer and analyzer
Xineev 8a4bf9b
Updated generator and analyzer tests
Xineev 239700a
Updated DocxReader
Xineev 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 hidden or 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,98 @@ | ||
| using CommandLine; | ||
| using System.Drawing; | ||
| using System.Drawing.Imaging; | ||
| using TagCloudGenerator.Core.Interfaces; | ||
| using TagCloudGenerator.Core.Models; | ||
| using TagCloudGenerator.Infrastructure; | ||
|
|
||
| namespace TagCloudConsoleClient | ||
| { | ||
| public class ConsoleClient : IClient | ||
| { | ||
| private readonly ITagCloudGenerator _generator; | ||
| private readonly ICollection<IFilter> _filters; | ||
| private readonly IReaderRepository _readersRepository; | ||
| private readonly INormalizer _normalizer; | ||
| private readonly IRenderer _renderer; | ||
|
|
||
| public ConsoleClient(ITagCloudGenerator generator, ICollection<IFilter> filters, IReaderRepository readers, INormalizer normalizer, IRenderer renderer) | ||
| { | ||
| _generator = generator; | ||
| _filters = filters; | ||
| _readersRepository = readers; | ||
| _normalizer = normalizer; | ||
| _renderer = renderer; | ||
| } | ||
|
|
||
| public void Run(string[] args) | ||
| { | ||
| Parser.Default.ParseArguments<Options>(args) | ||
| .WithParsed(RunWithOptions) | ||
| .WithNotParsed(errors => { }); | ||
| } | ||
|
|
||
| private void RunWithOptions(Options opts) | ||
| { | ||
| if (!File.Exists(opts.InputFile)) | ||
| { | ||
| Console.WriteLine($"Error: input file '{opts.InputFile}' does not exist."); | ||
| return; | ||
| } | ||
|
|
||
| var canvasSettings = new CanvasSettings() | ||
| .SetSize(opts.Width, opts.Height) | ||
| .SetBackgroundColor(TryParseColor(opts.BackgroundColor)) | ||
| .WithShowRectangles() | ||
| .SetPadding(opts.Padding); | ||
|
|
||
| var textSettings = new TextSettings() | ||
| .SetFontFamily(opts.FontFamily) | ||
| .SetFontSizeRange(opts.MinFontSize, opts.MaxFontSize) | ||
| .SetTextColor(TryParseColor(opts.TextColor)); | ||
|
|
||
| var inputFile = opts.InputFile; | ||
| var outputFile = opts.OutputFile; | ||
|
|
||
| Console.WriteLine("Starting tag cloud generation..."); | ||
| Console.WriteLine($"Input file: {inputFile}"); | ||
| Console.WriteLine($"Output file: {outputFile}"); | ||
|
|
||
|
|
||
| _readersRepository.TryGetReader(inputFile) | ||
| .Then(reader => reader.TryRead(inputFile)) | ||
| .Then(words => _normalizer.Normalize(words)) | ||
| .Then(words => _generator.Generate(words, canvasSettings, textSettings, _filters)) | ||
| .Then(items => _renderer.Render(items, canvasSettings, textSettings)) | ||
| .Then(image => Result.OfAction(() => | ||
| { | ||
| image.Save(outputFile, ImageFormat.Png); | ||
| image.Dispose(); | ||
| }, "Failed to save output image")) | ||
| .OnFail(error => | ||
| { | ||
| Console.WriteLine("Error during generation:"); | ||
| Console.WriteLine(error); | ||
| }); | ||
| } | ||
|
|
||
| private static Color? TryParseColor(string colorStr) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(colorStr)) | ||
| return null; | ||
|
|
||
| try | ||
| { | ||
| var known = Color.FromName(colorStr); | ||
| if (known.IsKnownColor) | ||
| return known; | ||
|
|
||
| return ColorTranslator.FromHtml(colorStr); | ||
| } | ||
| catch | ||
| { | ||
| Console.WriteLine($"Color '{colorStr}' could not be parsed. Default color will be used."); | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,41 @@ | ||
| using CommandLine; | ||
|
|
||
| namespace TagCloudConsoleClient | ||
| { | ||
| public class Options | ||
| { | ||
| [Value(0, MetaName = "input", | ||
| HelpText = "Path to the input file containing text.", | ||
| Required = true)] | ||
| public string InputFile { get; set; } | ||
|
|
||
| [Value(1, MetaName = "output", | ||
| HelpText = "Path for the output image (e.g., out.png).", | ||
| Required = true)] | ||
| public string OutputFile { get; set; } | ||
|
|
||
| [Option("width", HelpText = "Canvas width (default is 1000).")] | ||
| public int? Width { get; set; } | ||
|
|
||
| [Option("height", HelpText = "Canvas height (default is 1000).")] | ||
| public int? Height { get; set; } | ||
|
|
||
| [Option("padding", HelpText = "Canvas padding (default if 50).")] | ||
| public int? Padding { get; set; } | ||
|
|
||
| [Option("bgcolor", HelpText = "Background color (name or #RRGGBB).")] | ||
| public string BackgroundColor { get; set; } | ||
|
|
||
| [Option("textcolor", HelpText = "Text color (name or #RRGGBB).")] | ||
| public string TextColor { get; set; } | ||
|
|
||
| [Option("font", HelpText = "Font family name (e.g., Arial).")] | ||
| public string FontFamily { get; set; } | ||
|
|
||
| [Option("minsize", HelpText = "Minimum font size.")] | ||
| public float? MinFontSize { get; set; } | ||
|
|
||
| [Option("maxsize", HelpText = "Maximum font size.")] | ||
| public float? MaxFontSize { get; set; } | ||
| } | ||
| } |
This file contains hidden or 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,26 @@ | ||
| using Autofac; | ||
| using TagCloudGenerator.Core.Interfaces; | ||
| using TagCloudGenerator.DI; | ||
| using TagCloudGenerator.Infrastructure.Filters; | ||
|
|
||
| namespace TagCloudConsoleClient | ||
| { | ||
| public class Program | ||
| { | ||
| public static void Main(string[] args) | ||
| { | ||
| var builder = new ContainerBuilder(); | ||
|
|
||
| builder.RegisterModule<TagCloudModule>(); | ||
| builder.RegisterType<NullWordsFilter>().As<IFilter>(); | ||
| builder.RegisterType<BoringWordsFilter>().As<IFilter>(); | ||
| builder.RegisterType<ConsoleClient>().As<IClient>(); | ||
|
|
||
| var container = builder.Build(); | ||
|
|
||
| using var scope = container.BeginLifetimeScope(); | ||
| var client = scope.Resolve<IClient>(); | ||
| client.Run(args); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0-windows</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>disable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\TagCloudGenerator\TagCloudGenerator.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or 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,90 @@ | ||
| using System.Drawing; | ||
| using TagCloudGenerator.Core.Interfaces; | ||
| using TagCloudGenerator.Infrastructure; | ||
|
|
||
| namespace TagCloudGenerator.Algorithms | ||
| { | ||
| public class BasicTagCloudAlgorithm : ITagCloudAlgorithm | ||
| { | ||
| private Point center; | ||
|
|
||
| private readonly List<Rectangle> rectangles = new List<Rectangle>(); | ||
|
|
||
| private double currentAngle = 0; | ||
| private double currentRadius = 0; | ||
|
|
||
| private double angleStep = 0.1; | ||
| private double radiusStep = 0.5; | ||
|
|
||
| public BasicTagCloudAlgorithm(Point center) | ||
| { | ||
| if (center.X < 0 || center.Y < 0) | ||
| center = new Point(0, 0); | ||
|
|
||
| this.center = center; | ||
| } | ||
|
|
||
| public BasicTagCloudAlgorithm() | ||
| { | ||
| center = new Point(0, 0); | ||
| } | ||
|
|
||
| public Result<Rectangle> PutNextRectangle(Size rectangleSize) | ||
| { | ||
| if (rectangleSize.Width <= 0 || rectangleSize.Height <= 0) | ||
| return Result.Fail<Rectangle>("Rectangle sizes must be positives"); | ||
|
|
||
| if (rectangles.Count == 0) | ||
| { | ||
| return Result.Ok(PutFirstRectangle(rectangleSize)); | ||
| } | ||
|
|
||
| return Result.Ok(PlaceNextRectangle(rectangleSize)); | ||
| } | ||
|
|
||
| private Rectangle PlaceNextRectangle(Size rectangleSize) | ||
| { | ||
| while (true) | ||
| { | ||
| double x = center.X + currentRadius * Math.Cos(currentAngle); | ||
| double y = center.Y + currentRadius * Math.Sin(currentAngle); | ||
|
|
||
| var potentialCenter = new Point((int)(x - rectangleSize.Width / 2), (int)(y - rectangleSize.Height / 2)); | ||
| var potentialRectangle = new Rectangle(potentialCenter, rectangleSize); | ||
|
|
||
| if (!IntersectWithAny(potentialRectangle)) | ||
| { | ||
| rectangles.Add(potentialRectangle); | ||
| return potentialRectangle; | ||
| } | ||
|
|
||
| currentAngle += angleStep; | ||
| if (currentAngle >= 2 * Math.PI) | ||
| { | ||
| currentAngle = 0; | ||
| currentRadius += radiusStep; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private bool IntersectWithAny(Rectangle potentialRectangle) | ||
| { | ||
| return rectangles.Any(rect => rect.IntersectsWith(potentialRectangle)); | ||
| } | ||
|
|
||
| private Rectangle PutFirstRectangle(Size rectangleSize) | ||
| { | ||
| var firstCenter = new Point((int)(center.X - rectangleSize.Width / 2), (int)(center.Y - rectangleSize.Width / 2)); | ||
| var first = new Rectangle(firstCenter, rectangleSize); | ||
| currentRadius = rectangleSize.Height / 2; | ||
| rectangles.Add(first); | ||
| return first; | ||
| } | ||
|
|
||
| public ITagCloudAlgorithm Reset() | ||
| { | ||
| rectangles.Clear(); | ||
| return this; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,10 @@ | ||
| using TagCloudGenerator.Core.Models; | ||
| using TagCloudGenerator.Infrastructure; | ||
|
|
||
| namespace TagCloudGenerator.Core.Interfaces | ||
| { | ||
| public interface IAnalyzer | ||
| { | ||
| Result<WordsWithFrequency> Analyze(List<string> words); | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| | ||
| namespace TagCloudGenerator.Core.Interfaces | ||
| { | ||
| public interface IClient | ||
| { | ||
| void Run(string[] args); | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| using TagCloudGenerator.Infrastructure; | ||
|
|
||
| namespace TagCloudGenerator.Core.Interfaces | ||
| { | ||
| public interface IFilter | ||
| { | ||
| Result<List<string>> Filter(List<string> words); | ||
|
|
||
| bool ShouldInclude(string word); | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| | ||
| namespace TagCloudGenerator.Core.Interfaces | ||
| { | ||
| public interface IFontSizeCalculator | ||
| { | ||
| float Calculate(int wordFrequency, int minFrequency, int maxFrequency, float minFontSize, float maxFontSize); | ||
| } | ||
| } |
This file contains hidden or 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,10 @@ | ||
| using TagCloudGenerator.Infrastructure; | ||
|
|
||
| namespace TagCloudGenerator.Core.Interfaces | ||
| { | ||
| public interface IFormatReader | ||
| { | ||
| bool CanRead(string filePath); | ||
| Result<List<string>> TryRead(string filePath); | ||
| } | ||
| } |
This file contains hidden or 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 TagCloudGenerator.Infrastructure; | ||
|
|
||
| namespace TagCloudGenerator.Core.Interfaces | ||
| { | ||
| public interface INormalizer | ||
| { | ||
| public Result<List<string>> Normalize(List<string> words); | ||
| } | ||
| } |
This file contains hidden or 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 TagCloudGenerator.Infrastructure; | ||
|
|
||
| namespace TagCloudGenerator.Core.Interfaces | ||
| { | ||
| public interface IReaderRepository | ||
| { | ||
| public Result<IFormatReader> TryGetReader(string filePath); | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| using System.Drawing; | ||
| using TagCloudGenerator.Core.Models; | ||
| using TagCloudGenerator.Infrastructure; | ||
|
|
||
| namespace TagCloudGenerator.Core.Interfaces | ||
| { | ||
| public interface IRenderer | ||
| { | ||
| public Result<Bitmap> Render(IEnumerable<CloudItem> items, CanvasSettings canvasSettings, TextSettings textSettings); | ||
| } | ||
| } |
This file contains hidden or 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,10 @@ | ||
| using TagCloudGenerator.Core.Models; | ||
| using TagCloudGenerator.Infrastructure; | ||
|
|
||
| namespace TagCloudGenerator.Core.Interfaces | ||
| { | ||
| public interface ISorter | ||
| { | ||
| public Result<WordsWithFrequency> Sort(WordsWithFrequency wordsWithFreqs); | ||
| } | ||
| } |
This file contains hidden or 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,12 @@ | ||
| using System.Drawing; | ||
| using TagCloudGenerator.Infrastructure; | ||
|
|
||
| namespace TagCloudGenerator.Core.Interfaces | ||
| { | ||
| public interface ITagCloudAlgorithm | ||
| { | ||
| Result<Rectangle> PutNextRectangle(Size rectangleSize); | ||
|
|
||
| public ITagCloudAlgorithm Reset(); | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| using System.Drawing; | ||
| using TagCloudGenerator.Core.Models; | ||
| using TagCloudGenerator.Infrastructure; | ||
|
|
||
| namespace TagCloudGenerator.Core.Interfaces | ||
| { | ||
| public interface ITagCloudGenerator | ||
| { | ||
| public Result<List<CloudItem>> Generate(List<string> words, CanvasSettings canvasSettings, TextSettings textSettings, ICollection<IFilter> filters); | ||
| } | ||
| } |
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.
Здесь тоже переход на Result нужен
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.
Обновил