Export (0) Print
Expand All

SpeechSynthesizer.AddLexicon Method

Adds a lexicon to the SpeechSynthesizer object.

Namespace:  System.Speech.Synthesis
Assembly:  System.Speech (in System.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 use a lexicon to specify custom pronunciations for specialized vocabulary in your application.

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, AppendSsmlMarkup, or AppendSsml methods take precedence over pronunciations specified in any lexicon. Inline pronunciations apply only to a single occurrence of a word. See Lexicons and Phonetic Alphabets for more information.

You can add multiple lexicons to a SpeechSynthesizer object.Two values are currently supported for the mediaType parameter:

  • 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 a Microsoft-proprietary format. This is a legacy format and we recommend that you use the PLS format described above.

The following example demonstrates the effect of adding and removing a lexicon that contains a custom pronunciation for the word "blue". The lexicon defines the pronunciation of "blue" to sound like "bleep". While the lexicon is loaded, the speech synthesizer uses the pronunciation defined in the lexicon.

using System;
using System.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.SetOutputToDefaultAudioDevice();

        // Speak the prompt.
        synth.Speak("My favorite color is blue.");
        
        // Add a lexicon that changes the pronunciation of "blue".
        synth.AddLexicon(new Uri("C:\\test\\Blue.pls"), "application/pls+xml");

        // Speak the prompt.
        synth.Speak("My favorite color is blue.");

        // Remove the lexicon.
        synth.RemoveLexicon(new Uri("C:\\test\\Blue.pls"));

        // Speak the prompt.
        synth.Speak("My favorite color is blue.");
      }

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

Following are the contents of the lexicon file Blue.pls:

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

<lexicon version="1.0" 
      xmlns="http://www.w3.org/2005/01/pronunciation-lexicon"
      alphabet="x-microsoft-ups" xml:lang="en-US">


  <lexeme>
    <grapheme> blue </grapheme>
    <phoneme> B L I P </phoneme>
  </lexeme>

</lexicon>

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft