From 59d38a8e6a72392ecb90c94722efac99a1f8ed58 Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Wed, 25 Dec 2024 17:25:07 +0500 Subject: [PATCH 01/29] start --- .../PointGenerators/ArchemedianSpiral.cs | 27 +++++++++++++ .../PointGenerators/DeltaShaped.cs | 39 +++++++++++++++++++ .../PointGenerators/HeartShaped.cs | 39 +++++++++++++++++++ .../PointGenerators/IPointGenerator.cs | 14 +++++++ TagsCloudContainer/Program.cs | 10 +++++ TagsCloudContainer/TagsCloudContainer.csproj | 10 +++++ TagsCloudContainer/TagsCloudContainer.sln | 25 ++++++++++++ .../TextProcessor/ITextProcessor.cs | 13 +++++++ .../TextProviders/ITextProvider.cs | 13 +++++++ .../TextProviders/TxtTextProvider.cs | 16 ++++++++ TagsCloudContainer/Word.cs | 13 +++++++ TagsCloudContainer/WordFilters/IWordFilter.cs | 13 +++++++ 12 files changed, 232 insertions(+) create mode 100644 TagsCloudContainer/PointGenerators/ArchemedianSpiral.cs create mode 100644 TagsCloudContainer/PointGenerators/DeltaShaped.cs create mode 100644 TagsCloudContainer/PointGenerators/HeartShaped.cs create mode 100644 TagsCloudContainer/PointGenerators/IPointGenerator.cs create mode 100644 TagsCloudContainer/Program.cs create mode 100644 TagsCloudContainer/TagsCloudContainer.csproj create mode 100644 TagsCloudContainer/TagsCloudContainer.sln create mode 100644 TagsCloudContainer/TextProcessor/ITextProcessor.cs create mode 100644 TagsCloudContainer/TextProviders/ITextProvider.cs create mode 100644 TagsCloudContainer/TextProviders/TxtTextProvider.cs create mode 100644 TagsCloudContainer/Word.cs create mode 100644 TagsCloudContainer/WordFilters/IWordFilter.cs diff --git a/TagsCloudContainer/PointGenerators/ArchemedianSpiral.cs b/TagsCloudContainer/PointGenerators/ArchemedianSpiral.cs new file mode 100644 index 00000000..89765fcb --- /dev/null +++ b/TagsCloudContainer/PointGenerators/ArchemedianSpiral.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TagsCloudContainer.PointGenerators +{ + public class ArchemedianSpiral : IPointGenerator + { + public IEnumerable GeneratePoints(Point start) + { + var zoom = 1; + var spiralStep = 0.0; + yield return start; + while (true) + { + spiralStep += Math.PI / 180; + var x = start.X + (int)(zoom * spiralStep * Math.Cos(spiralStep)); + var y = start.Y + (int)(zoom * spiralStep * Math.Sin(spiralStep)); + var next = new Point(x, y); + yield return next; + } + } + } +} diff --git a/TagsCloudContainer/PointGenerators/DeltaShaped.cs b/TagsCloudContainer/PointGenerators/DeltaShaped.cs new file mode 100644 index 00000000..14d2a3dc --- /dev/null +++ b/TagsCloudContainer/PointGenerators/DeltaShaped.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TagsCloudContainer.PointGenerators +{ + public class DeltaShaped : IPointGenerator + { + public IEnumerable GeneratePoints(Point start) + { + var zoom = 5; + yield return start; + while (true) + { + foreach (var pair in Delta()) + { + var x = start.X + (int)(zoom * pair.Item1); + var y = start.Y + (int)(zoom * pair.Item2); + var next = new Point(x, y); + yield return next; + } + zoom += 2; + } + } + + public IEnumerable<(double, double)> Delta() + { + for (var t = 0.0; t < 2 * Math.PI; t += Math.PI / 180) + { + var x = 2 * Math.Cos(t) + Math.Cos(2 * t); + var y = 2 * Math.Sin(t) - Math.Sin(2 * t); + yield return (x, y); + } + } + } +} diff --git a/TagsCloudContainer/PointGenerators/HeartShaped.cs b/TagsCloudContainer/PointGenerators/HeartShaped.cs new file mode 100644 index 00000000..aa87222f --- /dev/null +++ b/TagsCloudContainer/PointGenerators/HeartShaped.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TagsCloudContainer.PointGenerators +{ + public class HeartShaped : IPointGenerator + { + public IEnumerable GeneratePoints(Point start) + { + var zoom = 1; + yield return start; + while (true) + { + foreach (var pair in Heart()) + { + var x = start.X + (int)(zoom * pair.Item1); + var y = start.Y + (int)(zoom * pair.Item2); + var next = new Point(x, y); + yield return next; + } + zoom += 1; + } + } + + public IEnumerable<(double, double)> Heart() + { + for (var t = 0.0; t < 2 * Math.PI; t += Math.PI / 180) + { + var x = 16 * Math.Sin(t) * Math.Sin(t) * Math.Sin(t); + var y = -13 * Math.Cos(t) + 5 * Math.Cos(2 * t) + 2 * Math.Cos(3 * t) + Math.Cos(4 * t); + yield return (x, y); + } + } + } +} diff --git a/TagsCloudContainer/PointGenerators/IPointGenerator.cs b/TagsCloudContainer/PointGenerators/IPointGenerator.cs new file mode 100644 index 00000000..fe0e83ba --- /dev/null +++ b/TagsCloudContainer/PointGenerators/IPointGenerator.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TagsCloudContainer.PointGenerators +{ + public interface IPointGenerator + { + IEnumerable GeneratePoints(Point start); + } +} diff --git a/TagsCloudContainer/Program.cs b/TagsCloudContainer/Program.cs new file mode 100644 index 00000000..dd2f0313 --- /dev/null +++ b/TagsCloudContainer/Program.cs @@ -0,0 +1,10 @@ +namespace TagsCloudContainer +{ + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } + } +} diff --git a/TagsCloudContainer/TagsCloudContainer.csproj b/TagsCloudContainer/TagsCloudContainer.csproj new file mode 100644 index 00000000..2150e379 --- /dev/null +++ b/TagsCloudContainer/TagsCloudContainer.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/TagsCloudContainer/TagsCloudContainer.sln b/TagsCloudContainer/TagsCloudContainer.sln new file mode 100644 index 00000000..9cbcd6a0 --- /dev/null +++ b/TagsCloudContainer/TagsCloudContainer.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.11.35327.3 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudContainer", "TagsCloudContainer.csproj", "{9A86D0EB-2E44-4B2D-A9C9-BCF4C6037DE4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9A86D0EB-2E44-4B2D-A9C9-BCF4C6037DE4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9A86D0EB-2E44-4B2D-A9C9-BCF4C6037DE4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9A86D0EB-2E44-4B2D-A9C9-BCF4C6037DE4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9A86D0EB-2E44-4B2D-A9C9-BCF4C6037DE4}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {37859005-F9FC-4CB8-81DF-92A65CC08048} + EndGlobalSection +EndGlobal diff --git a/TagsCloudContainer/TextProcessor/ITextProcessor.cs b/TagsCloudContainer/TextProcessor/ITextProcessor.cs new file mode 100644 index 00000000..a0b1371a --- /dev/null +++ b/TagsCloudContainer/TextProcessor/ITextProcessor.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TagsCloudContainer.TextProcessor +{ + public interface ITextProcessor + { + public Dictionary Words { get; set; } + } +} diff --git a/TagsCloudContainer/TextProviders/ITextProvider.cs b/TagsCloudContainer/TextProviders/ITextProvider.cs new file mode 100644 index 00000000..94f696eb --- /dev/null +++ b/TagsCloudContainer/TextProviders/ITextProvider.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TagsCloudContainer.TextProviders +{ + public interface ITextProvider + { + public string ReadFile(string filePath); + } +} diff --git a/TagsCloudContainer/TextProviders/TxtTextProvider.cs b/TagsCloudContainer/TextProviders/TxtTextProvider.cs new file mode 100644 index 00000000..d23e2f17 --- /dev/null +++ b/TagsCloudContainer/TextProviders/TxtTextProvider.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TagsCloudContainer.TextProviders +{ + internal class TxtTextProvider : ITextProvider + { + public string ReadFile(string filePath) + { + throw new NotImplementedException(); + } + } +} diff --git a/TagsCloudContainer/Word.cs b/TagsCloudContainer/Word.cs new file mode 100644 index 00000000..3ce4fd80 --- /dev/null +++ b/TagsCloudContainer/Word.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TagsCloudContainer +{ + public class Word + { + public string Value { get; set; } + } +} diff --git a/TagsCloudContainer/WordFilters/IWordFilter.cs b/TagsCloudContainer/WordFilters/IWordFilter.cs new file mode 100644 index 00000000..d581b42d --- /dev/null +++ b/TagsCloudContainer/WordFilters/IWordFilter.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TagsCloudContainer.WordFilters +{ + public interface IWordFilter + { + public bool Pass(Word word); + } +} From 45b91dbce11eeeb13f86f1582c367faf6719d03c Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Wed, 25 Dec 2024 19:02:16 +0500 Subject: [PATCH 02/29] add simple filter --- .../TextProviders/ITextProvider.cs | 11 +++--- .../TextProviders/TxtTextProvider.cs | 14 ++++---- .../WordFilters/SimpleFilter.cs | 35 +++++++++++++++++++ 3 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 TagsCloudContainer/WordFilters/SimpleFilter.cs diff --git a/TagsCloudContainer/TextProviders/ITextProvider.cs b/TagsCloudContainer/TextProviders/ITextProvider.cs index 94f696eb..dc040250 100644 --- a/TagsCloudContainer/TextProviders/ITextProvider.cs +++ b/TagsCloudContainer/TextProviders/ITextProvider.cs @@ -4,10 +4,9 @@ using System.Text; using System.Threading.Tasks; -namespace TagsCloudContainer.TextProviders +namespace TagsCloudContainer.TextProviders; + +public interface ITextProvider { - public interface ITextProvider - { - public string ReadFile(string filePath); - } -} + public string ReadFile(string filePath); +} \ No newline at end of file diff --git a/TagsCloudContainer/TextProviders/TxtTextProvider.cs b/TagsCloudContainer/TextProviders/TxtTextProvider.cs index d23e2f17..b2c9d8a5 100644 --- a/TagsCloudContainer/TextProviders/TxtTextProvider.cs +++ b/TagsCloudContainer/TextProviders/TxtTextProvider.cs @@ -4,13 +4,13 @@ using System.Text; using System.Threading.Tasks; -namespace TagsCloudContainer.TextProviders +namespace TagsCloudContainer.TextProviders; + +public class TxtTextProvider : ITextProvider { - internal class TxtTextProvider : ITextProvider + public string ReadFile(string filePath) { - public string ReadFile(string filePath) - { - throw new NotImplementedException(); - } + if (!File.Exists(filePath)) throw new FileNotFoundException(); + return File.ReadAllText(filePath).ToLower(); } -} +} \ No newline at end of file diff --git a/TagsCloudContainer/WordFilters/SimpleFilter.cs b/TagsCloudContainer/WordFilters/SimpleFilter.cs new file mode 100644 index 00000000..a58938cc --- /dev/null +++ b/TagsCloudContainer/WordFilters/SimpleFilter.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TagsCloudContainer.WordFilters +{ + public class SimpleFilter : IWordFilter + { + private readonly HashSet _words = + [ + "я", + "мы", + "он", + "она", + "оно", + "они" + ]; + public bool Pass(Word word) + { + return !_words.Contains(word.Value); + } + + public void AddBoringWord(Word word) + { + _words.Add(word.Value); + } + + public void AddBoringWord(string word) + { + _words.Add(word); + } + } +} From 441cd696d3e73a0d8b4340e0c5b92d458334aa3e Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Wed, 25 Dec 2024 22:01:24 +0500 Subject: [PATCH 03/29] add textprocessor --- .../TagsCloudContainer.Tests.csproj | 29 +++++++++++++ TagsCloudContainer.Tests/TextFile1.txt | 1 + .../TxtTextProviderShould.cs | 23 +++++++++++ TagsCloudContainer/Program.cs | 13 +++++- TagsCloudContainer/TagsCloudContainer.sln | 6 +++ .../TextProcessor/ITextProcessor.cs | 1 - .../TextProcessor/TextProcessor.cs | 41 +++++++++++++++++++ TagsCloudContainer/Word.cs | 15 ++++++- TagsCloudContainer/WordFilters/IWordFilter.cs | 2 +- .../WordFilters/SimpleFilter.cs | 2 +- 10 files changed, 126 insertions(+), 7 deletions(-) create mode 100644 TagsCloudContainer.Tests/TagsCloudContainer.Tests.csproj create mode 100644 TagsCloudContainer.Tests/TextFile1.txt create mode 100644 TagsCloudContainer.Tests/TxtTextProviderShould.cs create mode 100644 TagsCloudContainer/TextProcessor/TextProcessor.cs diff --git a/TagsCloudContainer.Tests/TagsCloudContainer.Tests.csproj b/TagsCloudContainer.Tests/TagsCloudContainer.Tests.csproj new file mode 100644 index 00000000..7142f9d3 --- /dev/null +++ b/TagsCloudContainer.Tests/TagsCloudContainer.Tests.csproj @@ -0,0 +1,29 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + + diff --git a/TagsCloudContainer.Tests/TextFile1.txt b/TagsCloudContainer.Tests/TextFile1.txt new file mode 100644 index 00000000..30339652 --- /dev/null +++ b/TagsCloudContainer.Tests/TextFile1.txt @@ -0,0 +1 @@ +он ОНА ОНО корзина творог печенье \ No newline at end of file diff --git a/TagsCloudContainer.Tests/TxtTextProviderShould.cs b/TagsCloudContainer.Tests/TxtTextProviderShould.cs new file mode 100644 index 00000000..e0db9a1d --- /dev/null +++ b/TagsCloudContainer.Tests/TxtTextProviderShould.cs @@ -0,0 +1,23 @@ +using TagsCloudContainer.TextProviders; +using FluentAssertions; + +namespace TagsCloudContainer.Tests +{ + public class TxtTextProviderShould + { + private ITextProvider _provider; + [SetUp] + public void Setup() + { + _provider = new TxtTextProvider(); + } + + [Test] + public void ThrowException() + { + Action act = () => _provider.ReadFile("123.txt"); + + act.Should().Throw(); + } + } +} \ No newline at end of file diff --git a/TagsCloudContainer/Program.cs b/TagsCloudContainer/Program.cs index dd2f0313..e32258e1 100644 --- a/TagsCloudContainer/Program.cs +++ b/TagsCloudContainer/Program.cs @@ -1,10 +1,19 @@ -namespace TagsCloudContainer +using TagsCloudContainer.TextProviders; +using TagsCloudContainer.WordFilters; + +namespace TagsCloudContainer { internal class Program { static void Main(string[] args) { - Console.WriteLine("Hello, World!"); + var provider = new TxtTextProvider(); + var filter = new SimpleFilter(); + var processor = new TextProcessor.TextProcessor(@"C:\test\test.txt", provider, filter); + foreach (var word in processor.Words) + { + Console.WriteLine(word.Key.Value + " : " + word.Value); + } } } } diff --git a/TagsCloudContainer/TagsCloudContainer.sln b/TagsCloudContainer/TagsCloudContainer.sln index 9cbcd6a0..cbb75ba6 100644 --- a/TagsCloudContainer/TagsCloudContainer.sln +++ b/TagsCloudContainer/TagsCloudContainer.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 17.11.35327.3 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudContainer", "TagsCloudContainer.csproj", "{9A86D0EB-2E44-4B2D-A9C9-BCF4C6037DE4}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudContainer.Tests", "..\TagsCloudContainer.Tests\TagsCloudContainer.Tests.csproj", "{A6199F1E-60D0-4BC3-8A43-F0E8D0A8F7E7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {9A86D0EB-2E44-4B2D-A9C9-BCF4C6037DE4}.Debug|Any CPU.Build.0 = Debug|Any CPU {9A86D0EB-2E44-4B2D-A9C9-BCF4C6037DE4}.Release|Any CPU.ActiveCfg = Release|Any CPU {9A86D0EB-2E44-4B2D-A9C9-BCF4C6037DE4}.Release|Any CPU.Build.0 = Release|Any CPU + {A6199F1E-60D0-4BC3-8A43-F0E8D0A8F7E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A6199F1E-60D0-4BC3-8A43-F0E8D0A8F7E7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A6199F1E-60D0-4BC3-8A43-F0E8D0A8F7E7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A6199F1E-60D0-4BC3-8A43-F0E8D0A8F7E7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/TagsCloudContainer/TextProcessor/ITextProcessor.cs b/TagsCloudContainer/TextProcessor/ITextProcessor.cs index a0b1371a..78ea8790 100644 --- a/TagsCloudContainer/TextProcessor/ITextProcessor.cs +++ b/TagsCloudContainer/TextProcessor/ITextProcessor.cs @@ -8,6 +8,5 @@ namespace TagsCloudContainer.TextProcessor { public interface ITextProcessor { - public Dictionary Words { get; set; } } } diff --git a/TagsCloudContainer/TextProcessor/TextProcessor.cs b/TagsCloudContainer/TextProcessor/TextProcessor.cs new file mode 100644 index 00000000..14301000 --- /dev/null +++ b/TagsCloudContainer/TextProcessor/TextProcessor.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using TagsCloudContainer.TextProviders; +using TagsCloudContainer.WordFilters; + +namespace TagsCloudContainer.TextProcessor +{ + public class TextProcessor : ITextProcessor + { + public readonly Dictionary Words; + public TextProcessor(string path, ITextProvider provider, params IWordFilter[] filters) + { + Words = []; + foreach (var word in GetWordsFromString(provider.ReadFile(path))) + { + var flag = true; + foreach (var filter in filters) + { + if (flag == false) break; + if (filter.Skips(word)) continue; + flag = false; + } + if (flag == false) continue; + if (!Words.TryGetValue(word, out var value)) + Words.Add(word, 1); + else + Words[word] += 1; + } + } + + private static IEnumerable GetWordsFromString(string input) + { + return Regex.Split(input, @"\W+") + .Select(s => new Word(s)); + } + } +} diff --git a/TagsCloudContainer/Word.cs b/TagsCloudContainer/Word.cs index 3ce4fd80..d7b057d2 100644 --- a/TagsCloudContainer/Word.cs +++ b/TagsCloudContainer/Word.cs @@ -6,8 +6,19 @@ namespace TagsCloudContainer { - public class Word + public class Word(string word) { - public string Value { get; set; } + public string Value { get; set; } = word; + + public override bool Equals(object? obj) + { + var other = obj as Word; + return Value == other.Value; + } + + public override int GetHashCode() + { + return Value.GetHashCode(); + } } } diff --git a/TagsCloudContainer/WordFilters/IWordFilter.cs b/TagsCloudContainer/WordFilters/IWordFilter.cs index d581b42d..d699cf22 100644 --- a/TagsCloudContainer/WordFilters/IWordFilter.cs +++ b/TagsCloudContainer/WordFilters/IWordFilter.cs @@ -8,6 +8,6 @@ namespace TagsCloudContainer.WordFilters { public interface IWordFilter { - public bool Pass(Word word); + public bool Skips(Word word); } } diff --git a/TagsCloudContainer/WordFilters/SimpleFilter.cs b/TagsCloudContainer/WordFilters/SimpleFilter.cs index a58938cc..4c2b9791 100644 --- a/TagsCloudContainer/WordFilters/SimpleFilter.cs +++ b/TagsCloudContainer/WordFilters/SimpleFilter.cs @@ -17,7 +17,7 @@ public class SimpleFilter : IWordFilter "оно", "они" ]; - public bool Pass(Word word) + public bool Skips(Word word) { return !_words.Contains(word.Value); } From 143db64f7d6953af14db99ce4da4e3ac932e2cfd Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Thu, 26 Dec 2024 13:28:51 +0500 Subject: [PATCH 04/29] add some tests for processor, filter, provider --- .../TagsCloudContainer.Tests.csproj | 6 +++++ TagsCloudContainer.Tests/TextFile1.txt | 2 +- .../TextProcessorShould.cs | 25 +++++++++++++++++++ .../TxtTextProviderShould.cs | 13 ++++++++-- .../TextProcessor/TextProcessor.cs | 9 ++++--- 5 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 TagsCloudContainer.Tests/TextProcessorShould.cs diff --git a/TagsCloudContainer.Tests/TagsCloudContainer.Tests.csproj b/TagsCloudContainer.Tests/TagsCloudContainer.Tests.csproj index 7142f9d3..6ff8d572 100644 --- a/TagsCloudContainer.Tests/TagsCloudContainer.Tests.csproj +++ b/TagsCloudContainer.Tests/TagsCloudContainer.Tests.csproj @@ -26,4 +26,10 @@ + + + Always + + + diff --git a/TagsCloudContainer.Tests/TextFile1.txt b/TagsCloudContainer.Tests/TextFile1.txt index 30339652..49da7359 100644 --- a/TagsCloudContainer.Tests/TextFile1.txt +++ b/TagsCloudContainer.Tests/TextFile1.txt @@ -1 +1 @@ -он ОНА ОНО корзина творог печенье \ No newline at end of file +он ОНА ОНО корзина творог печенье Корзина ++ ,- = корЗина \ No newline at end of file diff --git a/TagsCloudContainer.Tests/TextProcessorShould.cs b/TagsCloudContainer.Tests/TextProcessorShould.cs new file mode 100644 index 00000000..b9cbe7e5 --- /dev/null +++ b/TagsCloudContainer.Tests/TextProcessorShould.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FluentAssertions; +using TagsCloudContainer.TextProviders; +using TagsCloudContainer.WordFilters; + +namespace TagsCloudContainer.Tests +{ + public class TextProcessorShould + { + [Test] + public void Process() + { + var processor = new TextProcessor.TextProcessor(@"TextFile1.txt", + new TxtTextProvider(), new SimpleFilter()); + + processor.Words.Count.Should().Be(3); + + processor.Words.MaxBy(word => word.Value).Value.Should().Be(3); + } + } +} diff --git a/TagsCloudContainer.Tests/TxtTextProviderShould.cs b/TagsCloudContainer.Tests/TxtTextProviderShould.cs index e0db9a1d..c1400311 100644 --- a/TagsCloudContainer.Tests/TxtTextProviderShould.cs +++ b/TagsCloudContainer.Tests/TxtTextProviderShould.cs @@ -5,7 +5,7 @@ namespace TagsCloudContainer.Tests { public class TxtTextProviderShould { - private ITextProvider _provider; + private TxtTextProvider _provider; [SetUp] public void Setup() { @@ -15,9 +15,18 @@ public void Setup() [Test] public void ThrowException() { - Action act = () => _provider.ReadFile("123.txt"); + Action act = () => _provider.ReadFile("NotExisted.txt"); act.Should().Throw(); } + + [Test] + public void ReturnLowerCase() + { + var result = _provider.ReadFile("TextFile1.txt"); + + foreach (var c in result.Where(c => char.IsLetter(c))) + char.IsLower(c).Should().BeTrue(); + } } } \ No newline at end of file diff --git a/TagsCloudContainer/TextProcessor/TextProcessor.cs b/TagsCloudContainer/TextProcessor/TextProcessor.cs index 14301000..115a82f0 100644 --- a/TagsCloudContainer/TextProcessor/TextProcessor.cs +++ b/TagsCloudContainer/TextProcessor/TextProcessor.cs @@ -25,7 +25,7 @@ public TextProcessor(string path, ITextProvider provider, params IWordFilter[] f flag = false; } if (flag == false) continue; - if (!Words.TryGetValue(word, out var value)) + if (!Words.TryGetValue(word, out var _)) Words.Add(word, 1); else Words[word] += 1; @@ -34,8 +34,11 @@ public TextProcessor(string path, ITextProvider provider, params IWordFilter[] f private static IEnumerable GetWordsFromString(string input) { - return Regex.Split(input, @"\W+") - .Select(s => new Word(s)); + var regex = new Regex("\\b(?:\\w|-)+\\b"); + + return regex.Matches(input) + .Cast() + .Select(w => new Word(w.Value)); } } } From baf26aa2b74e62959df750a721b86ac931dcdb5f Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Thu, 26 Dec 2024 16:14:19 +0500 Subject: [PATCH 05/29] add layout with some tests --- TagsCloudContainer.Tests/CloudLayoutShould.cs | 58 +++++++++++++++ .../IPointGeneratorShould.cs | 51 ++++++++++++++ TagsCloudContainer/CloudLayout.cs | 70 +++++++++++++++++++ .../PointGenerators/DeltaShaped.cs | 2 +- .../PointGenerators/HeartShaped.cs | 2 +- TagsCloudContainer/TagsCloudContainer.csproj | 4 ++ 6 files changed, 185 insertions(+), 2 deletions(-) create mode 100644 TagsCloudContainer.Tests/CloudLayoutShould.cs create mode 100644 TagsCloudContainer.Tests/IPointGeneratorShould.cs create mode 100644 TagsCloudContainer/CloudLayout.cs diff --git a/TagsCloudContainer.Tests/CloudLayoutShould.cs b/TagsCloudContainer.Tests/CloudLayoutShould.cs new file mode 100644 index 00000000..0771bdd9 --- /dev/null +++ b/TagsCloudContainer.Tests/CloudLayoutShould.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using NUnit.Framework; +using FluentAssertions; +using System.Reflection; +using System.Drawing; +using System.IO; +using System.Runtime.InteropServices; +using FluentAssertions.Execution; +using NUnit.Framework.Interfaces; +using TagsCloudContainer; +using TagsCloudContainer.PointGenerators; + + +namespace TagsCloudContainer.Tests +{ + [TestFixture] + public class CloudLayoutShould + { + private CloudLayout layout; + [TestCase(1, 2, TestName = "Odd coordinate value results in an even size value")] + [TestCase(2, 5, TestName = "Even coordinate value results in an odd size value")] + public void MakeRightSizeLayout(int coordinateValue, int sizeValue) + { + var center = new Point(coordinateValue, coordinateValue); + var size = new Size(sizeValue, sizeValue); + + layout = new CloudLayout(center, new ArchemedianSpiral()); + + layout.Size.Should().BeEquivalentTo(size); + } + + [TestCase(-1, 1, TestName = "Negative X")] + [TestCase(1, -1, TestName = "Negative Y")] + [TestCase(0, 1, TestName = "Zero X")] + [TestCase(1, 0, TestName = "Zero Y")] + public void GetOnlyPositiveCenterCoordinates(int x, int y) + { + Action makeLayout = () => new CloudLayout(new Point(x, y), new ArchemedianSpiral()); + + makeLayout.Should().Throw() + .WithMessage("Center coordinates values have to be greater than Zero"); + } + + [Test] + public void PutNextRectangle_ShouldKeepEnteredSize() + { + layout = new CloudLayout(new Point(5, 5), new ArchemedianSpiral()); + var enteredSize = new Size(3, 4); + var returnedSize = layout.PutNextRectangle(enteredSize).Size; + + returnedSize.Should().BeEquivalentTo(enteredSize); + } + } +} diff --git a/TagsCloudContainer.Tests/IPointGeneratorShould.cs b/TagsCloudContainer.Tests/IPointGeneratorShould.cs new file mode 100644 index 00000000..8e8f03cd --- /dev/null +++ b/TagsCloudContainer.Tests/IPointGeneratorShould.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using NUnit.Framework; +using FluentAssertions; +using FluentAssertions.Execution; +using TagsCloudContainer.PointGenerators; + +namespace TagsCloudContainer.Tests +{ + [TestFixture] + public class IPointGeneratorShould + { + [TestCaseSource(nameof(TestCases))] + public void GeneratePoints_MovingAwayFromTheStartFor(IPointGenerator pointGenerator) + { + var start = new Point(0, 0); + var points = pointGenerator.GeneratePoints(start); + var nearPoint = points.ElementAt(100); + var farPoint = points.ElementAt(1000); + + DistanceBetween(start, nearPoint).Should().BeLessThan(DistanceBetween(start, farPoint)); + } + + [TestCaseSource(nameof(TestCases))] + public void GeneratePoints_ReturnsStartAsFirstPointFor(IPointGenerator pointGenerator) + { + var start = new Point(100, 100); + var firstReturned = pointGenerator.GeneratePoints(start) + .First(); + + firstReturned.Should().BeEquivalentTo(start); + } + + private static IEnumerable TestCases() + { + yield return new ArchemedianSpiral(); + yield return new HeartShaped(); + yield return new DeltaShaped(); + } + + private static int DistanceBetween(Point start, Point destination) + { + return (int)Math.Sqrt((start.X - destination.X) * (start.X - destination.X) + + (start.Y - destination.Y) * (start.Y - destination.Y)); + } + } +} diff --git a/TagsCloudContainer/CloudLayout.cs b/TagsCloudContainer/CloudLayout.cs new file mode 100644 index 00000000..380a645e --- /dev/null +++ b/TagsCloudContainer/CloudLayout.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TagsCloudContainer.PointGenerators; + +namespace TagsCloudContainer +{ + public class CloudLayout + { + public readonly Point Center; + public readonly Size Size; + private readonly IEnumerable _points; + public List Rectangles { get; set; } + + + public CloudLayout(Point center, IPointGenerator pointGenerator) + { + if (center.X <= 0 || center.Y <= 0) + throw new ArgumentException("Center coordinates values have to be greater than Zero"); + Center = center; + Size = CountSize(center); + Rectangles = []; + _points = pointGenerator.GeneratePoints(Center); + } + + public CloudLayout(Size size, IPointGenerator pointGenerator) + { + Size = size; + Center = FindCenter(size); + Rectangles = []; + _points = pointGenerator.GeneratePoints(Center); + } + + + private Size CountSize(Point center) + { + var width = (center.X % 2 == 0) ? center.X * 2 + 1 : Center.X * 2; + var height = (center.Y % 2 == 0) ? center.Y * 2 + 1 : center.Y * 2; + return new Size(width, height); + } + + private static Point FindCenter(Size size) + { + return new Point(size.Width / 2, size.Height / 2); + } + + public Rectangle PutNextRectangle(Size rectangleSize) + { + foreach (var point in _points) + { + var supposed = new Rectangle(new Point(point.X - rectangleSize.Width / 2, point.Y - rectangleSize.Height / 2), + rectangleSize); + if (IntersectsWithAnyOther(supposed, Rectangles)) + continue; + Rectangles.Add(supposed); + return supposed; + } + throw new ArgumentException("Not Enough Points Generated"); + } + + public static bool IntersectsWithAnyOther(Rectangle supposed, List others) + { + return others.Any(x => x.IntersectsWith(supposed)); + } + } +} + diff --git a/TagsCloudContainer/PointGenerators/DeltaShaped.cs b/TagsCloudContainer/PointGenerators/DeltaShaped.cs index 14d2a3dc..57fbf46a 100644 --- a/TagsCloudContainer/PointGenerators/DeltaShaped.cs +++ b/TagsCloudContainer/PointGenerators/DeltaShaped.cs @@ -26,7 +26,7 @@ public IEnumerable GeneratePoints(Point start) } } - public IEnumerable<(double, double)> Delta() + public static IEnumerable<(double, double)> Delta() { for (var t = 0.0; t < 2 * Math.PI; t += Math.PI / 180) { diff --git a/TagsCloudContainer/PointGenerators/HeartShaped.cs b/TagsCloudContainer/PointGenerators/HeartShaped.cs index aa87222f..43e94c27 100644 --- a/TagsCloudContainer/PointGenerators/HeartShaped.cs +++ b/TagsCloudContainer/PointGenerators/HeartShaped.cs @@ -26,7 +26,7 @@ public IEnumerable GeneratePoints(Point start) } } - public IEnumerable<(double, double)> Heart() + public static IEnumerable<(double, double)> Heart() { for (var t = 0.0; t < 2 * Math.PI; t += Math.PI / 180) { diff --git a/TagsCloudContainer/TagsCloudContainer.csproj b/TagsCloudContainer/TagsCloudContainer.csproj index 2150e379..71159cfa 100644 --- a/TagsCloudContainer/TagsCloudContainer.csproj +++ b/TagsCloudContainer/TagsCloudContainer.csproj @@ -7,4 +7,8 @@ enable + + + + From 814975414210f0ab0134ccd40d9d06aa734159fb Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Thu, 26 Dec 2024 17:59:35 +0500 Subject: [PATCH 06/29] add interface for TextProcessor --- TagsCloudContainer.Tests/TextProcessorShould.cs | 6 +++--- TagsCloudContainer/Program.cs | 4 ++-- TagsCloudContainer/TextProcessor/ITextProcessor.cs | 3 +++ TagsCloudContainer/TextProcessor/TextProcessor.cs | 12 ++++++------ 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/TagsCloudContainer.Tests/TextProcessorShould.cs b/TagsCloudContainer.Tests/TextProcessorShould.cs index b9cbe7e5..e6e8c9a8 100644 --- a/TagsCloudContainer.Tests/TextProcessorShould.cs +++ b/TagsCloudContainer.Tests/TextProcessorShould.cs @@ -14,12 +14,12 @@ public class TextProcessorShould [Test] public void Process() { - var processor = new TextProcessor.TextProcessor(@"TextFile1.txt", + var result = new TextProcessor.TextProcessor().Words(@"TextFile1.txt", new TxtTextProvider(), new SimpleFilter()); - processor.Words.Count.Should().Be(3); + result.Count.Should().Be(3); - processor.Words.MaxBy(word => word.Value).Value.Should().Be(3); + result.MaxBy(word => word.Value).Value.Should().Be(3); } } } diff --git a/TagsCloudContainer/Program.cs b/TagsCloudContainer/Program.cs index e32258e1..bf463b9c 100644 --- a/TagsCloudContainer/Program.cs +++ b/TagsCloudContainer/Program.cs @@ -9,8 +9,8 @@ static void Main(string[] args) { var provider = new TxtTextProvider(); var filter = new SimpleFilter(); - var processor = new TextProcessor.TextProcessor(@"C:\test\test.txt", provider, filter); - foreach (var word in processor.Words) + var processor = new TextProcessor.TextProcessor(); + foreach (var word in processor.Words(@"C:\test\test.txt", provider, filter)) { Console.WriteLine(word.Key.Value + " : " + word.Value); } diff --git a/TagsCloudContainer/TextProcessor/ITextProcessor.cs b/TagsCloudContainer/TextProcessor/ITextProcessor.cs index 78ea8790..ef7f49b3 100644 --- a/TagsCloudContainer/TextProcessor/ITextProcessor.cs +++ b/TagsCloudContainer/TextProcessor/ITextProcessor.cs @@ -3,10 +3,13 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using TagsCloudContainer.TextProviders; +using TagsCloudContainer.WordFilters; namespace TagsCloudContainer.TextProcessor { public interface ITextProcessor { + public Dictionary Words(string path, ITextProvider provider, params IWordFilter[] filters); } } diff --git a/TagsCloudContainer/TextProcessor/TextProcessor.cs b/TagsCloudContainer/TextProcessor/TextProcessor.cs index 115a82f0..74086c35 100644 --- a/TagsCloudContainer/TextProcessor/TextProcessor.cs +++ b/TagsCloudContainer/TextProcessor/TextProcessor.cs @@ -11,10 +11,9 @@ namespace TagsCloudContainer.TextProcessor { public class TextProcessor : ITextProcessor { - public readonly Dictionary Words; - public TextProcessor(string path, ITextProvider provider, params IWordFilter[] filters) + public Dictionary Words(string path, ITextProvider provider, params IWordFilter[] filters) { - Words = []; + var words = new Dictionary(); foreach (var word in GetWordsFromString(provider.ReadFile(path))) { var flag = true; @@ -25,11 +24,12 @@ public TextProcessor(string path, ITextProvider provider, params IWordFilter[] f flag = false; } if (flag == false) continue; - if (!Words.TryGetValue(word, out var _)) - Words.Add(word, 1); + if (!words.TryGetValue(word, out var _)) + words.Add(word, 1); else - Words[word] += 1; + words[word] += 1; } + return words; } private static IEnumerable GetWordsFromString(string input) From 1b8576460b89f5610535d3e771625b32ec3d5cb4 Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Thu, 26 Dec 2024 18:01:24 +0500 Subject: [PATCH 07/29] add Tag and interface for Tag generator --- TagsCloudContainer/Tag.cs | 26 +++++++++++++++++++ .../TagGenerator/ITagsGenerator.cs | 14 ++++++++++ .../TagGenerator/RandomColorTagGenerator.cs | 17 ++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 TagsCloudContainer/Tag.cs create mode 100644 TagsCloudContainer/TagGenerator/ITagsGenerator.cs create mode 100644 TagsCloudContainer/TagGenerator/RandomColorTagGenerator.cs diff --git a/TagsCloudContainer/Tag.cs b/TagsCloudContainer/Tag.cs new file mode 100644 index 00000000..4ae341ec --- /dev/null +++ b/TagsCloudContainer/Tag.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Mime; +using System.Text; +using System.Threading.Tasks; +using System.Drawing; + +namespace TagsCloudContainer; + +public class Tag +{ + public readonly string Value; + public readonly Font Font; + public readonly Color Color; + public readonly Size Frame; + + public Tag(Graphics g, Word word, Font font, Color color) + { + Value = word.Value; + Font = font; + Color = color; + var rect = g.MeasureString(Value, Font).ToSize(); + Frame = new Size(rect.Width + 2, rect.Height + 2); + } +} \ No newline at end of file diff --git a/TagsCloudContainer/TagGenerator/ITagsGenerator.cs b/TagsCloudContainer/TagGenerator/ITagsGenerator.cs new file mode 100644 index 00000000..d78cf983 --- /dev/null +++ b/TagsCloudContainer/TagGenerator/ITagsGenerator.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TagsCloudContainer.TextProcessor; + +namespace TagsCloudContainer.TagGenerator +{ + public interface ITagsGenerator + { + IEnumerable GetTags(ITextProcessor processor); + } +} diff --git a/TagsCloudContainer/TagGenerator/RandomColorTagGenerator.cs b/TagsCloudContainer/TagGenerator/RandomColorTagGenerator.cs new file mode 100644 index 00000000..6580ad43 --- /dev/null +++ b/TagsCloudContainer/TagGenerator/RandomColorTagGenerator.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TagsCloudContainer.TextProcessor; + +namespace TagsCloudContainer.TagGenerator +{ + public class RandomColorTagGenerator : ITagsGenerator + { + public IEnumerable GetTags(ITextProcessor processor) + { + throw new NotImplementedException(); + } + } +} From 3f413ead857a5652221cc02d016b1b0b5304e883 Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Fri, 27 Dec 2024 14:16:11 +0500 Subject: [PATCH 08/29] remove and sort usings, text processor refactoring --- TagsCloudContainer.Tests/CloudLayoutShould.cs | 14 +------- .../IPointGeneratorShould.cs | 9 +---- .../TextProcessorShould.cs | 9 ++--- .../TxtTextProviderShould.cs | 2 +- TagsCloudContainer/CloudLayout.cs | 7 +--- .../PointGenerators/ArchemedianSpiral.cs | 7 +--- .../PointGenerators/DeltaShaped.cs | 7 +--- .../PointGenerators/HeartShaped.cs | 7 +--- .../PointGenerators/IPointGenerator.cs | 7 +--- TagsCloudContainer/Program.cs | 2 +- TagsCloudContainer/Tag.cs | 8 +---- .../TagGenerator/ITagsGenerator.cs | 7 +--- .../TagGenerator/RandomColorTagGenerator.cs | 7 +--- .../TextProcessor/ITextProcessor.cs | 7 +--- .../TextProcessor/TextProcessor.cs | 25 ++++--------- .../TextProviders/ITextProvider.cs | 8 +---- .../TextProviders/TxtTextProvider.cs | 8 +---- TagsCloudContainer/Word.cs | 8 +---- .../WordFilters/BoringWordFilter.cs | 29 +++++++++++++++ TagsCloudContainer/WordFilters/IWordFilter.cs | 8 +---- .../WordFilters/SimpleFilter.cs | 35 ------------------- 21 files changed, 55 insertions(+), 166 deletions(-) create mode 100644 TagsCloudContainer/WordFilters/BoringWordFilter.cs delete mode 100644 TagsCloudContainer/WordFilters/SimpleFilter.cs diff --git a/TagsCloudContainer.Tests/CloudLayoutShould.cs b/TagsCloudContainer.Tests/CloudLayoutShould.cs index 0771bdd9..84905921 100644 --- a/TagsCloudContainer.Tests/CloudLayoutShould.cs +++ b/TagsCloudContainer.Tests/CloudLayoutShould.cs @@ -1,17 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using NUnit.Framework; -using FluentAssertions; -using System.Reflection; +using FluentAssertions; using System.Drawing; -using System.IO; -using System.Runtime.InteropServices; -using FluentAssertions.Execution; -using NUnit.Framework.Interfaces; -using TagsCloudContainer; using TagsCloudContainer.PointGenerators; diff --git a/TagsCloudContainer.Tests/IPointGeneratorShould.cs b/TagsCloudContainer.Tests/IPointGeneratorShould.cs index 8e8f03cd..7d0b0061 100644 --- a/TagsCloudContainer.Tests/IPointGeneratorShould.cs +++ b/TagsCloudContainer.Tests/IPointGeneratorShould.cs @@ -1,12 +1,5 @@ -using System; -using System.Collections.Generic; +using FluentAssertions; using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using NUnit.Framework; -using FluentAssertions; -using FluentAssertions.Execution; using TagsCloudContainer.PointGenerators; namespace TagsCloudContainer.Tests diff --git a/TagsCloudContainer.Tests/TextProcessorShould.cs b/TagsCloudContainer.Tests/TextProcessorShould.cs index e6e8c9a8..05162811 100644 --- a/TagsCloudContainer.Tests/TextProcessorShould.cs +++ b/TagsCloudContainer.Tests/TextProcessorShould.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using FluentAssertions; +using FluentAssertions; using TagsCloudContainer.TextProviders; using TagsCloudContainer.WordFilters; @@ -15,7 +10,7 @@ public class TextProcessorShould public void Process() { var result = new TextProcessor.TextProcessor().Words(@"TextFile1.txt", - new TxtTextProvider(), new SimpleFilter()); + new TxtTextProvider(), new BoringWordFilter()); result.Count.Should().Be(3); diff --git a/TagsCloudContainer.Tests/TxtTextProviderShould.cs b/TagsCloudContainer.Tests/TxtTextProviderShould.cs index c1400311..b028cc68 100644 --- a/TagsCloudContainer.Tests/TxtTextProviderShould.cs +++ b/TagsCloudContainer.Tests/TxtTextProviderShould.cs @@ -1,5 +1,5 @@ -using TagsCloudContainer.TextProviders; using FluentAssertions; +using TagsCloudContainer.TextProviders; namespace TagsCloudContainer.Tests { diff --git a/TagsCloudContainer/CloudLayout.cs b/TagsCloudContainer/CloudLayout.cs index 380a645e..1a40cf39 100644 --- a/TagsCloudContainer/CloudLayout.cs +++ b/TagsCloudContainer/CloudLayout.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Drawing; using TagsCloudContainer.PointGenerators; namespace TagsCloudContainer diff --git a/TagsCloudContainer/PointGenerators/ArchemedianSpiral.cs b/TagsCloudContainer/PointGenerators/ArchemedianSpiral.cs index 89765fcb..d88b9d6b 100644 --- a/TagsCloudContainer/PointGenerators/ArchemedianSpiral.cs +++ b/TagsCloudContainer/PointGenerators/ArchemedianSpiral.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Drawing; namespace TagsCloudContainer.PointGenerators { diff --git a/TagsCloudContainer/PointGenerators/DeltaShaped.cs b/TagsCloudContainer/PointGenerators/DeltaShaped.cs index 57fbf46a..fd6ee263 100644 --- a/TagsCloudContainer/PointGenerators/DeltaShaped.cs +++ b/TagsCloudContainer/PointGenerators/DeltaShaped.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Drawing; namespace TagsCloudContainer.PointGenerators { diff --git a/TagsCloudContainer/PointGenerators/HeartShaped.cs b/TagsCloudContainer/PointGenerators/HeartShaped.cs index 43e94c27..da49931c 100644 --- a/TagsCloudContainer/PointGenerators/HeartShaped.cs +++ b/TagsCloudContainer/PointGenerators/HeartShaped.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Drawing; namespace TagsCloudContainer.PointGenerators { diff --git a/TagsCloudContainer/PointGenerators/IPointGenerator.cs b/TagsCloudContainer/PointGenerators/IPointGenerator.cs index fe0e83ba..ad4ad966 100644 --- a/TagsCloudContainer/PointGenerators/IPointGenerator.cs +++ b/TagsCloudContainer/PointGenerators/IPointGenerator.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Drawing; namespace TagsCloudContainer.PointGenerators { diff --git a/TagsCloudContainer/Program.cs b/TagsCloudContainer/Program.cs index bf463b9c..8729eaa1 100644 --- a/TagsCloudContainer/Program.cs +++ b/TagsCloudContainer/Program.cs @@ -8,7 +8,7 @@ internal class Program static void Main(string[] args) { var provider = new TxtTextProvider(); - var filter = new SimpleFilter(); + var filter = new BoringWordFilter(); var processor = new TextProcessor.TextProcessor(); foreach (var word in processor.Words(@"C:\test\test.txt", provider, filter)) { diff --git a/TagsCloudContainer/Tag.cs b/TagsCloudContainer/Tag.cs index 4ae341ec..d5c62ed2 100644 --- a/TagsCloudContainer/Tag.cs +++ b/TagsCloudContainer/Tag.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.Mime; -using System.Text; -using System.Threading.Tasks; -using System.Drawing; +using System.Drawing; namespace TagsCloudContainer; diff --git a/TagsCloudContainer/TagGenerator/ITagsGenerator.cs b/TagsCloudContainer/TagGenerator/ITagsGenerator.cs index d78cf983..29513981 100644 --- a/TagsCloudContainer/TagGenerator/ITagsGenerator.cs +++ b/TagsCloudContainer/TagGenerator/ITagsGenerator.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TagsCloudContainer.TextProcessor; +using TagsCloudContainer.TextProcessor; namespace TagsCloudContainer.TagGenerator { diff --git a/TagsCloudContainer/TagGenerator/RandomColorTagGenerator.cs b/TagsCloudContainer/TagGenerator/RandomColorTagGenerator.cs index 6580ad43..ae17db86 100644 --- a/TagsCloudContainer/TagGenerator/RandomColorTagGenerator.cs +++ b/TagsCloudContainer/TagGenerator/RandomColorTagGenerator.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TagsCloudContainer.TextProcessor; +using TagsCloudContainer.TextProcessor; namespace TagsCloudContainer.TagGenerator { diff --git a/TagsCloudContainer/TextProcessor/ITextProcessor.cs b/TagsCloudContainer/TextProcessor/ITextProcessor.cs index ef7f49b3..aa3f2e39 100644 --- a/TagsCloudContainer/TextProcessor/ITextProcessor.cs +++ b/TagsCloudContainer/TextProcessor/ITextProcessor.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TagsCloudContainer.TextProviders; +using TagsCloudContainer.TextProviders; using TagsCloudContainer.WordFilters; namespace TagsCloudContainer.TextProcessor diff --git a/TagsCloudContainer/TextProcessor/TextProcessor.cs b/TagsCloudContainer/TextProcessor/TextProcessor.cs index 74086c35..4e2f0101 100644 --- a/TagsCloudContainer/TextProcessor/TextProcessor.cs +++ b/TagsCloudContainer/TextProcessor/TextProcessor.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Text.RegularExpressions; -using System.Threading.Tasks; +using System.Text.RegularExpressions; using TagsCloudContainer.TextProviders; using TagsCloudContainer.WordFilters; @@ -16,18 +11,12 @@ public Dictionary Words(string path, ITextProvider provider, params I var words = new Dictionary(); foreach (var word in GetWordsFromString(provider.ReadFile(path))) { - var flag = true; - foreach (var filter in filters) - { - if (flag == false) break; - if (filter.Skips(word)) continue; - flag = false; - } - if (flag == false) continue; - if (!words.TryGetValue(word, out var _)) - words.Add(word, 1); - else - words[word] += 1; + if (filters.All(filter => !filter.Skips(word))) + continue; + + if (!words.ContainsKey(word)) + words.Add(word, 0); + words[word]++; } return words; } diff --git a/TagsCloudContainer/TextProviders/ITextProvider.cs b/TagsCloudContainer/TextProviders/ITextProvider.cs index dc040250..91349a35 100644 --- a/TagsCloudContainer/TextProviders/ITextProvider.cs +++ b/TagsCloudContainer/TextProviders/ITextProvider.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace TagsCloudContainer.TextProviders; +namespace TagsCloudContainer.TextProviders; public interface ITextProvider { diff --git a/TagsCloudContainer/TextProviders/TxtTextProvider.cs b/TagsCloudContainer/TextProviders/TxtTextProvider.cs index b2c9d8a5..a0aba801 100644 --- a/TagsCloudContainer/TextProviders/TxtTextProvider.cs +++ b/TagsCloudContainer/TextProviders/TxtTextProvider.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace TagsCloudContainer.TextProviders; +namespace TagsCloudContainer.TextProviders; public class TxtTextProvider : ITextProvider { diff --git a/TagsCloudContainer/Word.cs b/TagsCloudContainer/Word.cs index d7b057d2..296bb36a 100644 --- a/TagsCloudContainer/Word.cs +++ b/TagsCloudContainer/Word.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace TagsCloudContainer +namespace TagsCloudContainer { public class Word(string word) { diff --git a/TagsCloudContainer/WordFilters/BoringWordFilter.cs b/TagsCloudContainer/WordFilters/BoringWordFilter.cs new file mode 100644 index 00000000..994b1a23 --- /dev/null +++ b/TagsCloudContainer/WordFilters/BoringWordFilter.cs @@ -0,0 +1,29 @@ +namespace TagsCloudContainer.WordFilters +{ + public class BoringWordFilter : IWordFilter + { + private readonly HashSet _forbiddenWords = + [ + "я", + "мы", + "он", + "она", + "оно", + "они" + ]; + public bool Skips(Word word) + { + return !_forbiddenWords.Contains(word.Value); + } + + public void AddBoringWord(Word word) + { + _forbiddenWords.Add(word.Value); + } + + public void AddBoringWord(string word) + { + _forbiddenWords.Add(word); + } + } +} diff --git a/TagsCloudContainer/WordFilters/IWordFilter.cs b/TagsCloudContainer/WordFilters/IWordFilter.cs index d699cf22..0dd8c469 100644 --- a/TagsCloudContainer/WordFilters/IWordFilter.cs +++ b/TagsCloudContainer/WordFilters/IWordFilter.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace TagsCloudContainer.WordFilters +namespace TagsCloudContainer.WordFilters { public interface IWordFilter { diff --git a/TagsCloudContainer/WordFilters/SimpleFilter.cs b/TagsCloudContainer/WordFilters/SimpleFilter.cs deleted file mode 100644 index 4c2b9791..00000000 --- a/TagsCloudContainer/WordFilters/SimpleFilter.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace TagsCloudContainer.WordFilters -{ - public class SimpleFilter : IWordFilter - { - private readonly HashSet _words = - [ - "я", - "мы", - "он", - "она", - "оно", - "они" - ]; - public bool Skips(Word word) - { - return !_words.Contains(word.Value); - } - - public void AddBoringWord(Word word) - { - _words.Add(word.Value); - } - - public void AddBoringWord(string word) - { - _words.Add(word); - } - } -} From 29ea26020576218a8c83e8739c1c1e34ccbf4fd5 Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Fri, 27 Dec 2024 14:22:41 +0500 Subject: [PATCH 09/29] Word became record --- TagsCloudContainer/Word.cs | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/TagsCloudContainer/Word.cs b/TagsCloudContainer/Word.cs index 296bb36a..45b2877b 100644 --- a/TagsCloudContainer/Word.cs +++ b/TagsCloudContainer/Word.cs @@ -1,18 +1,4 @@ namespace TagsCloudContainer { - public class Word(string word) - { - public string Value { get; set; } = word; - - public override bool Equals(object? obj) - { - var other = obj as Word; - return Value == other.Value; - } - - public override int GetHashCode() - { - return Value.GetHashCode(); - } - } + public record Word(string Value); } From e9254ec49e051b8ada9014bc0d4a4cd9ed31fae4 Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Fri, 27 Dec 2024 14:36:26 +0500 Subject: [PATCH 10/29] add interface for string parsers --- .../TextProcessorShould.cs | 3 ++- .../TxtTextProviderShould.cs | 2 +- TagsCloudContainer/Program.cs | 6 ++++-- .../StringParsers/IStringParser.cs | 13 ++++++++++++ .../StringParsers/RegexParser.cs | 21 +++++++++++++++++++ .../TextProcessor/ITextProcessor.cs | 5 +++-- .../TextProcessor/TextProcessor.cs | 14 +++---------- 7 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 TagsCloudContainer/StringParsers/IStringParser.cs create mode 100644 TagsCloudContainer/StringParsers/RegexParser.cs diff --git a/TagsCloudContainer.Tests/TextProcessorShould.cs b/TagsCloudContainer.Tests/TextProcessorShould.cs index 05162811..82e0ea04 100644 --- a/TagsCloudContainer.Tests/TextProcessorShould.cs +++ b/TagsCloudContainer.Tests/TextProcessorShould.cs @@ -1,4 +1,5 @@ using FluentAssertions; +using TagsCloudContainer.StringParsers; using TagsCloudContainer.TextProviders; using TagsCloudContainer.WordFilters; @@ -10,7 +11,7 @@ public class TextProcessorShould public void Process() { var result = new TextProcessor.TextProcessor().Words(@"TextFile1.txt", - new TxtTextProvider(), new BoringWordFilter()); + new TxtTextProvider(), new RegexParser(),new BoringWordFilter()); result.Count.Should().Be(3); diff --git a/TagsCloudContainer.Tests/TxtTextProviderShould.cs b/TagsCloudContainer.Tests/TxtTextProviderShould.cs index b028cc68..ef395abc 100644 --- a/TagsCloudContainer.Tests/TxtTextProviderShould.cs +++ b/TagsCloudContainer.Tests/TxtTextProviderShould.cs @@ -13,7 +13,7 @@ public void Setup() } [Test] - public void ThrowException() + public void ThrowExceptionIfFileNotFounded() { Action act = () => _provider.ReadFile("NotExisted.txt"); diff --git a/TagsCloudContainer/Program.cs b/TagsCloudContainer/Program.cs index 8729eaa1..d3da4e1f 100644 --- a/TagsCloudContainer/Program.cs +++ b/TagsCloudContainer/Program.cs @@ -1,4 +1,5 @@ -using TagsCloudContainer.TextProviders; +using TagsCloudContainer.StringParsers; +using TagsCloudContainer.TextProviders; using TagsCloudContainer.WordFilters; namespace TagsCloudContainer @@ -10,7 +11,8 @@ static void Main(string[] args) var provider = new TxtTextProvider(); var filter = new BoringWordFilter(); var processor = new TextProcessor.TextProcessor(); - foreach (var word in processor.Words(@"C:\test\test.txt", provider, filter)) + var parser = new RegexParser(); + foreach (var word in processor.Words(@"C:\test\test.txt", provider, parser, filter)) { Console.WriteLine(word.Key.Value + " : " + word.Value); } diff --git a/TagsCloudContainer/StringParsers/IStringParser.cs b/TagsCloudContainer/StringParsers/IStringParser.cs new file mode 100644 index 00000000..0faf6e12 --- /dev/null +++ b/TagsCloudContainer/StringParsers/IStringParser.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TagsCloudContainer.StringParsers +{ + public interface IStringParser + { + public IEnumerable GetWordsFromString(string input); + } +} diff --git a/TagsCloudContainer/StringParsers/RegexParser.cs b/TagsCloudContainer/StringParsers/RegexParser.cs new file mode 100644 index 00000000..47bce418 --- /dev/null +++ b/TagsCloudContainer/StringParsers/RegexParser.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace TagsCloudContainer.StringParsers +{ + public class RegexParser : IStringParser + { + public IEnumerable GetWordsFromString(string input) + { + var regex = new Regex("\\b(?:\\w|-)+\\b"); + + return regex.Matches(input) + .Cast() + .Select(w => new Word(w.Value)); + } + } +} diff --git a/TagsCloudContainer/TextProcessor/ITextProcessor.cs b/TagsCloudContainer/TextProcessor/ITextProcessor.cs index aa3f2e39..cd68989f 100644 --- a/TagsCloudContainer/TextProcessor/ITextProcessor.cs +++ b/TagsCloudContainer/TextProcessor/ITextProcessor.cs @@ -1,10 +1,11 @@ -using TagsCloudContainer.TextProviders; +using TagsCloudContainer.StringParsers; +using TagsCloudContainer.TextProviders; using TagsCloudContainer.WordFilters; namespace TagsCloudContainer.TextProcessor { public interface ITextProcessor { - public Dictionary Words(string path, ITextProvider provider, params IWordFilter[] filters); + public Dictionary Words(string path, ITextProvider provider, IStringParser parser, params IWordFilter[] filters); } } diff --git a/TagsCloudContainer/TextProcessor/TextProcessor.cs b/TagsCloudContainer/TextProcessor/TextProcessor.cs index 4e2f0101..18045066 100644 --- a/TagsCloudContainer/TextProcessor/TextProcessor.cs +++ b/TagsCloudContainer/TextProcessor/TextProcessor.cs @@ -1,4 +1,5 @@ using System.Text.RegularExpressions; +using TagsCloudContainer.StringParsers; using TagsCloudContainer.TextProviders; using TagsCloudContainer.WordFilters; @@ -6,10 +7,10 @@ namespace TagsCloudContainer.TextProcessor { public class TextProcessor : ITextProcessor { - public Dictionary Words(string path, ITextProvider provider, params IWordFilter[] filters) + public Dictionary Words(string path, ITextProvider provider, IStringParser parser, params IWordFilter[] filters) { var words = new Dictionary(); - foreach (var word in GetWordsFromString(provider.ReadFile(path))) + foreach (var word in parser.GetWordsFromString(provider.ReadFile(path))) { if (filters.All(filter => !filter.Skips(word))) continue; @@ -20,14 +21,5 @@ public Dictionary Words(string path, ITextProvider provider, params I } return words; } - - private static IEnumerable GetWordsFromString(string input) - { - var regex = new Regex("\\b(?:\\w|-)+\\b"); - - return regex.Matches(input) - .Cast() - .Select(w => new Word(w.Value)); - } } } From 5259a193695199ab92528922bbcd270b6ea39fc5 Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Fri, 27 Dec 2024 17:06:23 +0500 Subject: [PATCH 11/29] add rnd color tag generator --- .../RandomColorTagGeneratorShould.cs | 33 +++++++++++++++++++ .../TextProcessorShould.cs | 4 +-- TagsCloudContainer/Program.cs | 4 +-- .../TagGenerator/ITagsGenerator.cs | 7 ++-- .../TagGenerator/RandomColorTagGenerator.cs | 27 ++++++++++++--- .../TextProcessor/ITextProcessor.cs | 2 +- .../TextProcessor/TextProcessor.cs | 5 +-- 7 files changed, 69 insertions(+), 13 deletions(-) create mode 100644 TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs diff --git a/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs b/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs new file mode 100644 index 00000000..72a9aa3a --- /dev/null +++ b/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FluentAssertions; +using TagsCloudContainer.StringParsers; +using TagsCloudContainer.TagGenerator; +using TagsCloudContainer.TextProviders; +using TagsCloudContainer.WordFilters; + +namespace TagsCloudContainer.Tests +{ + public class RandomColorTagGeneratorShould + { + [Test] + public void SetRightFontSize() + { + var bitmap = new Bitmap(1, 1); + using var graphics = Graphics.FromImage(bitmap); + + var processor = new TextProcessor.TextProcessor(@"TextFile1.txt", + new TxtTextProvider(), new RegexParser(), new BoringWordFilter()); + var generator = new RandomColorTagGenerator(processor, graphics, new Font("arial", 12)); + var result = generator.GenerateTags().First(); + + + result.Font.Name.Should().Be("Arial"); + result.Font.Size.Should().Be(36); + } + } +} diff --git a/TagsCloudContainer.Tests/TextProcessorShould.cs b/TagsCloudContainer.Tests/TextProcessorShould.cs index 82e0ea04..04d20060 100644 --- a/TagsCloudContainer.Tests/TextProcessorShould.cs +++ b/TagsCloudContainer.Tests/TextProcessorShould.cs @@ -10,8 +10,8 @@ public class TextProcessorShould [Test] public void Process() { - var result = new TextProcessor.TextProcessor().Words(@"TextFile1.txt", - new TxtTextProvider(), new RegexParser(),new BoringWordFilter()); + var result = new TextProcessor.TextProcessor(@"TextFile1.txt", + new TxtTextProvider(), new RegexParser(), new BoringWordFilter()).Words(); result.Count.Should().Be(3); diff --git a/TagsCloudContainer/Program.cs b/TagsCloudContainer/Program.cs index d3da4e1f..dbd20d62 100644 --- a/TagsCloudContainer/Program.cs +++ b/TagsCloudContainer/Program.cs @@ -10,9 +10,9 @@ static void Main(string[] args) { var provider = new TxtTextProvider(); var filter = new BoringWordFilter(); - var processor = new TextProcessor.TextProcessor(); var parser = new RegexParser(); - foreach (var word in processor.Words(@"C:\test\test.txt", provider, parser, filter)) + var processor = new TextProcessor.TextProcessor(@"C:\test\test.txt", provider, parser, filter); + foreach (var word in processor.Words()) { Console.WriteLine(word.Key.Value + " : " + word.Value); } diff --git a/TagsCloudContainer/TagGenerator/ITagsGenerator.cs b/TagsCloudContainer/TagGenerator/ITagsGenerator.cs index 29513981..96c48cb0 100644 --- a/TagsCloudContainer/TagGenerator/ITagsGenerator.cs +++ b/TagsCloudContainer/TagGenerator/ITagsGenerator.cs @@ -1,9 +1,12 @@ -using TagsCloudContainer.TextProcessor; +using TagsCloudContainer.StringParsers; +using TagsCloudContainer.TextProcessor; +using TagsCloudContainer.TextProviders; +using TagsCloudContainer.WordFilters; namespace TagsCloudContainer.TagGenerator { public interface ITagsGenerator { - IEnumerable GetTags(ITextProcessor processor); + IEnumerable GenerateTags(); } } diff --git a/TagsCloudContainer/TagGenerator/RandomColorTagGenerator.cs b/TagsCloudContainer/TagGenerator/RandomColorTagGenerator.cs index ae17db86..5832484c 100644 --- a/TagsCloudContainer/TagGenerator/RandomColorTagGenerator.cs +++ b/TagsCloudContainer/TagGenerator/RandomColorTagGenerator.cs @@ -1,12 +1,31 @@ -using TagsCloudContainer.TextProcessor; +using System.Drawing; +using System.Linq; +using TagsCloudContainer.StringParsers; +using TagsCloudContainer.TextProcessor; +using TagsCloudContainer.TextProviders; +using TagsCloudContainer.WordFilters; namespace TagsCloudContainer.TagGenerator { - public class RandomColorTagGenerator : ITagsGenerator + public class RandomColorTagGenerator(ITextProcessor processor, Graphics graphics, Font defaultFont) : ITagsGenerator { - public IEnumerable GetTags(ITextProcessor processor) + private static readonly Random Random = new(); + + public IEnumerable GenerateTags() + { + return processor.Words() + .Select(kvp => new Tag(graphics, kvp.Key, SetFont(defaultFont, kvp.Value), GetRandomColor())); + } + + + private static Font SetFont(Font font, int amount) + { + return new Font(font.FontFamily, font.Size * amount); + } + + private static Color GetRandomColor() { - throw new NotImplementedException(); + return Color.FromArgb(Random.Next(50, 255), Random.Next(0, 255), Random.Next(0, 255), Random.Next(0, 255)); } } } diff --git a/TagsCloudContainer/TextProcessor/ITextProcessor.cs b/TagsCloudContainer/TextProcessor/ITextProcessor.cs index cd68989f..9f331c2a 100644 --- a/TagsCloudContainer/TextProcessor/ITextProcessor.cs +++ b/TagsCloudContainer/TextProcessor/ITextProcessor.cs @@ -6,6 +6,6 @@ namespace TagsCloudContainer.TextProcessor { public interface ITextProcessor { - public Dictionary Words(string path, ITextProvider provider, IStringParser parser, params IWordFilter[] filters); + public Dictionary Words(); } } diff --git a/TagsCloudContainer/TextProcessor/TextProcessor.cs b/TagsCloudContainer/TextProcessor/TextProcessor.cs index 18045066..81a1d341 100644 --- a/TagsCloudContainer/TextProcessor/TextProcessor.cs +++ b/TagsCloudContainer/TextProcessor/TextProcessor.cs @@ -5,9 +5,10 @@ namespace TagsCloudContainer.TextProcessor { - public class TextProcessor : ITextProcessor + public class TextProcessor(string path, ITextProvider provider, IStringParser parser, + params IWordFilter[] filters) : ITextProcessor { - public Dictionary Words(string path, ITextProvider provider, IStringParser parser, params IWordFilter[] filters) + public Dictionary Words() { var words = new Dictionary(); foreach (var word in parser.GetWordsFromString(provider.ReadFile(path))) From 78bb80a9998d277d5cd2de034f3c7ae3bb60de79 Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Fri, 3 Jan 2025 13:40:22 +0500 Subject: [PATCH 12/29] add shortwordfilter --- .../RandomColorTagGeneratorShould.cs | 8 +------- TagsCloudContainer.Tests/TextFile1.txt | 2 +- TagsCloudContainer.Tests/TextProcessorShould.cs | 2 +- .../TextProcessor/TextProcessor.cs | 2 +- .../WordFilters/ShortWordFilter.cs | 16 ++++++++++++++++ 5 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 TagsCloudContainer/WordFilters/ShortWordFilter.cs diff --git a/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs b/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs index 72a9aa3a..bb9ac472 100644 --- a/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs +++ b/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs @@ -1,10 +1,5 @@ -using System; -using System.Collections.Generic; +using FluentAssertions; using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using FluentAssertions; using TagsCloudContainer.StringParsers; using TagsCloudContainer.TagGenerator; using TagsCloudContainer.TextProviders; @@ -24,7 +19,6 @@ public void SetRightFontSize() new TxtTextProvider(), new RegexParser(), new BoringWordFilter()); var generator = new RandomColorTagGenerator(processor, graphics, new Font("arial", 12)); var result = generator.GenerateTags().First(); - result.Font.Name.Should().Be("Arial"); result.Font.Size.Should().Be(36); diff --git a/TagsCloudContainer.Tests/TextFile1.txt b/TagsCloudContainer.Tests/TextFile1.txt index 49da7359..4bd0f5da 100644 --- a/TagsCloudContainer.Tests/TextFile1.txt +++ b/TagsCloudContainer.Tests/TextFile1.txt @@ -1 +1 @@ -он ОНА ОНО корзина творог печенье Корзина ++ ,- = корЗина \ No newline at end of file +он ОНА ОНО корзина творог печенье Корзина ++ ,- = корЗина по ха за \ No newline at end of file diff --git a/TagsCloudContainer.Tests/TextProcessorShould.cs b/TagsCloudContainer.Tests/TextProcessorShould.cs index 04d20060..ebd8bf74 100644 --- a/TagsCloudContainer.Tests/TextProcessorShould.cs +++ b/TagsCloudContainer.Tests/TextProcessorShould.cs @@ -11,7 +11,7 @@ public class TextProcessorShould public void Process() { var result = new TextProcessor.TextProcessor(@"TextFile1.txt", - new TxtTextProvider(), new RegexParser(), new BoringWordFilter()).Words(); + new TxtTextProvider(), new RegexParser(), new BoringWordFilter(), new ShortWordFilter()).Words(); result.Count.Should().Be(3); diff --git a/TagsCloudContainer/TextProcessor/TextProcessor.cs b/TagsCloudContainer/TextProcessor/TextProcessor.cs index 81a1d341..2617e127 100644 --- a/TagsCloudContainer/TextProcessor/TextProcessor.cs +++ b/TagsCloudContainer/TextProcessor/TextProcessor.cs @@ -13,7 +13,7 @@ public Dictionary Words() var words = new Dictionary(); foreach (var word in parser.GetWordsFromString(provider.ReadFile(path))) { - if (filters.All(filter => !filter.Skips(word))) + if (filters.Any(filter => !filter.Skips(word))) continue; if (!words.ContainsKey(word)) diff --git a/TagsCloudContainer/WordFilters/ShortWordFilter.cs b/TagsCloudContainer/WordFilters/ShortWordFilter.cs new file mode 100644 index 00000000..ac54321f --- /dev/null +++ b/TagsCloudContainer/WordFilters/ShortWordFilter.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TagsCloudContainer.WordFilters +{ + public class ShortWordFilter : IWordFilter + { + public bool Skips(Word word) + { + return word.Value.Length > 2; + } + } +} From cf302b3f73033ed1166cbbf89850cf4ee9686491 Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Fri, 3 Jan 2025 19:12:41 +0500 Subject: [PATCH 13/29] hypothesis --- TagsCloudContainer/Configuration/Config.cs | 13 ++++++ .../Configuration/DependencyInjection.cs | 22 +++++++++ TagsCloudContainer/LabelAttribute.cs | 13 ++++++ .../PointGenerators/ArchemedianSpiral.cs | 1 + .../PointGenerators/DeltaShaped.cs | 1 + .../PointGenerators/HeartShaped.cs | 1 + TagsCloudContainer/Program.cs | 45 ++++++++++++++++++- TagsCloudContainer/TagsCloudContainer.csproj | 1 + .../TextProviders/TxtTextProvider.cs | 1 + 9 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 TagsCloudContainer/Configuration/Config.cs create mode 100644 TagsCloudContainer/Configuration/DependencyInjection.cs create mode 100644 TagsCloudContainer/LabelAttribute.cs diff --git a/TagsCloudContainer/Configuration/Config.cs b/TagsCloudContainer/Configuration/Config.cs new file mode 100644 index 00000000..cf3d7a63 --- /dev/null +++ b/TagsCloudContainer/Configuration/Config.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TagsCloudContainer.PointGenerators; + +namespace TagsCloudContainer.Configuration; + +public class Config +{ + public Type PointGenerator { get; set; } +} \ No newline at end of file diff --git a/TagsCloudContainer/Configuration/DependencyInjection.cs b/TagsCloudContainer/Configuration/DependencyInjection.cs new file mode 100644 index 00000000..0d149900 --- /dev/null +++ b/TagsCloudContainer/Configuration/DependencyInjection.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Autofac; + +namespace TagsCloudContainer.Configuration; + +public class DependencyInjection +{ + public IContainer BuildContainer(Config config) + { + var container = new ContainerBuilder(); + + container.RegisterType(config.PointGenerator) + .AsImplementedInterfaces(); + + + return container.Build(); + } +} \ No newline at end of file diff --git a/TagsCloudContainer/LabelAttribute.cs b/TagsCloudContainer/LabelAttribute.cs new file mode 100644 index 00000000..19d9fbe4 --- /dev/null +++ b/TagsCloudContainer/LabelAttribute.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TagsCloudContainer +{ + public class LabelAttribute(string labelText) : Attribute + { + public string LabelText { get; set; } = labelText; + } +} diff --git a/TagsCloudContainer/PointGenerators/ArchemedianSpiral.cs b/TagsCloudContainer/PointGenerators/ArchemedianSpiral.cs index d88b9d6b..a942a18b 100644 --- a/TagsCloudContainer/PointGenerators/ArchemedianSpiral.cs +++ b/TagsCloudContainer/PointGenerators/ArchemedianSpiral.cs @@ -2,6 +2,7 @@ namespace TagsCloudContainer.PointGenerators { + [Label("Спираль")] public class ArchemedianSpiral : IPointGenerator { public IEnumerable GeneratePoints(Point start) diff --git a/TagsCloudContainer/PointGenerators/DeltaShaped.cs b/TagsCloudContainer/PointGenerators/DeltaShaped.cs index fd6ee263..5e54e480 100644 --- a/TagsCloudContainer/PointGenerators/DeltaShaped.cs +++ b/TagsCloudContainer/PointGenerators/DeltaShaped.cs @@ -2,6 +2,7 @@ namespace TagsCloudContainer.PointGenerators { + [Label("Стринги")] public class DeltaShaped : IPointGenerator { public IEnumerable GeneratePoints(Point start) diff --git a/TagsCloudContainer/PointGenerators/HeartShaped.cs b/TagsCloudContainer/PointGenerators/HeartShaped.cs index da49931c..6c09c918 100644 --- a/TagsCloudContainer/PointGenerators/HeartShaped.cs +++ b/TagsCloudContainer/PointGenerators/HeartShaped.cs @@ -2,6 +2,7 @@ namespace TagsCloudContainer.PointGenerators { + [Label("Сердце")] public class HeartShaped : IPointGenerator { public IEnumerable GeneratePoints(Point start) diff --git a/TagsCloudContainer/Program.cs b/TagsCloudContainer/Program.cs index dbd20d62..708a939c 100644 --- a/TagsCloudContainer/Program.cs +++ b/TagsCloudContainer/Program.cs @@ -1,4 +1,8 @@ -using TagsCloudContainer.StringParsers; +using System.Reflection; +using System.Threading.Channels; +using TagsCloudContainer.Configuration; +using TagsCloudContainer.PointGenerators; +using TagsCloudContainer.StringParsers; using TagsCloudContainer.TextProviders; using TagsCloudContainer.WordFilters; @@ -16,6 +20,43 @@ static void Main(string[] args) { Console.WriteLine(word.Key.Value + " : " + word.Value); } + + foreach (var imp in FindImplemetations()) + { + Console.WriteLine(imp.Key + " : " + imp.Value); + } + + var config = new Config(); + + ConfigureCloudView(config); + Console.WriteLine(config.PointGenerator); + + } + + private static void ConfigureCloudView(Config config) + { + Console.WriteLine("Выберите внешний вид облака из возможных:"); + var pointGenerators = FindImplemetations(); + foreach (var point in pointGenerators) + Console.WriteLine(point.Key); + Console.WriteLine("Введите, соблюдая орфографию"); + var pointGenerator = Console.ReadLine(); + if (pointGenerators.ContainsKey(pointGenerator)) + config.PointGenerator = pointGenerators[pointGenerator]; + else + { + Console.WriteLine("Такой формы не предусмотрено"); + ConfigureCloudView(config); + } + } + + private static Dictionary FindImplemetations() + { + var assembly = Assembly.GetExecutingAssembly(); + var type = typeof(T); + return assembly.GetTypes() + .Where(t => type.IsAssignableFrom(t) && !t.IsInterface) + .ToDictionary(x => x.GetCustomAttribute().LabelText, x => x); + } } } -} diff --git a/TagsCloudContainer/TagsCloudContainer.csproj b/TagsCloudContainer/TagsCloudContainer.csproj index 71159cfa..7bda4840 100644 --- a/TagsCloudContainer/TagsCloudContainer.csproj +++ b/TagsCloudContainer/TagsCloudContainer.csproj @@ -8,6 +8,7 @@ + diff --git a/TagsCloudContainer/TextProviders/TxtTextProvider.cs b/TagsCloudContainer/TextProviders/TxtTextProvider.cs index a0aba801..fb493c65 100644 --- a/TagsCloudContainer/TextProviders/TxtTextProvider.cs +++ b/TagsCloudContainer/TextProviders/TxtTextProvider.cs @@ -1,5 +1,6 @@ namespace TagsCloudContainer.TextProviders; +[Label(".txt")] public class TxtTextProvider : ITextProvider { public string ReadFile(string filePath) From 66b48b9b0e0660613a4c05606f93d9f2e6838b60 Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Fri, 3 Jan 2025 22:10:04 +0500 Subject: [PATCH 14/29] add chosing color --- TagsCloudContainer/Configuration/Config.cs | 5 + .../Configuration/DependencyInjection.cs | 15 ++- TagsCloudContainer/Program.cs | 101 ++++++++++++++++-- TagsCloudContainer/RainbowColors.cs | 26 +++++ .../TagGenerator/SingleColorTagGenerator.cs | 25 +++++ 5 files changed, 160 insertions(+), 12 deletions(-) create mode 100644 TagsCloudContainer/RainbowColors.cs create mode 100644 TagsCloudContainer/TagGenerator/SingleColorTagGenerator.cs diff --git a/TagsCloudContainer/Configuration/Config.cs b/TagsCloudContainer/Configuration/Config.cs index cf3d7a63..ef9f94b6 100644 --- a/TagsCloudContainer/Configuration/Config.cs +++ b/TagsCloudContainer/Configuration/Config.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -10,4 +11,8 @@ namespace TagsCloudContainer.Configuration; public class Config { public Type PointGenerator { get; set; } + + public bool RandomColor; + public Color Color { get; set; } + } \ No newline at end of file diff --git a/TagsCloudContainer/Configuration/DependencyInjection.cs b/TagsCloudContainer/Configuration/DependencyInjection.cs index 0d149900..be56b05d 100644 --- a/TagsCloudContainer/Configuration/DependencyInjection.cs +++ b/TagsCloudContainer/Configuration/DependencyInjection.cs @@ -4,6 +4,7 @@ using System.Text; using System.Threading.Tasks; using Autofac; +using TagsCloudContainer.TagGenerator; namespace TagsCloudContainer.Configuration; @@ -14,7 +15,19 @@ public IContainer BuildContainer(Config config) var container = new ContainerBuilder(); container.RegisterType(config.PointGenerator) - .AsImplementedInterfaces(); + .AsImplementedInterfaces() + .SingleInstance(); + + if (config.RandomColor) + container.RegisterType() + .AsImplementedInterfaces() + .SingleInstance(); + else + { + container.RegisterType() + .AsImplementedInterfaces() + .SingleInstance(); + } return container.Build(); diff --git a/TagsCloudContainer/Program.cs b/TagsCloudContainer/Program.cs index 708a939c..3ab10778 100644 --- a/TagsCloudContainer/Program.cs +++ b/TagsCloudContainer/Program.cs @@ -1,10 +1,14 @@ -using System.Reflection; +using System.Drawing; +using System.Reflection; using System.Threading.Channels; using TagsCloudContainer.Configuration; using TagsCloudContainer.PointGenerators; using TagsCloudContainer.StringParsers; using TagsCloudContainer.TextProviders; using TagsCloudContainer.WordFilters; +using System.Linq; +using System.Security.Cryptography.X509Certificates; +using System.ComponentModel; namespace TagsCloudContainer { @@ -28,9 +32,84 @@ static void Main(string[] args) var config = new Config(); - ConfigureCloudView(config); - Console.WriteLine(config.PointGenerator); - + //ConfigureCloudView(config); + + ConfigureColor(config); + Console.WriteLine(config.Color); + Console.WriteLine(config.RandomColor); + + } + + private static void ConfigureColor(Config config) + { + var assembly = Assembly.GetExecutingAssembly(); + Console.WriteLine("Выборите цвет из возможных:"); + var colors = Enum.GetValues(typeof(RainbowColors)); + foreach (var color in colors) + { + var fieldInfo = color.GetType().GetField(color.ToString()); + var attribute = (LabelAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(LabelAttribute)); + + var label = attribute.LabelText; + Console.WriteLine(label); + } + + Console.WriteLine("В случае неправильного ввода - цвет будет выбираться случайным образом"); + var inp = Console.ReadLine(); + switch (inp) + { + case "Красный": + config.Color = Color.Red; + break; + case "Оранжевый": + config.Color = Color.Orange; + break; + case "Желтый": + config.Color = Color.Yellow; + break; + case "Зеленый": + config.Color = Color.Green; + break; + case "Голубой": + config.Color = Color.Blue; + break; + case "Синий": + config.Color = Color.Indigo; + break; + case "Фиолетовый": + config.Color = Color.Violet; + break; + + default: + Console.WriteLine("Цвет будет выбираться случайным образом"); + config.RandomColor = true; + break; + } + + + + + //// Запрашиваем у пользователя ввод цвета + //Console.Write("Введите цвет текста (например, Red, Green): "); + //string userInput = Console.ReadLine(); + + //// Пробуем преобразовать ввод в ConsoleColor + //if (Enum.TryParse(userInput, true, out ConsoleColor selectedColor)) + //{ + // // Устанавливаем цвет текста + // Console.ForegroundColor = selectedColor; + + // // Выводим сообщение с выбранным цветом + // Console.WriteLine("Выбранный цвет текста: " + userInput); + //} + //else + //{ + // Console.WriteLine("Некорректный цвет. Попробуйте снова."); + //} + + //// Сбрасываем цвет текста на стандартный + //Console.ResetColor(); + } private static void ConfigureCloudView(Config config) @@ -51,12 +130,12 @@ private static void ConfigureCloudView(Config config) } private static Dictionary FindImplemetations() - { - var assembly = Assembly.GetExecutingAssembly(); - var type = typeof(T); - return assembly.GetTypes() - .Where(t => type.IsAssignableFrom(t) && !t.IsInterface) - .ToDictionary(x => x.GetCustomAttribute().LabelText, x => x); - } + { + var assembly = Assembly.GetExecutingAssembly(); + var type = typeof(T); + return assembly.GetTypes() + .Where(t => type.IsAssignableFrom(t) && !t.IsInterface) + .ToDictionary(x => x.GetCustomAttribute().LabelText, x => x); } } +} diff --git a/TagsCloudContainer/RainbowColors.cs b/TagsCloudContainer/RainbowColors.cs new file mode 100644 index 00000000..70702b7d --- /dev/null +++ b/TagsCloudContainer/RainbowColors.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TagsCloudContainer +{ + public enum RainbowColors + { + [Label("Красный")] + Red, + [Label("Оранжевый")] + Orange, + [Label("Желтый")] + Yellow, + [Label("Зеленый")] + Green, + [Label("Голубой")] + Blue, + [Label("Синий")] + Indigo, + [Label("Фиолетовый")] + Violet, + } +} diff --git a/TagsCloudContainer/TagGenerator/SingleColorTagGenerator.cs b/TagsCloudContainer/TagGenerator/SingleColorTagGenerator.cs new file mode 100644 index 00000000..7139fc0e --- /dev/null +++ b/TagsCloudContainer/TagGenerator/SingleColorTagGenerator.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TagsCloudContainer.TextProcessor; + +namespace TagsCloudContainer.TagGenerator +{ + public class SingleColorTagGenerator(ITextProcessor processor, Graphics graphics, Font defaultFont, Color color ): ITagsGenerator + { + public IEnumerable GenerateTags() + { + return processor.Words() + .Select(kvp => new Tag(graphics, kvp.Key, SetFont(defaultFont, kvp.Value), color)); + } + + private static Font SetFont(Font font, int amount) + { + return new Font(font.FontFamily, font.Size * amount); + } + } +} From c3290794d4bfacc0460468a4c38d26f1898cbbf0 Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Sat, 4 Jan 2025 17:20:07 +0500 Subject: [PATCH 15/29] add IColorProvider and refactor TagGenerator --- .../RandomColorTagGeneratorShould.cs | 6 ++- .../TextProcessorShould.cs | 2 +- .../ColorProviders/ColorProvider.cs | 11 +++++ .../ColorProviders/IColorProvider.cs | 8 ++++ .../ColorProviders/RandomColorProvider.cs | 12 ++++++ .../Configuration/DependencyInjection.cs | 20 ++++----- TagsCloudContainer/PictureMaker.cs | 28 ++++++++++++ TagsCloudContainer/Program.cs | 2 +- TagsCloudContainer/Tag.cs | 17 +------- .../TagGenerator/ITagsGenerator.cs | 2 +- .../TagGenerator/RandomColorTagGenerator.cs | 31 ------------- .../TagGenerator/SingleColorTagGenerator.cs | 25 ----------- .../TagGenerator/TagGenerator.cs | 43 +++++++++++++++++++ .../TextProcessor/ITextProcessor.cs | 2 +- .../TextProcessor/TextProcessor.cs | 2 +- 15 files changed, 122 insertions(+), 89 deletions(-) create mode 100644 TagsCloudContainer/ColorProviders/ColorProvider.cs create mode 100644 TagsCloudContainer/ColorProviders/IColorProvider.cs create mode 100644 TagsCloudContainer/ColorProviders/RandomColorProvider.cs create mode 100644 TagsCloudContainer/PictureMaker.cs delete mode 100644 TagsCloudContainer/TagGenerator/RandomColorTagGenerator.cs delete mode 100644 TagsCloudContainer/TagGenerator/SingleColorTagGenerator.cs create mode 100644 TagsCloudContainer/TagGenerator/TagGenerator.cs diff --git a/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs b/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs index bb9ac472..ac5003f5 100644 --- a/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs +++ b/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs @@ -1,5 +1,6 @@ using FluentAssertions; using System.Drawing; +using TagsCloudContainer.ColorProviders; using TagsCloudContainer.StringParsers; using TagsCloudContainer.TagGenerator; using TagsCloudContainer.TextProviders; @@ -17,8 +18,9 @@ public void SetRightFontSize() var processor = new TextProcessor.TextProcessor(@"TextFile1.txt", new TxtTextProvider(), new RegexParser(), new BoringWordFilter()); - var generator = new RandomColorTagGenerator(processor, graphics, new Font("arial", 12)); - var result = generator.GenerateTags().First(); + var words = processor.WordFrequencies(); + var generator = new TagGenerator.TagGenerator(new RandomColorProvider(), graphics, new Font("arial", 12)); + var result = generator.GenerateTags(words).First(); result.Font.Name.Should().Be("Arial"); result.Font.Size.Should().Be(36); diff --git a/TagsCloudContainer.Tests/TextProcessorShould.cs b/TagsCloudContainer.Tests/TextProcessorShould.cs index ebd8bf74..c9dfed03 100644 --- a/TagsCloudContainer.Tests/TextProcessorShould.cs +++ b/TagsCloudContainer.Tests/TextProcessorShould.cs @@ -11,7 +11,7 @@ public class TextProcessorShould public void Process() { var result = new TextProcessor.TextProcessor(@"TextFile1.txt", - new TxtTextProvider(), new RegexParser(), new BoringWordFilter(), new ShortWordFilter()).Words(); + new TxtTextProvider(), new RegexParser(), new BoringWordFilter(), new ShortWordFilter()).WordFrequencies(); result.Count.Should().Be(3); diff --git a/TagsCloudContainer/ColorProviders/ColorProvider.cs b/TagsCloudContainer/ColorProviders/ColorProvider.cs new file mode 100644 index 00000000..b91dc33b --- /dev/null +++ b/TagsCloudContainer/ColorProviders/ColorProvider.cs @@ -0,0 +1,11 @@ +using System.Drawing; + +namespace TagsCloudContainer.ColorProviders; + +public class ColorProvider(Color color) : IColorProvider +{ + public Color GetColor() + { + return color; + } +} \ No newline at end of file diff --git a/TagsCloudContainer/ColorProviders/IColorProvider.cs b/TagsCloudContainer/ColorProviders/IColorProvider.cs new file mode 100644 index 00000000..a4bf0558 --- /dev/null +++ b/TagsCloudContainer/ColorProviders/IColorProvider.cs @@ -0,0 +1,8 @@ +using System.Drawing; + +namespace TagsCloudContainer.ColorProviders; + +public interface IColorProvider +{ + public Color GetColor(); +} \ No newline at end of file diff --git a/TagsCloudContainer/ColorProviders/RandomColorProvider.cs b/TagsCloudContainer/ColorProviders/RandomColorProvider.cs new file mode 100644 index 00000000..d70e559c --- /dev/null +++ b/TagsCloudContainer/ColorProviders/RandomColorProvider.cs @@ -0,0 +1,12 @@ +using System.Drawing; + +namespace TagsCloudContainer.ColorProviders; + +public class RandomColorProvider : IColorProvider +{ + private static readonly Random Random = new(); + public Color GetColor() + { + return Color.FromArgb(Random.Next(50, 255), Random.Next(0, 255), Random.Next(0, 255), Random.Next(0, 255)); + } +} \ No newline at end of file diff --git a/TagsCloudContainer/Configuration/DependencyInjection.cs b/TagsCloudContainer/Configuration/DependencyInjection.cs index be56b05d..93f3e4ea 100644 --- a/TagsCloudContainer/Configuration/DependencyInjection.cs +++ b/TagsCloudContainer/Configuration/DependencyInjection.cs @@ -18,16 +18,16 @@ public IContainer BuildContainer(Config config) .AsImplementedInterfaces() .SingleInstance(); - if (config.RandomColor) - container.RegisterType() - .AsImplementedInterfaces() - .SingleInstance(); - else - { - container.RegisterType() - .AsImplementedInterfaces() - .SingleInstance(); - } + //if (config.RandomColor) + // container.RegisterType() + // .AsImplementedInterfaces() + // .SingleInstance(); + //else + //{ + // container.RegisterType() + // .AsImplementedInterfaces() + // .SingleInstance(); + //} return container.Build(); diff --git a/TagsCloudContainer/PictureMaker.cs b/TagsCloudContainer/PictureMaker.cs new file mode 100644 index 00000000..cf0476fe --- /dev/null +++ b/TagsCloudContainer/PictureMaker.cs @@ -0,0 +1,28 @@ +using System.Drawing; +using TagsCloudContainer.PointGenerators; + +namespace TagsCloudContainer; + +public class PictureMaker +{ + public void DrawPicture(IPointGenerator pointGenerator, IEnumerable tags, string filename, Point startPoint) + { + var layout = new CloudLayout(startPoint, pointGenerator); + var image = new Bitmap(layout.Size.Width, layout.Size.Height); + foreach (var tag in tags) + { + var rectangle = layout.PutNextRectangle(tag.Frame); + DrawTag(image, rectangle, tag); + } + image.Save(filename); + } + + private static void DrawTag(Bitmap image, Rectangle rectangle, Tag tag) + { + var brush = new SolidBrush(tag.Color); + var formGraphics = Graphics.FromImage(image); + formGraphics.DrawString(tag.Word.Value, tag.Font, brush, rectangle.Location); + brush.Dispose(); + formGraphics.Dispose(); + } +} \ No newline at end of file diff --git a/TagsCloudContainer/Program.cs b/TagsCloudContainer/Program.cs index 3ab10778..14f6895d 100644 --- a/TagsCloudContainer/Program.cs +++ b/TagsCloudContainer/Program.cs @@ -20,7 +20,7 @@ static void Main(string[] args) var filter = new BoringWordFilter(); var parser = new RegexParser(); var processor = new TextProcessor.TextProcessor(@"C:\test\test.txt", provider, parser, filter); - foreach (var word in processor.Words()) + foreach (var word in processor.WordFrequencies()) { Console.WriteLine(word.Key.Value + " : " + word.Value); } diff --git a/TagsCloudContainer/Tag.cs b/TagsCloudContainer/Tag.cs index d5c62ed2..2dd79ff4 100644 --- a/TagsCloudContainer/Tag.cs +++ b/TagsCloudContainer/Tag.cs @@ -2,19 +2,4 @@ namespace TagsCloudContainer; -public class Tag -{ - public readonly string Value; - public readonly Font Font; - public readonly Color Color; - public readonly Size Frame; - - public Tag(Graphics g, Word word, Font font, Color color) - { - Value = word.Value; - Font = font; - Color = color; - var rect = g.MeasureString(Value, Font).ToSize(); - Frame = new Size(rect.Width + 2, rect.Height + 2); - } -} \ No newline at end of file +public record Tag(Word Word, Font Font, Color Color, Size Frame); diff --git a/TagsCloudContainer/TagGenerator/ITagsGenerator.cs b/TagsCloudContainer/TagGenerator/ITagsGenerator.cs index 96c48cb0..9fff70ba 100644 --- a/TagsCloudContainer/TagGenerator/ITagsGenerator.cs +++ b/TagsCloudContainer/TagGenerator/ITagsGenerator.cs @@ -7,6 +7,6 @@ namespace TagsCloudContainer.TagGenerator { public interface ITagsGenerator { - IEnumerable GenerateTags(); + IEnumerable GenerateTags(Dictionary wordsDictionary); } } diff --git a/TagsCloudContainer/TagGenerator/RandomColorTagGenerator.cs b/TagsCloudContainer/TagGenerator/RandomColorTagGenerator.cs deleted file mode 100644 index 5832484c..00000000 --- a/TagsCloudContainer/TagGenerator/RandomColorTagGenerator.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System.Drawing; -using System.Linq; -using TagsCloudContainer.StringParsers; -using TagsCloudContainer.TextProcessor; -using TagsCloudContainer.TextProviders; -using TagsCloudContainer.WordFilters; - -namespace TagsCloudContainer.TagGenerator -{ - public class RandomColorTagGenerator(ITextProcessor processor, Graphics graphics, Font defaultFont) : ITagsGenerator - { - private static readonly Random Random = new(); - - public IEnumerable GenerateTags() - { - return processor.Words() - .Select(kvp => new Tag(graphics, kvp.Key, SetFont(defaultFont, kvp.Value), GetRandomColor())); - } - - - private static Font SetFont(Font font, int amount) - { - return new Font(font.FontFamily, font.Size * amount); - } - - private static Color GetRandomColor() - { - return Color.FromArgb(Random.Next(50, 255), Random.Next(0, 255), Random.Next(0, 255), Random.Next(0, 255)); - } - } -} diff --git a/TagsCloudContainer/TagGenerator/SingleColorTagGenerator.cs b/TagsCloudContainer/TagGenerator/SingleColorTagGenerator.cs deleted file mode 100644 index 7139fc0e..00000000 --- a/TagsCloudContainer/TagGenerator/SingleColorTagGenerator.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TagsCloudContainer.TextProcessor; - -namespace TagsCloudContainer.TagGenerator -{ - public class SingleColorTagGenerator(ITextProcessor processor, Graphics graphics, Font defaultFont, Color color ): ITagsGenerator - { - public IEnumerable GenerateTags() - { - return processor.Words() - .Select(kvp => new Tag(graphics, kvp.Key, SetFont(defaultFont, kvp.Value), color)); - } - - private static Font SetFont(Font font, int amount) - { - return new Font(font.FontFamily, font.Size * amount); - } - } -} diff --git a/TagsCloudContainer/TagGenerator/TagGenerator.cs b/TagsCloudContainer/TagGenerator/TagGenerator.cs new file mode 100644 index 00000000..d66b027e --- /dev/null +++ b/TagsCloudContainer/TagGenerator/TagGenerator.cs @@ -0,0 +1,43 @@ +using System.Drawing; +using System.Linq; +using TagsCloudContainer.ColorProviders; +using TagsCloudContainer.StringParsers; +using TagsCloudContainer.TextProcessor; +using TagsCloudContainer.TextProviders; +using TagsCloudContainer.WordFilters; + +namespace TagsCloudContainer.TagGenerator +{ + public class TagGenerator : ITagsGenerator + { + private readonly IColorProvider _colorProvider; + private readonly Graphics _graphics; + private readonly Font _defaultFont; + + + public TagGenerator(IColorProvider colorProvider, Graphics graphics, Font defaultFont ) + { + _colorProvider = colorProvider; + _graphics = graphics; + _defaultFont = defaultFont; + } + + public IEnumerable GenerateTags(Dictionary wordsDictionary) + { + return wordsDictionary + .Select(kvp => new Tag(kvp.Key, SetFont(_defaultFont, kvp.Value), _colorProvider.GetColor(), + SetFrameSize(kvp.Key, _defaultFont, 1, _graphics))); + } + + private static Size SetFrameSize(Word word, Font font, int frameGap, Graphics graphics) + { + var rect = graphics.MeasureString(word.Value, font).ToSize(); + return new Size(rect.Width + frameGap, rect.Height + frameGap); + } + + private static Font SetFont(Font font, int amount) + { + return new Font(font.FontFamily, font.Size * amount); + } + } +} diff --git a/TagsCloudContainer/TextProcessor/ITextProcessor.cs b/TagsCloudContainer/TextProcessor/ITextProcessor.cs index 9f331c2a..e730a310 100644 --- a/TagsCloudContainer/TextProcessor/ITextProcessor.cs +++ b/TagsCloudContainer/TextProcessor/ITextProcessor.cs @@ -6,6 +6,6 @@ namespace TagsCloudContainer.TextProcessor { public interface ITextProcessor { - public Dictionary Words(); + public Dictionary WordFrequencies(); } } diff --git a/TagsCloudContainer/TextProcessor/TextProcessor.cs b/TagsCloudContainer/TextProcessor/TextProcessor.cs index 2617e127..867f5d38 100644 --- a/TagsCloudContainer/TextProcessor/TextProcessor.cs +++ b/TagsCloudContainer/TextProcessor/TextProcessor.cs @@ -8,7 +8,7 @@ namespace TagsCloudContainer.TextProcessor public class TextProcessor(string path, ITextProvider provider, IStringParser parser, params IWordFilter[] filters) : ITextProcessor { - public Dictionary Words() + public Dictionary WordFrequencies() { var words = new Dictionary(); foreach (var word in parser.GetWordsFromString(provider.ReadFile(path))) From 529f90ceb1577bc94582365c68c604ffb4d3582d Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Sat, 4 Jan 2025 22:02:25 +0500 Subject: [PATCH 16/29] pattern filter for filters --- TagsCloudContainer/Program.cs | 25 ----------------- .../TextProcessor/TextProcessor.cs | 28 +++++++------------ .../WordFilters/BoringWordFilter.cs | 12 ++++---- TagsCloudContainer/WordFilters/IWordFilter.cs | 2 +- .../WordFilters/ShortWordFilter.cs | 4 +-- .../WordFilters/ToLowerFilter.cs | 15 ++++++++++ 6 files changed, 35 insertions(+), 51 deletions(-) create mode 100644 TagsCloudContainer/WordFilters/ToLowerFilter.cs diff --git a/TagsCloudContainer/Program.cs b/TagsCloudContainer/Program.cs index 14f6895d..589c2014 100644 --- a/TagsCloudContainer/Program.cs +++ b/TagsCloudContainer/Program.cs @@ -85,31 +85,6 @@ private static void ConfigureColor(Config config) config.RandomColor = true; break; } - - - - - //// Запрашиваем у пользователя ввод цвета - //Console.Write("Введите цвет текста (например, Red, Green): "); - //string userInput = Console.ReadLine(); - - //// Пробуем преобразовать ввод в ConsoleColor - //if (Enum.TryParse(userInput, true, out ConsoleColor selectedColor)) - //{ - // // Устанавливаем цвет текста - // Console.ForegroundColor = selectedColor; - - // // Выводим сообщение с выбранным цветом - // Console.WriteLine("Выбранный цвет текста: " + userInput); - //} - //else - //{ - // Console.WriteLine("Некорректный цвет. Попробуйте снова."); - //} - - //// Сбрасываем цвет текста на стандартный - //Console.ResetColor(); - } private static void ConfigureCloudView(Config config) diff --git a/TagsCloudContainer/TextProcessor/TextProcessor.cs b/TagsCloudContainer/TextProcessor/TextProcessor.cs index 867f5d38..a39404ac 100644 --- a/TagsCloudContainer/TextProcessor/TextProcessor.cs +++ b/TagsCloudContainer/TextProcessor/TextProcessor.cs @@ -3,24 +3,16 @@ using TagsCloudContainer.TextProviders; using TagsCloudContainer.WordFilters; -namespace TagsCloudContainer.TextProcessor +namespace TagsCloudContainer.TextProcessor; + +public class TextProcessor(string path, ITextProvider provider, IStringParser parser, + params IWordFilter[] filters) : ITextProcessor { - public class TextProcessor(string path, ITextProvider provider, IStringParser parser, - params IWordFilter[] filters) : ITextProcessor + public Dictionary WordFrequencies() { - public Dictionary WordFrequencies() - { - var words = new Dictionary(); - foreach (var word in parser.GetWordsFromString(provider.ReadFile(path))) - { - if (filters.Any(filter => !filter.Skips(word))) - continue; - - if (!words.ContainsKey(word)) - words.Add(word, 0); - words[word]++; - } - return words; - } + var words = parser.GetWordsFromString(provider.ReadFile(path)); + return filters.Aggregate(words, (current, filter) => filter.Process(current)) + .GroupBy(word => word) + .ToDictionary(group => group.Key, group => group.Count()); } -} +} \ No newline at end of file diff --git a/TagsCloudContainer/WordFilters/BoringWordFilter.cs b/TagsCloudContainer/WordFilters/BoringWordFilter.cs index 994b1a23..71c4ae1c 100644 --- a/TagsCloudContainer/WordFilters/BoringWordFilter.cs +++ b/TagsCloudContainer/WordFilters/BoringWordFilter.cs @@ -1,4 +1,5 @@ -namespace TagsCloudContainer.WordFilters + +namespace TagsCloudContainer.WordFilters { public class BoringWordFilter : IWordFilter { @@ -11,10 +12,6 @@ public class BoringWordFilter : IWordFilter "оно", "они" ]; - public bool Skips(Word word) - { - return !_forbiddenWords.Contains(word.Value); - } public void AddBoringWord(Word word) { @@ -25,5 +22,10 @@ public void AddBoringWord(string word) { _forbiddenWords.Add(word); } + + public IEnumerable Process(IEnumerable words) + { + return words.Where(w => !_forbiddenWords.Contains(w.Value)); + } } } diff --git a/TagsCloudContainer/WordFilters/IWordFilter.cs b/TagsCloudContainer/WordFilters/IWordFilter.cs index 0dd8c469..532cb64d 100644 --- a/TagsCloudContainer/WordFilters/IWordFilter.cs +++ b/TagsCloudContainer/WordFilters/IWordFilter.cs @@ -2,6 +2,6 @@ { public interface IWordFilter { - public bool Skips(Word word); + public IEnumerable Process(IEnumerable words); } } diff --git a/TagsCloudContainer/WordFilters/ShortWordFilter.cs b/TagsCloudContainer/WordFilters/ShortWordFilter.cs index ac54321f..a1d0c649 100644 --- a/TagsCloudContainer/WordFilters/ShortWordFilter.cs +++ b/TagsCloudContainer/WordFilters/ShortWordFilter.cs @@ -8,9 +8,9 @@ namespace TagsCloudContainer.WordFilters { public class ShortWordFilter : IWordFilter { - public bool Skips(Word word) + public IEnumerable Process(IEnumerable words) { - return word.Value.Length > 2; + return words.Where(w => w.Value.Length > 2); } } } diff --git a/TagsCloudContainer/WordFilters/ToLowerFilter.cs b/TagsCloudContainer/WordFilters/ToLowerFilter.cs new file mode 100644 index 00000000..4c441a2e --- /dev/null +++ b/TagsCloudContainer/WordFilters/ToLowerFilter.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TagsCloudContainer.WordFilters; + +public class ToLowerFilter : IWordFilter +{ + public IEnumerable Process(IEnumerable words) + { + return words.Select(w => new Word(w.Value.ToLower())); + } +} \ No newline at end of file From 821147e4776ffe736304673361a49073c4ad6b72 Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Sun, 5 Jan 2025 19:30:20 +0500 Subject: [PATCH 17/29] container refuse to wark ( --- Client/Client.csproj | 20 +++ Client/DependencyInjection.cs | 72 ++++++++++ Client/Program.cs | 124 ++++++++++++++++++ Client/TestFile.txt | 2 + .../RandomColorTagGeneratorShould.cs | 4 +- .../TextProcessorShould.cs | 4 +- .../TxtTextProviderShould.cs | 13 +- TagsCloudContainer/Configuration/Config.cs | 11 +- .../Configuration/DependencyInjection.cs | 35 ----- TagsCloudContainer/PictureMaker.cs | 17 ++- TagsCloudContainer/Program.cs | 116 ---------------- TagsCloudContainer/TagsCloudContainer.csproj | 2 +- TagsCloudContainer/TagsCloudContainer.sln | 10 +- .../TextProcessor/TextProcessor.cs | 4 +- .../TextProviders/ITextProvider.cs | 2 +- .../TextProviders/TxtTextProvider.cs | 13 +- 16 files changed, 270 insertions(+), 179 deletions(-) create mode 100644 Client/Client.csproj create mode 100644 Client/DependencyInjection.cs create mode 100644 Client/Program.cs create mode 100644 Client/TestFile.txt delete mode 100644 TagsCloudContainer/Configuration/DependencyInjection.cs delete mode 100644 TagsCloudContainer/Program.cs diff --git a/Client/Client.csproj b/Client/Client.csproj new file mode 100644 index 00000000..314949c4 --- /dev/null +++ b/Client/Client.csproj @@ -0,0 +1,20 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + + + Always + + + + diff --git a/Client/DependencyInjection.cs b/Client/DependencyInjection.cs new file mode 100644 index 00000000..3ff5cd06 --- /dev/null +++ b/Client/DependencyInjection.cs @@ -0,0 +1,72 @@ +using Autofac; +using TagsCloudContainer; +using TagsCloudContainer.ColorProviders; +using TagsCloudContainer.Configuration; +using TagsCloudContainer.PointGenerators; +using TagsCloudContainer.StringParsers; +using TagsCloudContainer.TagGenerator; +using TagsCloudContainer.TextProcessor; +using TagsCloudContainer.TextProviders; +using TagsCloudContainer.WordFilters; + +namespace Client; + +public class DependencyInjection +{ + public static IContainer BuildContainer(Config config) + { + var container = new ContainerBuilder(); + container.RegisterInstance(config).AsSelf(); + + if (config.FilePath.EndsWith(".txt")) + container.RegisterType() + .As() + .WithParameter("_filePath", config.FilePath) + .SingleInstance(); + + container.RegisterType(config.PointGenerator) + .As() + .SingleInstance(); + + if (config.Color != null) + container.RegisterType() + .As() + .WithParameter("color", config.Color) + .SingleInstance(); + else + container.RegisterType() + .As() + .SingleInstance(); + + container.RegisterType() + .As() + .SingleInstance(); + + container.RegisterType() + .As() + .WithParameter("defaultFont", config.Font) + .SingleInstance(); + + container.RegisterType() + .As() + .SingleInstance(); + + container.RegisterType().AsSelf() + .SingleInstance(); + container.RegisterType().AsSelf() + .SingleInstance(); + container.RegisterType().AsSelf() + .SingleInstance(); + + //container.RegisterType().AsSelf() + // .SingleInstance(); + + container.RegisterType() + .AsSelf() + .WithParameter("fileName", config.PicturePath) + .WithParameter("startPoint", config.StartPoint) + .SingleInstance(); + + return container.Build(); + } +} \ No newline at end of file diff --git a/Client/Program.cs b/Client/Program.cs new file mode 100644 index 00000000..1b899105 --- /dev/null +++ b/Client/Program.cs @@ -0,0 +1,124 @@ +using System.Drawing; +using System.Reflection; +using System.Threading.Channels; +using TagsCloudContainer.Configuration; +using TagsCloudContainer.PointGenerators; +using TagsCloudContainer.StringParsers; +using TagsCloudContainer.TextProviders; +using TagsCloudContainer.WordFilters; +using System.Linq; +using System.Security.Cryptography.X509Certificates; +using System.ComponentModel; +using TagsCloudContainer; +using TagsCloudContainer.TextProcessor; +using Autofac; + +namespace Client +{ + internal class Program + { + static void Main(string[] args) + { + var config = new Config(); + + ConfigureFileSource(config); + ConfigureCloudView(config); + ConfigureColor(config); + ConfigurePathToSave(config); + ConfigureStartPoint(config); + ConfigureFont(config); + + var container = DependencyInjection.BuildContainer(config); + using var scope = container.BeginLifetimeScope(); + scope.Resolve().DrawPicture(); + Console.WriteLine($"результат сохранен в {config.PicturePath}"); + } + + private static void ConfigureFont(Config config) + { + config.Font = new Font("arial", 12); + } + + private static void ConfigurePathToSave(Config config) + { + Console.WriteLine("Введите полный путь и название файла для сохранения"); + var inp = Console.ReadLine(); + config.PicturePath = inp.Length == 0 ? "1.bmp" : inp; + } + + private static void ConfigureStartPoint(Config config) + { + Console.WriteLine("Введите координаты центра поля для рисования" + + "\n При некорректном вводе координаты центра составят ( 1000, 1000)"); + var xLine = Console.ReadLine(); + var yLine = Console.ReadLine(); + if (int.TryParse(xLine, out var xResult) && + int.TryParse(yLine, out var yResult)) + config.StartPoint = new Point(xResult, yResult); + config.StartPoint = new Point(1000, 1000); + } + + private static void ConfigureFileSource(Config config) + { + Console.WriteLine("Введите имя файла источника тэгов"); + var inp = Console.ReadLine(); + config.FilePath = inp.Length == 0 ? @"C:\\shpora\\Container\\di-updated\\Client\\TestFile.txt" : inp; + } + + private static string GetLabel(RainbowColors color) + { + var fieldInfo = color.GetType().GetField(color.ToString()); + var attribute = (LabelAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(LabelAttribute)); + + return attribute.LabelText; + } + + private static void ConfigureColor(Config config) + { + Console.WriteLine("Выборите цвет из возможных:"); + var colors = Enum.GetValues(typeof(RainbowColors)) + .Cast() + .ToDictionary(color => GetLabel(color).ToLower(), color => color); + + foreach (var color in colors) + Console.WriteLine("\t" + color.Key); + + Console.WriteLine("В случае неправильного ввода - цвет будет выбираться случайным образом"); + var inp = Console.ReadLine().ToLower(); + if (colors.ContainsKey(inp)) + { + config.Color = Color.FromName(colors[inp].ToString()); + Console.WriteLine($"Выбран {inp} цвет"); + } + else + Console.WriteLine("Цвет будет выбираться случайно"); + + } + + private static void ConfigureCloudView(Config config) + { + Console.WriteLine("Выберите внешний вид облака из возможных:"); + var pointGenerators = FindImplemetations(); + foreach (var point in pointGenerators) + Console.WriteLine("\t" + point.Key); + Console.WriteLine("Введите, соблюдая орфографию"); + var pointGenerator = Console.ReadLine().ToLower(); + if (pointGenerators.ContainsKey(pointGenerator)) + config.PointGenerator = pointGenerators[pointGenerator]; + else + { + Console.WriteLine("Такой формы не предусмотрено"); + ConfigureCloudView(config); + } + } + + private static Dictionary FindImplemetations() + { + var assembly = Assembly.LoadFrom("TagsCloudContainer.dll"); + var type = typeof(T); + return assembly.GetTypes() + .Where(t => type.IsAssignableFrom(t) && !t.IsInterface) + .ToDictionary(x => x.GetCustomAttribute().LabelText.ToLower(), x => x); + } + } +} diff --git a/Client/TestFile.txt b/Client/TestFile.txt new file mode 100644 index 00000000..b72e73dd --- /dev/null +++ b/Client/TestFile.txt @@ -0,0 +1,2 @@ +корзина корзина корзина корзина фрукты фрукты фрукты овощи овощи овощи яблоки груши бананы смородина персики картофель свекла +морковь он я ты она за по \ No newline at end of file diff --git a/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs b/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs index ac5003f5..2d3c3142 100644 --- a/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs +++ b/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs @@ -16,8 +16,8 @@ public void SetRightFontSize() var bitmap = new Bitmap(1, 1); using var graphics = Graphics.FromImage(bitmap); - var processor = new TextProcessor.TextProcessor(@"TextFile1.txt", - new TxtTextProvider(), new RegexParser(), new BoringWordFilter()); + var processor = new TextProcessor.TextProcessor( + new TxtTextProvider(@"TextFile1.txt"), new RegexParser(), new BoringWordFilter()); var words = processor.WordFrequencies(); var generator = new TagGenerator.TagGenerator(new RandomColorProvider(), graphics, new Font("arial", 12)); var result = generator.GenerateTags(words).First(); diff --git a/TagsCloudContainer.Tests/TextProcessorShould.cs b/TagsCloudContainer.Tests/TextProcessorShould.cs index c9dfed03..474786b5 100644 --- a/TagsCloudContainer.Tests/TextProcessorShould.cs +++ b/TagsCloudContainer.Tests/TextProcessorShould.cs @@ -10,8 +10,8 @@ public class TextProcessorShould [Test] public void Process() { - var result = new TextProcessor.TextProcessor(@"TextFile1.txt", - new TxtTextProvider(), new RegexParser(), new BoringWordFilter(), new ShortWordFilter()).WordFrequencies(); + var result = new TextProcessor.TextProcessor( + new TxtTextProvider(@"TextFile1.txt"), new RegexParser(), new BoringWordFilter(), new ShortWordFilter()).WordFrequencies(); result.Count.Should().Be(3); diff --git a/TagsCloudContainer.Tests/TxtTextProviderShould.cs b/TagsCloudContainer.Tests/TxtTextProviderShould.cs index ef395abc..461f2818 100644 --- a/TagsCloudContainer.Tests/TxtTextProviderShould.cs +++ b/TagsCloudContainer.Tests/TxtTextProviderShould.cs @@ -9,24 +9,15 @@ public class TxtTextProviderShould [SetUp] public void Setup() { - _provider = new TxtTextProvider(); + _provider = new TxtTextProvider("NotExisted.txt"); } [Test] public void ThrowExceptionIfFileNotFounded() { - Action act = () => _provider.ReadFile("NotExisted.txt"); + Action act = () => _provider.ReadFile(); act.Should().Throw(); } - - [Test] - public void ReturnLowerCase() - { - var result = _provider.ReadFile("TextFile1.txt"); - - foreach (var c in result.Where(c => char.IsLetter(c))) - char.IsLower(c).Should().BeTrue(); - } } } \ No newline at end of file diff --git a/TagsCloudContainer/Configuration/Config.cs b/TagsCloudContainer/Configuration/Config.cs index ef9f94b6..70f5c65f 100644 --- a/TagsCloudContainer/Configuration/Config.cs +++ b/TagsCloudContainer/Configuration/Config.cs @@ -11,8 +11,15 @@ namespace TagsCloudContainer.Configuration; public class Config { public Type PointGenerator { get; set; } + + public Color? Color { get; set; } - public bool RandomColor; - public Color Color { get; set; } + public string FilePath { get; set; } + + public string PicturePath { get; set; } + + public Point StartPoint { get; set; } + + public Font Font { get; set; } } \ No newline at end of file diff --git a/TagsCloudContainer/Configuration/DependencyInjection.cs b/TagsCloudContainer/Configuration/DependencyInjection.cs deleted file mode 100644 index 93f3e4ea..00000000 --- a/TagsCloudContainer/Configuration/DependencyInjection.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Autofac; -using TagsCloudContainer.TagGenerator; - -namespace TagsCloudContainer.Configuration; - -public class DependencyInjection -{ - public IContainer BuildContainer(Config config) - { - var container = new ContainerBuilder(); - - container.RegisterType(config.PointGenerator) - .AsImplementedInterfaces() - .SingleInstance(); - - //if (config.RandomColor) - // container.RegisterType() - // .AsImplementedInterfaces() - // .SingleInstance(); - //else - //{ - // container.RegisterType() - // .AsImplementedInterfaces() - // .SingleInstance(); - //} - - - return container.Build(); - } -} \ No newline at end of file diff --git a/TagsCloudContainer/PictureMaker.cs b/TagsCloudContainer/PictureMaker.cs index cf0476fe..44c848f3 100644 --- a/TagsCloudContainer/PictureMaker.cs +++ b/TagsCloudContainer/PictureMaker.cs @@ -5,7 +5,20 @@ namespace TagsCloudContainer; public class PictureMaker { - public void DrawPicture(IPointGenerator pointGenerator, IEnumerable tags, string filename, Point startPoint) + private readonly IPointGenerator pointGenerator; + private readonly IEnumerable tags; + private readonly string fileName; + private readonly Point startPoint; + + public PictureMaker(IPointGenerator pointGenerator, IEnumerable tags, string fileName, Point startPoint) + { + this.pointGenerator = pointGenerator; + this.tags = tags; + this.fileName = fileName; + this.startPoint = startPoint; + } + + public void DrawPicture() { var layout = new CloudLayout(startPoint, pointGenerator); var image = new Bitmap(layout.Size.Width, layout.Size.Height); @@ -14,7 +27,7 @@ public void DrawPicture(IPointGenerator pointGenerator, IEnumerable tags, s var rectangle = layout.PutNextRectangle(tag.Frame); DrawTag(image, rectangle, tag); } - image.Save(filename); + image.Save(fileName); } private static void DrawTag(Bitmap image, Rectangle rectangle, Tag tag) diff --git a/TagsCloudContainer/Program.cs b/TagsCloudContainer/Program.cs deleted file mode 100644 index 589c2014..00000000 --- a/TagsCloudContainer/Program.cs +++ /dev/null @@ -1,116 +0,0 @@ -using System.Drawing; -using System.Reflection; -using System.Threading.Channels; -using TagsCloudContainer.Configuration; -using TagsCloudContainer.PointGenerators; -using TagsCloudContainer.StringParsers; -using TagsCloudContainer.TextProviders; -using TagsCloudContainer.WordFilters; -using System.Linq; -using System.Security.Cryptography.X509Certificates; -using System.ComponentModel; - -namespace TagsCloudContainer -{ - internal class Program - { - static void Main(string[] args) - { - var provider = new TxtTextProvider(); - var filter = new BoringWordFilter(); - var parser = new RegexParser(); - var processor = new TextProcessor.TextProcessor(@"C:\test\test.txt", provider, parser, filter); - foreach (var word in processor.WordFrequencies()) - { - Console.WriteLine(word.Key.Value + " : " + word.Value); - } - - foreach (var imp in FindImplemetations()) - { - Console.WriteLine(imp.Key + " : " + imp.Value); - } - - var config = new Config(); - - //ConfigureCloudView(config); - - ConfigureColor(config); - Console.WriteLine(config.Color); - Console.WriteLine(config.RandomColor); - - } - - private static void ConfigureColor(Config config) - { - var assembly = Assembly.GetExecutingAssembly(); - Console.WriteLine("Выборите цвет из возможных:"); - var colors = Enum.GetValues(typeof(RainbowColors)); - foreach (var color in colors) - { - var fieldInfo = color.GetType().GetField(color.ToString()); - var attribute = (LabelAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(LabelAttribute)); - - var label = attribute.LabelText; - Console.WriteLine(label); - } - - Console.WriteLine("В случае неправильного ввода - цвет будет выбираться случайным образом"); - var inp = Console.ReadLine(); - switch (inp) - { - case "Красный": - config.Color = Color.Red; - break; - case "Оранжевый": - config.Color = Color.Orange; - break; - case "Желтый": - config.Color = Color.Yellow; - break; - case "Зеленый": - config.Color = Color.Green; - break; - case "Голубой": - config.Color = Color.Blue; - break; - case "Синий": - config.Color = Color.Indigo; - break; - case "Фиолетовый": - config.Color = Color.Violet; - break; - - default: - Console.WriteLine("Цвет будет выбираться случайным образом"); - config.RandomColor = true; - break; - } - } - - private static void ConfigureCloudView(Config config) - { - Console.WriteLine("Выберите внешний вид облака из возможных:"); - var pointGenerators = FindImplemetations(); - foreach (var point in pointGenerators) - Console.WriteLine(point.Key); - Console.WriteLine("Введите, соблюдая орфографию"); - var pointGenerator = Console.ReadLine(); - if (pointGenerators.ContainsKey(pointGenerator)) - config.PointGenerator = pointGenerators[pointGenerator]; - else - { - Console.WriteLine("Такой формы не предусмотрено"); - ConfigureCloudView(config); - } - } - - private static Dictionary FindImplemetations() - { - var assembly = Assembly.GetExecutingAssembly(); - var type = typeof(T); - return assembly.GetTypes() - .Where(t => type.IsAssignableFrom(t) && !t.IsInterface) - .ToDictionary(x => x.GetCustomAttribute().LabelText, x => x); - } - } -} diff --git a/TagsCloudContainer/TagsCloudContainer.csproj b/TagsCloudContainer/TagsCloudContainer.csproj index 7bda4840..f3e5d07a 100644 --- a/TagsCloudContainer/TagsCloudContainer.csproj +++ b/TagsCloudContainer/TagsCloudContainer.csproj @@ -1,7 +1,7 @@  - Exe + Library net8.0 enable enable diff --git a/TagsCloudContainer/TagsCloudContainer.sln b/TagsCloudContainer/TagsCloudContainer.sln index cbb75ba6..8274b4ed 100644 --- a/TagsCloudContainer/TagsCloudContainer.sln +++ b/TagsCloudContainer/TagsCloudContainer.sln @@ -3,9 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.11.35327.3 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudContainer", "TagsCloudContainer.csproj", "{9A86D0EB-2E44-4B2D-A9C9-BCF4C6037DE4}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TagsCloudContainer", "TagsCloudContainer.csproj", "{9A86D0EB-2E44-4B2D-A9C9-BCF4C6037DE4}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudContainer.Tests", "..\TagsCloudContainer.Tests\TagsCloudContainer.Tests.csproj", "{A6199F1E-60D0-4BC3-8A43-F0E8D0A8F7E7}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TagsCloudContainer.Tests", "..\TagsCloudContainer.Tests\TagsCloudContainer.Tests.csproj", "{A6199F1E-60D0-4BC3-8A43-F0E8D0A8F7E7}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "..\Client\Client.csproj", "{D35DD45C-AA81-4D59-BA31-261C006DB97E}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -21,6 +23,10 @@ Global {A6199F1E-60D0-4BC3-8A43-F0E8D0A8F7E7}.Debug|Any CPU.Build.0 = Debug|Any CPU {A6199F1E-60D0-4BC3-8A43-F0E8D0A8F7E7}.Release|Any CPU.ActiveCfg = Release|Any CPU {A6199F1E-60D0-4BC3-8A43-F0E8D0A8F7E7}.Release|Any CPU.Build.0 = Release|Any CPU + {D35DD45C-AA81-4D59-BA31-261C006DB97E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D35DD45C-AA81-4D59-BA31-261C006DB97E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D35DD45C-AA81-4D59-BA31-261C006DB97E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D35DD45C-AA81-4D59-BA31-261C006DB97E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/TagsCloudContainer/TextProcessor/TextProcessor.cs b/TagsCloudContainer/TextProcessor/TextProcessor.cs index a39404ac..ce9fc715 100644 --- a/TagsCloudContainer/TextProcessor/TextProcessor.cs +++ b/TagsCloudContainer/TextProcessor/TextProcessor.cs @@ -5,12 +5,12 @@ namespace TagsCloudContainer.TextProcessor; -public class TextProcessor(string path, ITextProvider provider, IStringParser parser, +public class TextProcessor(ITextProvider provider, IStringParser parser, params IWordFilter[] filters) : ITextProcessor { public Dictionary WordFrequencies() { - var words = parser.GetWordsFromString(provider.ReadFile(path)); + var words = parser.GetWordsFromString(provider.ReadFile()); return filters.Aggregate(words, (current, filter) => filter.Process(current)) .GroupBy(word => word) .ToDictionary(group => group.Key, group => group.Count()); diff --git a/TagsCloudContainer/TextProviders/ITextProvider.cs b/TagsCloudContainer/TextProviders/ITextProvider.cs index 91349a35..cb7a29b5 100644 --- a/TagsCloudContainer/TextProviders/ITextProvider.cs +++ b/TagsCloudContainer/TextProviders/ITextProvider.cs @@ -2,5 +2,5 @@ public interface ITextProvider { - public string ReadFile(string filePath); + public string ReadFile(); } \ No newline at end of file diff --git a/TagsCloudContainer/TextProviders/TxtTextProvider.cs b/TagsCloudContainer/TextProviders/TxtTextProvider.cs index fb493c65..907f7050 100644 --- a/TagsCloudContainer/TextProviders/TxtTextProvider.cs +++ b/TagsCloudContainer/TextProviders/TxtTextProvider.cs @@ -3,9 +3,16 @@ [Label(".txt")] public class TxtTextProvider : ITextProvider { - public string ReadFile(string filePath) + private readonly string _filePath; + + public TxtTextProvider(string filePath) + { + _filePath = filePath; + } + + public string ReadFile() { - if (!File.Exists(filePath)) throw new FileNotFoundException(); - return File.ReadAllText(filePath).ToLower(); + if (!File.Exists(_filePath)) throw new FileNotFoundException(); + return File.ReadAllText(_filePath).ToLower(); } } \ No newline at end of file From 2392c9266590f49087643bab0f2f032040cb0efe Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Mon, 6 Jan 2025 16:17:35 +0500 Subject: [PATCH 18/29] mvp --- Client/DependencyInjection.cs | 19 +++++++++---------- .../RandomColorTagGeneratorShould.cs | 2 +- TagsCloudContainer/PictureMaker.cs | 7 +++++-- .../TagGenerator/TagGenerator.cs | 6 +++--- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/Client/DependencyInjection.cs b/Client/DependencyInjection.cs index 3ff5cd06..0b98314f 100644 --- a/Client/DependencyInjection.cs +++ b/Client/DependencyInjection.cs @@ -21,9 +21,13 @@ public static IContainer BuildContainer(Config config) if (config.FilePath.EndsWith(".txt")) container.RegisterType() .As() - .WithParameter("_filePath", config.FilePath) + .WithParameter("filePath", config.FilePath) .SingleInstance(); + container.RegisterType() + .As() + .SingleInstance(); + container.RegisterType(config.PointGenerator) .As() .SingleInstance(); @@ -47,20 +51,15 @@ public static IContainer BuildContainer(Config config) .WithParameter("defaultFont", config.Font) .SingleInstance(); - container.RegisterType() - .As() - .SingleInstance(); + - container.RegisterType().AsSelf() + container.RegisterType().As() .SingleInstance(); - container.RegisterType().AsSelf() + container.RegisterType().As() .SingleInstance(); - container.RegisterType().AsSelf() + container.RegisterType().As() .SingleInstance(); - //container.RegisterType().AsSelf() - // .SingleInstance(); - container.RegisterType() .AsSelf() .WithParameter("fileName", config.PicturePath) diff --git a/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs b/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs index 2d3c3142..6dc114f0 100644 --- a/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs +++ b/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs @@ -19,7 +19,7 @@ public void SetRightFontSize() var processor = new TextProcessor.TextProcessor( new TxtTextProvider(@"TextFile1.txt"), new RegexParser(), new BoringWordFilter()); var words = processor.WordFrequencies(); - var generator = new TagGenerator.TagGenerator(new RandomColorProvider(), graphics, new Font("arial", 12)); + var generator = new TagGenerator.TagGenerator(new RandomColorProvider(), new Font("arial", 12)); var result = generator.GenerateTags(words).First(); result.Font.Name.Should().Be("Arial"); diff --git a/TagsCloudContainer/PictureMaker.cs b/TagsCloudContainer/PictureMaker.cs index 44c848f3..da33e0d2 100644 --- a/TagsCloudContainer/PictureMaker.cs +++ b/TagsCloudContainer/PictureMaker.cs @@ -1,5 +1,7 @@ using System.Drawing; using TagsCloudContainer.PointGenerators; +using TagsCloudContainer.TagGenerator; +using TagsCloudContainer.TextProcessor; namespace TagsCloudContainer; @@ -10,10 +12,11 @@ public class PictureMaker private readonly string fileName; private readonly Point startPoint; - public PictureMaker(IPointGenerator pointGenerator, IEnumerable tags, string fileName, Point startPoint) + public PictureMaker(IPointGenerator pointGenerator, ITagsGenerator tagGenerator, + ITextProcessor textProcessor, string fileName, Point startPoint) { this.pointGenerator = pointGenerator; - this.tags = tags; + this.tags = tagGenerator.GenerateTags(textProcessor.WordFrequencies()); this.fileName = fileName; this.startPoint = startPoint; } diff --git a/TagsCloudContainer/TagGenerator/TagGenerator.cs b/TagsCloudContainer/TagGenerator/TagGenerator.cs index d66b027e..5b048608 100644 --- a/TagsCloudContainer/TagGenerator/TagGenerator.cs +++ b/TagsCloudContainer/TagGenerator/TagGenerator.cs @@ -15,10 +15,10 @@ public class TagGenerator : ITagsGenerator private readonly Font _defaultFont; - public TagGenerator(IColorProvider colorProvider, Graphics graphics, Font defaultFont ) + public TagGenerator(IColorProvider colorProvider, Font defaultFont ) { _colorProvider = colorProvider; - _graphics = graphics; + _graphics = Graphics.FromImage(new Bitmap(1, 1)); _defaultFont = defaultFont; } @@ -26,7 +26,7 @@ public IEnumerable GenerateTags(Dictionary wordsDictionary) { return wordsDictionary .Select(kvp => new Tag(kvp.Key, SetFont(_defaultFont, kvp.Value), _colorProvider.GetColor(), - SetFrameSize(kvp.Key, _defaultFont, 1, _graphics))); + SetFrameSize(kvp.Key, SetFont(_defaultFont, kvp.Value), 1, _graphics))); } private static Size SetFrameSize(Word word, Font font, int frameGap, Graphics graphics) From db6772b4c85a9583c6eb3e77c100432267e93040 Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Tue, 7 Jan 2025 18:50:06 +0500 Subject: [PATCH 19/29] add DocTextProvider --- Client/Client.csproj | 3 ++ Client/DependencyInjection.cs | 16 +++++------ Client/Program.cs | 14 ++++++++-- Client/test.doc | Bin 0 -> 9216 bytes .../RandomColorTagGeneratorShould.cs | 1 - TagsCloudContainer/Configuration/Config.cs | 10 ++----- TagsCloudContainer/LabelAttribute.cs | 8 +----- TagsCloudContainer/RainbowColors.cs | 8 +----- .../StringParsers/IStringParser.cs | 8 +----- .../StringParsers/RegexParser.cs | 7 +---- .../TagGenerator/ITagsGenerator.cs | 7 +---- TagsCloudContainer/TagsCloudContainer.csproj | 1 + .../TextProcessor/ITextProcessor.cs | 6 +--- .../TextProcessor/TextProcessor.cs | 3 +- .../TextProviders/DocTextProvider.cs | 26 ++++++++++++++++++ .../WordFilters/BoringWordFilter.cs | 3 +- .../WordFilters/ShortWordFilter.cs | 8 +----- 17 files changed, 61 insertions(+), 68 deletions(-) create mode 100644 Client/test.doc create mode 100644 TagsCloudContainer/TextProviders/DocTextProvider.cs diff --git a/Client/Client.csproj b/Client/Client.csproj index 314949c4..4c9fcd11 100644 --- a/Client/Client.csproj +++ b/Client/Client.csproj @@ -12,6 +12,9 @@ + + Always + Always diff --git a/Client/DependencyInjection.cs b/Client/DependencyInjection.cs index 0b98314f..4be2c332 100644 --- a/Client/DependencyInjection.cs +++ b/Client/DependencyInjection.cs @@ -17,13 +17,11 @@ public static IContainer BuildContainer(Config config) { var container = new ContainerBuilder(); container.RegisterInstance(config).AsSelf(); - - if (config.FilePath.EndsWith(".txt")) - container.RegisterType() - .As() - .WithParameter("filePath", config.FilePath) - .SingleInstance(); - + + container.RegisterType(config.SupportedReadingFormats[Path.GetExtension(config.FilePath)]) + .As() + .WithParameter("filePath", config.FilePath) + .SingleInstance(); container.RegisterType() .As() .SingleInstance(); @@ -50,8 +48,8 @@ public static IContainer BuildContainer(Config config) .As() .WithParameter("defaultFont", config.Font) .SingleInstance(); - - + + container.RegisterType().As() .SingleInstance(); diff --git a/Client/Program.cs b/Client/Program.cs index 1b899105..ddb58b1e 100644 --- a/Client/Program.cs +++ b/Client/Program.cs @@ -17,10 +17,11 @@ namespace Client { internal class Program { - static void Main(string[] args) + static void Main() { var config = new Config(); + ConfigureSupportedReadingFormats(config); ConfigureFileSource(config); ConfigureCloudView(config); ConfigureColor(config); @@ -34,6 +35,15 @@ static void Main(string[] args) Console.WriteLine($"результат сохранен в {config.PicturePath}"); } + private static void ConfigureSupportedReadingFormats(Config config) + { + Console.WriteLine("Поддерживаются следующие форматы файлов для чтения:"); + var textProviders = FindImplemetations(); + foreach (var point in textProviders) + Console.WriteLine("\t" + point.Key); + config.SupportedReadingFormats = textProviders; + } + private static void ConfigureFont(Config config) { config.Font = new Font("arial", 12); @@ -62,7 +72,7 @@ private static void ConfigureFileSource(Config config) { Console.WriteLine("Введите имя файла источника тэгов"); var inp = Console.ReadLine(); - config.FilePath = inp.Length == 0 ? @"C:\\shpora\\Container\\di-updated\\Client\\TestFile.txt" : inp; + config.FilePath = inp.Length == 0 ? @"TestFile.txt" : inp; } private static string GetLabel(RainbowColors color) diff --git a/Client/test.doc b/Client/test.doc new file mode 100644 index 0000000000000000000000000000000000000000..ff5bcc6fcc24acfb10f2518a57f949dfe6200ef7 GIT binary patch literal 9216 zcmeI2OKenC7=Zsfy=`ZZmNL}xR4!!%EKu4Gh0+%@ZJ}kL4=4(VV)}qkrJdSGz{G{o zn7GiSh6N#-s0q5!n5sKB>Oxi+(S!vdF}N}&E)aBqk&fSg?_3#%wlggTi<#4WbI(1m z|3Clv@44rk+l${Vy!!s9%YKriP$O9~5%Wn-Lb^eGfqP#d;vueS6R}v#O(vQN50C|J z#dXy&G#SKf&@s;iFZf^{JOuM05As3F;{x{e0qSl>)g%k-lPG^9V#y8};5sBPGOFo^ zB0MIs$#iNa&KK#FPgN!(|F#8E*-G;h?eEUtGkyN`J)qD3Lh!>PD1?Wh2o}Q#E zMS)xtSb@(vu#>@%rM9*N3DFI$Nd)I$+&sIz&BpP%NewP3eMwOI8ie~G; z4@(cfy|$JXlbgCgl)SAu>9x^@W@_P18MW;hOtW61Wje9u1$UsQ(R(V(=tvGzFR{($ zH1%jj`yMPCrG+Tg)rHU@EYgC#`)D&57~|ne9TH_X;O2(+O=iz;aINEZ^|Q6#H!Iqb z)1FSQXY4!mKT}>E)9an7u4kUiLM;tEKbbIl3;4@sUw5jS?ij`MEe0u_QJS$PFHf$( z1Uqlu5qD*(lU;r7-Rn2oj~9IOmRHuV{rs}Zd6j+|M#z`OuIP!bfvQOi<}-_=&VHXb72UT+vJCe=7F%GyZB!AByLQlca(LZN z_%N=sdrdBtc&}v&6BAOzzo5KY`F#B97Qey$durl)3_sA$Y{F^AqB}wUkF;YC@oXB) z)S2@ZP(M*Wyx+kim+!a8jJ%8mG8ULq7N}-d9AfWSW!ABOv}C6DP1J5=|Jh*fruKAq z&JKJRk}R1j-fouLd-ZC&RM2j_#g3PwooZNT%L$vcw%+yT=6^Kb&fYgz+7- zUiD^Ed{=Bqm{WjC@@vS;6Q>RBaRqy3)vAu)c5r&5_i7W}SzEH5a+bXwn^OaLugb8_ z`xEpe;Kq%6Z7frNZdzbTLy0kXTy<-Va4uBhZI757d70wTBb~oMPk(MEDu2gKVn!zZ zU|WD)S5`o}0um0et*nvSG6{8X2;bq6mE*6JTI1)#tHuY+Vpu&kU?Nu0s2pV89sV$RBmG{p0t9U_0|;Rhkx zKDzC<55)D`?=B8gk1-<77ZdjY+9^BP?>gjXlcCL}D}N(h(2< literal 0 HcmV?d00001 diff --git a/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs b/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs index 6dc114f0..78454fba 100644 --- a/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs +++ b/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs @@ -2,7 +2,6 @@ using System.Drawing; using TagsCloudContainer.ColorProviders; using TagsCloudContainer.StringParsers; -using TagsCloudContainer.TagGenerator; using TagsCloudContainer.TextProviders; using TagsCloudContainer.WordFilters; diff --git a/TagsCloudContainer/Configuration/Config.cs b/TagsCloudContainer/Configuration/Config.cs index 70f5c65f..664ab43f 100644 --- a/TagsCloudContainer/Configuration/Config.cs +++ b/TagsCloudContainer/Configuration/Config.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TagsCloudContainer.PointGenerators; +using System.Drawing; namespace TagsCloudContainer.Configuration; @@ -22,4 +16,6 @@ public class Config public Font Font { get; set; } + public Dictionary SupportedReadingFormats { get; set; } + } \ No newline at end of file diff --git a/TagsCloudContainer/LabelAttribute.cs b/TagsCloudContainer/LabelAttribute.cs index 19d9fbe4..263149f8 100644 --- a/TagsCloudContainer/LabelAttribute.cs +++ b/TagsCloudContainer/LabelAttribute.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace TagsCloudContainer +namespace TagsCloudContainer { public class LabelAttribute(string labelText) : Attribute { diff --git a/TagsCloudContainer/RainbowColors.cs b/TagsCloudContainer/RainbowColors.cs index 70702b7d..ace69583 100644 --- a/TagsCloudContainer/RainbowColors.cs +++ b/TagsCloudContainer/RainbowColors.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace TagsCloudContainer +namespace TagsCloudContainer { public enum RainbowColors { diff --git a/TagsCloudContainer/StringParsers/IStringParser.cs b/TagsCloudContainer/StringParsers/IStringParser.cs index 0faf6e12..ebe6bcae 100644 --- a/TagsCloudContainer/StringParsers/IStringParser.cs +++ b/TagsCloudContainer/StringParsers/IStringParser.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace TagsCloudContainer.StringParsers +namespace TagsCloudContainer.StringParsers { public interface IStringParser { diff --git a/TagsCloudContainer/StringParsers/RegexParser.cs b/TagsCloudContainer/StringParsers/RegexParser.cs index 47bce418..632da356 100644 --- a/TagsCloudContainer/StringParsers/RegexParser.cs +++ b/TagsCloudContainer/StringParsers/RegexParser.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Text.RegularExpressions; -using System.Threading.Tasks; +using System.Text.RegularExpressions; namespace TagsCloudContainer.StringParsers { diff --git a/TagsCloudContainer/TagGenerator/ITagsGenerator.cs b/TagsCloudContainer/TagGenerator/ITagsGenerator.cs index 9fff70ba..421c4f02 100644 --- a/TagsCloudContainer/TagGenerator/ITagsGenerator.cs +++ b/TagsCloudContainer/TagGenerator/ITagsGenerator.cs @@ -1,9 +1,4 @@ -using TagsCloudContainer.StringParsers; -using TagsCloudContainer.TextProcessor; -using TagsCloudContainer.TextProviders; -using TagsCloudContainer.WordFilters; - -namespace TagsCloudContainer.TagGenerator +namespace TagsCloudContainer.TagGenerator { public interface ITagsGenerator { diff --git a/TagsCloudContainer/TagsCloudContainer.csproj b/TagsCloudContainer/TagsCloudContainer.csproj index f3e5d07a..912bf120 100644 --- a/TagsCloudContainer/TagsCloudContainer.csproj +++ b/TagsCloudContainer/TagsCloudContainer.csproj @@ -9,6 +9,7 @@ + diff --git a/TagsCloudContainer/TextProcessor/ITextProcessor.cs b/TagsCloudContainer/TextProcessor/ITextProcessor.cs index e730a310..b22fb0fd 100644 --- a/TagsCloudContainer/TextProcessor/ITextProcessor.cs +++ b/TagsCloudContainer/TextProcessor/ITextProcessor.cs @@ -1,8 +1,4 @@ -using TagsCloudContainer.StringParsers; -using TagsCloudContainer.TextProviders; -using TagsCloudContainer.WordFilters; - -namespace TagsCloudContainer.TextProcessor +namespace TagsCloudContainer.TextProcessor { public interface ITextProcessor { diff --git a/TagsCloudContainer/TextProcessor/TextProcessor.cs b/TagsCloudContainer/TextProcessor/TextProcessor.cs index ce9fc715..03f2fe10 100644 --- a/TagsCloudContainer/TextProcessor/TextProcessor.cs +++ b/TagsCloudContainer/TextProcessor/TextProcessor.cs @@ -1,5 +1,4 @@ -using System.Text.RegularExpressions; -using TagsCloudContainer.StringParsers; +using TagsCloudContainer.StringParsers; using TagsCloudContainer.TextProviders; using TagsCloudContainer.WordFilters; diff --git a/TagsCloudContainer/TextProviders/DocTextProvider.cs b/TagsCloudContainer/TextProviders/DocTextProvider.cs new file mode 100644 index 00000000..baa97914 --- /dev/null +++ b/TagsCloudContainer/TextProviders/DocTextProvider.cs @@ -0,0 +1,26 @@ +using NPOI.HWPF; + +namespace TagsCloudContainer.TextProviders; + +[Label(".doc")] +public class DocTextProvider : ITextProvider +{ + private readonly string _filePath; + + public DocTextProvider(string filePath) + { + _filePath = filePath; + } + + public string ReadFile() + { + if (!File.Exists(_filePath)) throw new FileNotFoundException(); + else + { + using var stream = new FileStream(_filePath, FileMode.Open, FileAccess.Read); + var document = new HWPFDocument(stream); + var range = document.GetRange(); + return range.Text; + } + } +} \ No newline at end of file diff --git a/TagsCloudContainer/WordFilters/BoringWordFilter.cs b/TagsCloudContainer/WordFilters/BoringWordFilter.cs index 71c4ae1c..30e1fd1e 100644 --- a/TagsCloudContainer/WordFilters/BoringWordFilter.cs +++ b/TagsCloudContainer/WordFilters/BoringWordFilter.cs @@ -1,5 +1,4 @@ - -namespace TagsCloudContainer.WordFilters +namespace TagsCloudContainer.WordFilters { public class BoringWordFilter : IWordFilter { diff --git a/TagsCloudContainer/WordFilters/ShortWordFilter.cs b/TagsCloudContainer/WordFilters/ShortWordFilter.cs index a1d0c649..90aa7144 100644 --- a/TagsCloudContainer/WordFilters/ShortWordFilter.cs +++ b/TagsCloudContainer/WordFilters/ShortWordFilter.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace TagsCloudContainer.WordFilters +namespace TagsCloudContainer.WordFilters { public class ShortWordFilter : IWordFilter { From 29142c4037f42dafdc309cd6122a27208176f19a Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Tue, 7 Jan 2025 19:28:30 +0500 Subject: [PATCH 20/29] DocXTextProvider --- Client/Client.csproj | 3 +++ Client/test.docx | Bin 0 -> 5400 bytes TagsCloudContainer/TagsCloudContainer.csproj | 1 + .../TextProviders/DocXTextProvider.cs | 22 ++++++++++++++++++ 4 files changed, 26 insertions(+) create mode 100644 Client/test.docx create mode 100644 TagsCloudContainer/TextProviders/DocXTextProvider.cs diff --git a/Client/Client.csproj b/Client/Client.csproj index 4c9fcd11..57477690 100644 --- a/Client/Client.csproj +++ b/Client/Client.csproj @@ -15,6 +15,9 @@ Always + + Always + Always diff --git a/Client/test.docx b/Client/test.docx new file mode 100644 index 0000000000000000000000000000000000000000..e504defcccc61ac994721ff81d27824eeae92734 GIT binary patch literal 5400 zcmaJ_c|4T;`ev+Cmce9cBqUq*Es5-V_AO*L)@(D#GImP#eakk&SZfd}hL9!7zONxs zNC=bV^_%IO_vG#TPWOD~`OF{B^*r;vulv64MOTxE7)(G;PEJ4y=Q1NWW9MEB__b-u7?hm18BdO(E2joI)HW&Jl(ko>_Sj;L5lB;R>*L^B zMMQcx)ybAKWNg?DqMgljnQuQAh`X3ib+8VZ+^hVWtYcWgp(6k_y00)Ul%C>FQaKJykay8DG!aHaSR1ZVNyqCEPVJ1FaVdpxM92fun%e<BrRY+=0qo|=4`)KMCf=TOz!G*O<50njKNj~yUa2oR(^|EluoFk) z=)`MzjT4Cx($>au!~fT>^1-&x4Hy(aetO(}48ilmmS|UG zLr6d{^GAL_XZf-5^u*JXrzh{;E(B^mxS%(B&C6DSyob@KI-tU^xO5OQEqUH6eKKQH z9~(qjCboL`yiPP{YOi*sW~+$EjH}-4jcH034KET>z8{Ol1hAxgeUvUEm8$lnDH!b3 z4DL2IhCH_{iVcr|#lF~^e+Tp`>SVb~>vSP2uR#V&u$jL$r+p4Fyi}>W0@NA_|1k8# z1fb^o`g9-8n8->>bF>~i^_M{rrH{3+-c|QEGm`E zbBMk{FBFzD{<=qKlNr-8AMTjSLAk|_-K@E=F|-|*?Aj(VEs-#io9x=jVvc4-4DQBd z#8G?;ol#q8VFEcmQ%SB%Dp?48i3E8F>*~9=8UL2ZcZKz>MSJ zz6Z=+@Lv*wOBo+GsJl0wj(^FCe&0DfCr1}-wj`=!qQ&;u!%fOBgGM%qW;i%1*A^g# z&%>i1vN%fI6um8BVeu*>Z|Fyt6QMeoNU{3%(3-~UTMKav=w6l2ROaf7kDfu+O(%ye z<+SWGa|4a+xPt8Yz6kNeuXB*B@w%5t^`85|k+3?uC(e2_Xg{M6IztacAG>T{b~DI` zZ?D%wQHclDe8_*we^|(E5ykN#>`5Wfanp&t+R?C6?+UW>emKM4wKv{_R7u>&(AFrf z-D~alddR`PftO|%BW~w2sqmaiqBltK-to1sa2v&)J@b#=!K=tm@0|6^rtysSj1XhX zf`QV@hhim_PD-(*Y=Z)F>AftkeR8`V)2TMh3|kN`+m3K<8l8gx(uXkNyDCR{224VR zUGALK`YuPZh|`11u*pg3=qzz|1$~xAxBK-D<{VwKsO>_x*7*fX(?E4d%6p&V$p-R_~ z?&irg_E)eSB)O}k)}}|1I}5FhUKw?*FTFypcn;f}#96BZkpYK*YlhvMZ;uKy1hY$W z>MO9ZXLQGd;;%6_HYl}|qIT3W9&MZ#YoXaOVK;vX3*+9BGZr%rQ_;gvkbIk+=5Mx) zrmdBONK?A0ezVapQufQfcZFk~iq4>hSZW#-SD~JP4Nsf#yI46Rek9rq*z90Dfk*_bk^JrYdJwSp|R3<+bvY=&%>cd-P%U2{F@b(RqZ2qs7%bA9vkP3PwU4wckhdr2^Z( z4XP6-E5$|6@o?HYH~?Q!_K8!Z8fjZdTw(Z8_^vweNDyeO9IdXucZ-v(B9@ecD~lrF zfmpM*MI)4D{Wa76`@QBmCU1s#@-7+geb$sOY4s zyU@FCZA~O*-9i%rH>^S9+l3Pv; z0rB?Ogg27V@fRh3mBm79dQbOD&|Q%F8XynlAk8HZyWF%;=7C;ws&l$+?WgcGg@z7oXd|O6$1zW z-I(`h+T&*CI^I? zy==niX?pSr2G{j>%jn64g6bae#tf`s^iT%|kEMznYLLuT>re@mbROf?qd0M$m5I(? zW!o^@N2glbIc1r3U&+(HmN4r(W)ROQAJNDc^s(n1`@ZiT`Q)#-ytg!n&6s;ViA}dy z2zndr(0JeERQp-xHm3d^cKq}!!r#&N^k?y{k+gp}f-5a?=l+xrw14M=gNM7fp^fdm zpIvHwa+`LC5XFNnY2~z|ad%pBqjgT=CE}atZ34?LPm^g;F&*vm`Bifq#1|CoW@%6N zw+8m_ZM(-$A{{r&(o($yNK#uqF14 zS|F0YHqI!5-fzRKxOlAJoc!~8qp5d`3&x-)ceeU%sef>)=^#g<#9!AXo<6N_CqvEi z>91)?UnF-1xO?}(f{W>DSkb_MYW~_gwqU`2fDf>OaKNu;DvSr3Pr4OR2spg(Kwm{G zJytsB_A`{o`rS5{I@vL7e-)kCB|~`3)@#>EMrk$E#2rfK`bLN&sk*@Yx@%c)K!JU ztofH-A}ddi_7kY{|JDCYqz73j;`$#;;y+RFp9&9$dV4#&JN^_UXp)W&j)BfCL=5l! zoD8CniuzPZRsqi`kfWf_L^MYN?R_XBpxJ07NpiY%6?M>T77}Q}ECkg=8u^%(10s;~ z4}vOhH3&8)R`gIH$y5o=ecn`!&%JkX7ZHi&Wmp2iL>pb~6XM6G;?~$Q!NTMaZhvd0 z5zB$J!>k_be%P11mdc92%{J1pM=R9Hf}c2TMHufajiwKC4K5R;F7t0l+}7hD^Q;ai z%crNH8(9}lU<@tkj)LcS(lGAOU^kSu-V6X2*G1*yCSy?@$Ex+F0gu!P1miQS=S&@c zU{z#zKvMoK0NID%77R3?T`>DBAf?fao_(v zX&3P?hAt4HB{c` zYDcpMub2h21~-P6T+?OSUYr(SLq@^JQiS_3kPR(Uca` zYyi@k&Lfzs<(){Im%$EK)btoT=~c;sSNuv)o}ofSf)zk*VwFQyU0QiEHA1s)nQb>G z2oxAm9r_)V7hLuB;BpDu04tpokQ%yvp^0Fa@+F@%=i?BV42FPUwIywF zY6!5TK8xlrvrPC18rUlAYy7@UNbMCTbAs(%pu$F>0uLKaf+arun7h@l4I$Z0S6kseahgHJFbW?ew$Jqqd8Yd5=*86Sj8p=$~eE? zu_2v>UydVCSHH?E*iaZLeF)}RBw>v0zrO1&#z$i&3_(P0>DZG^x3Ik3EKshsT+&#( zZq_O8HSTWc^wuvaN#GV`Xt5TC$FB=|JHJXN-& zl}dC(RYvr-IwL$z^o^Vqs{45evyTP{?{iYXp;a@X{9nNB>OK$CJe_h5QiC7&R)yTi zg7(Y_N}G&qjj=yz3c2iHm%X{Wd3vMur2WpSM<40s{W`}JI(&fbqqoCZa4I=O{-*$= z!v&bRB2FQ3bBUFqzb6!Kfe$ZCS|3g(LCOawF+JvKFQ8!{jcJm^tKNF!J(H=UAX)pS zl9RwLbwebtSQNcPa!PjKK%V*%qCwB6mUOjbh$P$IE+zDnikiiRkN(?9mZ?+TX@U*( zh;Ym_gFRC?206NWmHU&ZoxEL%n08HQ~j|6-x~X< zeZ7u-u~L-F(dcHSbRp8zT(cjL;aoLrnHMpA8T7P%QZv){t%#w7OfXM%D>mQSr=Bc{ zSDU`z;MzRjcg-AtOTH&=`c(V$E=si&9oF(j-O2Zp|Djs@x0j;h%dTEYmn@G7Ee%oh z@vljM-84got8xYzDMSHPU=5MzSbEk3`w&L1S1Vun&?mC=bA_!6IypCAE%q=scU#eM zO&Wny1B4z@&s;qIVeq^q0!y2Ge&6hLDFUK_u49=ej10|bom5zrW@P*D2)OA3gT-}~ zDki|DnTJMn@C^Ovfy?8E`5_WKLxcb@!LKFM*+mR~3H47qvy%E9cy>d8*WX`8k9*aB zZEb#spB-%RYlL6cfQzO7!2i8e_#J(A@WT(kzw8#SQ2c2W{{2a3d)|K@<1Wry|1~!L z4nNx>;2Yy#R*6f8pYUI8^6%iY#SPy@{IVt-6+glM=qY~3pOyB1{`Xsyf4}@pLHr$l xHgWO#|I0$D{tf@V8vOoBRs5 literal 0 HcmV?d00001 diff --git a/TagsCloudContainer/TagsCloudContainer.csproj b/TagsCloudContainer/TagsCloudContainer.csproj index 912bf120..9f2c4773 100644 --- a/TagsCloudContainer/TagsCloudContainer.csproj +++ b/TagsCloudContainer/TagsCloudContainer.csproj @@ -9,6 +9,7 @@ + diff --git a/TagsCloudContainer/TextProviders/DocXTextProvider.cs b/TagsCloudContainer/TextProviders/DocXTextProvider.cs new file mode 100644 index 00000000..3c1b5963 --- /dev/null +++ b/TagsCloudContainer/TextProviders/DocXTextProvider.cs @@ -0,0 +1,22 @@ +using Xceed.Words.NET; + +namespace TagsCloudContainer.TextProviders; + +[Label(".docx")] +public class DocXTextProvider : ITextProvider +{ + private readonly string _filePath; + + public DocXTextProvider(string filePath) + { + _filePath = filePath; + } + + public string ReadFile() + { + if (!File.Exists(_filePath)) throw new FileNotFoundException(); + else + using (var document = DocX.Load(_filePath)) + return document.Text; + } +} \ No newline at end of file From 2cb1edd08a3529c86226a07713f5a9fd829896f6 Mon Sep 17 00:00:00 2001 From: d2em0n Date: Sat, 11 Jan 2025 16:35:26 +0500 Subject: [PATCH 21/29] minus unused usings --- Client/Program.cs | 7 ------- TagsCloudContainer/TagGenerator/TagGenerator.cs | 5 ----- TagsCloudContainer/WordFilters/ToLowerFilter.cs | 8 +------- 3 files changed, 1 insertion(+), 19 deletions(-) diff --git a/Client/Program.cs b/Client/Program.cs index ddb58b1e..9d3ef74c 100644 --- a/Client/Program.cs +++ b/Client/Program.cs @@ -1,16 +1,9 @@ using System.Drawing; using System.Reflection; -using System.Threading.Channels; using TagsCloudContainer.Configuration; using TagsCloudContainer.PointGenerators; -using TagsCloudContainer.StringParsers; using TagsCloudContainer.TextProviders; -using TagsCloudContainer.WordFilters; -using System.Linq; -using System.Security.Cryptography.X509Certificates; -using System.ComponentModel; using TagsCloudContainer; -using TagsCloudContainer.TextProcessor; using Autofac; namespace Client diff --git a/TagsCloudContainer/TagGenerator/TagGenerator.cs b/TagsCloudContainer/TagGenerator/TagGenerator.cs index 5b048608..dff3a334 100644 --- a/TagsCloudContainer/TagGenerator/TagGenerator.cs +++ b/TagsCloudContainer/TagGenerator/TagGenerator.cs @@ -1,10 +1,5 @@ using System.Drawing; -using System.Linq; using TagsCloudContainer.ColorProviders; -using TagsCloudContainer.StringParsers; -using TagsCloudContainer.TextProcessor; -using TagsCloudContainer.TextProviders; -using TagsCloudContainer.WordFilters; namespace TagsCloudContainer.TagGenerator { diff --git a/TagsCloudContainer/WordFilters/ToLowerFilter.cs b/TagsCloudContainer/WordFilters/ToLowerFilter.cs index 4c441a2e..9252c8d1 100644 --- a/TagsCloudContainer/WordFilters/ToLowerFilter.cs +++ b/TagsCloudContainer/WordFilters/ToLowerFilter.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace TagsCloudContainer.WordFilters; +namespace TagsCloudContainer.WordFilters; public class ToLowerFilter : IWordFilter { From 8eeaab8a426c32932557ac4e25ce1f9fdb0d451b Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Sun, 12 Jan 2025 16:01:48 +0500 Subject: [PATCH 22/29] fix color and pointgenerator checking --- Client/Program.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Client/Program.cs b/Client/Program.cs index 9d3ef74c..f9db896b 100644 --- a/Client/Program.cs +++ b/Client/Program.cs @@ -53,8 +53,8 @@ private static void ConfigureStartPoint(Config config) { Console.WriteLine("Введите координаты центра поля для рисования" + "\n При некорректном вводе координаты центра составят ( 1000, 1000)"); - var xLine = Console.ReadLine(); - var yLine = Console.ReadLine(); + var xLine = ReadValue("Координата Х"); + var yLine = ReadValue("Координата Y"); if (int.TryParse(xLine, out var xResult) && int.TryParse(yLine, out var yResult)) config.StartPoint = new Point(xResult, yResult); @@ -88,9 +88,9 @@ private static void ConfigureColor(Config config) Console.WriteLine("В случае неправильного ввода - цвет будет выбираться случайным образом"); var inp = Console.ReadLine().ToLower(); - if (colors.ContainsKey(inp)) + if (colors.TryGetValue(inp, out var colorName)) { - config.Color = Color.FromName(colors[inp].ToString()); + config.Color = Color.FromName(colorName.ToString()); Console.WriteLine($"Выбран {inp} цвет"); } else @@ -106,8 +106,8 @@ private static void ConfigureCloudView(Config config) Console.WriteLine("\t" + point.Key); Console.WriteLine("Введите, соблюдая орфографию"); var pointGenerator = Console.ReadLine().ToLower(); - if (pointGenerators.ContainsKey(pointGenerator)) - config.PointGenerator = pointGenerators[pointGenerator]; + if (pointGenerators.TryGetValue(pointGenerator, out var pointGeneratorName)) + config.PointGenerator = pointGeneratorName; else { Console.WriteLine("Такой формы не предусмотрено"); @@ -123,5 +123,11 @@ private static Dictionary FindImplemetations() .Where(t => type.IsAssignableFrom(t) && !t.IsInterface) .ToDictionary(x => x.GetCustomAttribute().LabelText.ToLower(), x => x); } + + private static string? ReadValue(string? argName = null) + { + Console.Write($"{argName ?? ""}: "); + return Console.ReadLine(); + } } } From ab0392fa3845e4e31d5f6ee26fa8493fde8616d9 Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Sun, 12 Jan 2025 16:05:52 +0500 Subject: [PATCH 23/29] fix color data integrity --- TagsCloudContainer/ColorProviders/ColorProvider.cs | 12 +++++++----- TagsCloudContainer/ColorProviders/IColorProvider.cs | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/TagsCloudContainer/ColorProviders/ColorProvider.cs b/TagsCloudContainer/ColorProviders/ColorProvider.cs index b91dc33b..07b3e61e 100644 --- a/TagsCloudContainer/ColorProviders/ColorProvider.cs +++ b/TagsCloudContainer/ColorProviders/ColorProvider.cs @@ -1,11 +1,13 @@ using System.Drawing; +using System.Runtime.CompilerServices; namespace TagsCloudContainer.ColorProviders; -public class ColorProvider(Color color) : IColorProvider +public class ColorProvider : IColorProvider { - public Color GetColor() - { - return color; - } + [CompilerGenerated] private readonly Color _color; + + public ColorProvider(Color color) => _color = color; + + public Color GetColor() => _color; } \ No newline at end of file diff --git a/TagsCloudContainer/ColorProviders/IColorProvider.cs b/TagsCloudContainer/ColorProviders/IColorProvider.cs index a4bf0558..682ceaa1 100644 --- a/TagsCloudContainer/ColorProviders/IColorProvider.cs +++ b/TagsCloudContainer/ColorProviders/IColorProvider.cs @@ -4,5 +4,5 @@ namespace TagsCloudContainer.ColorProviders; public interface IColorProvider { - public Color GetColor(); + Color GetColor(); } \ No newline at end of file From 825fa467ff79250692946a3eb4a230e443953b9f Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Sun, 12 Jan 2025 16:07:44 +0500 Subject: [PATCH 24/29] fix random for multithreading in randomcolorprovider --- TagsCloudContainer/ColorProviders/RandomColorProvider.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/TagsCloudContainer/ColorProviders/RandomColorProvider.cs b/TagsCloudContainer/ColorProviders/RandomColorProvider.cs index d70e559c..2d186faa 100644 --- a/TagsCloudContainer/ColorProviders/RandomColorProvider.cs +++ b/TagsCloudContainer/ColorProviders/RandomColorProvider.cs @@ -4,9 +4,8 @@ namespace TagsCloudContainer.ColorProviders; public class RandomColorProvider : IColorProvider { - private static readonly Random Random = new(); public Color GetColor() { - return Color.FromArgb(Random.Next(50, 255), Random.Next(0, 255), Random.Next(0, 255), Random.Next(0, 255)); + return Color.FromArgb(Random.Shared.Next(50, 255), Random.Shared.Next(0, 255), Random.Shared.Next(0, 255), Random.Shared.Next(0, 255)); } } \ No newline at end of file From 2fa68165ae786bd725f4b79985c112cb4736693f Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Sun, 12 Jan 2025 16:33:15 +0500 Subject: [PATCH 25/29] no public mod to interface methods --- TagsCloudContainer/StringParsers/IStringParser.cs | 2 +- .../TextProviders/DocTextProvider.cs | 14 ++++++-------- .../TextProviders/DocXTextProvider.cs | 8 ++++---- .../TextProviders/TxtTextProvider.cs | 3 ++- TagsCloudContainer/WordFilters/IWordFilter.cs | 2 +- 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/TagsCloudContainer/StringParsers/IStringParser.cs b/TagsCloudContainer/StringParsers/IStringParser.cs index ebe6bcae..f85d252c 100644 --- a/TagsCloudContainer/StringParsers/IStringParser.cs +++ b/TagsCloudContainer/StringParsers/IStringParser.cs @@ -2,6 +2,6 @@ { public interface IStringParser { - public IEnumerable GetWordsFromString(string input); + IEnumerable GetWordsFromString(string input); } } diff --git a/TagsCloudContainer/TextProviders/DocTextProvider.cs b/TagsCloudContainer/TextProviders/DocTextProvider.cs index baa97914..cfdd9cc1 100644 --- a/TagsCloudContainer/TextProviders/DocTextProvider.cs +++ b/TagsCloudContainer/TextProviders/DocTextProvider.cs @@ -14,13 +14,11 @@ public DocTextProvider(string filePath) public string ReadFile() { - if (!File.Exists(_filePath)) throw new FileNotFoundException(); - else - { - using var stream = new FileStream(_filePath, FileMode.Open, FileAccess.Read); - var document = new HWPFDocument(stream); - var range = document.GetRange(); - return range.Text; - } + if (!File.Exists(_filePath)) + throw new FileNotFoundException(); + using var stream = new FileStream(_filePath, FileMode.Open, FileAccess.Read); + var document = new HWPFDocument(stream); + var range = document.GetRange(); + return range.Text; } } \ No newline at end of file diff --git a/TagsCloudContainer/TextProviders/DocXTextProvider.cs b/TagsCloudContainer/TextProviders/DocXTextProvider.cs index 3c1b5963..e4e83b22 100644 --- a/TagsCloudContainer/TextProviders/DocXTextProvider.cs +++ b/TagsCloudContainer/TextProviders/DocXTextProvider.cs @@ -14,9 +14,9 @@ public DocXTextProvider(string filePath) public string ReadFile() { - if (!File.Exists(_filePath)) throw new FileNotFoundException(); - else - using (var document = DocX.Load(_filePath)) - return document.Text; + if (!File.Exists(_filePath)) + throw new FileNotFoundException(); + using var document = DocX.Load(_filePath); + return document.Text; } } \ No newline at end of file diff --git a/TagsCloudContainer/TextProviders/TxtTextProvider.cs b/TagsCloudContainer/TextProviders/TxtTextProvider.cs index 907f7050..02dd593c 100644 --- a/TagsCloudContainer/TextProviders/TxtTextProvider.cs +++ b/TagsCloudContainer/TextProviders/TxtTextProvider.cs @@ -12,7 +12,8 @@ public TxtTextProvider(string filePath) public string ReadFile() { - if (!File.Exists(_filePath)) throw new FileNotFoundException(); + if (!File.Exists(_filePath)) + throw new FileNotFoundException(); return File.ReadAllText(_filePath).ToLower(); } } \ No newline at end of file diff --git a/TagsCloudContainer/WordFilters/IWordFilter.cs b/TagsCloudContainer/WordFilters/IWordFilter.cs index 532cb64d..e5e6bcd4 100644 --- a/TagsCloudContainer/WordFilters/IWordFilter.cs +++ b/TagsCloudContainer/WordFilters/IWordFilter.cs @@ -2,6 +2,6 @@ { public interface IWordFilter { - public IEnumerable Process(IEnumerable words); + IEnumerable Process(IEnumerable words); } } From 946768e4998dd8981972af7488eb6654e35fc6c9 Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Sun, 12 Jan 2025 16:53:33 +0500 Subject: [PATCH 26/29] fix TxtText provider --- .../RandomColorTagGeneratorShould.cs | 2 +- .../TextProcessorShould.cs | 2 +- TagsCloudContainer/CloudLayout.cs | 4 ++-- TagsCloudContainer/PictureMaker.cs | 22 +++++++++---------- .../TextProviders/TxtTextProvider.cs | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs b/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs index 78454fba..21810049 100644 --- a/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs +++ b/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs @@ -16,7 +16,7 @@ public void SetRightFontSize() using var graphics = Graphics.FromImage(bitmap); var processor = new TextProcessor.TextProcessor( - new TxtTextProvider(@"TextFile1.txt"), new RegexParser(), new BoringWordFilter()); + new TxtTextProvider(@"TextFile1.txt"), new RegexParser(), new ToLowerFilter(), new BoringWordFilter()); var words = processor.WordFrequencies(); var generator = new TagGenerator.TagGenerator(new RandomColorProvider(), new Font("arial", 12)); var result = generator.GenerateTags(words).First(); diff --git a/TagsCloudContainer.Tests/TextProcessorShould.cs b/TagsCloudContainer.Tests/TextProcessorShould.cs index 474786b5..07a5cfa9 100644 --- a/TagsCloudContainer.Tests/TextProcessorShould.cs +++ b/TagsCloudContainer.Tests/TextProcessorShould.cs @@ -11,7 +11,7 @@ public class TextProcessorShould public void Process() { var result = new TextProcessor.TextProcessor( - new TxtTextProvider(@"TextFile1.txt"), new RegexParser(), new BoringWordFilter(), new ShortWordFilter()).WordFrequencies(); + new TxtTextProvider(@"TextFile1.txt"), new RegexParser(), new ToLowerFilter(), new BoringWordFilter(), new ShortWordFilter()).WordFrequencies(); result.Count.Should().Be(3); diff --git a/TagsCloudContainer/CloudLayout.cs b/TagsCloudContainer/CloudLayout.cs index 1a40cf39..bcc26d51 100644 --- a/TagsCloudContainer/CloudLayout.cs +++ b/TagsCloudContainer/CloudLayout.cs @@ -5,10 +5,10 @@ namespace TagsCloudContainer { public class CloudLayout { - public readonly Point Center; + private readonly Point Center; public readonly Size Size; private readonly IEnumerable _points; - public List Rectangles { get; set; } + private List Rectangles { get; set; } public CloudLayout(Point center, IPointGenerator pointGenerator) diff --git a/TagsCloudContainer/PictureMaker.cs b/TagsCloudContainer/PictureMaker.cs index da33e0d2..1422e33f 100644 --- a/TagsCloudContainer/PictureMaker.cs +++ b/TagsCloudContainer/PictureMaker.cs @@ -7,30 +7,30 @@ namespace TagsCloudContainer; public class PictureMaker { - private readonly IPointGenerator pointGenerator; - private readonly IEnumerable tags; - private readonly string fileName; - private readonly Point startPoint; + private readonly IPointGenerator _pointGenerator; + private readonly IEnumerable _tags; + private readonly string _fileName; + private readonly Point _startPoint; public PictureMaker(IPointGenerator pointGenerator, ITagsGenerator tagGenerator, ITextProcessor textProcessor, string fileName, Point startPoint) { - this.pointGenerator = pointGenerator; - this.tags = tagGenerator.GenerateTags(textProcessor.WordFrequencies()); - this.fileName = fileName; - this.startPoint = startPoint; + _pointGenerator = pointGenerator; + _tags = tagGenerator.GenerateTags(textProcessor.WordFrequencies()); + _fileName = fileName; + _startPoint = startPoint; } public void DrawPicture() { - var layout = new CloudLayout(startPoint, pointGenerator); + var layout = new CloudLayout(_startPoint, _pointGenerator); var image = new Bitmap(layout.Size.Width, layout.Size.Height); - foreach (var tag in tags) + foreach (var tag in _tags) { var rectangle = layout.PutNextRectangle(tag.Frame); DrawTag(image, rectangle, tag); } - image.Save(fileName); + image.Save(_fileName); } private static void DrawTag(Bitmap image, Rectangle rectangle, Tag tag) diff --git a/TagsCloudContainer/TextProviders/TxtTextProvider.cs b/TagsCloudContainer/TextProviders/TxtTextProvider.cs index 02dd593c..1d774fb6 100644 --- a/TagsCloudContainer/TextProviders/TxtTextProvider.cs +++ b/TagsCloudContainer/TextProviders/TxtTextProvider.cs @@ -14,6 +14,6 @@ public string ReadFile() { if (!File.Exists(_filePath)) throw new FileNotFoundException(); - return File.ReadAllText(_filePath).ToLower(); + return File.ReadAllText(_filePath); } } \ No newline at end of file From 7c1ca09dbc3e4ff34e027fcdedcad305d75ffa61 Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Sun, 12 Jan 2025 17:27:22 +0500 Subject: [PATCH 27/29] dispose idisposable --- TagsCloudContainer.Tests/CloudLayoutShould.cs | 5 ++--- ...omColorTagGeneratorShould.cs => TagGeneratorShould.cs} | 7 ++----- TagsCloudContainer/PictureMaker.cs | 8 +++----- 3 files changed, 7 insertions(+), 13 deletions(-) rename TagsCloudContainer.Tests/{RandomColorTagGeneratorShould.cs => TagGeneratorShould.cs} (80%) diff --git a/TagsCloudContainer.Tests/CloudLayoutShould.cs b/TagsCloudContainer.Tests/CloudLayoutShould.cs index 84905921..c99dd167 100644 --- a/TagsCloudContainer.Tests/CloudLayoutShould.cs +++ b/TagsCloudContainer.Tests/CloudLayoutShould.cs @@ -8,7 +8,6 @@ namespace TagsCloudContainer.Tests [TestFixture] public class CloudLayoutShould { - private CloudLayout layout; [TestCase(1, 2, TestName = "Odd coordinate value results in an even size value")] [TestCase(2, 5, TestName = "Even coordinate value results in an odd size value")] public void MakeRightSizeLayout(int coordinateValue, int sizeValue) @@ -16,7 +15,7 @@ public void MakeRightSizeLayout(int coordinateValue, int sizeValue) var center = new Point(coordinateValue, coordinateValue); var size = new Size(sizeValue, sizeValue); - layout = new CloudLayout(center, new ArchemedianSpiral()); + var layout = new CloudLayout(center, new ArchemedianSpiral()); layout.Size.Should().BeEquivalentTo(size); } @@ -36,7 +35,7 @@ public void GetOnlyPositiveCenterCoordinates(int x, int y) [Test] public void PutNextRectangle_ShouldKeepEnteredSize() { - layout = new CloudLayout(new Point(5, 5), new ArchemedianSpiral()); + var layout = new CloudLayout(new Point(5, 5), new ArchemedianSpiral()); var enteredSize = new Size(3, 4); var returnedSize = layout.PutNextRectangle(enteredSize).Size; diff --git a/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs b/TagsCloudContainer.Tests/TagGeneratorShould.cs similarity index 80% rename from TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs rename to TagsCloudContainer.Tests/TagGeneratorShould.cs index 21810049..79753465 100644 --- a/TagsCloudContainer.Tests/RandomColorTagGeneratorShould.cs +++ b/TagsCloudContainer.Tests/TagGeneratorShould.cs @@ -7,18 +7,15 @@ namespace TagsCloudContainer.Tests { - public class RandomColorTagGeneratorShould + public class TagGeneratorShould { [Test] public void SetRightFontSize() { - var bitmap = new Bitmap(1, 1); - using var graphics = Graphics.FromImage(bitmap); - var processor = new TextProcessor.TextProcessor( new TxtTextProvider(@"TextFile1.txt"), new RegexParser(), new ToLowerFilter(), new BoringWordFilter()); var words = processor.WordFrequencies(); - var generator = new TagGenerator.TagGenerator(new RandomColorProvider(), new Font("arial", 12)); + var generator = new TagGenerator.TagGenerator(new RandomColorProvider(), new System.Drawing.Font("arial", 12)); var result = generator.GenerateTags(words).First(); result.Font.Name.Should().Be("Arial"); diff --git a/TagsCloudContainer/PictureMaker.cs b/TagsCloudContainer/PictureMaker.cs index 1422e33f..1b58c696 100644 --- a/TagsCloudContainer/PictureMaker.cs +++ b/TagsCloudContainer/PictureMaker.cs @@ -24,7 +24,7 @@ public PictureMaker(IPointGenerator pointGenerator, ITagsGenerator tagGenerator, public void DrawPicture() { var layout = new CloudLayout(_startPoint, _pointGenerator); - var image = new Bitmap(layout.Size.Width, layout.Size.Height); + using var image = new Bitmap(layout.Size.Width, layout.Size.Height); foreach (var tag in _tags) { var rectangle = layout.PutNextRectangle(tag.Frame); @@ -35,10 +35,8 @@ public void DrawPicture() private static void DrawTag(Bitmap image, Rectangle rectangle, Tag tag) { - var brush = new SolidBrush(tag.Color); - var formGraphics = Graphics.FromImage(image); + using var brush = new SolidBrush(tag.Color); + using var formGraphics = Graphics.FromImage(image); formGraphics.DrawString(tag.Word.Value, tag.Font, brush, rectangle.Location); - brush.Dispose(); - formGraphics.Dispose(); } } \ No newline at end of file From ef193e7107a937f4eaa5dce14e1d3fa864dbafe2 Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Sun, 12 Jan 2025 17:31:30 +0500 Subject: [PATCH 28/29] fix regexparser --- TagsCloudContainer/StringParsers/RegexParser.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/TagsCloudContainer/StringParsers/RegexParser.cs b/TagsCloudContainer/StringParsers/RegexParser.cs index 632da356..9887a74f 100644 --- a/TagsCloudContainer/StringParsers/RegexParser.cs +++ b/TagsCloudContainer/StringParsers/RegexParser.cs @@ -4,11 +4,10 @@ namespace TagsCloudContainer.StringParsers { public class RegexParser : IStringParser { + private readonly Regex _regex = new("\\b(?:\\w|-)+\\b", RegexOptions.Compiled); public IEnumerable GetWordsFromString(string input) { - var regex = new Regex("\\b(?:\\w|-)+\\b"); - - return regex.Matches(input) + return _regex.Matches(input) .Cast() .Select(w => new Word(w.Value)); } From cb067a406e77af86f8e5f7b66caac735465a10bc Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Sun, 12 Jan 2025 17:55:38 +0500 Subject: [PATCH 29/29] fix packages --- TagsCloudContainer.Tests/TagGeneratorShould.cs | 1 - TagsCloudContainer/TagsCloudContainer.csproj | 1 - 2 files changed, 2 deletions(-) diff --git a/TagsCloudContainer.Tests/TagGeneratorShould.cs b/TagsCloudContainer.Tests/TagGeneratorShould.cs index 79753465..3ab47e91 100644 --- a/TagsCloudContainer.Tests/TagGeneratorShould.cs +++ b/TagsCloudContainer.Tests/TagGeneratorShould.cs @@ -1,5 +1,4 @@ using FluentAssertions; -using System.Drawing; using TagsCloudContainer.ColorProviders; using TagsCloudContainer.StringParsers; using TagsCloudContainer.TextProviders; diff --git a/TagsCloudContainer/TagsCloudContainer.csproj b/TagsCloudContainer/TagsCloudContainer.csproj index 9f2c4773..e35483a8 100644 --- a/TagsCloudContainer/TagsCloudContainer.csproj +++ b/TagsCloudContainer/TagsCloudContainer.csproj @@ -11,7 +11,6 @@ -