-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordCounterService.cs
35 lines (32 loc) · 1.14 KB
/
WordCounterService.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace WordCounter
{
public class WordCounterService
{
readonly IDataSource m_DataSource;
const string k_Punctuation = " ,.?!;:—\"\r\n";
public WordCounterService(IDataSource dataSource)
{
m_DataSource = dataSource;
}
public async IAsyncEnumerable<WordCountUpdate> GetWordCountUpdates(string[] words, CancellationToken cancellationToken = default, IProgress<int> progress = default)
{
await foreach (var text in m_DataSource.GetData())
{
progress?.Report(1);
cancellationToken.ThrowIfCancellationRequested();
foreach (var word in words)
{
var occurrencesCount = text.Split(k_Punctuation.ToArray()).Count(x => x.Equals(word, StringComparison.OrdinalIgnoreCase));
if (occurrencesCount > 0)
{
yield return new WordCountUpdate(word, occurrencesCount);
}
}
}
}
}
}