improve acronym generator

This commit is contained in:
13xforever 2022-08-17 03:47:57 +05:00
parent e27773567c
commit 133f89cff7
3 changed files with 20 additions and 6 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
@ -400,17 +401,19 @@ public static class StringUtils
return len == 0 ? "" : str.Substring(start, len);
}
internal static string GetAcronym(this string str)
internal static string GetAcronym(this string? str, bool includeAllCaps = false, bool includeAllDigits = false)
{
if (string.IsNullOrEmpty(str))
return str;
return "";
var result = "";
bool previousWasLetter = false;
var previousWasLetter = false;
foreach (var c in str)
{
var isLetter = char.IsLetterOrDigit(c);
if (isLetter && !previousWasLetter)
if ((isLetter && !previousWasLetter)
|| (includeAllCaps && char.IsUpper(c))
|| (includeAllDigits && char.IsDigit(c)))
result += c;
previousWasLetter = isLetter;
}

View File

@ -51,13 +51,13 @@ public static class TimeParser
}
else
{
var a = tzi.StandardName.GetAcronym();
var a = tzi.StandardName.GetAcronym(includeAllCaps: true, includeAllDigits: true);
if (TimeZoneAcronyms.ContainsKey(a))
continue;
if (!standardNames.ContainsKey(a))
standardNames[a] = tzi;
a = tzi.DaylightName.GetAcronym();
a = tzi.DaylightName.GetAcronym(includeAllCaps: true, includeAllDigits: true);
if (TimeZoneAcronyms.ContainsKey(a) || standardNames.ContainsKey(a))
continue;

View File

@ -110,6 +110,17 @@ ignorance the hard way.""
Assert.That(coef, Is.GreaterThan(0.95), "Dice Coefficient");
}
[TestCase("Metal Gear Solid 4", "MGS4", "MGS4", "MGS4", "MGS4")]
[TestCase("UTC-11", "U1", "UTC1", "U11", "UTC11")]
[TestCase("camelCaseString13", "c", "cCS", "c13", "cCS13")]
public void AcronymGenerationTest(string input, string expectedDefault, string expectedWithUpper, string expectedWithDigits, string expectedWithUpperAndDigits)
{
Assert.That(input.GetAcronym(), Is.EqualTo(expectedDefault));
Assert.That(input.GetAcronym(includeAllCaps: true), Is.EqualTo(expectedWithUpper));
Assert.That(input.GetAcronym(includeAllDigits: true), Is.EqualTo(expectedWithDigits));
Assert.That(input.GetAcronym(includeAllCaps: true, includeAllDigits: true), Is.EqualTo(expectedWithUpperAndDigits));
}
public static double DiceCoefficient(string input, string comparedTo)
{
var ngrams = input.ToBiGrams()[1..^1];