如何使用ISpLexicon :: GetWords获取单词列表?

问题描述 投票:1回答:1

我正在使用Microsoft SAPI开发一个Text-To-Speech应用程序。我发现可以在一个字典中添加自定义的单词代词(如果我错了,请纠正我)。我实现了一个允许在这个词典中添加单词的函数。这是我的代码:

int addPrononciation( const char* addPron, const char* phon )
{
   hr = cpLexicon.CoCreateInstance( CLSID_SpLexicon );
   hr = cpContainerLexicon.CoCreateInstance( CLSID_SpLexicon );

   hr = SpEnumTokens( SPCAT_VOICES, NULL, NULL, &cpEnum );
   cpEnum->Item( saveVoice, &cpVoiceToken ); //get saveVoice token defined at line 136
   cpVoice->SetVoice( cpVoiceToken ); //Initialization of the voice

   hr = cpContainerLexicon->AddLexicon( cpLexicon, eLEXTYPE_APP );
   langId = MAKELANGID( LANG_ENGLISH, SUBLANG_ENGLISH_US );
   hr = SpCreatePhoneConverter( langId, NULL, NULL, &cpPhoneConv );

   int wchars_num = MultiByteToWideChar( CP_ACP, 0, addPron, -1, NULL, 0 );
   wchar_t* pronWstr = new wchar_t[ wchars_num ];
   MultiByteToWideChar( CP_ACP, 0, addPron, -1, pronWstr, wchars_num );

   int phonWchars_num = MultiByteToWideChar( CP_ACP, 0, phon, -1, NULL, 0 );
   wchar_t* phonWstr = new wchar_t[ phonWchars_num ];
   MultiByteToWideChar( CP_ACP, 0, phon, -1, phonWstr, phonWchars_num );

   if(SUCCEEDED( hr ))
   {
      hr = cpPhoneConv->PhoneToId( phonWstr, wszId );
      hr = cpVoice->Speak( phonWstr, SPF_DEFAULT, NULL );
      hr = cpLexicon->AddPronunciation( pronWstr, langId, SPPS_Noun, wszId );
      hr = cpVoice->Speak( pronWstr, SPF_DEFAULT, NULL );
      if( SUCCEEDED( hr ) )
      {
         printf( "Success\n" );
      }
      else
      {
         printf( "Failed\n" );
      }
   }

   cpEnum.Release();
   cpVoiceToken.Release();
   cpContainerLexicon.Release();
   cpLexicon.Release();
   cpPhoneConv.Release();
   delete new wchar_t[ wchars_num ];
   delete new wchar_t[ phonWchars_num ];

   return true;
}

现在我想使用ISpLexicon :: GetWords列出这些单词。

我已经在微软网站上阅读了documentation并尝试实现该功能,但我无法弄清楚如何初始化变量spWordList

这是我的代码:

 ZeroMemory( &spWordList, sizeof( spWordList ) );
 if( SUCCEEDED( hr ) )
 {
     hr = cpLexicon->GetWords( eLEXTYPE_APP, &dwGeneration, &dwCookie, &spWordList );
     printf( "Words: %ls\n", spWordList ); //print words but the output is null
 }
 CoTaskMemFree( spWordList.pvBuffer );

我正在尝试打印单词,但输出为空。我认为spWordList变量没有初始化。以下是变量值的屏幕截图。

Variable values

我该如何初始化它?

c++ text-to-speech sapi speech-synthesis
1个回答
0
投票

我发现了如何初始化spWordList。你必须用eLEXTYPE_APP替换eLEXTYPE_USER。但是,你可以像我一样保留它们。您将在下面找到有关如何列出单词的示例。

ZeroMemory( &spWordList, sizeof( spWordList ) );
hr = S_FALSE;
if( hr == S_FALSE )
{
  hr = cpLexicon->GetWords( eLEXTYPE_USER | eLEXTYPE_APP, &dwGeneration, &dwCookie, &spWordList );
  for( spWord = spWordList.pFirstWord; spWord != NULL; spWord = spWord->pNextWord )
  {
     for( spWordPron = spWord->pFirstWordPronunciation; spWordPron != NULL; spWordPron = spWordPron->pNextWordPronunciation )
     {
        printf( "Words in dictionnary: %i\n", dwGeneration );
        printf( "Word: %ls\n", spWord->pszWord );
        //you can also display the pronunciation of words if you wish
     }
  }
}
CoTaskMemFree( spWordList.pvBuffer );

在代码中,我循环遍历整个字典。请注意,列出的单词是随机显示的。如果我找到有关ISpLexicon :: GetWords的其他重要信息,我会更新我的答案

© www.soinside.com 2019 - 2024. All rights reserved.