using System.Text.RegularExpressions;
using System.Collections.Generic;
public static void Main(string[] args)
var speech = @"Thank you for reaching us! Our office is at 416 Willow Lane, Suite 422, 40512.
For immediate assistance, call (436) 447-8632.
For technical support, contact our helpline at 450.478.7923.
Visit us at 406 Innovation Drive, Floor 4, 41073, for in-person assistance.
Please send all returns to 412 Commerce Way, Warehouse 4, 40291.
For questions, call 448-601-9876.
Our service center is located at 457 Pioneer Avenue, Unit 4B, 43085.
Call us anytime at 459.234.1890.
Thank you for choosing us! Visit our headquarters at 436 Beacon Street,
Suite 405, 45429, or call 447-458-0981.
For more information, reach out to us at 478.450.7821
or visit our office at 456 Oak Hill Road, 40508.
Schedule your appointment today by calling 479.455.2609.
Our address is 455 Crescent Boulevard, Building 4, 44114.
Our new showroom is at 466 Cypress Avenue, Level 4, 43215.
Call (476) 467-1098 for inquiries.
Contact customer service at 471.438.6203,
or drop by our branch at 456 Meadow Lane, 40031.
For billing questions, visit us at 406 Gateway Drive,
Suite 404, 45440, or dial 412.407.9087.
Send correspondence to 490 Elmwood Street, Office 4A, 40205.
For assistance, call 470-450-8790.
Need support? Call 473.441.3216, or stop by our
location at 451 Ridgeview Drive, Unit 4, 44118.
Our help desk is open 24/7 at 474-458-9832.
We’re located at 476 Main Street, Floor 4, 40503.
The product was last updated in 2023 and is currently priced at $12.99.
Our annual report highlights a revenue of $1,000,000 in 2022, with projections for 2024 set at $1.2 million.
Tickets for the event are priced at $100 each, and early bird specials are available for $85.
The package weighs 4.56 lbs and costs $45.99 for shipping to domestic locations.
Our promotional offer runs until December 31, 2024, and includes discounts 20% on purchases over $500.
For customer service, press 490.
For all other calls, press 413.
We will return all calls within 48 hours.
Reach us at 1(800)482-4652";
Console.WriteLine($"Original Text: {speech}");
speech = ApplyNumberFixRules(speech);
Console.WriteLine($"Final Result: {speech}");
public static string ApplyNumberFixRules(string script)
var subPlaceholders = new Dictionary<string, string>();
script = Regex.Replace(script, @"<sub[^>]*>.*?</sub>", match =>
var placeholder = Guid.NewGuid().ToString();
subPlaceholders[placeholder] = match.Value;
script = ApplyRuleFormatPhoneNumbers(script);
script = ApplyRuleAddHyphenAfter4(script);
script = ApplyRuleHandle47Prefix(script);
script = ApplyRuleConvertNumbersToWords(script);
script = script.Replace("4-", "four-");
script = RemovePhoneMarkers(script);
foreach (var placeholder in subPlaceholders)
script = script.Replace(placeholder.Key, placeholder.Value);
private static string ApplyRuleFormatPhoneNumbers(string script)
var phoneNumberPattern = @"\b1[-.\s]?\(?800\)?[-.\s]?\d{3}[-.\s]?\d{4}\b|\b\d{3}[-.\s]?\d{3}[-.\s]?\d{4}\b";
return Regex.Replace(script, phoneNumberPattern, match =>
if (Regex.IsMatch(phone, @"^1[-.\s]?\(?800\)?"))
phone = phone.Replace("(", "").Replace(")", "").Replace("-", "").Replace(".", "").Replace(" ", "");
var firstSeg = "one eight-hundred,,";
var secondSeg = phone.Substring(4, 3);
var thirdSeg = phone.Substring(7);
return $"{firstSeg} {ConvertSegmentToWords(secondSeg)},, {ConvertSegmentToWords(thirdSeg)}";
phone = phone.Replace("(", "").Replace(")", "").Replace("-", "").Replace(".", "").Replace(" ", "");
var firstSegment = phone.Substring(0, 3);
var secondSegment = phone.Substring(3, 3);
var thirdSegment = phone.Substring(6);
return $"{ApplyRuleHandle47Prefix(ConvertSegmentToWords(firstSegment))},, {ConvertSegmentToWords(secondSegment)},, {ConvertSegmentToWords(thirdSegment)}";
private static string ConvertSegmentToWords(string segment)
if (Regex.IsMatch(segment, @"^\d+$"))
var result = segment.Select(NumberToWord).ToList();
return $"{result[0]}-{result[1]} {result[2]}";
return $"{result[0]}-{result[1]} {result[2]} {result[3]}";
return string.Join(" ", result);
private static string ApplyRuleHandle47Prefix(string script)
script = Regex.Replace(script, @"\b47\d{1,}\b(?![^<]*</phone>)", match =>
return "fourseven " + string.Join(" ", value.Substring(2).Select(NumberToWord));
return Regex.Replace(script, @"\bfour seven\b(?![^<]*</phone>)", match => "fourseven");
private static string ApplyRuleAddHyphenAfter4(string script)
return Regex.Replace(script, @"\b4\d{2,}\b", match =>
return "four-" + string.Join(" ", value.Substring(1).Select(NumberToWord));
private static string ApplyRuleConvertNumbersToWords(string script)
return Regex.Replace(script, @"\b\d{3,}(,\d{3})*(\.\d+)?\b(?![^<]*</phone>)", match =>
var number = match.Value;
if (Regex.IsMatch(number, @"^(19|20)\d{2}$")) return number;
if (Regex.IsMatch(script.Substring(0, match.Index).TrimEnd(), @"\$\s*$")) return number;
if (Regex.IsMatch(script.Substring(match.Index + match.Value.Length).TrimStart(), @"^%")) return number;
if (Regex.IsMatch(number, @"^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$")) return number;
if (number.Contains(".")) return number;
if (number.Contains(",")) return number;
if (number.Length <= 2) return number;
return string.Join(" ", number.Select(NumberToWord));
private static string RemovePhoneMarkers(string script)
return script.Replace("<phone>", "").Replace("</phone>", "");
private static string NumberToWord(char digit)