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

Ослина Анастасия #15

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
36 changes: 36 additions & 0 deletions TagsCloudVisualization/App/ConsoleApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using TagsCloudVisualization.ConsoleCommands;
using TagsCloudVisualization.FileReaders;
using TagsCloudVisualization.Layouters;
using TagsCloudVisualization.Renderers;
using TagsCloudVisualization.WordPreprocessors;

namespace TagsCloudVisualization.App;

public class ConsoleApp : IApp
{
private readonly IWordPreprocessor wordPreprocessor;
private readonly FileReaderFactory fileReaderFactory;
private readonly ICloudLayouter cloudLayouter;
private readonly ICloudRenderer cloudRenderer;
private readonly string inputFilePath;

public ConsoleApp(IWordPreprocessor wordPreprocessor,
FileReaderFactory fileReaderFactory,
ICloudLayouter cloudLayouter,
ICloudRenderer cloudRenderer,
Options options)
{
this.wordPreprocessor = wordPreprocessor;
this.fileReaderFactory = fileReaderFactory;
this.cloudLayouter = cloudLayouter;
this.cloudRenderer = cloudRenderer;
inputFilePath = options.InputFilePath;
}

public void Run()
{
var text = fileReaderFactory.GetFileReader(inputFilePath).Read(inputFilePath);
var words = wordPreprocessor.ProcessTextToWords(text);
cloudRenderer.Render(cloudLayouter.CreateTagsCloud(words));
}
}
6 changes: 6 additions & 0 deletions TagsCloudVisualization/App/IApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace TagsCloudVisualization.App;

public interface IApp
{
public void Run();
}
20 changes: 20 additions & 0 deletions TagsCloudVisualization/BitmapProcessors/BitmapProcessorFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Autofac;
using TagsCloudVisualization.Enums;

namespace TagsCloudVisualization.BitmapProcessors;

public class BitmapProcessorFactory
{
private readonly IComponentContext context;

public BitmapProcessorFactory(IComponentContext context)
=> this.context = context;

public IBitmapProcessor GetBitmapProcessor(OutputImageFormat option)
{
if (context.IsRegisteredWithKey<IBitmapProcessor>(option))
return context.ResolveKeyed<IBitmapProcessor>(option);

throw new NotSupportedException($"Image format {option.ToString()} is not supported.");
}
}
19 changes: 19 additions & 0 deletions TagsCloudVisualization/BitmapProcessors/DefaultBitmapProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Drawing;
using TagsCloudVisualization.ConsoleCommands;
using TagsCloudVisualization.Enums;

namespace TagsCloudVisualization.BitmapProcessors;

public class DefaultBitmapProcessor : IBitmapProcessor
{
private readonly OutputImageFormat imageFormat;

public DefaultBitmapProcessor(Options options)
=> imageFormat = options.ImageFormat;

public void SaveImage(Bitmap bitmap, string imageDirectory, string imageName)
{
var filePath = Path.Combine(imageDirectory, $"{imageName}.{imageFormat.ToString().ToLowerInvariant()}");
bitmap.Save(filePath);
}
}
8 changes: 8 additions & 0 deletions TagsCloudVisualization/BitmapProcessors/IBitmapProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Drawing;

namespace TagsCloudVisualization.BitmapProcessors;

public interface IBitmapProcessor
{
public void SaveImage(Bitmap bitmap, string imageDirectory, string imageName);
}
26 changes: 26 additions & 0 deletions TagsCloudVisualization/BitmapProcessors/PdfBitmapProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Drawing;
using System.Drawing.Imaging;
using PdfSharp.Drawing;
using PdfSharp.Pdf;

namespace TagsCloudVisualization.BitmapProcessors;

public class PdfBitmapProcessor : IBitmapProcessor
{
public void SaveImage(Bitmap bitmap, string imageDirectory, string imageName)
{
using var memoryStream = new MemoryStream();
bitmap.Save(memoryStream, ImageFormat.Png);
memoryStream.Position = 0;

var document = new PdfDocument();
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);

var image = XImage.FromStream(memoryStream);
gfx.DrawImage(image, 0, 0, page.Width, page.Height);

var pdfPath = Path.Combine(imageDirectory, $"{imageName}.pdf");
document.Save(pdfPath);
}
}
48 changes: 48 additions & 0 deletions TagsCloudVisualization/ConsoleCommands/Options.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using CommandLine;
using TagsCloudVisualization.Enums;

