CatStation/Content.Shared/Speech/EntitySystems/SpanishAccentSystem.cs
DrSmugleaf 65f5f67cd5
Make every EntitySystem partial (#45220)
* Make every EntitySystem partial

* Oobs thats for a diff PR

* Also oops on that one

* Test systems

* git are we fr

* Update file templates

* cleanup empty classes while we are here

* forgor

---------

Co-authored-by: AJCM-git <ajcm_mail@disroot.org>
2026-08-14 18:24:47 +00:00

61 lines
2.3 KiB
C#

using System.Text.RegularExpressions;
using System.Text;
using Content.Shared.Speech.Components;
namespace Content.Shared.Speech.EntitySystems;
public sealed partial class SpanishAccentSystem : RelayAccentSystem<SpanishAccentComponent>
{
// for words in all lowercase (multiple "s" are allowed)
private static readonly Regex RegexLower = new(@"(?<!\w)(s+h*[bcdfgjklmnpqrtvwxz])");
// For Capitalized Words (Station -> Estation; Capital "S" Is Replaced Directly, Multiple "S" Are Allowed)
private static readonly Regex RegexCaps = new(@"(?<!\w)S(s*h*[bcdfgjklmnpqrtvwxz])");
// FOR WORDS IN ALL UPPERCASE (ONLY ONE "S" IS ALLOWED, ASSUMING IT'S NOT AN ACRONYM)
private static readonly Regex RegexUpper = new(@"(?<!\w)(SH*[BCDFGJKLMNPQRTVWXZ])");
public override string Accentuate(string message, Entity<SpanishAccentComponent>? 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();
}
}