2024-08-08 05:08:28 +02:00
|
|
|
using System.Text;
|
2021-10-11 20:18:39 +02:00
|
|
|
using Content.Server.Speech.Components;
|
|
|
|
|
|
|
|
|
|
namespace Content.Server.Speech.EntitySystems
|
|
|
|
|
{
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class SpanishAccentSystem : EntitySystem
|
2021-10-11 20:18:39 +02:00
|
|
|
{
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
SubscribeLocalEvent<SpanishAccentComponent, AccentGetEvent>(OnAccent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Accentuate(string message)
|
|
|
|
|
{
|
|
|
|
|
// Insert E before every S
|
|
|
|
|
message = InsertS(message);
|
|
|
|
|
// If a sentence ends with ?, insert a reverse ? at the beginning of the sentence
|
2024-08-08 05:08:28 +02:00
|
|
|
message = ReplacePunctuation(message);
|
2021-10-11 20:18:39 +02:00
|
|
|
return message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string InsertS(string message)
|
|
|
|
|
{
|
|
|
|
|
// Replace every new Word that starts with s/S
|
|
|
|
|
var msg = message.Replace(" s", " es").Replace(" S", " Es");
|
|
|
|
|
|
|
|
|
|
// Still need to check if the beginning of the message starts
|
2023-01-19 03:56:45 +01:00
|
|
|
if (msg.StartsWith("s", StringComparison.Ordinal))
|
2021-10-11 20:18:39 +02:00
|
|
|
{
|
|
|
|
|
return msg.Remove(0, 1).Insert(0, "es");
|
|
|
|
|
}
|
2023-01-19 03:56:45 +01:00
|
|
|
else if (msg.StartsWith("S", StringComparison.Ordinal))
|
2021-10-11 20:18:39 +02:00
|
|
|
{
|
|
|
|
|
return msg.Remove(0, 1).Insert(0, "Es");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-08 05:08:28 +02:00
|
|
|
private string ReplacePunctuation(string message)
|
2021-10-11 20:18:39 +02:00
|
|
|
{
|
|
|
|
|
var sentences = AccentSystem.SentenceRegex.Split(message);
|
2024-08-08 05:08:28 +02:00
|
|
|
var msg = new StringBuilder();
|
2021-10-11 20:18:39 +02:00
|
|
|
foreach (var s in sentences)
|
|
|
|
|
{
|
2024-08-08 05:08:28 +02:00
|
|
|
var toInsert = new StringBuilder();
|
|
|
|
|
for (var i = s.Length - 1; i >= 0 && "?!‽".Contains(s[i]); i--)
|
2021-10-11 20:18:39 +02:00
|
|
|
{
|
2024-08-08 05:08:28 +02:00
|
|
|
toInsert.Append(s[i] switch
|
|
|
|
|
{
|
|
|
|
|
'?' => '¿',
|
|
|
|
|
'!' => '¡',
|
|
|
|
|
'‽' => '⸘',
|
|
|
|
|
_ => ' '
|
|
|
|
|
});
|
2021-10-11 20:18:39 +02:00
|
|
|
}
|
2024-08-08 05:08:28 +02:00
|
|
|
if (toInsert.Length == 0)
|
2021-10-11 20:18:39 +02:00
|
|
|
{
|
2024-08-08 05:08:28 +02:00
|
|
|
msg.Append(s);
|
|
|
|
|
} else
|
|
|
|
|
{
|
|
|
|
|
msg.Append(s.Insert(s.Length - s.TrimStart().Length, toInsert.ToString()));
|
2021-10-11 20:18:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
2024-08-08 05:08:28 +02:00
|
|
|
return msg.ToString();
|
2021-10-11 20:18:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnAccent(EntityUid uid, SpanishAccentComponent component, AccentGetEvent args)
|
|
|
|
|
{
|
|
|
|
|
args.Message = Accentuate(args.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|