-
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
Сибогатов Ринат #204
base: master
Are you sure you want to change the base?
Сибогатов Ринат #204
Changes from 5 commits
d0feb67
5cffcd9
0ce51a9
4072767
ac8b8ae
d7d53fd
4148cb1
5e0054f
3f50bf8
2320a14
21aa80d
553a053
ed0c9b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.Drawing; | ||
|
||
namespace TagsCloudContainer.Interfaces | ||
{ | ||
public interface ICircularCloudLayouter | ||
{ | ||
public Point CloudCenter { get; } | ||
public IList<Rectangle> Rectangles { get; } | ||
Rectangle PutNextRectangle(string word, Font font); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
| ||
namespace TagsCloudContainer.Interfaces | ||
{ | ||
public interface IFileReader | ||
{ | ||
IEnumerable<string> ReadWords(string filePath); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Drawing; | ||
|
||
namespace TagsCloudContainer.Interfaces | ||
{ | ||
public interface IImageSettings | ||
{ | ||
Color BackgroundColor { get; } | ||
Color FontColor { get; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Почему решил сделать неполные свойства? |
||
Font GetFont(); | ||
int ImageWidth { get; set; } | ||
int ImageHeight { get; set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Так как это уже элементы интерфейса Image, то можно опустить слова Image То есть: Width |
||
|
||
void UpdateImageSettings(int width, int height); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Также и здесь UpdateSize или SetSize There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не вижу изменений |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Drawing; | ||
|
||
namespace TagsCloudContainer.Interfaces | ||
{ | ||
public interface INextPointProvider | ||
{ | ||
Point GetNextPoint(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
| ||
namespace TagsCloudContainer.Interfaces | ||
{ | ||
public interface IPreprocessor | ||
{ | ||
IEnumerable<string> Process(IEnumerable<string> words, string boringWordsFilePath); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
| ||
namespace TagsCloudContainer.Interfaces | ||
{ | ||
public interface ITagCloudClient | ||
{ | ||
void Run(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Drawing; | ||
|
||
namespace TagsCloudContainer.Interfaces | ||
{ | ||
public interface ITagCloudGenerator | ||
{ | ||
Bitmap GenerateTagCloud(IEnumerable<string> words, IImageSettings imageSettings); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using CommandLine; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using TagsCloudContainer.TagsCloud; | ||
using TagsCloudContainer.Utility; | ||
|
||
namespace TagsCloudContainer | ||
{ | ||
public class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
Parser.Default.ParseArguments<CommandLineOptions>(args) | ||
.WithParsed<CommandLineOptions>(o => | ||
{ | ||
using (var serviceProvider = Startup.ConfigureServices()) | ||
{ | ||
var tagCloudApp = serviceProvider.GetRequiredService<TagCloudApp>(); | ||
tagCloudApp.Run(o); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. давай поменяем o на что-то длинное и понятное) |
||
} | ||
}); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Spire.Doc; | ||
using Spire.Doc.Documents; | ||
using TagsCloudContainer.Interfaces; | ||
|
||
namespace TagsCloudContainer.Readers | ||
{ | ||
public class DocReader : IFileReader | ||
{ | ||
public IEnumerable<string> ReadWords(string filePath) | ||
{ | ||
var words = new List<string>(); | ||
|
||
try | ||
{ | ||
var document = new Document(); | ||
document.LoadFromFile(filePath); | ||
|
||
foreach (Section section in document.Sections) | ||
{ | ||
foreach (Paragraph paragraph in section.Paragraphs) | ||
{ | ||
words.AddRange(paragraph.Text.Split(' ', StringSplitOptions.RemoveEmptyEntries)); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Как сократить вложенность? |
||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Error reading .doc file: {ex.Message}"); | ||
} | ||
|
||
return words; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using NPOI.XWPF.UserModel; | ||
using TagsCloudContainer.Interfaces; | ||
|
||
namespace TagsCloudContainer.Readers | ||
{ | ||
public class DocxReader : IFileReader | ||
{ | ||
public IEnumerable<string> ReadWords(string filePath) | ||
{ | ||
var words = new List<string>(); | ||
|
||
try | ||
{ | ||
using (var doc = new XWPFDocument(File.OpenRead(filePath))) | ||
{ | ||
foreach (var paragraph in doc.Paragraphs) | ||
{ | ||
words.AddRange(paragraph.Text.Split(' ', StringSplitOptions.RemoveEmptyEntries)); | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Error reading .docx file: {ex.Message}"); | ||
} | ||
|
||
return words; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using TagsCloudContainer.Interfaces; | ||
|
||
namespace TagsCloudContainer.Readers | ||
{ | ||
public class TxtReader : IFileReader | ||
{ | ||
public IEnumerable<string> ReadWords(string filePath) | ||
{ | ||
try | ||
{ | ||
string[] lines = File.ReadAllLines(filePath); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. почему тут явно определил тип вместо var? |
||
|
||
var words = lines.SelectMany(line => line.Split(' ')); | ||
|
||
var nonEmptyWords = words.Where(word => !string.IsNullOrEmpty(word)); | ||
|
||
return nonEmptyWords; | ||
} | ||
catch (Exception ex) | ||
{ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лишняя пустая строка |
||
Console.WriteLine($"Error reading file: {ex.Message}"); | ||
return Enumerable.Empty<string>(); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using TagsCloudContainer.Interfaces; | ||
using TagsCloudContainer.Readers; | ||
using TagsCloudContainer.TagsCloud; | ||
|
||
namespace TagsCloudContainer | ||
{ | ||
public class Startup | ||
{ | ||
public static ServiceProvider ConfigureServices() | ||
{ | ||
return new ServiceCollection() | ||
.AddSingleton<IFileReader, TxtReader>() | ||
.AddSingleton<IPreprocessor, WordPreprocessor>() | ||
.AddSingleton<IImageSettings, ImageSettings>() | ||
.AddSingleton<ITagCloudGenerator, TagCloudGenerator>() | ||
.AddScoped(provider => | ||
{ | ||
var fileReader = provider.GetRequiredService<IFileReader>(); | ||
var preprocessor = provider.GetRequiredService<IPreprocessor>(); | ||
var tagCloudGenerator = provider.GetRequiredService<ITagCloudGenerator>(); | ||
var imageSettings = provider.GetRequiredService<IImageSettings>(); | ||
|
||
|
||
return new TagCloudApp(preprocessor, imageSettings); | ||
}) | ||
.BuildServiceProvider(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System.Drawing; | ||
using TagsCloudContainer.Interfaces; | ||
|
||
namespace TagsCloudContainer.TagsCloud | ||
{ | ||
public class CircularCloudLayouter : ICircularCloudLayouter | ||
{ | ||
private readonly Point center; | ||
private readonly List<Rectangle> rectangles; | ||
private readonly INextPointProvider pointProvider; | ||
|
||
public CircularCloudLayouter(Point center, INextPointProvider pointProvider) | ||
{ | ||
this.center = center; | ||
rectangles = new(); | ||
this.pointProvider = pointProvider; | ||
} | ||
|
||
public Point CloudCenter => center; | ||
public IList<Rectangle> Rectangles => rectangles; | ||
|
||
public Rectangle PutNextRectangle(Size rectangleSize) | ||
{ | ||
ValidateRectangleSize(rectangleSize); | ||
|
||
var currentRectangle = CreateNewRectangle(rectangleSize); | ||
rectangles.Add(currentRectangle); | ||
|
||
return currentRectangle; | ||
} | ||
|
||
public Rectangle PutNextRectangle(string word, Font font) | ||
{ | ||
var textSize = MeasureTextSize(word, font); | ||
return PutNextRectangle(textSize); | ||
} | ||
|
||
private Size MeasureTextSize(string text, Font font) | ||
{ | ||
using (var temporaryBitmap = new Bitmap(1, 1)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Как поступить с магическими числами? Почему тут 1? Как это понять? |
||
using (var temporaryGraphics = Graphics.FromImage(temporaryBitmap)) | ||
{ | ||
return Size.Ceiling(temporaryGraphics.MeasureString(text, font)); | ||
} | ||
} | ||
|
||
private void ValidateRectangleSize(Size rectangleSize) | ||
{ | ||
if (rectangleSize.Width <= 0 || rectangleSize.Height <= 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. для 0 нужно завести константы |
||
{ | ||
throw new ArgumentException("Width and height of the rectangle must be greater than zero"); | ||
} | ||
} | ||
|
||
private Rectangle CreateNewRectangle(Size rectangleSize) | ||
{ | ||
while (true) | ||
{ | ||
var nextPoint = pointProvider.GetNextPoint(); | ||
var rectangleLocation = GetUpperLeftCorner(nextPoint, rectangleSize); | ||
var rectangle = new Rectangle(rectangleLocation, rectangleSize); | ||
|
||
if (!RectanglesIntersect(rectangle)) | ||
{ | ||
return rectangle; | ||
} | ||
} | ||
} | ||
|
||
private Point GetUpperLeftCorner(Point rectangleCenter, Size rectangleSize) | ||
{ | ||
return new Point(rectangleCenter.X - rectangleSize.Width / 2, rectangleCenter.Y - rectangleSize.Height / 2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. для 2 надо константы |
||
} | ||
|
||
private bool RectanglesIntersect(Rectangle newRectangle) | ||
{ | ||
return rectangles.Any(rect => rect.IntersectsWith(newRectangle)); | ||
} | ||
|
||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Drawing; | ||
using TagsCloudContainer.Interfaces; | ||
|
||
namespace TagsCloudContainer.TagsCloud | ||
{ | ||
public class ImageSettings : IImageSettings | ||
{ | ||
public Color BackgroundColor { get; } = Color.White; | ||
public Color FontColor { get; } = Color.Black; | ||
public int ImageWidth { get; set; } = 1600; | ||
public int ImageHeight { get; set; } = 1200; | ||
|
||
public Font GetFont() | ||
{ | ||
return new Font("Verdana", 20); | ||
} | ||
|
||
public void UpdateImageSettings(int width, int height) | ||
{ | ||
ImageWidth = width; | ||
ImageHeight = height; | ||
} | ||
|
||
} | ||
} |
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.
Почему решил сделать неполные свойства?