-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTranscriptReplacement.cs
More file actions
35 lines (30 loc) · 1 KB
/
TranscriptReplacement.cs
File metadata and controls
35 lines (30 loc) · 1 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
namespace PrimeDictate;
internal static class TranscriptReplacement
{
/// <summary>
/// Applies user rules in order of longest find-string first so multi-word phrases win over shorter overlaps.
/// Matching is case-insensitive; replacement text is literal.
/// </summary>
public static string Apply(string text, IReadOnlyList<TranscriptReplacementRule> rules)
{
if (string.IsNullOrEmpty(text) || rules.Count == 0)
{
return text;
}
var ordered = rules
.Select(r => (Find: r.Find.Trim(), Replace: r.Replace ?? string.Empty))
.Where(pair => pair.Find.Length > 0)
.OrderByDescending(pair => pair.Find.Length)
.ToList();
if (ordered.Count == 0)
{
return text;
}
var result = text;
foreach (var (find, replace) in ordered)
{
result = result.Replace(find, replace, StringComparison.OrdinalIgnoreCase);
}
return result;
}
}