Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: VB to Delphi Translation

  1. #1
    My Village Called 0l33l's Avatar
    Join Date
    Jul 2004
    Location
    Berkeley, CA
    Posts
    10,516

    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

  2. #2
    Variable Bitrate
    Join Date
    Jan 2005
    Location
    UK
    Posts
    265
    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.

  3. #3
    Variable Bitrate
    Join Date
    Jan 2005
    Location
    UK
    Posts
    265
    I haven't read the article, but this may be of help also:

    http://bdn.borland.com/article/0,1410,29583,00.html

  4. #4
    My Village Called 0l33l's Avatar
    Join Date
    Jul 2004
    Location
    Berkeley, CA
    Posts
    10,516
    Quote 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!
    Quote 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

  5. #5
    My Village Called 0l33l's Avatar
    Join Date
    Jul 2004
    Location
    Berkeley, CA
    Posts
    10,516
    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

  6. #6
    Variable Bitrate
    Join Date
    Jan 2005
    Location
    UK
    Posts
    265
    Quote 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.

  7. #7
    My Village Called 0l33l's Avatar
    Join Date
    Jul 2004
    Location
    Berkeley, CA
    Posts
    10,516
    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?

  8. #8
    Variable Bitrate
    Join Date
    Jan 2005
    Location
    UK
    Posts
    265
    Quote 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.

  9. #9
    Variable Bitrate
    Join Date
    Jan 2005
    Location
    UK
    Posts
    265
    Quote 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.

  10. #10
    My Village Called 0l33l's Avatar
    Join Date
    Jul 2004
    Location
    Berkeley, CA
    Posts
    10,516
    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.

Page 1 of 2 12 LastLast

Similar Threads

  1. developing in VB
    By ShinkunoNamida in forum Software & Software Development
    Replies: 17
    Last Post: 07-27-2008, 11:13 AM
  2. Delphi SDK examples?
    By Nisos in forum FreeDrive
    Replies: 11
    Last Post: 04-23-2005, 06:22 PM
  3. advice for starting out in VB programming
    By Vchat20 in forum Software & Software Development
    Replies: 10
    Last Post: 02-06-2005, 02:00 PM
  4. Using VB to search and play a track in Winamp
    By deadweasel in forum Software & Software Development
    Replies: 23
    Last Post: 01-26-2004, 07:25 PM
  5. Developing a VB app with IR input???
    By mp3stang02 in forum Software & Software Development
    Replies: 8
    Last Post: 08-26-2002, 01:42 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •