Announcement

Collapse
No announcement yet.

VB to Delphi Translation

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • VB to Delphi Translation

    I have this code in Visual Basic that I need to get working in Delphi. It is used to get SAPI to recognize words without having an XML file. I can't figure out why I can't get at least parts of the code to work in Delphi

    http://vbfrance.com/code.aspx?ID=16631

    My Current Code:
    Code:
    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, OleServer, SpeechLib_TLB;
    
    type
      TForm1 = class(TForm)
        SpSharedRecoContext: TSpSharedRecoContext;
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
      Grammar: ISpeechRecoGrammar;
      PropertiesRule: ISpeechGrammarRule;
      ItemRule: ISpeechGrammarRule;
      TopLevelRule: ISpeechGrammarRule;
    
      //WithEvents RecoContext As SpSharedRecoContext;
    
    Const
      m_GrammarId = 10;
    
    implementation
    
    {$R *.dfm}
    
    // ===================================
    // Emulate VB function IS NOTHING
    // ===================================
    
    function IsNothing(Obj : OleVariant) : boolean;
    begin
      Result := IDispatch(Obj) = nil;
    end;
    
    // ============================================
    // Emulate VB function VarX := Nothing
    // ============================================
    
    function varNothing : IDispatch;
    var Retvar : IDispatch;
    begin
      Retvar := nil;
      Result := Retvar;
    end;
    
    
    procedure TForm1.FormCreate(Sender: TObject);
    var
      AfterCmdState: ISpeechGrammarRuleState;
      temp : OleVariant;
    begin
      SpSharedRecoContext.EventInterests := SREAllEvents;
      Grammar := SpSharedRecoContext.CreateGrammar(m_GrammarId);
    
      TopLevelRule := Grammar.Rules.Add('TopLevelRule', SRATopLevel Or SRADynamic, 1);
      PropertiesRule := Grammar.Rules.Add('PropertiesRule', SRADynamic, 2);
      ItemRule := Grammar.Rules.Add('ItemRule', SRADynamic, 3);
    
      AfterCmdState := TopLevelRule.AddState;
    
      TopLevelRule.InitialState.AddWordTransition(AfterCmdState, 'test', temp, temp, '****', 0, temp, temp);
    
      Grammar.Rules.Commit;
      Grammar.CmdSetRuleState('TopLevelRule', SGDSActive);
    end;
    
    end.
    Thanks in advance
    PowerVoice v1 | NaviVoice Source
    GammaControl v2.4
    SKINbedder v3

    1995 Lexus SC300

  • #2
    You've made a number of mistakes in your parameters in the function call that raises the exception. Have a look at:

    http://www.mail-archive.com/delphi-e.../msg01264.html

    I took your code and adapted it with the parameters used in here, and it seems to execute (I haven't gone any further than checking that no exception is raised). I think the constant name of SPWT_LEXICAL used in the example may not be correct, but it maps to a valid value so it still works. The correct range of constants can be found in the type library file just below the SpeechGrammarWordType type statement (there are three constants).

    Good luck!

    Ric.
    EZSync - http://www.richmet.com/ezsync

    Comment


    • #3
      I haven't read the article, but this may be of help also:

      http://bdn.borland.com/article/0,1410,29583,00.html
      EZSync - http://www.richmet.com/ezsync

      Comment


      • #4
        Originally posted by ric355
        You've made a number of mistakes in your parameters in the function call that raises the exception. Have a look at:

        http://www.mail-archive.com/delphi-e.../msg01264.html

        I took your code and adapted it with the parameters used in here, and it seems to execute (I haven't gone any further than checking that no exception is raised). I think the constant name of SPWT_LEXICAL used in the example may not be correct, but it maps to a valid value so it still works. The correct range of constants can be found in the type library file just below the SpeechGrammarWordType type statement (there are three constants).

        Good luck!

        Ric.
        Perfect!
        Originally posted by ric355
        I haven't read the article, but this may be of help also:

        http://bdn.borland.com/article/0,1410,29583,00.html
        Guess the base of NaviVoice... look at the first screenshot of NV... its identical to that app
        PowerVoice v1 | NaviVoice Source
        GammaControl v2.4
        SKINbedder v3

        1995 Lexus SC300

        Comment


        • #5
          Code:
          unit Unit1;
          
          interface
          
          uses
            Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
            Dialogs, SpeechLib_TLB, OleServer, StdCtrls;
          
          type
            TForm1 = class(TForm)
              SpSharedRecoContext1: TSpSharedRecoContext;
              Button1: TButton;
              procedure SpSharedRecoContext1Hypothesis(ASender: TObject;
                StreamNumber: Integer; StreamPosition: OleVariant;
                const Result: ISpeechRecoResult);
              procedure Button1Click(Sender: TObject);
            private
              { Private declarations }
            public
              { Public declarations }
              procedure CreateGrammar;
              procedure AddWord(word: String);
            end;
          
          var
            Form1: TForm1;
            SRGrammar: ISpeechRecoGrammar;
            Rule: ISpeechGrammarRule;
            WordNumber: Integer;
          
            // Create this object and fill it with the words
            // or phrases you want to recognise before calling CreateGrammar
            Phrases: TStringList;
          
          implementation
          
          {$R *.dfm}
          
          procedure TForm1.SpSharedRecoContext1Hypothesis(ASender: TObject;
            StreamNumber: Integer; StreamPosition: OleVariant;
            const Result: ISpeechRecoResult);
          var text: String; confidence: Integer;
          begin
          text:=Result.PhraseInfo.GetText(0,-1,True);
          Confidence:=Integer(Result.PhraseInfo.Rule.Confidence);
          showmessage(text);
          end;
          
          procedure TForm1.AddWord(word: String);
          Var Value: OleVariant;
          begin
            Inc(WordNumber);
            Value:=WordNumber;
            Rule.InitialState.AddWordTransition(Nil, word, ' ', SGLexical, 'value',1,value,1.0);
          end;
          
          procedure TForm1.CreateGrammar;
          var
          i : integer;
          begin
            //OnAudioLevel event is not fired by default - this changes that
            SpSharedRecoContext1.EventInterests := SREAllEvents;
            SRGrammar := SpSharedRecoContext1.CreateGrammar(0);
            Rule:=SRGrammar.Rules.Add('word',SRATopLevel+SRADefaultToActive,0);
            WordNumber:=0;
            for i := 0 to Phrases.Count-1 do begin
              AddWord(Phrases[i]);
            end;
            Rule:=Nil;
            SRGrammar.Rules.Commit;
            SRGrammar.CmdSetRuleIdState(0, SGDSActive);
          end;
          
          procedure TForm1.Button1Click(Sender: TObject);
          begin
          addword('test');
          end;
          
          end.
          It raises an exception when I click on the button
          PowerVoice v1 | NaviVoice Source
          GammaControl v2.4
          SKINbedder v3

          1995 Lexus SC300

          Comment


          • #6
            Originally posted by 0l33l
            Code:
            unit Unit1;
            
            interface
            
            uses
              Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
              Dialogs, SpeechLib_TLB, OleServer, StdCtrls;
            
            type
              TForm1 = class(TForm)
                SpSharedRecoContext1: TSpSharedRecoContext;
                Button1: TButton;
                procedure SpSharedRecoContext1Hypothesis(ASender: TObject;
                  StreamNumber: Integer; StreamPosition: OleVariant;
                  const Result: ISpeechRecoResult);
                procedure Button1Click(Sender: TObject);
              private
                { Private declarations }
              public
                { Public declarations }
                procedure CreateGrammar;
                procedure AddWord(word: String);
              end;
            
            var
              Form1: TForm1;
              SRGrammar: ISpeechRecoGrammar;
              Rule: ISpeechGrammarRule;
              WordNumber: Integer;
            
              // Create this object and fill it with the words
              // or phrases you want to recognise before calling CreateGrammar
              Phrases: TStringList;
            
            implementation
            
            {$R *.dfm}
            
            procedure TForm1.SpSharedRecoContext1Hypothesis(ASender: TObject;
              StreamNumber: Integer; StreamPosition: OleVariant;
              const Result: ISpeechRecoResult);
            var text: String; confidence: Integer;
            begin
            text:=Result.PhraseInfo.GetText(0,-1,True);
            Confidence:=Integer(Result.PhraseInfo.Rule.Confidence);
            showmessage(text);
            end;
            
            procedure TForm1.AddWord(word: String);
            Var Value: OleVariant;
            begin
              Inc(WordNumber);
              Value:=WordNumber;
              Rule.InitialState.AddWordTransition(Nil, word, ' ', SGLexical, 'value',1,value,1.0);
            end;
            
            procedure TForm1.CreateGrammar;
            var
            i : integer;
            begin
              //OnAudioLevel event is not fired by default - this changes that
              SpSharedRecoContext1.EventInterests := SREAllEvents;
              SRGrammar := SpSharedRecoContext1.CreateGrammar(0);
              Rule:=SRGrammar.Rules.Add('word',SRATopLevel+SRADefaultToActive,0);
              WordNumber:=0;
              for i := 0 to Phrases.Count-1 do begin
                AddWord(Phrases[i]);
              end;
              Rule:=Nil;
              SRGrammar.Rules.Commit;
              SRGrammar.CmdSetRuleIdState(0, SGDSActive);
            end;
            
            procedure TForm1.Button1Click(Sender: TObject);
            begin
            addword('test');
            end;
            
            end.
            It raises an exception when I click on the button
            Don't you need to call 'CreateGrammar' ?

            Note I fiddled with this a bit more last night after posting, and the call to CmdSetRuleIdState was failing.
            EZSync - http://www.richmet.com/ezsync

            Comment


            • #7
              Code:
              unit Unit1;
              
              interface
              
              uses
                Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
                Dialogs, SpeechLib_TLB, OleServer, StdCtrls;
              
              type
                TForm1 = class(TForm)
                  SpSharedRecoContext1: TSpSharedRecoContext;
                  Button1: TButton;
                  procedure SpSharedRecoContext1Hypothesis(ASender: TObject;
                    StreamNumber: Integer; StreamPosition: OleVariant;
                    const Result: ISpeechRecoResult);
                  procedure Button1Click(Sender: TObject);
                private
                  { Private declarations }
                public
                  { Public declarations }
                  procedure CreateGrammar;
                  procedure AddWord(word: String);
                end;
              
              var
                Form1: TForm1;
                SRGrammar: ISpeechRecoGrammar;
                Rule: ISpeechGrammarRule;
                WordNumber: Integer;
              
                // Create this object and fill it with the words
                // or phrases you want to recognise before calling CreateGrammar
                Phrases: TStringList;
              
              implementation
              
              {$R *.dfm}
              
              procedure TForm1.SpSharedRecoContext1Hypothesis(ASender: TObject;
                StreamNumber: Integer; StreamPosition: OleVariant;
                const Result: ISpeechRecoResult);
              var text: String; confidence: Integer;
              begin
              text:=Result.PhraseInfo.GetText(0,-1,True);
              Confidence:=Integer(Result.PhraseInfo.Rule.Confidence);
              showmessage(text);
              end;
              
              procedure TForm1.AddWord(word: String);
              Var Value: OleVariant;
              begin
                Inc(WordNumber);
                Value:=WordNumber;
                Rule.InitialState.AddWordTransition(Nil, word, ' ', SPWT_LEXICAL, 'value',1,value,1.0);
              end;
              
              procedure TForm1.CreateGrammar;
              var
              i : integer;
              begin
                //OnAudioLevel event is not fired by default - this changes that
                SpSharedRecoContext1.EventInterests := SREAllEvents;
                SRGrammar := SpSharedRecoContext1.CreateGrammar(0);
                Rule:=SRGrammar.Rules.Add('word',SRATopLevel+SRADefaultToActive,0);
                WordNumber:=0;
                for i := 0 to Phrases.Count-1 do begin
                  AddWord(Phrases[i]);
                end;
                Rule:=Nil;
                SRGrammar.Rules.Commit;
                SRGrammar.CmdSetRuleIdState(0, SGDSActive);
              end;
              
              procedure TForm1.Button1Click(Sender: TObject);
              begin
              addword('test');
              CreateGrammar;
              end;
              
              end.
              It fails when I press the button.
              Can you just post your source?
              PowerVoice v1 | NaviVoice Source
              GammaControl v2.4
              SKINbedder v3

              1995 Lexus SC300

              Comment


              • #8
                Originally posted by 0l33l
                It fails when I press the button.
                Can you just post your source?

                I have no source to post. I just fiddled around for a while then threw it away. What I was saying is it is bound to fail when you press the button because all you're doing in the button's event handler is calling 'addword'. You need to have called 'CreateGrammar' first. You previously had the code in the FormCreate but I don't see a FormCreate any more.
                EZSync - http://www.richmet.com/ezsync

                Comment


                • #9
                  Originally posted by ric355
                  I have no source to post. I just fiddled around for a while then threw it away. What I was saying is it is bound to fail when you press the button because all you're doing in the button's event handler is calling 'addword'. You need to have called 'CreateGrammar' first. You previously had the code in the FormCreate but I don't see a FormCreate any more.
                  In addition to this, the code you've posted doesn't instantiate the 'Phrases' object, so you'll get an access violation inside CreateGrammar.
                  EZSync - http://www.richmet.com/ezsync

                  Comment


                  • #10
                    Code:
                    unit Unit1;
                    
                    interface
                    
                    uses
                      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
                      Dialogs, SpeechLib_TLB, OleServer, StdCtrls;
                    
                    type
                      TForm1 = class(TForm)
                        SpSharedRecoContext1: TSpSharedRecoContext;
                        Button1: TButton;
                        procedure SpSharedRecoContext1Hypothesis(ASender: TObject;
                          StreamNumber: Integer; StreamPosition: OleVariant;
                          const Result: ISpeechRecoResult);
                        procedure Button1Click(Sender: TObject);
                        procedure FormCreate(Sender: TObject);
                      private
                        { Private declarations }
                      public
                        { Public declarations }
                        procedure CreateGrammar;
                        procedure AddWord(word: String);
                      end;
                    
                    var
                      Form1: TForm1;
                      SRGrammar: ISpeechRecoGrammar;
                      Rule: ISpeechGrammarRule;
                      WordNumber: Integer;
                    
                      // Create this object and fill it with the words
                      // or phrases you want to recognise before calling CreateGrammar
                      Phrases: TStringList;
                    
                    implementation
                    
                    {$R *.dfm}
                    
                    procedure TForm1.SpSharedRecoContext1Hypothesis(ASender: TObject;
                      StreamNumber: Integer; StreamPosition: OleVariant;
                      const Result: ISpeechRecoResult);
                    var text: String; confidence: Integer;
                    begin
                    text:=Result.PhraseInfo.GetText(0,-1,True);
                    Confidence:=Integer(Result.PhraseInfo.Rule.Confidence);
                    showmessage(text);
                    end;
                    
                    procedure TForm1.AddWord(word: String);
                    Var Value: OleVariant;
                    begin
                      Inc(WordNumber);
                      Value:=WordNumber;
                      Rule.InitialState.AddWordTransition(Nil, word, ' ', SPWT_LEXICAL, 'value',1,value,1.0);
                    end;
                    
                    procedure TForm1.CreateGrammar;
                    var
                    i : integer;
                    begin
                      //OnAudioLevel event is not fired by default - this changes that
                      SpSharedRecoContext1.EventInterests := SREAllEvents;
                      SRGrammar := SpSharedRecoContext1.CreateGrammar(0);
                      Rule:=SRGrammar.Rules.Add('word',SRATopLevel+SRADefaultToActive,0);
                      WordNumber:=0;
                      for i := 0 to Phrases.Count-1 do begin
                        AddWord(Phrases[i]);
                      end;
                      Rule:=Nil;
                      SRGrammar.Rules.Commit;
                      SRGrammar.CmdSetRuleIdState(0, SGDSActive);
                    end;
                    
                    procedure TForm1.Button1Click(Sender: TObject);
                    begin
                    addword('test');
                    end;
                    
                    procedure TForm1.FormCreate(Sender: TObject);
                    begin
                    Phrases := TStringList.Create;
                    CreateGrammar;
                    end;
                    
                    end.
                    Same thing... and I create the Phrases and call creategrammar. It fails when I press the button.
                    PowerVoice v1 | NaviVoice Source
                    GammaControl v2.4
                    SKINbedder v3

                    1995 Lexus SC300

                    Comment


                    • #11
                      Originally posted by 0l33l
                      [code]Same thing... and I create the Phrases and call creategrammar. It fails when I press the button.
                      Are you getting the fail in the call to CmdSetRuleIdState I mentioned in post #6? It's an OLE error. If so, I'm as stuck as you are.

                      Ric.
                      EZSync - http://www.richmet.com/ezsync

                      Comment


                      • #12
                        Originally posted by ric355
                        Are you getting the fail in the call to CmdSetRuleIdState I mentioned in post #6? It's an OLE error. If so, I'm as stuck as you are.

                        Ric.
                        When mine fails its at Rule.InitialState.AddWordTransition(Nil, word, ' ', SPWT_LEXICAL, 'value',1,value,1.0);
                        PowerVoice v1 | NaviVoice Source
                        GammaControl v2.4
                        SKINbedder v3

                        1995 Lexus SC300

                        Comment


                        • #13
                          Originally posted by 0l33l
                          When mine fails its at Rule.InitialState.AddWordTransition(Nil, word, ' ', SPWT_LEXICAL, 'value',1,value,1.0);
                          You haven't said what the error is, but if it's an access violation then that's probably because of the line Rule:=nil in CreateGrammar. Take it out. AddWord uses Rule, and Rule will be nil by the time you press your button.

                          Really those global variables ought to be class variables so they can be tidied up with the form class on shutdown.
                          EZSync - http://www.richmet.com/ezsync

                          Comment


                          • #14
                            Code:
                            unit Unit1;
                            
                            interface
                            
                            uses
                              Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
                              Dialogs, SpeechLib_TLB, OleServer, StdCtrls;
                            
                            type
                              TForm1 = class(TForm)
                                SpSharedRecoContext1: TSpSharedRecoContext;
                                Button1: TButton;
                                Button2: TButton;
                                procedure SpSharedRecoContext1Hypothesis(ASender: TObject;
                                  StreamNumber: Integer; StreamPosition: OleVariant;
                                  const Result: ISpeechRecoResult);
                                procedure Button1Click(Sender: TObject);
                                procedure FormCreate(Sender: TObject);
                                procedure Button2Click(Sender: TObject);
                              private
                                { Private declarations }
                              public
                                { Public declarations }
                                procedure CreateGrammar;
                                procedure AddWord(word: String);
                              end;
                            
                            var
                              Form1: TForm1;
                              SRGrammar: ISpeechRecoGrammar;
                              Rule: ISpeechGrammarRule;
                              WordNumber: Integer;
                            
                              // Create this object and fill it with the words
                              // or phrases you want to recognise before calling CreateGrammar
                              Phrases: TStringList;
                            
                            implementation
                            
                            {$R *.dfm}
                            
                            procedure TForm1.SpSharedRecoContext1Hypothesis(ASender: TObject;
                              StreamNumber: Integer; StreamPosition: OleVariant;
                              const Result: ISpeechRecoResult);
                            var text: String; confidence: Integer;
                            begin
                            text:=Result.PhraseInfo.GetText(0,-1,True);
                            Confidence:=Integer(Result.PhraseInfo.Rule.Confidence);
                            showmessage(text);
                            end;
                            
                            procedure TForm1.AddWord(word: String);
                            Var Value: OleVariant;
                            begin
                              Inc(WordNumber);
                              Value:=WordNumber;
                              Rule.InitialState.AddWordTransition(Nil, word, ' ', SPWT_LEXICAL, 'value',1,value,1.0);
                            end;
                            
                            procedure TForm1.CreateGrammar;
                            var
                            i : integer;
                            begin
                              //OnAudioLevel event is not fired by default - this changes that
                              SpSharedRecoContext1.EventInterests := SREAllEvents;
                              SRGrammar := SpSharedRecoContext1.CreateGrammar(0);
                              Rule:=SRGrammar.Rules.Add('word',SRATopLevel+SRADefaultToActive,0);
                              WordNumber:=0;
                              for i := 0 to Phrases.Count-1 do begin
                                AddWord(Phrases[i]);
                              end;
                              //Rule:=Nil;
                              SRGrammar.Rules.Commit;
                              SRGrammar.CmdSetRuleIdState(0, SGDSActive);
                            end;
                            
                            procedure TForm1.Button1Click(Sender: TObject);
                            begin
                            //addword('test');
                            end;
                            
                            procedure TForm1.FormCreate(Sender: TObject);
                            begin
                            Phrases := TStringList.Create;
                            Phrases.Add('testing');
                            Phrases.Add('hi');
                            CreateGrammar;
                            end;
                            
                            procedure TForm1.Button2Click(Sender: TObject);
                            begin
                            //addword('hi');
                            end;
                            
                            end.


                            Sweet! That works now
                            Now I just have to figure out how to suit it for my needs

                            Thanks ric355!
                            PowerVoice v1 | NaviVoice Source
                            GammaControl v2.4
                            SKINbedder v3

                            1995 Lexus SC300

                            Comment

                            Working...
                            X