-
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 7 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,9 @@ | ||
namespace TagsCloudContainer.Enums | ||
{ | ||
public enum FileType | ||
{ | ||
Doc, | ||
Docx, | ||
Txt | ||
} | ||
} |
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,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>(); | ||
} | ||
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. Отдельный респект за try-catch 🚀 |
||
} | ||
} | ||
} |
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,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": | ||
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. здесь тоже можно использовать enum 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. Не вижу изменений |
||
return FileType.Doc; | ||
case ".docx": | ||
return FileType.Docx; | ||
case ".txt": | ||
return FileType.Txt; | ||
default: | ||
throw new InvalidOperationException("Unsupported file extension"); | ||
} | ||
} | ||
} | ||
} |
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,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(); | ||
} | ||
} | ||
} |
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) | ||
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,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
|
||
} |
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.
Почему решил сделать неполные свойства?