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

Сибогатов Ринат #204

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
9 changes: 9 additions & 0 deletions TagsCloudContainer/Enums/FileType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace TagsCloudContainer.Enums
{
public enum FileType
{
Doc,
Docx,
Txt
}
}
11 changes: 11 additions & 0 deletions TagsCloudContainer/Interfaces/ICircularCloudLayouter.cs
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; }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почему решил сделать неполные свойства?

Rectangle PutNextRectangle(string word, Font font);
}
}
8 changes: 8 additions & 0 deletions TagsCloudContainer/Interfaces/IFileReader.cs
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);
}
}
15 changes: 15 additions & 0 deletions TagsCloudContainer/Interfaces/IImageSettings.cs
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; }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почему решил сделать неполные свойства?

Font GetFont();
int ImageWidth { get; set; }
int ImageHeight { get; set; }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Так как это уже элементы интерфейса Image, то можно опустить слова Image

То есть:

Width
Height


void UpdateImageSettings(int width, int height);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Также и здесь

UpdateSize или SetSize

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не вижу изменений

}
}
9 changes: 9 additions & 0 deletions TagsCloudContainer/Interfaces/INextPointProvider.cs
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();
}
}
8 changes: 8 additions & 0 deletions TagsCloudContainer/Interfaces/IPreprocessor.cs
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);
}
}
8 changes: 8 additions & 0 deletions TagsCloudContainer/Interfaces/ITagCloudClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

namespace TagsCloudContainer.Interfaces
{
public interface ITagCloudClient
{
void Run();
}
}
9 changes: 9 additions & 0 deletions TagsCloudContainer/Interfaces/ITagCloudGenerator.cs
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);
}
}
23 changes: 23 additions & 0 deletions TagsCloudContainer/Program.cs
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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

давай поменяем o на что-то длинное и понятное)

}
});
}
}
}
28 changes: 28 additions & 0 deletions TagsCloudContainer/Readers/DocReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Spire.Doc;
using Spire.Doc.Documents;
using TagsCloudContainer.Interfaces;

namespace TagsCloudContainer.Readers
{
public class DocReader : IFileReader
{
public IEnumerable<string> ReadWords(string filePath)
{
try
{
var document = new Document();
document.LoadFromFile(filePath);

return document.Sections
.Cast<Section>()
.SelectMany(section => section.Paragraphs.Cast<Paragraph>())
.SelectMany(paragraph => paragraph.Text.Split(' ', StringSplitOptions.RemoveEmptyEntries));
}
catch (Exception ex)
{
Console.WriteLine($"Error reading .doc file: {ex.Message}");
return Enumerable.Empty<string>();
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Отдельный респект за try-catch 🚀

}
}
}
30 changes: 30 additions & 0 deletions TagsCloudContainer/Readers/DocxReader.cs
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;
}
}
}
50 changes: 50 additions & 0 deletions TagsCloudContainer/Readers/FileReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.IO;
using TagsCloudContainer.Enums;
using TagsCloudContainer.Interfaces;
using TagsCloudContainer.Readers;

namespace TagsCloudContainer.TagsCloud
{
public class FileReader
{
public IEnumerable<string> ReadFile(string filePath)
{
var fileReader = GetFileReader(filePath);
return fileReader.ReadWords(filePath);
}

private IFileReader GetFileReader(string filePath)
{
FileType fileType = GetFileType(filePath);

switch (fileType)
{
case FileType.Doc:
return new DocReader();
case FileType.Docx:
return new DocxReader();
case FileType.Txt:
return new TxtReader();
default:
throw new InvalidOperationException("Unsupported file extension");
}
}

private FileType GetFileType(string filePath)
{
string fileExtension = Path.GetExtension(filePath)?.ToLower();

switch (fileExtension)
{
case ".doc":

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

здесь тоже можно использовать enum

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не вижу изменений

return FileType.Doc;
case ".docx":
return FileType.Docx;
case ".txt":
return FileType.Txt;
default:
throw new InvalidOperationException("Unsupported file extension");
}
}
}
}
27 changes: 27 additions & 0 deletions TagsCloudContainer/Readers/TxtReader.cs
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);

Choose a reason for hiding this comment

The 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)
{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лишняя пустая строка

Console.WriteLine($"Error reading file: {ex.Message}");
return Enumerable.Empty<string>();
}
}
}
}
31 changes: 31 additions & 0 deletions TagsCloudContainer/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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<FileReader>()
.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>();
var fReader = provider.GetRequiredService<FileReader>();

return new TagCloudApp(preprocessor, imageSettings, fReader);
})
.BuildServiceProvider();
}
}
}
91 changes: 91 additions & 0 deletions TagsCloudContainer/TagsCloud/CircularCloudLayouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
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 const int MinPositiveValue = 1;
private Size MeasureTextSize(string text, Font font)
{
// размер минимального временного изображения для измерения текста
var imageSizeForTextMeasurement = new Size(MinPositiveValue, MinPositiveValue);

// временное изображение с заданным размером
using (var temporaryBitmap = new Bitmap(imageSizeForTextMeasurement.Width, imageSizeForTextMeasurement.Height))
{
using (var temporaryGraphics = Graphics.FromImage(temporaryBitmap))
{
var textSize = Size.Ceiling(temporaryGraphics.MeasureString(text, font));

return textSize;
}
}
}

private void ValidateRectangleSize(Size rectangleSize)
{
if (rectangleSize.Width <= 0 || rectangleSize.Height <= 0)

Choose a reason for hiding this comment

The 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);

Choose a reason for hiding this comment

The 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));
}


}
}
12 changes: 12 additions & 0 deletions TagsCloudContainer/TagsCloud/ColorHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TagsCloudContainer.TagsCloud
{
public class ColorHelper
{
}
sibogatovr marked this conversation as resolved.
Show resolved Hide resolved
}
Loading