SpeechSynthesizer.AddLexicon Method

Adds a lexicon to the SpeechSynthesizer object.

Namespace:  Microsoft.Speech.Synthesis
Assembly:  Microsoft.Speech (in Microsoft.Speech.dll)

public void AddLexicon(
	Uri uri,
	string mediaType
)

Parameters

uri
Type: System.Uri
The location of the lexicon information.
mediaType
Type: System.String
The media type of the lexicon. Media type values are not case sensitive.

A pronunciation lexicon is a collection of words or phrases together with their pronunciations, which consist of letters and characters from a supported phonetic alphabet. You can add multiple lexicons to a SpeechSynthesizer.

Two values are currently supported for mediaType:

  • The value application/pls+xml indicates that the lexicon conforms to the Pronunciation Lexicon Specification (PLS) Version 1.0. This is the preferred format to use.

  • The value application/vdn.ms-sapi-lex indicates that the lexicon format is Uncompressed Lexicon, which is specific to the Microsoft Speech API. This is a legacy format and we recommend that you use the PLS format described above.

Pronunciations specified in an external lexicon file take precedence over the pronunciations of the speech synthesizer's internal lexicon or dictionary. However, pronunciations specified inline in prompts created with any of the AppendTextWithPronunciation(String, String), AppendSsmlMarkup(String), or AppendSsml(XmlReader) methods take precedence over pronunciations specified in any lexicon. Inline pronunciations apply only to a single occurrence of a word. For more information, see Lexicons and Phonetic Alphabets (Microsoft.Speech).

The following example specifies a lexicon that defines a pronunciation for an uncommon word: whatchamacallit. Typically, you would use AppendTextWithPronunciation(String, String) to specify a pronunciation for a single occurrence of an uncommon word. However, this example demonstrates how to attach a lexicon to a SpeechSynthesizer instance and how the pronunciation defined in a lexicon may improve the SpeechSynthesizer's pronunciation of an uncommon word. A custom lexicon overrides the pronunciations in the SpeechSynthesizer's internal lexicon and applies to all occurrences of the words it defines.

using System;
using Microsoft.Speech.Synthesis;

namespace SampleSynthesis
{
  class Program
  {
    static void Main(string[] args)
    {

      // Initialize a new instance of the SpeechSynthesizer.
      using (SpeechSynthesizer synth = new SpeechSynthesizer())
      {

        // Configure the audio output. 
        synth.SetOutputToWaveFile(@"C:\test\LexiconTest.wav");

        // Create a SoundPlayer instance to play the output audio file.
        System.Media.SoundPlayer m_SoundPlayer =
          new System.Media.SoundPlayer(@"C:\test\LexiconTest.wav");

        // Create a prompt.
        PromptBuilder builder = new PromptBuilder();
        builder.AppendText("Gimme the whatchamacallit.");

        // Append the lexicon file.
        synth.AddLexicon(new Uri("c:\\test\\whatchamacallit.pls"), "application/pls+xml");

        // Speak the prompt and play back the output file.
        synth.Speak(builder);
        m_SoundPlayer.Play();

        // Remove the lexicon file.
        synth.RemoveLexicon(new Uri ("c:\\test\\whatchamacallit.pls"));

        // Speak the prompt and play back the output file.
        synth.Speak(builder);
        m_SoundPlayer.Play();
      }

      Console.WriteLine();
      Console.WriteLine("Press any key to exit...");
      Console.ReadKey();
    }
  }
}

The following are the contents for the lexicon file whatchamacallit.pls:

<?xml version="1.0" encoding="UTF-8"?>

<lexicon version="1.0" 
      xmlns="http://www.w3.org/2005/01/pronunciation-lexicon"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://www.w3.org/2005/01/pronunciation-lexicon 
        http://www.w3.org/TR/2007/CR-pronunciation-lexicon-20071212/pls.xsd"
      alphabet="x-microsoft-ups" xml:lang="en-US">


  <lexeme>
    <grapheme> whatchamacallit </grapheme>
    <phoneme> W S1 AX T CH AX M AX K S2 AA L IH T </phoneme>
  </lexeme>

</lexicon>
Show: