/// <summary> /// Parse phone map data for this instance. /// </summary> /// <param name="phoneMap">Phone map.</param> /// <returns>Update target data.</returns> private ICollection ParseData(PhoneMap phoneMap) { if (phoneMap.Language != Language) { string message = Helper.NeutralFormat("The language [{0}] of phone map " + "does not match with that [{1}] of phoneme.", Localor.LanguageToString(phoneMap.Language), Localor.LanguageToString(Language)); } ICollection ret = null; if (StringComparer.Ordinal.Compare(phoneMap.Source, PhoneMap.TtsPhone) == 0) { if (phoneMap.Target == PhoneMap.SapiVisemeId) { _sapiVisemeIds = new Dictionary<string, int>(); foreach (string phone in phoneMap.Items.Keys) { int id = 0; if (!int.TryParse(phoneMap.Items[phone], out id)) { string message = Helper.NeutralFormat("Invalid SAPI id [{0}] for phone [{1}] found.", phoneMap.Items[phone], phone); throw new InvalidDataException(message); } _sapiVisemeIds.Add(phone, id); } ret = _sapiVisemeIds; } if (phoneMap.Target == PhoneMap.SrPhone) { _tts2SrMap = new Collection<string>(); _tts2srMapType = TtsToSrMappingType.PhoneBased; foreach (string phone in phoneMap.Items.Keys) { _tts2SrMap.Add(phone); _tts2SrMap.Add(phoneMap.Items[phone]); } ret = _tts2SrMap; } if (phoneMap.Target == PhoneMap.IpaPhone) { _ipaPhones = new Dictionary<string, string>(); foreach (string phone in phoneMap.Items.Keys) { _ipaPhones.Add(phone, phoneMap.Items[phone]); } ret = _ipaPhones; } } if (StringComparer.Ordinal.Compare(phoneMap.Source, PhoneMap.TtsSyllable) == 0) { if (phoneMap.Target == PhoneMap.SrPhone) { _tts2SrMap = new Collection<string>(); _tts2srMapType = TtsToSrMappingType.SyllableBased; foreach (string phone in phoneMap.Items.Keys) { _tts2SrMap.Add(phone); _tts2SrMap.Add(phoneMap.Items[phone]); } ret = _tts2SrMap; } } return ret; }
/// <summary> /// Generates the fake phone for the real phone number. /// </summary> /// <param name="originalPhone">The original phone number.</param> /// <returns></returns> protected string GenerateFakePhoneNumberForPhone(string originalPhone) { var originalPhoneDigits = new string( originalPhone.Where(c => char.IsDigit(c)).ToArray()); var newPhoneDigits = PhoneMap.GetOrAdd(originalPhoneDigits, (key) => { if (originalPhoneDigits.Length == 7 || originalPhoneDigits.Length == 10 || originalPhoneDigits.Length == 11) { string lineNumber = DataFaker.Random.Replace("####"); string number = string.Empty; if (originalPhoneDigits.Length == 11) { number = "1"; } // // Generate area code. // if (originalPhoneDigits.Length >= 10) { var areaCode = new[] { Convert.ToChar('0' + DataFaker.Random.Number(2, 9)), Convert.ToChar('0' + DataFaker.Random.Number(0, 9)), Convert.ToChar('0' + DataFaker.Random.Number(0, 9)) }; number = number + new string( areaCode ); } // // Generate exchange code. // var exchangeCode = new[] { Convert.ToChar('0' + DataFaker.Random.Number(2, 9)), Convert.ToChar('0' + DataFaker.Random.Number(0, 9)), Convert.ToChar('0' + DataFaker.Random.Number(0, 9)) }; number = number + new string( exchangeCode ); number = number + DataFaker.Random.Replace("####"); return(number); } else { string format = string.Join("", Enumerable.Repeat("#", originalPhoneDigits.Length)); return(DataFaker.Random.Replace(format)); } }); var newPhone = originalPhone.Select(c => c).ToArray(); int digits = 0; for (int i = 0; i < newPhone.Length; i++) { if (char.IsDigit(newPhone[i])) { newPhone[i] = newPhoneDigits[digits++]; } } return(new string( newPhone )); }
private void EnsureMapLoaded(ICollection map, string fileName) { if (map == null || map.Count == 0) { using (StreamReader reader = Localor.LoadResource(Language, fileName)) { if (reader != null) { PhoneMap phoneMap = new PhoneMap(); phoneMap.Load(reader); phoneMap.Validate(); map = ParseData(phoneMap); } } } if (map == null || map.Count == 0) { Localor.ReportMissingStockedLanguageData(Localor.LanguageToString(Language), fileName); } }
/// <summary> /// Map pronunciation using phonemap. /// Only support map syllable based pronunciation. /// Todo: Support phone based, unit based pronunciation. /// </summary> /// <param name="pronunciation">Pronunciation to be mapped.</param> /// <param name="phoneMap">Phone map used to map pronunciation.</param> /// <returns>Mapped pronunciation.</returns> public static string MapPronunciation(string pronunciation, PhoneMap phoneMap) { if (string.IsNullOrEmpty(pronunciation)) { throw new ArgumentNullException("pronunciation"); } string[] syllables = Pronunciation.SplitIntoSyllables(pronunciation); StringBuilder mappedPronunciation = new StringBuilder(); int lastSyllableEnd = 0; int currentSyllableStart = 0; foreach (string syllable in syllables) { currentSyllableStart = pronunciation.IndexOf(syllable, StringComparison.Ordinal); Debug.Assert(currentSyllableStart >= 0); mappedPronunciation.Append(pronunciation.Substring(lastSyllableEnd, currentSyllableStart - lastSyllableEnd)); if (phoneMap.Items.ContainsKey(syllable)) { mappedPronunciation.Append(phoneMap.Items[syllable]); } else { mappedPronunciation.Length = 0; break; } lastSyllableEnd = currentSyllableStart + syllable.Length; } return mappedPronunciation.ToString(); }