Skip to content
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
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions TagsCloudContainer/Actions/AlgorithmSettingsAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using TagsCloudContainer.Client;
using TagsCloudContainer.Infrastucture.Settings;
using TagsCloudContainer.Infrastucture.UiActions;

namespace TagsCloudContainer.Actions
{
public class AlgorithmSettingsAction : IUiAction
{
private AlgorithmSettings algorithmSettings;
private ITagCloudClient tagCloudClient;

public AlgorithmSettingsAction(AlgorithmSettings settings, ITagCloudClient tagCloudClient)
{
this.algorithmSettings = settings;
this.tagCloudClient = tagCloudClient;
}

public string Category => "���������";

This comment was marked as resolved.


public string Name => "��������";

public string Description => "�������� ��������� ���������";

public void Perform()
{
tagCloudClient.SetSettings(algorithmSettings);
}
}
}
32 changes: 32 additions & 0 deletions TagsCloudContainer/Actions/DrawTagCloudAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using TagsCloudContainer.Client;
using TagsCloudContainer.Infrastucture.Settings;
using TagsCloudContainer.Infrastucture.UiActions;

namespace TagsCloudContainer.Actions
{
public class DrawTagCloudAction : IUiAction
{
private ImageSettings imageSettings;
private FileSettings fileSettings;
private ITagCloudClient tagCloudClient;

public DrawTagCloudAction(ImageSettings imageSettings, FileSettings fileSettings, ITagCloudClient tagCloudClient)
{
this.tagCloudClient = tagCloudClient;
this.imageSettings = imageSettings;
this.fileSettings = fileSettings;
}

public string Category => "�����������";

public string Name => "���������� �����������";

public string Description => "���������� ����������� ������ �����";

public void Perform()
{
tagCloudClient.DrawImage(fileSettings.SourceFilePath,
fileSettings.BoringFilePath, imageSettings.Width, imageSettings.Height);
}
}
}
29 changes: 29 additions & 0 deletions TagsCloudContainer/Actions/ImageSettingsAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using TagsCloudContainer.Client;
using TagsCloudContainer.Infrastucture.Settings;
using TagsCloudContainer.Infrastucture.UiActions;

namespace TagsCloudContainer.Actions
{
public class ImageSettingsAction : IUiAction
{
private ImageSettings imageSettings;
private ITagCloudClient tagCloudClient;

public ImageSettingsAction(ImageSettings settings, ITagCloudClient tagCloudClient)
{
this.imageSettings = settings;
this.tagCloudClient = tagCloudClient;
}

public string Category => "���������";

public string Name => "�����������";

public string Description => "�������� ��������� �����������";

public void Perform()
{
tagCloudClient.SetSettings(imageSettings);
}
}
}
29 changes: 29 additions & 0 deletions TagsCloudContainer/Actions/SaveImageAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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()
{
tagCloudClient.SaveImage(fileSettings.ImagePath);
}
}
}
29 changes: 29 additions & 0 deletions TagsCloudContainer/Actions/SelectBoringWordsFileAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using TagsCloudContainer.Client;
using TagsCloudContainer.Infrastucture.Settings;
using TagsCloudContainer.Infrastucture.UiActions;

namespace TagsCloudContainer.Actions
{
public class SelectBoringWordsFileAction : IUiAction
{
private FileSettings fileSetting;
private ITagCloudClient tagCloudClient;

public SelectBoringWordsFileAction(FileSettings settings, ITagCloudClient tagCloudClient)
{
this.fileSetting = settings;
this.tagCloudClient = tagCloudClient;
}

public string Category => "�����";

public string Name => "���� �� �������� �������";

public string Description => "������� ���� �� ������� ������� ����";

public void Perform()
{
fileSetting.BoringFilePath = tagCloudClient.SetBoringFilePath(fileSetting.BoringFilePath);
}
}
}
29 changes: 29 additions & 0 deletions TagsCloudContainer/Actions/SelectSourceFileAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using TagsCloudContainer.Client;
using TagsCloudContainer.Infrastucture.Settings;
using TagsCloudContainer.Infrastucture.UiActions;