namespace TagsCloudVisualization.ConsoleCommands;

public class Options
{
[Option('i', "inputFilePath", Required = true,
HelpText = "Set path to a file containing words in one column (under one word per row).")]
public string InputFilePath { get; set; }

[Option('o', "outputDirectory", Required = true,
HelpText = "Set directory for output image.")]
public string OutputDirectory { get; set; }

[Option('f', "font", Default = "Arial", HelpText = "Set font for tags cloud words.")]
public string TagsFont { get; set; }

[Option("minFontSize", Default = 5, HelpText = "Set min font size for tags cloud words.")]
public int MinTagsFontSize { get; set; }

[Option("maxFontSize", Default = 80, HelpText = "Set max font size for tags cloud words.")]
public int MaxTagsFontSize { get; set; }

[Option('h', "imageHeight", Default = 1080, HelpText = "Set output image height.")]
public int ImageHeight { get; set; }

[Option('w', "imageWidth", Default = 1920, HelpText = "Set output image width.")]
public int ImageWidth { get; set; }

[Option("imageFormat", Default = OutputImageFormat.Png,
HelpText = "Set output image format. Possible values: Jpeg, Jpg, Png, Tiff, Bmp, Gif, Pdf.")]
public OutputImageFormat ImageFormat { get; set; }

[Option('b', "backgroundColor", Default = "Empty",
HelpText = "Set background color for tags cloud. Example : -b white")]
public string BackgroundColor { get; set; }

[Option("pathToMyStem", Default = null, HelpText = "Set path to mystem.exe.")]
public string PathToMyStem { get; set; }

[Option("numOfColors", Default = 85, HelpText = "Set number of colors for gradient color generator.")]
public int NumOfColors { get; set; }

[Option("colorOption", Default = ColorOption.Random,
HelpText = $"Set option of color generator for words. Possible values: Random, Gradient.")]
public ColorOption ColorOption { get; set; }
}
75 changes: 75 additions & 0 deletions TagsCloudVisualization/ContainerConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Autofac;
using TagsCloudVisualization.App;
using TagsCloudVisualization.BitmapProcessors;
using TagsCloudVisualization.ConsoleCommands;
using TagsCloudVisualization.Distributors;
using TagsCloudVisualization.Enums;
using TagsCloudVisualization.FileReaders;
using TagsCloudVisualization.Layouters;
using TagsCloudVisualization.Layouters.RectangleSizeCalculators;
using TagsCloudVisualization.MyStemWrapper;
using TagsCloudVisualization.Renderers;
using TagsCloudVisualization.Renderers.ColorGenerators;
using TagsCloudVisualization.WordPreprocessors;
using TagsCloudVisualization.WordPreprocessors.FontCreators;
using TagsCloudVisualization.WordPreprocessors.WordValidators;

namespace TagsCloudVisualization;

public static class ContainerConfig
{
public static IContainer Configure(Options options)
{
var builder = new ContainerBuilder();
builder.RegisterInstance(options).AsSelf();
builder.RegisterType<FileReaderFactory>().AsSelf();
builder.RegisterType<ColorGeneratorFactory>().AsSelf();
builder.RegisterType<BitmapProcessorFactory>().AsSelf();
builder.RegisterType<SpiralDistribution>().As<ICloudDistribution>();
builder.RegisterType<DefaultRenderer>().As<ICloudRenderer>();
builder.RegisterType<DefaultFontCreator>().As<IFontCreator>();
builder.RegisterType<DefaultWordValidator>().As<IWordValidator>();
builder.RegisterType<DefaultRectangleSizeCalculator>().As<IRectangleSizeCalculator>();
builder.RegisterType<DefaultWordPreprocessor>().As<IWordPreprocessor>();
builder.RegisterType<CircularCloudLayouter>().As<ICloudLayouter>();
builder.RegisterType<ConsoleApp>().As<IApp>();
ConfigureFileReaders(builder);
ConfigureColorGenerators(builder);
ConfigureBitmapProcessors(builder);

var myStem = new MyStem
{
PathToMyStem = options.PathToMyStem ?? Path.GetFullPath("Utilities\\mystem.exe"),
Parameters = "-nig --format json"
};
builder.RegisterInstance(myStem).AsSelf().SingleInstance();

return builder.Build();
}

private static void ConfigureFileReaders(ContainerBuilder builder)
{
builder.RegisterType<TextFileReader>().Keyed<IFileReader>(".txt");
builder.RegisterType<DocxFileReader>().Keyed<IFileReader>(".docx");
builder.RegisterType<DocFileReader>().Keyed<IFileReader>(".doc");
}

private static void ConfigureColorGenerators(ContainerBuilder builder)
{
builder.RegisterType<GradientColorGenerator>().Keyed<IColorGenerator>(ColorOption.Gradient);
builder.RegisterType<DefaultColorGenerator>().Keyed<IColorGenerator>(ColorOption.Random);
}

private static void ConfigureBitmapProcessors(ContainerBuilder builder)
{
builder.RegisterType<DefaultBitmapProcessor>()
.Keyed<IBitmapProcessor>(OutputImageFormat.Jpg)
.Keyed<IBitmapProcessor>(OutputImageFormat.Png)
.Keyed<IBitmapProcessor>(OutputImageFormat.Gif)
.Keyed<IBitmapProcessor>(OutputImageFormat.Tiff)
.Keyed<IBitmapProcessor>(OutputImageFormat.Bmp)
.Keyed<IBitmapProcessor>(OutputImageFormat.Jpeg);
builder.RegisterType<PdfBitmapProcessor>()
.Keyed<IBitmapProcessor>(OutputImageFormat.Pdf);
}
}
8 changes: 8 additions & 0 deletions TagsCloudVisualization/Distributors/ICloudDistribution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Drawing;

