Search code examples
delphispeech-recognitionsapivoice-recognition

SAPI Speech recognition delphi


I to need create a programmatic equivalent using delphi language... or could someone post a link on how to do grammars in peech recogniton using the delphi. Or any examples of XML grammar that has programmatic equivalent in Delphi. sorry for my english.

**Programmatic Equivalent ** 

Ref: http://msdn.microsoft.com/en-us/library/ms723634(v=VS.85).aspx

        SPSTATEHANDLE hsHelloWorld;
        hr = cpRecoGrammar->GetRule(L"HelloWorld", NULL,
                        SPRAF_TopLevel | SPRAF_Active, TRUE,
                        &hsHelloWorld);
        hr = cpRecoGrammar->AddWordTransition(hsHelloWorld, NULL,
                L"hello world", L" ",
                SPWT_LEXICAL, NULL, NULL);
        hr = cpRecoGrammar->AddWordTransition(hsHelloWorld, NULL,
                L"hiya|there", L"|",
                SPWT_LEXICAL, NULL, NULL);
        hr = cpRecoGrammar->Commit(NULL);

XML Grammar Sample(s):

    <GRAMMAR>
        <!-- Create a simple "hello world" rule -->
        <RULE NAME="HelloWorld" TOPLEVEL="ACTIVE">
            <P>hello world</P>
        </RULE>
        <RULE NAME="HelloWorld_Disp" TOPLEVEL="ACTIVE">
            <P DISP="Hiya there!">hello world</P>
        </RULE>
        <RULE NAME="Question_Pron" TOPLEVEL="ACTIVE">
            <P DISP="I don't understand" PRON="eh">what</P>
        </RULE>
        <RULE NAME="NurseryRhyme" TOPLEVEL="ACTIVE">
            <P>hey</P>
            <P MIN="2" MAX="2">diddle</P>
        </RULE>
        <RULE NAME="UseWeights" TOPLEVEL="ACTIVE">
            <LIST>
                <P WEIGHT=".95">recognize speech</P>
                <P WEIGHT=".05">wreck a nice beach</P>
            </LIST>
        </RULE>
        <RULE NAME="UseProps" TOPLEVEL="ACTIVE">
            <P PROPNAME="NOVALUE">one</P>
            <P PROPNAME="NUMBER" VAL="2">two</P>
            <P PROPNAME="STRING" VALSTR="three">three</P>
        </RULE>
    </GRAMMAR>

Solution

  • Guy's I finally able to get the Answer ....
    This might be useful to others... :)

    this is the actual component that i created. just modify it for your needs.

    Function TSRRule.AddWord (Word : String; Value : string = ''; Separator : char = '|') : integer;
    var
      OleValue : OleVariant;
    begin
      result := 0;
      if Fwordlist.IndexOf(Word) = -1 then
         begin
           OleValue := Value;
           Fwordlist.Add(Word);
           FRule.InitialState.AddWordTransition(nil,  word, Separator, SPWT_LEXICAL, FRuleName+'_value',Fwordlist.Count, OleValue, 1.0);
           FWordCount := Fwordlist.Count;
           result := FWordCount;
         end;
    end;
    

    Calling function...

    FSpRunTimeGrammar := SpInProcRecoContext.CreateGrammar(2); // we assign  another grammr on index 2
    
       SrRule1 := TSRRule.Create(1,'Rule1',FSpRunTimeGrammar);
       with SrRule1 do
          begin
             AddWord('Maxtor');
             AddWord('Open NotePad','Notepad.exe');
             AddWord('Maxtor Dexter TrandPack','',' ');
             commit;
          end;
       SrRule2 := TSRRule.Create(2,'Rule2',FSpRunTimeGrammar);
       with SrRule1 do
          begin
             AddWord('the box');
             AddWord('WeLcOmE SaPi');
             AddWord('Halo World');
             commit;
          end;
       FSpRunTimeGrammar.CmdSetRuleState('Rule1',SGDSActive);
       FSpRunTimeGrammar.CmdSetRuleState('Rule2',SGDSActive);
    

    Please Leave comment for clarifications.... good luck!