namespace TagsCloudContainer.Actions
{
public class SelectSourceFileAction : IUiAction
{
private FileSettings fileSettings;
private ITagCloudClient tagCloudClient;

public SelectSourceFileAction(FileSettings settings, ITagCloudClient tagCloudClient)
{
this.fileSettings = settings;
this.tagCloudClient = tagCloudClient;
}

public string Category => "�����";

public string Name => "���� c� �������";

public string Description => "������� ���� �� ������� ��� ������ �����";

public void Perform()
{
fileSettings.SourceFilePath = tagCloudClient.SetSourceFilePath(fileSettings.SourceFilePath);
}
}
}
45 changes: 45 additions & 0 deletions TagsCloudContainer/Algorithm/CircularCloudLayouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using TagsCloudContainer.Infrastucture;
using TagsCloudContainer.Infrastucture.Settings;

namespace TagsCloudContainer.Algorithm
{
public class CircularCloudLayouter : ICloudLayouter
{
private readonly AlgorithmSettings algorithmSettings;
private readonly ImageSettings imageSettings;


public CircularCloudLayouter(AlgorithmSettings algorithmSettings, ImageSettings imageSettings)
{
this.algorithmSettings = algorithmSettings;
this.imageSettings = imageSettings;
}

public List<TextRectangle> GetRectangles(Dictionary<string, int> wordFrequencies)
{
var rectanglePlacer = new RectanglePlacer(
algorithmSettings,
new Point(imageSettings.Width / 2, imageSettings.Height / 2));

This comment was marked as resolved.


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)

This comment was marked as resolved.

{
return fontSize + (wordFrequencies[word] - wordFrequencies.Values.Min()) * 20 / (wordFrequencies.Values.Max());
}
}
}
24 changes: 24 additions & 0 deletions TagsCloudContainer/Algorithm/FileParser.cs
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;
}

}
}
9 changes: 9 additions & 0 deletions TagsCloudContainer/Algorithm/ICloudLayouter.cs
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);
}
}
7 changes: 7 additions & 0 deletions TagsCloudContainer/Algorithm/IFileParser.cs
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);
}
}
9 changes: 9 additions & 0 deletions TagsCloudContainer/Algorithm/IRectanglePlacer.cs
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);
}
}
7 changes: 7 additions & 0 deletions TagsCloudContainer/Algorithm/IWordProcessor.cs
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);
}
}
54 changes: 54 additions & 0 deletions TagsCloudContainer/Algorithm/RectanglePlacer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using TagsCloudContainer.Infrastucture;
using TagsCloudContainer.Infrastucture.Settings;

namespace TagsCloudContainer.Algorithm
{
public sealed class RectanglePlacer: IRectanglePlacer
{
private Point center;
private double radius;
private double angle;
private double deltaRadius;
private double deltaAngle;

public RectanglePlacer(AlgorithmSettings algorithmSettings, Point center)
{
if (center.X < 0 || center.Y < 0)
throw new ArgumentException("the coordinates of the center must be positive numbers");
this.center = center;
this.deltaRadius = algorithmSettings.DeltaRadius;
this.deltaAngle = algorithmSettings.DeltaAngle;
this.radius = algorithmSettings.Radius;
this.angle = algorithmSettings.Angle;
}

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)
{
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 += deltaAngle;
radius += deltaRadius;
}
}

}
}
41 changes: 41 additions & 0 deletions TagsCloudContainer/Algorithm/WordProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using StopWord;

namespace TagsCloudContainer.Algorithm
{
public class WordProcessor : IWordProcessor
{

private readonly IFileParser parser;

public WordProcessor(IFileParser parser)
{
this.parser = parser;
}

public Dictionary<string, int> CalculateFrequencyInterestingWords(string sourceFilePath, string boringFilePath)
{
var wordFrequencies = new Dictionary<string, int>();
var interestingWords = GetInterestingWords(sourceFilePath, boringFilePath);

for (int i = 0; i < interestingWords.Count; i++)

This comment was marked as resolved.

{
if (!wordFrequencies.ContainsKey(interestingWords[i]))
wordFrequencies.Add(interestingWords[i], 0);
wordFrequencies[interestingWords[i]]++;
}

return wordFrequencies.OrderByDescending(x => x.Value).ToDictionary();
}

public List<string> GetInterestingWords(string sourceFilePath, string boringFilePath)
{
var boringWords = parser.ReadWordsInFile(boringFilePath);
var sourceWords = parser.ReadWordsInFile(sourceFilePath);
var stopWords = StopWords.GetStopWords("ru");

var interestingWords = sourceWords.Where(word => !boringWords.Contains(word) && !stopWords.Contains(word)).ToList();

return interestingWords;
}
}
}
Loading