-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
132 lines (114 loc) · 4.18 KB
/
MainWindow.xaml.cs
File metadata and controls
132 lines (114 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
namespace TextAnalyzer
{
public partial class MainWindow : Window
{
private DispatcherTimer? _analysisTimer;
private bool _isWindowLoaded = false;
private const string PlaceholderText = "Вставьте или напишите свой текст здесь...";
public MainWindow()
{
InitializeComponent();
SetupAnalysisTimer();
this.Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
_isWindowLoaded = true;
SetPlaceholder();
AnalyzeText();
}
private void SetupAnalysisTimer()
{
_analysisTimer = new DispatcherTimer();
_analysisTimer.Interval = TimeSpan.FromMilliseconds(300);
_analysisTimer.Tick += (s, e) =>
{
if (_analysisTimer != null)
{
_analysisTimer.Stop();
}
AnalyzeText();
};
}
private void InputTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (!_isWindowLoaded || _analysisTimer == null)
{
return;
}
_analysisTimer.Stop();
_analysisTimer.Start();
}
private void AnalyzeText()
{
string text = InputTextBox.Text;
if (text == PlaceholderText)
{
text = "";
}
int wordCount = Regex.Matches(text, @"[\p{L}\p{N}']+").Count;
WordsCountLabel.Text = wordCount.ToString("N0");
CharsCountLabel.Text = text.Length.ToString("N0");
int sentenceCount = Regex.Matches(text, @"[.!?]+").Count;
SentencesCountLabel.Text = sentenceCount.ToString("N0");
int paragraphCount = text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Length;
ParagraphsCountLabel.Text = paragraphCount.ToString("N0");
const int wordsPerMinute = 200;
double minutesToRead = (double)wordCount / wordsPerMinute;
TimeSpan timeToRead = TimeSpan.FromMinutes(minutesToRead);
if (timeToRead.TotalSeconds < 60)
{
ReadingTimeLabel.Text = $"~ {timeToRead.Seconds} сек";
}
else
{
ReadingTimeLabel.Text = $"~ {timeToRead.Minutes} мин {timeToRead.Seconds} сек";
}
if (string.IsNullOrWhiteSpace(text))
{
FrequencyListView.ItemsSource = null;
return;
}
var wordFrequencies = Regex.Matches(text, @"[\p{L}\p{N}']+")
.Cast<Match>()
.Select(m => m.Value.ToLowerInvariant())
.GroupBy(w => w)
.ToDictionary(g => g.Key, g => g.Count())
.OrderByDescending(kvp => kvp.Value)
.Take(10);
FrequencyListView.ItemsSource = wordFrequencies;
}
private void SetPlaceholder()
{
InputTextBox.Text = PlaceholderText;
InputTextBox.Foreground = new SolidColorBrush(Color.FromRgb(119, 119, 119));
}
private void RemovePlaceholder()
{
InputTextBox.Text = "";
InputTextBox.Foreground = new SolidColorBrush(Colors.White);
}
private void InputTextBox_GotFocus(object sender, RoutedEventArgs e)
{
if (InputTextBox.Text == PlaceholderText)
{
RemovePlaceholder();
}
}
private void InputTextBox_LostFocus(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(InputTextBox.Text))
{
SetPlaceholder();
}
}
}
}