-
Notifications
You must be signed in to change notification settings - Fork 240
Медведев Фома #220
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
Kpokoko
wants to merge
3
commits into
kontur-courses:master
Choose a base branch
from
Kpokoko: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
Медведев Фома #220
Changes from 2 commits
Commits
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,51 @@ | ||
| using System.Drawing; | ||
| using System.Drawing.Imaging; | ||
| using TagsCloudContainer; | ||
|
|
||
| namespace TagsCloudApp; | ||
|
|
||
| public class CloudVisualizer | ||
| { | ||
| private static Bitmap _bitmap; | ||
| public static Graphics Graphics { get; private set; } | ||
|
|
||
| public static void PrepareGraphics(Size imageSize) | ||
| { | ||
| _bitmap = new Bitmap(imageSize.Width, imageSize.Height); | ||
| Graphics = Graphics.FromImage(_bitmap); | ||
| } | ||
|
|
||
| public static void Draw(TextRectangleContainerProcessor processor, | ||
| ImageGeneratorInfo info, | ||
| string inputFile, | ||
| IWordMeasurer wordMeasurer) | ||
| { | ||
| var path = info.OutputFileName; | ||
| var dir = Path.GetDirectoryName(path); | ||
| if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) | ||
| Directory.CreateDirectory(dir); | ||
|
|
||
| Graphics.Clear(info.BackgroundColor); | ||
|
|
||
| using var brush = new SolidBrush(info.TextColor); | ||
|
|
||
| var format = new StringFormat | ||
| { | ||
| Alignment = StringAlignment.Center, | ||
| LineAlignment = StringAlignment.Center | ||
| }; | ||
|
|
||
| foreach (var container in processor.ProcessFile(inputFile, wordMeasurer)) | ||
| { | ||
| if (container is null) | ||
| continue; | ||
| var rect = container.Rectangle; | ||
| using var font = | ||
| new Font(info.Font.FontFamily, info.Font.Size * container.FontSizeScale, info.Font.Style); | ||
| Graphics.DrawString(container.Text, font, brush, rect, format); | ||
| } | ||
|
|
||
| _bitmap.Save(path); | ||
| } | ||
|
|
||
| } |
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,129 @@ | ||
| using System.Drawing; | ||
| using ErrorHandling; | ||
| using TagsCloudContainer; | ||
|
|
||
| namespace TagsCloudApp; | ||
|
|
||
| public class ConsoleClient : IClient | ||
| { | ||
| private const string ReadFontColorPrompt = "Введите английское название цвета шрифта (enter для дефолтного значения):"; | ||
| private const string ReadBGColorPrompt = "Введите английское название цвета фона (enter для дефолтного значения):"; | ||
| private const string ReadFontNamePrompt = "Введите название шрифта (enter для дефолтного значения):"; | ||
| private const string ReadFontSizePrompt = "Введите размер изображения через пробел двумя числами (enter для дефолтного значения):"; | ||
| private const string ReadOutputFileNamePrompt = "Введите желаемое имя для выходного файла (enter для дефолтного значения):"; | ||
| private const string ReadBanWordsPrompt = "Введите через запятую слова, которые хотите игнорировать (enter, если хотите видеть все слова)"; | ||
| private const string ReadOutputFileFormat = "Введите желаемый формат выходного файла (enter для дефолтного значения)"; | ||
|
|
||
| public ImageGeneratorInfo GetImageGeneratorInfo() | ||
| { | ||
| var textColor = ReadColor(ReadFontColorPrompt).GetValueOrThrow(); | ||
| var bgColor = ReadColor(ReadBGColorPrompt).GetValueOrThrow(); | ||
| var font = ReadFont(ReadFontNamePrompt).GetValueOrThrow(); | ||
| var imageSize = ReadSize(ReadFontSizePrompt).GetValueOrThrow(); | ||
| var outputFile = ReadString(ReadOutputFileNamePrompt); | ||
| var banWords = ReadList(ReadBanWordsPrompt); | ||
| var outputFileFormat = ReadFormat(ReadOutputFileFormat).GetValueOrThrow(); | ||
|
|
||
| return new ImageGeneratorInfo( | ||
| textColor, | ||
| bgColor, | ||
| font, | ||
| imageSize, | ||
| outputFile, | ||
| banWords, | ||
| outputFileFormat | ||
| ); | ||
| } | ||
|
|
||
| private Result<Color?> ReadColor(string prompt) | ||
| { | ||
| Console.WriteLine(prompt); | ||
| var input = Console.ReadLine(); | ||
| if (string.IsNullOrEmpty(input)) | ||
| return Result.Ok<Color?>(null); | ||
| var color = Color.FromName(input); | ||
| if (color.IsKnownColor) | ||
| return Result.Ok<Color?>(color); | ||
| return Result.Fail<Color?>($"Цвета {color} нет, попробуй ещё раз"); | ||
| } | ||
|
|
||
| private Result<Font?> ReadFont(string prompt, float defaultSize = 60) | ||
| { | ||
| Console.WriteLine(prompt); | ||
| var input = Console.ReadLine(); | ||
| if (string.IsNullOrEmpty(input)) | ||
| return Result.Ok<Font?>(null); | ||
| if (FontFamily.Families.Any(f => f.Name.Equals(input, StringComparison.OrdinalIgnoreCase))) | ||
| return Result.Ok<Font?>(new Font(input, defaultSize)); | ||
| return Result.Fail<Font?>($"Шрифта {input} нет, попробуй ещё раз"); | ||
| } | ||
|
|
||
| private Result<Size?> ReadSize(string prompt) | ||
| { | ||
| Console.WriteLine(prompt); | ||
| var input = Console.ReadLine(); | ||
| if (string.IsNullOrEmpty(input)) | ||
| return Result.Ok<Size?>(null); | ||
| var parts = input.Split(' ', StringSplitOptions.RemoveEmptyEntries); | ||
| if (parts.Length == 2 && | ||
| int.TryParse(parts[0], out var width) && | ||
| int.TryParse(parts[1], out var height)) | ||
| { | ||
| return Result.Ok<Size?>(new Size(width, height)); | ||
| } | ||
| return Result.Fail<Size?>("Нужно ввести два числа через пробел"); | ||
| } | ||
|
|
||
| private string? ReadString(string prompt) | ||
| { | ||
| Console.WriteLine(prompt); | ||
| var input = Console.ReadLine(); | ||
| return string.IsNullOrEmpty(input) ? null : input; | ||
| } | ||
|
|
||
| private List<string>? ReadList(string prompt) | ||
| { | ||
| Console.WriteLine(prompt); | ||
| var input = Console.ReadLine(); | ||
| return string.IsNullOrEmpty(input) ? new List<string>() : input.Split(',').Select(s => s.Trim()).ToList(); | ||
| } | ||
|
|
||
| private Result<string?> ReadFormat(string prompt) | ||
| { | ||
| Console.WriteLine(prompt); | ||
| var input = Console.ReadLine(); | ||
| if (string.IsNullOrEmpty(input)) | ||
| return Result.Ok<string?>(null); | ||
| if (input[0] == '.') | ||
| input = input[1..]; | ||
| switch (input) | ||
| { | ||
| case "png": | ||
| break; | ||
| case "jpg": | ||
| break; | ||
| case "jpeg": | ||
| break; | ||
| case "bmp": | ||
| break; | ||
| default: | ||
| return Result.Fail<string?>( | ||
| $"Формат {input} не поддерживается, повторите попытку! (Доступные форматы: png, jpg, jpeg, bmp"); | ||
| } | ||
| return Result.Ok<string?>(input); | ||
| } | ||
|
|
||
| public Result<string> GetImagePath() | ||
| { | ||
| Console.WriteLine("Введи название входного файла:"); | ||
| var fileName = Console.ReadLine(); | ||
| if (string.IsNullOrEmpty(fileName)) | ||
| return Result.Fail<string>("Имя файла не может быть пустым!"); | ||
| if (!File.Exists(fileName)) | ||
| { | ||
| var fullPath = Path.GetFullPath(fileName); | ||
| return Result.Fail<string>($"Файл {fileName} не найден по пути {fullPath}, повтори ввод"); | ||
| } | ||
| return Result.Ok<string>(fileName); | ||
| } | ||
| } |
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 ErrorHandling; | ||
| using TagsCloudContainer; | ||
|
|
||
| namespace TagsCloudApp; | ||
|
|
||
| public interface IClient | ||
| { | ||
| public ImageGeneratorInfo GetImageGeneratorInfo(); | ||
|
|
||
| public Result<string> GetImagePath(); | ||
| } |
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,27 @@ | ||
| using System.Drawing; | ||
| using Autofac; | ||
| using TagsCloudApp; | ||
| using TagsCloudContainer; | ||
|
|
||
| public static class Program | ||
| { | ||
| public static void Main() | ||
| { | ||
| var client = new ConsoleClient(); | ||
| var imageGeneratorInfo = client.GetImageGeneratorInfo(); | ||
| var fileName = client.GetImagePath().GetValueOrThrow(); | ||
| var imageSize = imageGeneratorInfo.ImageSize; | ||
| var imageCenter = new Point(imageSize.Width / 2, imageSize.Height / 2); | ||
| CloudVisualizer.PrepareGraphics(imageSize); | ||
| var container = ContainerComposer.Compose(imageCenter, imageGeneratorInfo.ImageSize, | ||
| CloudVisualizer.Graphics, imageGeneratorInfo.Font, imageGeneratorInfo.BanWords); | ||
|
|
||
| var wordProcessor = container | ||
| .Resolve<TextRectangleContainerProcessor>(); | ||
|
|
||
| var measurer = container | ||
| .Resolve<IWordMeasurer>(); | ||
|
|
||
| CloudVisualizer.Draw(wordProcessor, imageGeneratorInfo, fileName, measurer); | ||
| } | ||
| } |
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</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\TagsCloudContainer\TagsCloudContainer.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,28 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudContainer", "TagsCloudContainer\TagsCloudContainer.csproj", "{B669CC7B-C5A0-40AE-AF5F-E2A98EBC343A}" | ||
| EndProject | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudApp", "TagsCloudApp\TagsCloudApp.csproj", "{39391998-1546-4A9D-B534-A4756C13A9DF}" | ||
| EndProject | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudVisualization", "TagsCloudVisualization\TagsCloudVisualization.csproj", "{305481A1-EF22-489A-8B87-338FDDEE48B5}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {B669CC7B-C5A0-40AE-AF5F-E2A98EBC343A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {B669CC7B-C5A0-40AE-AF5F-E2A98EBC343A}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {B669CC7B-C5A0-40AE-AF5F-E2A98EBC343A}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {B669CC7B-C5A0-40AE-AF5F-E2A98EBC343A}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {39391998-1546-4A9D-B534-A4756C13A9DF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {39391998-1546-4A9D-B534-A4756C13A9DF}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {39391998-1546-4A9D-B534-A4756C13A9DF}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {39391998-1546-4A9D-B534-A4756C13A9DF}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {305481A1-EF22-489A-8B87-338FDDEE48B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {305481A1-EF22-489A-8B87-338FDDEE48B5}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {305481A1-EF22-489A-8B87-338FDDEE48B5}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {305481A1-EF22-489A-8B87-338FDDEE48B5}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| EndGlobal |
35 changes: 35 additions & 0 deletions
35
TagsCloudContainer/TagsCloudContainer/BoringWordsProcessor.cs
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,35 @@ | ||
| using WeCantSpell.Hunspell; | ||
|
|
||
| namespace TagsCloudContainer; | ||
|
|
||
| public class BoringWordsProcessor | ||
| { | ||
| private const float BoringWordQuantityThreshold = 0.35f; | ||
| private readonly List<string> _banWords; | ||
|
|
||
| public BoringWordsProcessor(List<string> banWords) | ||
| { | ||
| this._banWords = banWords; | ||
| } | ||
|
|
||
| public Dictionary<string, int> WordsToLowerAndRemoveBoringWords(List<string> words) | ||
| { | ||
| var banWordsDict = WordList.CreateFromWords(_banWords); | ||
| var frequencyDict = new Dictionary<string, int>(); | ||
| foreach (var word in words) | ||
| { | ||
| var lowerCaseWord = word.ToLower(); | ||
| if (banWordsDict.Check(lowerCaseWord)) | ||
| continue; | ||
| frequencyDict.TryAdd(lowerCaseWord, 0); | ||
| ++frequencyDict[lowerCaseWord]; | ||
| var a = (float)frequencyDict[lowerCaseWord] / words.Count; | ||
| } | ||
|
|
||
| return frequencyDict | ||
| .OrderByDescending(x => x.Value) | ||
| .ThenBy(x => x.Key) | ||
| .Where(x => ((float)x.Value / words.Count) < BoringWordQuantityThreshold) | ||
| .ToDictionary(x => x.Key, x => x.Value); | ||
| } | ||
| } | ||
48 changes: 48 additions & 0 deletions
48
TagsCloudContainer/TagsCloudContainer/CalculateNextRectangleSize.cs
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,48 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagsCloudContainer; | ||
|
|
||
| public class RectangleSizeCalculator | ||
| { | ||
| private Size _maxSize; | ||
| private Size _minSize; | ||
| private int _maxFrequency; | ||
| private bool _isMaxFrequencyInitialized; | ||
|
|
||
| internal RectangleSizeCalculator(Size imageSize) | ||
| { | ||
| _maxSize = new Size((int)(imageSize.Width), (int)(imageSize.Height * 0.1)); | ||
| _minSize = new Size((int)(imageSize.Width * 0.01), (int)(imageSize.Height * 0.005)); | ||
| } | ||
|
|
||
| internal Size CalculateNextRectangleSize(KeyValuePair<string, int> wordInfo, | ||
| IWordMeasurer wordMeasurer, out float scale) | ||
| { | ||
| var wordFrequency = wordInfo.Value; | ||
| if (!_isMaxFrequencyInitialized) | ||
| { | ||
| _isMaxFrequencyInitialized = true; | ||
| _maxFrequency = wordFrequency; | ||
| } | ||
|
|
||
| scale = (float)wordFrequency / _maxFrequency; | ||
| if (scale < 0.05) | ||
| return Size.Empty; | ||
| var rawSize = wordMeasurer.Measure(wordInfo.Key, scale); | ||
| return ClampSize(rawSize); | ||
| } | ||
|
|
||
| private Size ClampSize(Size size) | ||
| { | ||
| if (size.Width < _minSize.Width) | ||
| size.Width = _minSize.Width; | ||
| else if (size.Width > _maxSize.Width) | ||
| size.Width = _maxSize.Width; | ||
| if (size.Height < _minSize.Height) | ||
| size.Height = _minSize.Height; | ||
| else if (size.Height > _maxSize.Height) | ||
| size.Height = _maxSize.Height; | ||
|
||
|
|
||
| return Size.Round(size); | ||
| } | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
TagsCloudContainer/TagsCloudContainer/ContainerComposer.cs
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,20 @@ | ||
| using System.Drawing; | ||
| using Autofac; | ||
| using TagsCloudVisualization; | ||
|
|
||
| namespace TagsCloudContainer; | ||
|
|
||
| public static class ContainerComposer | ||
| { | ||
| public static IContainer Compose(Point center, Size imageSize, Graphics graphics, Font font, List<string> banWords) | ||
| { | ||
| var builder = new ContainerBuilder(); | ||
| builder.RegisterInstance(new CircularCloudLayouter(center, imageSize)).As<ILayouter>(); | ||
| builder.RegisterInstance(new TxtReader()).As<IFileReader>(); | ||
| builder.RegisterInstance(new RectangleSizeCalculator(imageSize)); | ||
| builder.RegisterInstance(new BoringWordsProcessor(banWords)); | ||
| builder.RegisterType<TextRectangleContainerProcessor>(); | ||
| builder.RegisterInstance(new WordMeasurer(graphics, font)).As<IWordMeasurer>(); | ||
| return builder.Build(); | ||
| } | ||
| } |
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,6 @@ | ||
| namespace TagsCloudContainer; | ||
|
|
||
| public interface IFileReader | ||
| { | ||
| internal List<string> ReadFile(string path); | ||
| } |
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 System.Drawing; | ||
|
|
||
| namespace TagsCloudContainer; | ||
|
|
||
| public interface IWordMeasurer | ||
| { | ||
| public Size Measure(string word, float scale); | ||
| public Font GetFont(float scale); | ||
| } |
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.
Унаследуй от интерфейса IBoringWordsProcessor и зарегай в Di контейнер