You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过SAPI 5.4的SpLexicon接口AddPronunciation函数添加词汇至Windows语音词典?

Complete Implementation for Adding Pronunciations via SAPI 5.4 SpLexicon

Got it, let's walk through finishing this SpLexicon implementation to add custom words and pronunciations to the Windows speech dictionary. You’ve got the initial setup right—here’s how to fill in the missing pieces and make it work end-to-end:

First, Add the Required COM Reference

Before writing code, you need to reference the SAPI library in your project:

  • In Visual Studio, right-click your project → Add → Reference
  • Go to the COM tab, find and select Microsoft Speech Object Library (this exposes the SpeechLib namespace containing SpLexicon)

Full Working Code Example

Here’s the complete code with all missing parts, including error handling and validation:

using System;
using System.Globalization;
using SpeechLib;

class SpeechDictionaryManager
{
    static void Main()
    {
        try
        {
            // Initialize SpLexicon instance
            SpLexicon lex = new SpLexicon();

            // Specify the word to add
            string myWord = "father";
            // Set language ID (American English)
            int langId = new CultureInfo("en-US").LCID;

            // 1. Set part of speech (use SpeechPartOfSpeech enum)
            SpeechPartOfSpeech partOfSpeech = SpeechPartOfSpeech.SPPS_Noun;

            // 2. Define the pronunciation (using SAPI's phonetic alphabet)
            // For American English, this matches the Microsoft Speech Platform phonetics
            string pronunciation = "F AA DH ER";

            // 3. Choose dictionary type: user-specific (no admin rights needed) vs system-wide (requires admin)
            SpeechLexiconType lexiconType = SpeechLexiconType.SPLEXICON_USER;

            // Add the pronunciation to the dictionary
            lex.AddPronunciation(myWord, langId, partOfSpeech, pronunciation, lexiconType);

            Console.WriteLine($"Successfully added pronunciation for '{myWord}'!");

            // Optional: Verify the addition by retrieving the pronunciation
            Console.WriteLine("\nVerifying the added entry:");
            ISpeechLexiconWord pronunciations = lex.GetPronunciations(myWord, langId);
            foreach (ISpeechLexiconPronunciation pron in pronunciations)
            {
                Console.WriteLine($"Pronunciation: {pron.Pronunciation}, Part of Speech: {pron.PartOfSpeech}");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error adding pronunciation: {ex.Message}");
            // If you get an access denied error, try running the app as administrator (for system dictionary)
        }
    }
}

Key Details to Note

  • Part of Speech: Use the SpeechPartOfSpeech enum (e.g., SPPS_Noun, SPPS_Verb, SPPS_Adjective). Pick the value that best matches your word’s usage.
  • Pronunciation Format: SAPI uses its own phonetic alphabet for American English. You can reference valid phonemes by checking existing word pronunciations (via GetPronunciations) or using Microsoft’s official phoneme chart for speech platforms.
  • Dictionary Type:
    • SPLEXICON_USER: Adds the word to the current user’s personal dictionary (no admin privileges required).
    • SPLEXICON_SYSTEM: Adds it to the system-wide dictionary (requires running your application as Administrator).
  • Error Handling: Always wrap SAPI calls in a try-catch block—common issues include access denied (for system dictionary) or invalid phoneme strings.

Important Notes

  • Changes to the speech dictionary may require restarting any speech recognition/synthesis applications to take effect.
  • SAPI 5.4 is included with most modern Windows releases by default, but if you’re targeting older systems, ensure the runtime is installed.

内容的提问来源于stack exchange,提问作者Exergist

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:57:48