首页 / C# / 如何使用C#获取与单词对应的发音音素?
如何使用C#获取与单词对应的发音音素?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何使用C#获取与单词对应的发音音素?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4768字,纯文字阅读大概需要7分钟。
内容图文
![如何使用C#获取与单词对应的发音音素?](/upload/InfoBanner/zyjiaocheng/768/e99d1418a0dd4b48bfb2eaeed1583f85.jpg)
我将在前言中说我在C#编程时非常新手.我正在开发一个应用程序,使用C#和SAPI v5.4(speechlib)以编程方式修改Windows语音字典.到目前为止,一切都运行良好但我需要更多地了解字符串在合成(浊音)时如何被解释.
我的理解是,在SAPI中,5.4单词被分解为phoneme representations,并且我在使用音素正确地“训练”单词发音方面取得了一些成功.我也知道我可以手动添加单词到Windows语音识别字典,提供录音,然后提取单词的发音(音素)……但这很麻烦.探讨如何在默认情况下合成单词也是有用的,即没有来自我的输入(比如合成器如何解释“海豚”?).
从编码的角度来看,这是我到目前为止所得到的:
using System;
using System.Speech.Synthesis;
namespace SpeechTest
{
class Program
{
static void Main(string[] args)
{
// Set up the speech synthesizer
SpeechSynthesizer synthesizer = new SpeechSynthesizer();
synthesizer.Volume = 100;
synthesizer.Rate = -2;
// Configure the audio output
synthesizer.SetOutputToDefaultAudioDevice();
// Initialize string to store word of interest (not in the speech dictionary)
string myWord = "dolphins";
// Speak the word of interest
synthesizer.Speak(myWord);
// Retrieve pronunciation of myWord
string myPronunciation = // *some code here*
Console.WriteLine("Press any key to exit...");
Console.ReadLine();
}
}
}
解决方法:
感谢Casey Chesnut的惊人工作,我已经找到了如何确定给定字符串的IPA电话.现在我只需弄清楚如何从IPA手机转换为SAPI符号,但这是针对一个单独的主题(有关如何从文本字符串中获取SAPI音素,请参阅here).
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Windows.Forms;
namespace SpeechTest
{
class Program
{
static void Main(string[] args)
{
string MyText = "dolphins"; // Initialze string for storing word (or words) of interest
string MyPronunciation = GetPronunciationFromText(MyText.Trim()); // Get IPA pronunciations of MyTe
MessageBox.Show(MyText + " = " + MyPronunciation); // Output MyText and MyPronunciation
}
public static string recoPhonemes;
public static string GetPronunciationFromText(string MyWord)
{
//this is a trick to figure out phonemes used by synthesis engine
//txt to wav
using (MemoryStream audioStream = new MemoryStream())
{
using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
synth.SetOutputToWaveStream(audioStream);
PromptBuilder pb = new PromptBuilder();
//pb.AppendBreak(PromptBreak.ExtraSmall); //'e' wont be recognized if this is large, or non-existent?
//synth.Speak(pb);
synth.Speak(MyWord);
//synth.Speak(pb);
synth.SetOutputToNull();
audioStream.Position = 0;
//now wav to txt (for reco phonemes)
recoPhonemes = String.Empty;
GrammarBuilder gb = new GrammarBuilder(MyWord);
Grammar g = new Grammar(gb); //TODO the hard letters to recognize are 'g' and 'e'
SpeechRecognitionEngine reco = new SpeechRecognitionEngine();
reco.SpeechHypothesized += new EventHandler<SpeechHypothesizedEventArgs>(reco_SpeechHypothesized);
reco.SpeechRecognitionRejected += new EventHandler<SpeechRecognitionRejectedEventArgs>(reco_SpeechRecognitionRejected);
reco.UnloadAllGrammars(); //only use the one word grammar
reco.LoadGrammar(g);
reco.SetInputToWaveStream(audioStream);
RecognitionResult rr = reco.Recognize();
reco.SetInputToNull();
if (rr != null)
{
recoPhonemes = StringFromWordArray(rr.Words, WordType.Pronunciation);
}
//txtRecoPho.Text = recoPhonemes;
return recoPhonemes;
}
}
}
public static string StringFromWordArray(ReadOnlyCollection<RecognizedWordUnit> words, WordType type)
{
string text = "";
foreach (RecognizedWordUnit word in words)
{
string wordText = "";
if (type == WordType.Text || type == WordType.Normalized)
{
wordText = word.Text;
}
else if (type == WordType.Lexical)
{
wordText = word.LexicalForm;
}
else if (type == WordType.Pronunciation)
{
wordText = word.Pronunciation;
//MessageBox.Show(word.LexicalForm);
}
else
{
throw new InvalidEnumArgumentException(String.Format("[0}: is not a valid input", type));
}
//Use display attribute
if ((word.DisplayAttributes & DisplayAttributes.OneTrailingSpace) != 0)
{
wordText += " ";
}
if ((word.DisplayAttributes & DisplayAttributes.TwoTrailingSpaces) != 0)
{
wordText += " ";
}
if ((word.DisplayAttributes & DisplayAttributes.ConsumeLeadingSpaces) != 0)
{
wordText = wordText.TrimStart();
}
if ((word.DisplayAttributes & DisplayAttributes.ZeroTrailingSpaces) != 0)
{
wordText = wordText.TrimEnd();
}
text += wordText;
}
return text;
}
public static void reco_SpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
{
recoPhonemes = StringFromWordArray(e.Result.Words, WordType.Pronunciation);
}
public static void reco_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
{
recoPhonemes = StringFromWordArray(e.Result.Words, WordType.Pronunciation);
}
}
public enum WordType
{
Text,
Normalized = Text,
Lexical,
Pronunciation
}
}
// Credit for method of retrieving IPA pronunciation from a string goes to Casey Chesnut (http://www.mperfect.net/speechSamples/)
内容总结
以上是互联网集市为您收集整理的如何使用C#获取与单词对应的发音音素?全部内容,希望文章能够帮你解决如何使用C#获取与单词对应的发音音素?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。