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

Савельев Григорий #200

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
da15221
Initial commit
luvairo-m Jan 22, 2024
0780e96
Переработка класса, отвечающего за отрисовку изображения. TagsCloudVi…
luvairo-m Jan 22, 2024
73a2267
Добавление точки входа в программу. Замена Word => Tag
luvairo-m Jan 23, 2024
b22dab8
Добавление фильтров и источников информации.
luvairo-m Jan 23, 2024
350b9c1
Правки в фильтрах.
luvairo-m Jan 24, 2024
b3a8bfe
Первая рабочая версия.
luvairo-m Jan 24, 2024
6fe9c1d
Добавление переворотов слов.
luvairo-m Jan 24, 2024
553449d
Добавлен CLI
luvairo-m Jan 24, 2024
a216c4e
Исправление ошибки ввода частей речи.
luvairo-m Jan 24, 2024
554883b
Первые правки.
luvairo-m Jan 26, 2024
c9e8946
Reformat кода.
luvairo-m Jan 26, 2024
5579be7
Reformat TagCloudVisualization.
luvairo-m Jan 26, 2024
9965d1e
Новая структура.
luvairo-m Jan 27, 2024
5ecfab1
Переработка фильтров. Расширение ServiceCollectionExtensions.
luvairo-m Jan 28, 2024
4892a1e
Опции для процессоров теперь неизменяемые.
luvairo-m Jan 28, 2024
b2084cb
Устранение бага с переворотом слова и бага, связанного с регистром ис…
luvairo-m Jan 28, 2024
609ddca
Реализация csvfilereader + docxfilereader.
luvairo-m Jan 28, 2024
f3c6de3
Сведение регистрации зависимостей к одному методу и добавление атрибу…
luvairo-m Jan 30, 2024
2f385c6
Добавление тестов.
luvairo-m Jan 30, 2024
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
1 change: 1 addition & 0 deletions TagsCloud/Builders/CloudOptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Reflection;
using TagsCloud.Contracts;
using TagsCloud.Entities;
using TagsCloud.Options;
using TagsCloudVisualization;

namespace TagsCloud.Builders;
Expand Down
1 change: 1 addition & 0 deletions TagsCloud/Builders/InputOptionsBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using TagsCloud.Contracts;
using TagsCloud.Entities;
using TagsCloud.Options;

namespace TagsCloud.Builders;

Expand Down
1 change: 1 addition & 0 deletions TagsCloud/Builders/OutputOptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using SixLabors.ImageSharp.Formats.Png;
using TagsCloud.Contracts;
using TagsCloud.Entities;
using TagsCloud.Options;

namespace TagsCloud.Builders;

Expand Down
10 changes: 10 additions & 0 deletions TagsCloud/Contracts/ICloudProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using TagsCloudVisualization;

namespace TagsCloud.Contracts;

public interface ICloudProcessor
{
void SetPositions(HashSet<WordTagGroup> wordGroups);
void SetFonts(HashSet<WordTagGroup> wordGroups);
void SetColors(HashSet<WordTagGroup> wordGroups);
}
2 changes: 1 addition & 1 deletion TagsCloud/Contracts/IFontMeasurer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace TagsCloud.Contracts;
public interface IFontMeasurer
{
MeasurerType Type { get; }
int GetFontSize(int count, int maxCount, int minCount, int maxSize, int minSize);
int GetFontSize(int wordFrequency, int minFrequency, int maxFrequency, int minFontSize, int maxFontSize);
}
8 changes: 8 additions & 0 deletions TagsCloud/Contracts/IInputProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using TagsCloudVisualization;

namespace TagsCloud.Contracts;

public interface IInputProcessor
{
HashSet<WordTagGroup> CollectWordGroupsFromFile(string filename);
}
8 changes: 8 additions & 0 deletions TagsCloud/Contracts/IOutputProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using TagsCloudVisualization;

namespace TagsCloud.Contracts;

public interface IOutputProcessor
{
void SaveVisualization(HashSet<WordTagGroup> wordGroups, string filename);
}
14 changes: 14 additions & 0 deletions TagsCloud/CustomAttributes/InjectionAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Extensions.DependencyInjection;

namespace TagsCloud.CustomAttributes;

[AttributeUsage(AttributeTargets.Class)]
public class InjectionAttribute : Attribute
{
public InjectionAttribute(ServiceLifetime lifeTime)
{
LifeTime = lifeTime;
}

public ServiceLifetime LifeTime { get; }
}
68 changes: 11 additions & 57 deletions TagsCloud/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System.Reflection;
using TagsCloud.Contracts;
using TagsCloud.Conveyors;
using TagsCloud.Processors;
using TagsCloud.CustomAttributes;

namespace TagsCloud.Extensions;

Expand All @@ -15,63 +14,18 @@ static ServiceCollectionExtensions()
assemblyTypes = Assembly.GetExecutingAssembly().GetTypes();
}

