-
Notifications
You must be signed in to change notification settings - Fork 303
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
Овечкин Илья #193
Open
magnat0
wants to merge
10
commits into
kontur-courses:master
Choose a base branch
from
magnat0: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
Овечкин Илья #193
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a03b6f7
add tests
magnat0 e5aa566
realization an application to create a tag cloud
magnat0 c9ca5fe
delete unused usings + change the modifier to private
magnat0 fc18be4
change the encoding + change client name
magnat0 476c62a
transfer code from tagCloudClient to Actions
magnat0 68f2a8f
small refactor
magnat0 51685ac
change class registration
magnat0 69b18b8
fix the namespace and refactor RectanglePlace
magnat0 c969104
edit class registration and delete the drawer constructor
magnat0 fa7e208
refactor tests
magnat0 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,26 @@ | ||
using TagsCloudContainer.Infrastucture.Settings; | ||
using TagsCloudContainer.Infrastucture.UiActions; | ||
|
||
namespace TagsCloudContainer.Actions | ||
{ | ||
public class AlgorithmSettingsAction : IUiAction | ||
{ | ||
private AlgorithmSettings algorithmSettings; | ||
|
||
public AlgorithmSettingsAction(AlgorithmSettings settings) | ||
{ | ||
this.algorithmSettings = settings; | ||
} | ||
|
||
public string Category => "Настроить"; | ||
|
||
public string Name => "Алгоритм"; | ||
|
||
public string Description => "Изменить настройки алгоритма"; | ||
|
||
public void Perform() | ||
{ | ||
SettingsForm.For(algorithmSettings).ShowDialog(); | ||
} | ||
} | ||
} |
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,30 @@ | ||
using TagsCloudContainer.Client; | ||
using TagsCloudContainer.Infrastucture.Settings; | ||
using TagsCloudContainer.Infrastucture.UiActions; | ||
|
||
namespace TagsCloudContainer.Actions | ||
{ | ||
public class DrawTagCloudAction : IUiAction | ||
{ | ||
private FileSettings fileSettings; | ||
private ITagCloudClient tagCloudClient; | ||
|
||
public DrawTagCloudAction(FileSettings fileSettings, ITagCloudClient tagCloudClient) | ||
{ | ||
this.tagCloudClient = tagCloudClient; | ||
this.fileSettings = fileSettings; | ||
} | ||
|
||
public string Category => "Изображение"; | ||
|
||
public string Name => "Отрисовать изображение"; | ||
|
||
public string Description => "Отрисовать изображение облака тегов"; | ||
|
||
public void Perform() | ||
{ | ||
tagCloudClient.DrawImage(fileSettings.SourceFilePath, | ||
fileSettings.BoringFilePath); | ||
} | ||
} | ||
} |
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,30 @@ | ||
using TagsCloudContainer.Infrastucture.Extensions; | ||
using TagsCloudContainer.Infrastucture.Settings; | ||
using TagsCloudContainer.Infrastucture.UiActions; | ||
|
||
namespace TagsCloudContainer.Actions | ||
{ | ||
public class ImageSettingsAction : IUiAction | ||
{ | ||
private ImageSettings imageSettings; | ||
private PictureBox pictureBox; | ||
|
||
public ImageSettingsAction(ImageSettings settings, PictureBox pictureBox) | ||
{ | ||
this.imageSettings = settings; | ||
this.pictureBox = pictureBox; | ||
} | ||
|
||
public string Category => "Настроить"; | ||
|
||
public string Name => "Изображение"; | ||
|
||
public string Description => "Изменить настройки изображения"; | ||
|
||
public void Perform() | ||
{ | ||
SettingsForm.For(imageSettings).ShowDialog(); | ||
pictureBox.RecreateImage(imageSettings); | ||
} | ||
} | ||
} |
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,40 @@ | ||
using TagsCloudContainer.Client; | ||
using TagsCloudContainer.Infrastucture.Settings; | ||
using TagsCloudContainer.Infrastucture.UiActions; | ||
|
||
namespace TagsCloudContainer.Actions | ||
{ | ||
public class SaveImageAction : IUiAction | ||
{ | ||
private FileSettings fileSettings; | ||
private ITagCloudClient tagCloudClient; | ||
|
||
public SaveImageAction(FileSettings settings, ITagCloudClient tagCloudClient) | ||
{ | ||
this.fileSettings = settings; | ||
this.tagCloudClient = tagCloudClient; | ||
} | ||
|
||
public string Category => "Изображение"; | ||
|
||
public string Name => "Сохранить"; | ||
|
||
public string Description => "Сохранить изображение"; | ||
|
||
public void Perform() | ||
{ | ||
var dialog = new SaveFileDialog | ||
{ | ||
CheckFileExists = false, | ||
InitialDirectory = Path.GetFullPath(fileSettings.ImagePath), | ||
DefaultExt = "png", | ||
FileName = "image.png", | ||
Filter = "Изображения (*.png)|*.png|Изображения (*.jpg)|*.jpg|Изображения (*.bmp)|*.bmp" | ||
}; | ||
var res = dialog.ShowDialog(); | ||
|
||
if (res == DialogResult.OK) | ||
tagCloudClient.SaveImage(dialog.FileName); | ||
} | ||
} | ||
} |
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,40 @@ | ||
using TagsCloudContainer.Infrastucture.Settings; | ||
using TagsCloudContainer.Infrastucture.UiActions; | ||
|
||
namespace TagsCloudContainer.Actions | ||
{ | ||
public class SelectBoringWordsFileAction : IUiAction | ||
{ | ||
private FileSettings fileSetting; | ||
|
||
public SelectBoringWordsFileAction(FileSettings settings) | ||
{ | ||
this.fileSetting = settings; | ||
} | ||
|
||
public string Category => "Файлы"; | ||
|
||
public string Name => "Файл со скучными словами"; | ||
|
||
public string Description => "Выбрать файл со списком скучных слов"; | ||
|
||
public void Perform() | ||
{ | ||
var filePath = fileSetting.BoringFilePath; | ||
var dialog = new OpenFileDialog() | ||
{ | ||
CheckFileExists = true, | ||
InitialDirectory = Path.GetFullPath(filePath), | ||
DefaultExt = "txt", | ||
FileName = "boring.txt", | ||
Filter = "Текстовые файлы (*.txt)|*.txt" | ||
}; | ||
var res = dialog.ShowDialog(); | ||
|
||
if (res == DialogResult.OK) | ||
filePath = dialog.FileName; | ||
|
||
fileSetting.BoringFilePath = filePath; | ||
} | ||
} | ||
} |
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,40 @@ | ||
using TagsCloudContainer.Infrastucture.Settings; | ||
using TagsCloudContainer.Infrastucture.UiActions; | ||
|
||
namespace TagsCloudContainer.Actions | ||
{ | ||
public class SelectSourceFileAction : IUiAction | ||
{ | ||
private FileSettings fileSettings; | ||
|
||
public SelectSourceFileAction(FileSettings settings) | ||
{ | ||
this.fileSettings = settings; | ||
} | ||
|
||
public string Category => "Файлы"; | ||
|
||
public string Name => "Файл cо словами"; | ||
|
||
public string Description => "Выбрать файл со словами для облака тегов"; | ||
|
||
public void Perform() | ||
{ | ||
var filePath = fileSettings.SourceFilePath; | ||
var dialog = new OpenFileDialog() | ||
{ | ||
CheckFileExists = true, | ||
InitialDirectory = Path.GetFullPath(filePath), | ||
DefaultExt = "txt", | ||
FileName = "source.txt", | ||
Filter = "Текстовые файлы (*.txt)|*.txt" | ||
}; | ||
var res = dialog.ShowDialog(); | ||
|
||
if (res == DialogResult.OK) | ||
filePath = dialog.FileName; | ||
|
||
fileSettings.SourceFilePath = filePath; | ||
} | ||
} | ||
} |
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,41 @@ | ||
using TagsCloudContainer.Infrastucture; | ||
using TagsCloudContainer.Infrastucture.Settings; | ||
|
||
namespace TagsCloudContainer.Algorithm | ||
{ | ||
public class CircularCloudLayouter : ICloudLayouter | ||
{ | ||
private readonly ImageSettings imageSettings; | ||
private readonly IRectanglePlacer rectanglePlacer; | ||
|
||
|
||
public CircularCloudLayouter(ImageSettings imageSettings, IRectanglePlacer rectanglePlacer) | ||
{ | ||
this.imageSettings = imageSettings; | ||
this.rectanglePlacer = rectanglePlacer; | ||
} | ||
|
||
public List<TextRectangle> GetRectangles(Dictionary<string, int> wordFrequencies) | ||
{ | ||
var rectangles = new List<TextRectangle>(); | ||
var bitmap = new Bitmap(imageSettings.Width, imageSettings.Height); | ||
var graphics = Graphics.FromImage(bitmap); | ||
|
||
foreach (var word in wordFrequencies.Keys) | ||
{ | ||
var fontSize = CalculateFontSize(wordFrequencies, word, imageSettings.Font.Size); | ||
var font = new Font(imageSettings.Font.FontFamily, fontSize, imageSettings.Font.Style, imageSettings.Font.Unit); | ||
var textSize = graphics.MeasureString(word, font); | ||
var rectangle = rectanglePlacer.GetPossibleNextRectangle(rectangles, textSize); | ||
rectangles.Add(new TextRectangle(rectangle, word, font)); | ||
} | ||
|
||
return rectangles; | ||
} | ||
|
||
private float CalculateFontSize(Dictionary<string, int> wordFrequencies, string word, float fontSize) | ||
{ | ||
return fontSize + (wordFrequencies[word] - wordFrequencies.Values.Min()) * 20 / (wordFrequencies.Values.Max()); | ||
} | ||
} | ||
} |
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,24 @@ | ||
namespace TagsCloudContainer.Algorithm | ||
{ | ||
public class FileParser : IFileParser | ||
{ | ||
public List<string> ReadWordsInFile(string filePath) | ||
{ | ||
var words = new List<string>(); | ||
var lines = File.ReadAllLines(filePath); | ||
|
||
foreach (var line in lines) | ||
{ | ||
var lineWords = line.ToLower().Trim().Split(' '); | ||
if (lineWords.Length > 1) | ||
{ | ||
throw new Exception("There is more than one word in a line"); | ||
} | ||
words.Add(lineWords[0]); | ||
} | ||
|
||
return words; | ||
} | ||
|
||
} | ||
} |
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 TagsCloudContainer.Infrastucture; | ||
|
||
namespace TagsCloudContainer.Algorithm | ||
{ | ||
public interface ICloudLayouter | ||
{ | ||
List<TextRectangle> GetRectangles(Dictionary<string, int> wordFrequencies); | ||
} | ||
} |
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 @@ | ||
namespace TagsCloudContainer.Algorithm | ||
{ | ||
public interface IFileParser | ||
{ | ||
List<string> ReadWordsInFile(string filePath); | ||
} | ||
} |
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 TagsCloudContainer.Infrastucture; | ||
|
||
namespace TagsCloudContainer.Algorithm | ||
{ | ||
public interface IRectanglePlacer | ||
{ | ||
RectangleF GetPossibleNextRectangle(List<TextRectangle> cloudRectangles, SizeF rectangleSize); | ||
} | ||
} |
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 @@ | ||
namespace TagsCloudContainer.Algorithm | ||
{ | ||
public interface IWordProcessor | ||
{ | ||
Dictionary<string, int> CalculateFrequencyInterestingWords(string sourceFilePath, string boringFilePath); | ||
} | ||
} |
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,54 @@ | ||
using System.Diagnostics; | ||
using TagsCloudContainer.Infrastucture; | ||
using TagsCloudContainer.Infrastucture.Settings; | ||
|
||
namespace TagsCloudContainer.Algorithm | ||
{ | ||
public sealed class RectanglePlacer : IRectanglePlacer | ||
{ | ||
private readonly AlgorithmSettings algorithmSettings; | ||
private readonly ImageSettings imageSettings; | ||
|
||
public RectanglePlacer(AlgorithmSettings algorithmSettings, ImageSettings imageSettings) | ||
{ | ||
if (imageSettings.Width / 2 < 0 || imageSettings.Height / 2 < 0) | ||
throw new ArgumentException("the coordinates of the center must be positive numbers"); | ||
|
||
this.algorithmSettings = algorithmSettings; | ||
this.imageSettings = imageSettings; | ||
} | ||
|
||
public RectangleF GetPossibleNextRectangle(List<TextRectangle> cloudRectangles, SizeF rectangleSize) | ||
{ | ||
if (rectangleSize.Width <= 0 || rectangleSize.Height <= 0) | ||
throw new ArgumentException("the width and height of the rectangle must be positive numbers"); | ||
|
||
return FindPossibleNextRectangle(cloudRectangles, rectangleSize); | ||
} | ||
|
||
private RectangleF FindPossibleNextRectangle(List<TextRectangle> cloudRectangles, SizeF rectangleSize) | ||
{ | ||
var radius = algorithmSettings.Radius; | ||
var angle = algorithmSettings.Angle; | ||
var center = new Point(imageSettings.Width / 2, imageSettings.Height / 2); | ||
|
||
while (true) | ||
{ | ||
var point = new Point( | ||
(int)(center.X + radius * Math.Cos(angle)), | ||
(int)(center.Y + radius * Math.Sin(angle)) | ||
); | ||
var possibleRectangle = new RectangleF(point, rectangleSize); | ||
|
||
if (!cloudRectangles.Any(textRectangle => textRectangle.Rectangle.IntersectsWith(possibleRectangle))) | ||
{ | ||
return possibleRectangle; | ||
} | ||
|
||
angle += algorithmSettings.DeltaAngle; | ||
radius += algorithmSettings.DeltaRadius; | ||
} | ||
} | ||
|
||
} | ||
} |
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.