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 12 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
14 changes: 14 additions & 0 deletions TagsCloudContainer/Enums/FileType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.ComponentModel;

namespace TagsCloudContainer.Enums
{
public enum FileType
{
[Description(".doc")]
Doc,
[Description(".docx")]
Docx,
[Description(".txt")]
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; init; }
public IList<Rectangle> Rectangles { get; init; }
Rectangle PutNextRectangle(string word, Font font);
}
}
10 changes: 10 additions & 0 deletions TagsCloudContainer/Interfaces/IFileReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

using TagsCloudContainer.Utility;

namespace TagsCloudContainer.Interfaces
{
public interface IFileReader
{
Result<IEnumerable<string>> ReadWords(string filePath);
}
}
16 changes: 16 additions & 0 deletions TagsCloudContainer/Interfaces/IImageSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Drawing;

namespace TagsCloudContainer.Interfaces
{
public interface IImageSettings
{
Color BackgroundColor { get; init; }
Color FontColor { get; init; }
Font GetFont();
int Width { get; set; }
int Height { get; set; }

void UpdateSettings(int width, int height);
}
}

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(options =>
{
using (var serviceProvider = Startup.ConfigureServices())
{
var tagCloudApp = serviceProvider.GetRequiredService<TagCloudApp>();
tagCloudApp.Run(options);
}
});
}
}
}
30 changes: 30 additions & 0 deletions TagsCloudContainer/Readers/DocReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Spire.Doc;
using Spire.Doc.Documents;
using TagsCloudContainer.Interfaces;
using TagsCloudContainer.Utility;

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

var words = document.Sections
.Cast<Section>()
.SelectMany(section => section.Paragraphs.Cast<Paragraph>())
.SelectMany(paragraph => paragraph.Text.Split(' ', StringSplitOptions.RemoveEmptyEntries));

return Result<IEnumerable<string>>.Success(words);
}
catch (Exception ex)
{
return Result<IEnumerable<string>>.Failure($"Error reading .doc file: {ex.Message}");
}
}
}
}
31 changes: 31 additions & 0 deletions TagsCloudContainer/Readers/DocxReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using NPOI.XWPF.UserModel;
using TagsCloudContainer.Interfaces;
using TagsCloudContainer.Utility;

namespace TagsCloudContainer.Readers
{
public class DocxReader : IFileReader
{
public Result<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));
}
}

return Result<IEnumerable<string>>.Success(words);
}
catch (Exception ex)
{
return Result<IEnumerable<string>>.Failure($"Error reading .docx file: {ex.Message}");
}
}
}
}
76 changes: 76 additions & 0 deletions TagsCloudContainer/Readers/FileReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System.ComponentModel;
using TagsCloudContainer.Enums;
using TagsCloudContainer.Interfaces;
using TagsCloudContainer.Readers;
using TagsCloudContainer.Utility;

namespace TagsCloudContainer.TagsCloud
{
public class FileReader
{
public Result<IEnumerable<string>> ReadFile(string filePath)
{
var fileReaderResult = GetFileReader(filePath);

if (fileReaderResult.IsSuccess)
{
var fileReader = fileReaderResult.Value;
return fileReader.ReadWords(filePath);
}
else
{
return Result<IEnumerable<string>>.Failure(fileReaderResult.ErrorMessage);
}
}

private Result<IFileReader> GetFileReader(string filePath)
{
try
{
FileType fileType = GetFileType(filePath);

switch (fileType)
{
case FileType.Doc:
return Result<IFileReader>.Success(new DocReader());
case FileType.Docx:
return Result<IFileReader>.Success(new DocxReader());
case FileType.Txt:
return Result<IFileReader>.Success(new TxtReader());
default:
return Result<IFileReader>.Failure("Unsupported file extension");
}
}
catch (Exception ex)
{
return Result<IFileReader>.Failure($"Error getting file reader: {ex.Message}");
}
}

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

var fileTypes = Enum.GetValues(typeof(FileType)).Cast<FileType>();

foreach (var fileType in fileTypes)
{
var descriptionAttribute = GetEnumDescription(fileType);
if (descriptionAttribute != null && descriptionAttribute.Equals(fileExtension))
{
return fileType;
}
}

throw new InvalidOperationException("Unsupported file extension");
}

static string GetEnumDescription(Enum value)
{
var field = value.GetType().GetField(value.ToString());
var attribute = (DescriptionAttribute)Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute));

return attribute?.Description;
}
}
}
26 changes: 26 additions & 0 deletions TagsCloudContainer/Readers/TxtReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using TagsCloudContainer.Interfaces;
using TagsCloudContainer.Utility;

namespace TagsCloudContainer.Readers
{
public class TxtReader : IFileReader
{
public Result<IEnumerable<string>> ReadWords(string filePath)
{
try
{
var lines = File.ReadAllLines(filePath);

var words = lines.SelectMany(line => line.Split(' '));

var nonEmptyWords = words.Where(word => !string.IsNullOrEmpty(word));

return Result<IEnumerable<string>>.Success(nonEmptyWords);
}
catch (Exception ex)
{
return Result<IEnumerable<string>>.Failure($"Error reading file: {ex.Message}");
}
}
}
}
18 changes: 18 additions & 0 deletions TagsCloudContainer/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# TagsCloudContainer

������ ������������ ����� ��������� ��� ��������� ������ ����� �� ����� � ������� ����. ��������� ������������ �������� ������� ����, �������� ������� ������ � ��������� �����, � ����� ���������� ���� ���� � ������� �������� ��� ����������.

## ��������� ���������:

- ��������� ���������������� �������� ������� ����.
- ����������� ���������� ���� ���� � ������� ��������.
- ����������� ��������� ����� ��� ����.
- ����������� ����������� �������� �����, ����, ������.
- �������� CLI.
- ����������� ������: \*.doc, \*.docx, \*.txt.
- ��������� ��������������� ����� ��� ����� ����������������.
- ������� ������ ��������� � �����������.

## ���������� ���� �� ������

- ������ ���� ������� ���� ���������, ��������� � ����� 'boring_words.txt'

Choose a reason for hiding this comment

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

Слетела кодировка

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();
}
}
}
Loading