-
Notifications
You must be signed in to change notification settings - Fork 295
Алешев Руслан #188
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
artBETEP
wants to merge
23
commits into
kontur-courses:master
Choose a base branch
from
artBETEP: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
Алешев Руслан #188
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
17a5d9f
init: начальная структура программы
22e56ab
feat: impliment basic text cloud layouting
271ef7f
feat: Добавлены тесты
77fb65f
feat: добавлена фильтрация слов
3d91d03
feat: добавлен интерфейс командной строки
af7d57d
fix: исправлен CloudBuilderTests
03e1d52
refactor: Изменен обработчик аргументов командной строки
9b16fc0
init: добавлен пустой проект для GUI
7a8a6b9
refactor: исправлен FrequencyAnalyzer
6601ff9
refactor: TextPreprocessing fixing
0f53408
fix: добавлены DI для классов
b1d2d86
feat: создание изображения вынесено из TagsCloudLayouter в класс Visu…
8655a10
feat: добавлен маппер цветов
74c589a
refactor: удалил пустые конструкторы
aab825f
feat: Добавлен NormalPointsProvider реализующий интерфейс IPointsProv…
7e628a2
fix: исправлена ошибка в TagsCloudLayouter
28f5212
init: добавлен проект приложения WinForms
c8cda5f
feat: настроен GUI
fccf8bf
feat: автоматическое сохранение и загрузка настроек в GUI
dda89b9
test: добавлены тесты
6373fa9
feat: добавлена настройка цвета фона в консольном приложении
7b562a2
feat: добавлена настройка цвета фона в GUI
6d2c3c2
init: создана заготовка класса Result и тестов
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,138 @@ | ||
| using CommandLine; | ||
| using System.Drawing; | ||
| using TagsCloudContainer.SettingsClasses; | ||
| using TagsCloudContainer.TagCloudBuilder; | ||
| using TagsCloudVisualization; | ||
|
|
||
| namespace TagsCloudContainer.CLI | ||
| { | ||
| class CommandLineOptions | ||
| { | ||
| [Option('f', "filename", Required = true, HelpText = "Input file name.")] | ||
| public string Filename { get; set; } | ||
|
|
||
| [Option('o', "output", Required = true, HelpText = "Output file name.")] | ||
| public string Output { get; set; } | ||
|
|
||
| [Option("font", Required = false, HelpText = "Set the font family name.")] | ||
| public string FontFamily { get; set; } | ||
|
|
||
| [Option("fontsize", Required = false, HelpText = "Set the font size. Must be a positive integer.")] | ||
| public int FontSize { get; set; } | ||
|
|
||
| [Option("colors", Required = false, HelpText = "List of color names. Separated by commas.")] | ||
| public string Colors { get; set; } | ||
|
|
||
| [Option("size", Required = false, HelpText = "Set the image size. Must be two positive integer.")] | ||
| public Size Size { get; set; } | ||
|
|
||
| [Option("exclude", Required = false, HelpText = "File with words to exclude.")] | ||
| public string Filter { get; set; } | ||
|
|
||
| [Option("layout", Required = false, HelpText = "Set cloud layouter - Spiral, Random or Normal.")] | ||
| public string Layout { get; set; } | ||
|
|
||
| public static AppSettings ParseArgs(CommandLineOptions options) | ||
| { | ||
| var appSettings = new AppSettings(); | ||
| appSettings.DrawingSettings = new(); | ||
|
|
||
| appSettings.TextFile = options.Filename; | ||
| appSettings.OutImagePath = options.Output; | ||
| appSettings.FilterFile = options.Filter; | ||
|
|
||
| appSettings.DrawingSettings.FontFamily = GetFontFamily(options.FontFamily); | ||
| appSettings.DrawingSettings.FontSize = GetFontSize(options.FontSize); | ||
| appSettings.DrawingSettings.Size = GetSize(options.Size); | ||
| appSettings.DrawingSettings.Colors = GetColors(options.Colors); | ||
| appSettings.DrawingSettings.PointsProvider = GetPointsProvider( | ||
| options.Layout, | ||
| appSettings.DrawingSettings.Size); | ||
|
|
||
| return appSettings; | ||
| } | ||
|
|
||
| private static int GetFontSize(int fontSize) | ||
| { | ||
| if (fontSize > 0) | ||
| { | ||
| return fontSize; | ||
| } | ||
| return 12; | ||
| } | ||
|
|
||
| private static Size GetSize(Size size) | ||
| { | ||
| if (size.IsEmpty) | ||
| { | ||
| return new Size(800, 600); | ||
| } | ||
| return size; | ||
| } | ||
|
|
||
| private static IPointsProvider GetPointsProvider(string layout, Size size) | ||
| { | ||
| var center = new Point(size.Width / 2, size.Height / 2); | ||
| IPointsProvider pointProvider = new SpiralPointsProvider(); | ||
|
|
||
| if (string.IsNullOrEmpty(layout)) | ||
| { | ||
| pointProvider.Initialize(center); | ||
| return pointProvider; | ||
| } | ||
|
|
||
| switch (layout.ToLowerInvariant()) | ||
| { | ||
| case "random": | ||
| pointProvider = new RandomPointsProvider(); | ||
| break; | ||
|
|
||
| case "normal": | ||
| pointProvider = new NormalPointsProvider(); | ||
| break; | ||
|
|
||
| case "Spiral": | ||
| pointProvider = new NormalPointsProvider(); | ||
| break; | ||
|
|
||
| default: | ||
| pointProvider = new SpiralPointsProvider(); | ||
| break; | ||
| } | ||
|
|
||
| pointProvider.Initialize(center); | ||
| return pointProvider; | ||
| } | ||
|
|
||
| private static FontFamily GetFontFamily(string fontName) | ||
| { | ||
| try | ||
| { | ||
| var fontFamily = new FontFamily(fontName); | ||
| return fontFamily; | ||
| } | ||
| catch (ArgumentException) | ||
| { | ||
| Console.WriteLine("Font {0} not found. Using default font.", fontName); | ||
| return new FontFamily("Arial"); | ||
| } | ||
| } | ||
|
|
||
| private static IList<Color> GetColors(string colors) | ||
| { | ||
| if (string.IsNullOrEmpty(colors)) | ||
| { | ||
| return new List<Color>() { Color.White }; | ||
| } | ||
|
|
||
| var c = colors.Split(',').Select(x => Color.FromName(x)).ToList(); | ||
|
|
||
| if (c.Count > 0) | ||
| { | ||
| return c.ToList(); | ||
| } | ||
|
|
||
| return new List<Color>() { Color.White }; | ||
| } | ||
| } | ||
| } |
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,36 @@ | ||
| using System.Drawing; | ||
|
|
||
| namespace TagsCloudContainer | ||
| { | ||
| public class ColorMapper | ||
| { | ||
| public static Dictionary<int, Color> MapColors(IEnumerable<int> numbers, IList<Color> colors) | ||
| { | ||
| var distinctNumbers = numbers.Distinct().OrderByDescending(x => x); | ||
| var colorMapping = new Dictionary<int, Color>(); | ||
|
|
||
| if (!distinctNumbers.Any() || !colors.Any()) | ||
| { | ||
| return colorMapping; | ||
| } | ||
|
|
||
| int partitionSize = distinctNumbers.Count() / colors.Count; | ||
|
|
||
| int i = 0; | ||
| int j = 0; | ||
|
|
||
| foreach (var number in distinctNumbers) | ||
| { | ||
| colorMapping[number] = colors[j]; | ||
| if (i >= partitionSize) | ||
| { | ||
| i = 0; | ||
| j = Math.Min(colors.Count - 1, j + 1); | ||
| } | ||
| i++; | ||
| } | ||
|
|
||
| return colorMapping; | ||
| } | ||
| } | ||
| } |
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,25 @@ | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using TagsCloudContainer.FrequencyAnalyzers; | ||
| using TagsCloudContainer.SettingsClasses; | ||
| using TagsCloudContainer.TagCloudBuilder; | ||
| using TagsCloudContainer.TextTools; | ||
| using TagsCloudVisualization; | ||
|
|
||
| namespace TagsCloudContainer | ||
| { | ||
| public class DependencyInjectionConfig | ||
| { | ||
| public static IServiceCollection AddCustomServices(IServiceCollection services) | ||
| { | ||
| services.AddSingleton<ITextReader, TextFileReader>(); | ||
| services.AddSingleton<IAnalyzer, FrequencyAnalyzer>(); | ||
| services.AddTransient<IPointsProvider, SpiralPointsProvider>(); | ||
| services.AddTransient<IPointsProvider, RandomPointsProvider>(); | ||
| services.AddTransient<IPointsProvider, NormalPointsProvider>(); | ||
| services.AddTransient<TagsCloudLayouter>(); | ||
| services.AddTransient<CloudDrawingSettings>(); | ||
|
|
||
| return services; | ||
| } | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
TagsCloudContainer/FrequencyAnalyzers/FrequencyAnalyzer.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 @@ | ||
| namespace TagsCloudContainer.FrequencyAnalyzers | ||
| { | ||
| public class FrequencyAnalyzer : IAnalyzer | ||
| { | ||
| //private readonly TextPreprocessing preprocessor; | ||
| private readonly Dictionary<string, int> wordFrequency; | ||
|
|
||
| public FrequencyAnalyzer() | ||
| { | ||
| wordFrequency = new Dictionary<string, int>(); | ||
| } | ||
|
|
||
| public void Analyze(string text, string exclude = "") | ||
| { | ||
| var preprocessor = new TextPreprocessing(exclude); | ||
|
|
||
| foreach (var word in preprocessor.Preprocess(text)) | ||
| { | ||
| if (wordFrequency.ContainsKey(word.ToLower())) | ||
| { | ||
| wordFrequency[word]++; | ||
| } | ||
| else | ||
| { | ||
| wordFrequency.Add(word, 1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public IEnumerable<(string, int)> GetAnalyzedText() | ||
| { | ||
| return wordFrequency.Select(p => (p.Key, p.Value)); | ||
| } | ||
| } | ||
| } | ||
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 TagsCloudContainer.FrequencyAnalyzers | ||
| { | ||
| public interface IAnalyzer | ||
| { | ||
| public void Analyze(string text, string exclude = ""); | ||
| public IEnumerable<(string, int)> GetAnalyzedText(); | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
TagsCloudContainer/FrequencyAnalyzers/TextPreprocessing.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,30 @@ | ||
| namespace TagsCloudContainer.FrequencyAnalyzers | ||
| { | ||
| public class TextPreprocessing | ||
| { | ||
| private readonly HashSet<string> excludedWords = new(); | ||
|
|
||
| public TextPreprocessing(string excludedWordsPath) | ||
| { | ||
| if (!File.Exists(excludedWordsPath)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var reader = new StreamReader(excludedWordsPath); | ||
| excludedWords = reader.ReadToEnd() | ||
| .Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries).ToHashSet(); | ||
| } | ||
|
|
||
| public IEnumerable<string> Preprocess(string text) | ||
| { | ||
| var words = text.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries) | ||
| .Select(x => x.ToLower()); | ||
| foreach (var word in words) | ||
| { | ||
| if (!excludedWords.Contains(word)) | ||
| yield return 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,39 @@ | ||
| using CommandLine; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using TagsCloudContainer.CLI; | ||
| using TagsCloudContainer.Drawer; | ||
| using TagsCloudContainer.FrequencyAnalyzers; | ||
| using TagsCloudContainer.SettingsClasses; | ||
| using TagsCloudContainer.TextTools; | ||
| using TagsCloudVisualization; | ||
|
|
||
| namespace TagsCloudContainer | ||
| { | ||
| public static class Program | ||
| { | ||
| public static void Main(string[] args) | ||
| { | ||
| var services = DependencyInjectionConfig.AddCustomServices(new ServiceCollection()); | ||
| var serviceProvider = services.BuildServiceProvider(); | ||
|
|
||
| var reader = serviceProvider.GetService<ITextReader>(); | ||
| var analyzer = serviceProvider.GetService<IAnalyzer>(); | ||
|
|
||
| var appSettings = new AppSettings(); | ||
|
|
||
| Parser.Default.ParseArguments<CommandLineOptions>(args) | ||
| .WithParsed(o => appSettings = CommandLineOptions.ParseArgs(o)); | ||
|
|
||
| var text = reader.ReadText(appSettings.TextFile); | ||
| var layouter = serviceProvider.GetService<TagsCloudLayouter>(); | ||
|
|
||
| analyzer.Analyze(text, appSettings.FilterFile); | ||
|
|
||
| layouter.Initialize(appSettings.DrawingSettings, analyzer.GetAnalyzedText()); | ||
|
|
||
| Visualizer.Draw(appSettings.DrawingSettings.Size, | ||
| layouter.GetTextImages()).Save(appSettings.OutImagePath); | ||
| Console.WriteLine("Resulting image saved to " + appSettings.OutImagePath); | ||
| } | ||
| } | ||
| } |
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 @@ | ||
| { | ||
| "profiles": { | ||
| "TagsCloudContainer": { | ||
| "commandName": "Project", | ||
| "commandLineArgs": "-f sample.txt -o testout.png --exclude excludedWords.txt --font Calibri --colors Gold,Pink,LightGoldenrodYellow --layout SpiralPointsProvider --fontsize 12 --size 500,500" | ||
| } | ||
| } | ||
| } |
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 @@ | ||
| namespace TagsCloudContainer.SettingsClasses | ||
| { | ||
| public class AppSettings | ||
| { | ||
| public string TextFile; | ||
| public string OutImagePath; | ||
| public string FilterFile; | ||
| public CloudDrawingSettings DrawingSettings; | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
TagsCloudContainer/SettingsClasses/CloudDrawingSettings.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,14 @@ | ||
| using System.Drawing; | ||
| using TagsCloudVisualization; | ||
|
|
||
| namespace TagsCloudContainer.SettingsClasses | ||
| { | ||
| public class CloudDrawingSettings | ||
| { | ||
| public FontFamily FontFamily = new("Arial"); | ||
| public float FontSize = 12; | ||
| public IList<Color> Colors = new List<Color>() { Color.AliceBlue }; | ||
| public Size Size = new(600, 600); | ||
| public IPointsProvider PointsProvider = new SpiralPointsProvider(); | ||
| } | ||
| } |
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 System.Drawing; | ||
|
|
||
| namespace TagsCloudVisualization | ||
| { | ||
| public interface IPointsProvider | ||
| { | ||
| public IEnumerable<Point> Points(); | ||
| public void Initialize(Point center); | ||
| } | ||
| } |
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.
This comment was marked as resolved.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.