public static ServiceCollection AddProcessors(this ServiceCollection collection)
public static ServiceCollection AddAllInjections(this ServiceCollection collection)
{
collection.AddSingleton<InputProcessor>();
collection.AddSingleton<CloudProcessor>();
collection.AddSingleton<OutputProcessor>();
var types = assemblyTypes
.Where(type => Attribute.IsDefined(type, typeof(InjectionAttribute)));

return collection;
}

public static ServiceCollection AddFilters(this ServiceCollection collection)
{
var filterType = typeof(IFilter);
var filters = GetTypesByInterface(filterType);

foreach (var filter in filters)
collection.AddSingleton(filterType, filter);

collection.AddSingleton<FilterConveyor>();

return collection;
}

public static ServiceCollection AddReaders(this ServiceCollection collection)
{
var readerType = typeof(IFileReader);
var readers = GetTypesByInterface(readerType);

foreach (var reader in readers)
collection.AddSingleton(readerType, reader);
foreach (var implementationType in types)
{
var attribute = implementationType.GetCustomAttribute<InjectionAttribute>();
var serviceType = implementationType.GetInterfaces().First();
collection.Add(new ServiceDescriptor(serviceType, implementationType, attribute!.LifeTime));
}

return collection;
}

public static ServiceCollection AddPainters(this ServiceCollection collection)
{
var painterType = typeof(IPainter);
var painters = GetTypesByInterface(painterType);

foreach (var painter in painters) collection.AddSingleton(painterType, painter);

return collection;
}

public static ServiceCollection AddMeasurers(this ServiceCollection collection)
{
var measurerType = typeof(IFontMeasurer);
var measurers = GetTypesByInterface(measurerType);

foreach (var measurer in measurers)
collection.AddSingleton(measurerType, measurer);

return collection;
}

private static IEnumerable<Type> GetTypesByInterface(Type interfaceType)
{
return assemblyTypes
.Where(type => type.GetInterfaces().Any(inter => inter == interfaceType));
}
}
5 changes: 4 additions & 1 deletion TagsCloud/FileReaders/CsvFileReader.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using CsvHelper;
using CsvHelper.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Globalization;
using TagsCloud.Contracts;
using TagsCloud.CustomAttributes;
using TagsCloud.Entities;

namespace TagsCloud.FileReaders;

[Injection(ServiceLifetime.Singleton)]
public class CsvFileReader : IFileReader
{
public string SupportedExtension => "csv";
Copy link

Choose a reason for hiding this comment

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

Объясни, пожалуйста, разницу между => и = в данном случае?

Expand All @@ -21,6 +24,6 @@ public IEnumerable<string> ReadContent(string filename, IPostFormatter postForma
using var csv = new CsvReader(reader, configuration);

foreach (var cell in csv.GetRecords<TableCell>())
yield return cell.Word;
yield return postFormatter is null ? cell.Word : postFormatter.Format(cell.Word);
}
}
4 changes: 3 additions & 1 deletion TagsCloud/FileReaders/DocxFileReader.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Microsoft.Extensions.DependencyInjection;
using TagsCloud.Contracts;
using TagsCloud.CustomAttributes;
using Xceed.Words.NET;

namespace TagsCloud.FileReaders;

