using System.Text.RegularExpressions; using System.Text; using Content.Shared.Speech.Components; namespace Content.Shared.Speech.EntitySystems; public sealed partial class SpanishAccentSystem : RelayAccentSystem { // for words in all lowercase (multiple "s" are allowed) private static readonly Regex RegexLower = new(@"(? Estation; Capital "S" Is Replaced Directly, Multiple "S" Are Allowed) private static readonly Regex RegexCaps = new(@"(?? ent = null) { // Insert E before every S that is followed by a consonant that makes a distinct sound // (H is excluded because [sh] is a single sound) message = InsertS(message); // If a sentence ends with ?, insert a reverse ? at the beginning of the sentence message = ReplacePunctuation(message); return message; } private string InsertS(string message) { // Replace every new Word that starts with s/S and a consonant message = RegexLower.Replace(message, "e$1"); message = RegexCaps.Replace(message, "Es$1"); message = RegexUpper.Replace(message, "E$1"); return message; } private string ReplacePunctuation(string message) { var sentences = AccentSystem.SentenceRegex.Split(message); var msg = new StringBuilder(); foreach (var s in sentences) { var toInsert = new StringBuilder(); for (var i = s.Length - 1; i >= 0 && "?!‽".Contains(s[i]); i--) { toInsert.Append(s[i] switch { '?' => '¿', '!' => '¡', '‽' => '⸘', _ => ' ' }); } if (toInsert.Length == 0) msg.Append(s); else msg.Append(s.Insert(s.Length - s.TrimStart().Length, toInsert.ToString())); } return msg.ToString(); } }