namespace TagsCloudVisualization.Distributors;

public interface ICloudDistribution
{
public Point GetNextPoint();
}
28 changes: 28 additions & 0 deletions TagsCloudVisualization/Distributors/SpiralDistribution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Drawing;
using TagsCloudVisualization.ConsoleCommands;

namespace TagsCloudVisualization.Distributors;

public class SpiralDistribution(Options options) : ICloudDistribution
{
private const double AngleStep = 0.02;
private const double RadiusStep = 0.01;
private double angle;
private double radius;
private readonly Point cloudCenter = new(options.ImageWidth / 2, options.ImageHeight / 2);

public Point GetNextPoint()
{
var nextPoint = ConvertPolarToCartesian();
angle += AngleStep;
radius += RadiusStep;
return nextPoint;
}

private Point ConvertPolarToCartesian()
{
var cartesian = new Point((int)(radius * Math.Cos(angle)), (int)(radius * Math.Sin(angle)));
cartesian.Offset(cloudCenter);
return cartesian;
}
}
17 changes: 17 additions & 0 deletions TagsCloudVisualization/Domain/Tag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Drawing;

namespace TagsCloudVisualization.Domain;

public class Tag
{
public readonly Rectangle Rectangle;
public readonly Font Font;
public readonly string Content;

public Tag(Rectangle rectangle, Font font, string content)
{
Rectangle = rectangle;
Font = font;
Content = content;
}
}
7 changes: 7 additions & 0 deletions TagsCloudVisualization/Enums/ColorOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TagsCloudVisualization.Enums;

public enum ColorOption
{
Random,
Gradient
}
12 changes: 12 additions & 0 deletions TagsCloudVisualization/Enums/OutputImageFormat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace TagsCloudVisualization.Enums;

public enum OutputImageFormat
{
Jpeg,
Jpg,
Png,
Gif,
Bmp,
Tiff,
Pdf
}
22 changes: 22 additions & 0 deletions TagsCloudVisualization/FileReaders/DocFileReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;

namespace TagsCloudVisualization.FileReaders;

public class DocFileReader : IFileReader
{
public string Read(string filePath)
{
var text = new StringBuilder();
var document = new Document();

document.LoadFromFile(filePath);

foreach (Section section in document.Sections)
foreach (Paragraph paragraph in section.Paragraphs)
text.Append(paragraph.Text);

return text.ToString();
}
}
18 changes: 18 additions & 0 deletions TagsCloudVisualization/FileReaders/DocxFileReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Text;
using NPOI.XWPF.UserModel;

namespace TagsCloudVisualization.FileReaders;

public class DocxFileReader : IFileReader
{
public string Read(string filePath)
{
var text = new StringBuilder();
using var doc = new XWPFDocument(File.OpenRead(filePath));

foreach (var paragraph in doc.Paragraphs)
text.Append(paragraph.Text);

return text.ToString();
}
}
Loading