-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathTranslator.cs
34 lines (30 loc) · 1007 Bytes
/
Translator.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
using System.Collections.Generic;
using System.Text;
namespace Algorithms.DataCompression;
/// <summary>
/// Provides method for text conversion by key mapping.
/// </summary>
public class Translator
{
/// <summary>
/// Converts the input text according to the translation keys.
/// </summary>
/// <param name="text">Input text.</param>
/// <param name="translationKeys">Translation keys used for text matching.</param>
/// <returns>Converted text according to the translation keys.</returns>
public string Translate(string text, Dictionary<string, string> translationKeys)
{
var sb = new StringBuilder();
var start = 0;
for (var i = 0; i < text.Length; i++)
{
var key = text.Substring(start, i - start + 1);
if (translationKeys.ContainsKey(key))
{
_ = sb.Append(translationKeys[key]);
start = i + 1;
}
}
return sb.ToString();
}
}