// TODO: Implement this reader in future
[Injection(ServiceLifetime.Singleton)]
public class DocxFileReader : IFileReader
{
public string SupportedExtension => "docx";
Expand Down
3 changes: 3 additions & 0 deletions TagsCloud/FileReaders/TxtFileReader.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using Microsoft.Extensions.DependencyInjection;
using TagsCloud.Contracts;
using TagsCloud.CustomAttributes;

namespace TagsCloud.FileReaders;

[Injection(ServiceLifetime.Singleton)]
public class TxtFileReader : IFileReader
{
public string SupportedExtension => "txt";
Expand Down
3 changes: 3 additions & 0 deletions TagsCloud/Filters/ExcludedFilter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using Microsoft.Extensions.DependencyInjection;
using TagsCloud.Contracts;
using TagsCloud.CustomAttributes;
using TagsCloudVisualization;

namespace TagsCloud.Filters;

[Injection(ServiceLifetime.Singleton)]
public class ExcludedFilter : IFilter
{
public void Apply(HashSet<WordTagGroup> wordGroups, IFilterOptions options)
Expand Down
3 changes: 3 additions & 0 deletions TagsCloud/Filters/SpeechPartFilter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using Microsoft.Extensions.DependencyInjection;
using TagsCloud.Contracts;
using TagsCloud.CustomAttributes;
using TagsCloudVisualization;

namespace TagsCloud.Filters;

[Injection(ServiceLifetime.Singleton)]
public class SpeechPartFilter : IFilter
{
public void Apply(HashSet<WordTagGroup> wordGroups, IFilterOptions options)
Expand Down
7 changes: 5 additions & 2 deletions TagsCloud/FontMeasurers/LinearFontMeasurer.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
using Microsoft.Extensions.DependencyInjection;
using TagsCloud.Contracts;
using TagsCloud.CustomAttributes;
using TagsCloud.Entities;

namespace TagsCloud.FontMeasurers;

[Injection(ServiceLifetime.Singleton)]
public class LinearFontMeasurer : IFontMeasurer
{
public MeasurerType Type => MeasurerType.Linear;

public int GetFontSize(int count, int maxCount, int minCount, int maxSize, int minSize)
public int GetFontSize(int wordFrequency, int minFrequency, int maxFrequency, int minFontSize, int maxFontSize)
{
var fontSize = minSize + (float)count / maxCount * (maxSize - minSize);
var fontSize = minFontSize + (float)wordFrequency / maxFrequency * (maxFontSize - minFontSize);
return (int)fontSize;
}
}
13 changes: 8 additions & 5 deletions TagsCloud/FontMeasurers/LogarithmicFontMeasurer.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
using Microsoft.Extensions.DependencyInjection;
using TagsCloud.Contracts;
using TagsCloud.CustomAttributes;
using TagsCloud.Entities;

namespace TagsCloud.FontMeasurers;

[Injection(ServiceLifetime.Singleton)]
public class LogarithmicFontMeasurer : IFontMeasurer
{
public MeasurerType Type => MeasurerType.Logarithmic;

public int GetFontSize(int count, int maxCount, int minCount, int maxSize, int minSize)
public int GetFontSize(int wordFrequency, int minFrequency, int maxFrequency, int minFontSize, int maxFontSize)
{
var minLog = Math.Log(minCount);
var divisor = Math.Log(maxCount) - minLog;
var weight = divisor == 0 ? 1 : (Math.Log(count) - minLog) / divisor;
return (int)(minSize + (maxSize - minSize) * weight);
var minLog = Math.Log(minFrequency);
var divisor = Math.Log(maxFrequency) - minLog;
var weight = divisor == 0 ? 1 : (Math.Log(wordFrequency) - minLog) / divisor;
return (int)(minFontSize + (maxFontSize - minFontSize) * weight);
}
}
17 changes: 14 additions & 3 deletions TagsCloud/Formatters/DefaultPostFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
using Microsoft.Extensions.DependencyInjection;
using TagsCloud.Contracts;
using TagsCloud.CustomAttributes;

namespace TagsCloud.Formatters;

[Injection(ServiceLifetime.Singleton)]
public class DefaultPostFormatter : IPostFormatter
{
private static readonly char[] separators = { ' ', '=', ';', ',', '.', ':', '!', '?' };

public string Format(string input)
{
return input.Split(separators, StringSplitOptions.RemoveEmptyEntries)[0].ToLower();
var idx = GetFirstNonLetterIndex(input);
return idx == -1 ? input : input[..idx];
}

private static int GetFirstNonLetterIndex(string line)
{
for (var i = 0; i < line.Length; i++)
if (!char.IsLetter(line[i]))
return i;

return -1;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using SixLabors.Fonts;
using SixLabors.ImageSharp;
using TagsCloud.Contracts;
using TagsCloud.Entities;
using TagsCloudVisualization;

namespace TagsCloud.Entities;
namespace TagsCloud.Options;

public class CloudProcessorOptions : ICloudProcessorOptions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using TagsCloud.Contracts;
using TagsCloud.Entities;

namespace TagsCloud.Entities;
namespace TagsCloud.Options;

public class InputProcessorOptions : IInputProcessorOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using SixLabors.ImageSharp.Formats;
using TagsCloud.Contracts;

namespace TagsCloud.Entities;
namespace TagsCloud.Options;

public class OutputProcessorOptions : IOutputProcessorOptions
{
Expand Down
3 changes: 3 additions & 0 deletions TagsCloud/Painters/AllRandomPainter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using Microsoft.Extensions.DependencyInjection;
using SixLabors.ImageSharp;
using TagsCloud.Contracts;
using TagsCloud.CustomAttributes;
using TagsCloud.Entities;
using TagsCloudVisualization;

namespace TagsCloud.Painters;

[Injection(ServiceLifetime.Singleton)]
public class AllRandomPainter : IPainter
{
private readonly Random random = new();
Expand Down
3 changes: 3 additions & 0 deletions TagsCloud/Painters/OneVsRestPainter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using Microsoft.Extensions.DependencyInjection;
using SixLabors.ImageSharp;
using TagsCloud.Contracts;
using TagsCloud.CustomAttributes;
using TagsCloud.Entities;
using TagsCloudVisualization;

namespace TagsCloud.Painters;

[Injection(ServiceLifetime.Singleton)]
public class OneVsRestPainter : IPainter
{
public ColoringStrategy Strategy => ColoringStrategy.OneVsRest;
Expand Down
Loading