www.pudn.com > myRBF.rar > SetOption.pas


unit SetOption; 
 
interface 
 
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls, Buttons, ComCtrls, ExtCtrls,ShellAPI, ShlObj,IniFiles; 
 
type 
  TFrmSetOption = class(TForm) 
    GroupBox1: TGroupBox; 
    LabTrainPath: TLabel; 
    EdTrain: TEdit; 
    BtnTrain: TSpeedButton; 
    BtnTest: TSpeedButton; 
    EdTest: TEdit; 
    LabTestPath: TLabel; 
    Panel1: TPanel; 
    Label1: TLabel; 
    ComBSN: TComboBox; 
    ComBNum: TComboBox; 
    Label2: TLabel; 
    Label3: TLabel; 
    EdTrainNum: TEdit; 
    EdTestNum: TEdit; 
    Label4: TLabel; 
    RdgFeature: TRadioGroup; 
    RdgNetwork: TRadioGroup; 
    btnOk: TBitBtn; 
    BitBtn2: TBitBtn; 
    procedure BitBtn2Click(Sender: TObject); 
    procedure BtnTrainClick(Sender: TObject); 
    procedure BtnTestClick(Sender: TObject); 
    procedure btnOkClick(Sender: TObject); 
    procedure FormShow(Sender: TObject); 
  private 
    { Private declarations } 
     
    procedure ChooseDictory(TPath:TEdit); //选择路径 
    //得到主路径  true:去尾字符串;false:转换C格式 Spath:为原路径。Dpath:目标路径 
    Procedure GetMainPath( Spath:string;var Dpath:string;Flag:Boolean); 
    //根据ReadTxt路径目录,把该目录下的所有文件名写入WriteTxt中。ReadTxt不带'\' 
    //Flag:0原始语音文件的写入 ;1:训练语音文件的写入 ;2:测试语音文件的写入 
    function GetTxtFilename(ReadTxt,WriteTxt:string;WordNum,Flag:integer):integer; 
  public 
    { Public declarations } 
 
  end; 
 function OpenForm():integer; stdcall; 
 //在文件路径种原字符串替换为目标字符串,例如\train\ 替换为\Sigma\,并转换为C格式 
 function replacStr(source,target:pchar):pchar;stdcall; 
var 
  FrmSetOption: TFrmSetOption; 
implementation 
 
{$R *.dfm} 
procedure TFrmSetOption.BitBtn2Click(Sender: TObject); 
begin 
  Close; 
  FrmSetOption.Free; 
end; 
 
procedure TFrmSetOption.BtnTrainClick(Sender: TObject); 
begin 
  ChooseDictory(Edtrain); 
end; 
 
procedure TFrmSetOption.BtnTestClick(Sender: TObject); 
begin 
   ChooseDictory(Edtest); 
end; 
 
procedure TFrmSetOption.ChooseDictory(TPath: TEdit); 
var 
  TitleName : string; 
  lpItemID : PItemIDList; 
  BrowseInfo : TBrowseInfo; 
  DisplayName : array[0..MAX_PATH] of char; 
  TempPath : array[0..MAX_PATH] of char; 
begin 
  FillChar(BrowseInfo, sizeof(TBrowseInfo), #0); 
  BrowseInfo.hwndOwner := Handle; 
  BrowseInfo.pszDisplayName := @DisplayName; 
  TitleName := '选择路径'; 
  BrowseInfo.lpszTitle := PChar(TitleName); 
  BrowseInfo.ulFlags := BIF_RETURNONLYFSDIRS; 
  lpItemID := SHBrowseForFolder(BrowseInfo); 
  if lpItemId <> nil then 
  begin 
    SHGetPathFromIDList(lpItemID, TempPath); 
    TPath.Text:=TempPath; 
    GlobalFreePtr(lpItemID); 
  end; 
end; 
 
procedure TFrmSetOption.GetMainPath( Spath:string; var Dpath:string;Flag:Boolean); 
var i,j,k:integer; 
    stemp1,stemp2,str,SReplace:string; 
begin 
  i:=0; k:=0; 
  SReplace:=''; 
  stemp1:=Spath; 
  while(i<=length(stemp1))  do 
  begin 
    j:=pos('\',stemp1); 
    if j=0 then break; 
    stemp2:=Copy(stemp1,1,j-1); 
    stemp1:=copy(stemp1,j+1,length(stemp1)-j); 
 
    k:=k+j; 
    if not flag then 
      SReplace:=SReplace+stemp2+'\\'; 
    inc(i); 
  end; 
  if flag then    //去最后字符串 
     str:=copy(Spath,1,k-1) 
  else 
     str:=SReplace;  //将路径替换为C语言格式(带\\)    
  Dpath:=str; 
end; 
 
procedure TFrmSetOption.btnOkClick(Sender: TObject); 
var 
  STrainPath,STestpath,s,stemp,str:string; 
  AIniFile,C_IniFile: TIniFile; 
  FileCount,i,i_feature,i_network:Integer; 
begin 
  strainpath:=trim(EdTrain.Text)+'\'+ComBSN.Text+ComBNum.Text+'ci'+'\train.txt'; 
  if not fileexists(strainpath) then begin 
    s:='生成train.txt?'; 
    i := Application.MessageBox(Pchar(s), '请确认', MB_ICONQUESTION + MB_YESNO); 
    if i = IDYes then begin 
      s:=trim(EdTrain.Text)+'\'+ComBSN.Text+ComBNum.Text+'ci'; 
      FileCount:=strtoint(ComBNum.Text)*strtoint(EdTrainNum.Text)* 3; 
      if FileCount<>GetTxtFilename(s,STrainPath,strtoint(ComBNum.Text),1)then begin 
        Application.MessageBox('训练特征文件数目有误', '警告', MB_ICONWARNING); 
        deletefile(strainpath); 
        exit; 
      end; 
    end 
    else exit; 
  end; 
  STestpath:=trim(EdTest.Text)+'\'+ComBSN.Text+ComBNum.Text+'ci'+'\test.txt'; 
  if not fileexists(stestpath) then begin 
    s:='生成test.txt?'; 
    i := Application.MessageBox(Pchar(s), '请确认', MB_ICONQUESTION + MB_YESNO); 
    if i = IDYes then begin 
      s:=trim(EdTest.Text)+'\'+ComBSN.Text+ComBNum.Text+'ci'; 
      FileCount:=strtoint(ComBNum.Text)*strtoint(EdTestNum.Text)* 3; 
      if FileCount<>GetTxtFilename(s,STestpath,strtoint(ComBNum.Text),2)then begin 
        Application.MessageBox('测试特征文件数目有误!', '警告', MB_ICONWARNING); 
        deletefile(STestpath); 
        exit; 
      end; 
    end 
    else exit; 
  end; 
  AIniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'SetOption.ini'); 
  AIniFile.WriteString('Path', 'TrainPath',strainpath); 
  AIniFile.WriteString('Path', 'TestPath',stestpath); 
  AIniFile.WriteString('S/N', 'S/N',ComBSN.Text); 
  AIniFile.WriteInteger('WordNum', 'WordNum',strtoint(ComBNum.Text)); 
  AIniFile.WriteInteger('PersonNum', 'TrainNum',strtoint(trim(EdTrainNum.Text))); 
  AIniFile.WriteInteger('PersonNum', 'TestNum',strtoint(trim(EdTestNum.Text))); 
  i_feature:=RdgFeature.ItemIndex ; 
  AIniFile.WriteString('Else', 'Feature',RdgFeature.Items[i_feature]); 
  i_network:=RdgNetwork.ItemIndex ; 
  AIniFile.WriteString('Else', 'Network',RdgNetwork.Items[i_network]); 
  AIniFile.Free; 
  //生成C所需INI文件 
  C_IniFile:=TIniFile.Create(ExtractFilePath(Application.ExeName) + 'C_SetOption.ini'); 
  with C_IniFile do begin 
     WriteInteger('Num', 'WordNum',strtoint(ComBNum.Text)); //词数  1 
     FileCount:=strtoint(ComBNum.Text)*strtoint(EdTrainNum.Text)* 3; 
     WriteInteger('Num', 'TrainNum',FileCount); //训练文件数2 
     FileCount:=strtoint(ComBNum.Text)*strtoint(EdTestNum.Text)* 3; 
     WriteInteger('Num', 'TestNum',FileCount); //测试文件数3 
     i:=strtoint(EdTrainNum.Text)*3; 
     WriteInteger('Num', 'SequNum',i); //SequNum  4 
     WriteInteger('Num', 'HIDDEN',strtoint(ComBNum.Text)); //隐结点数 5 
     WriteInteger('Num', 'Dimension',1024); //维数 (1024) 6 
 
 
     s:=trim(EdTrain.Text)+'\'+ComBSN.Text+ComBNum.Text+'ci'+'\'; 
     GetMainPath( s,stemp,false); 
     WriteString('Path', 'TrainPath',stemp); //训练路径带'\\'  1 
     s:=trim(EdTest.Text)+'\'+ComBSN.Text+ComBNum.Text+'ci'+'\'; 
     GetMainPath( s,stemp,false); 
     WriteString('Path', 'TestPath',stemp); //测试路径带'\\' 2 
     s:=replacStr('\train\','\codebook\'); //中心路径 
     WriteString('Path', 'Codebook',s); //中心路径带'\\'  3 
     s:=replacStr('\train\','\sigma\'); //半径路径 
     WriteString('Path', 'Sigma',s); //半径路径带'\\'  4 
     s:=replacStr('\train\','\weight\'); //权值路径 
     WriteString('Path', 'Weight',s); //权值路径带'\\' 5 
     WriteString('Else', 'Feature',RdgFeature.Items[i_feature]);  //特征值6 
     WriteString('Else', 'Network',RdgNetwork.Items[i_network]);  //网络7 
     WriteString('Else', 'SNR',ComBSN.Text);  //网络7 
     Free; 
  end; 
  close; 
end; 
 
procedure TFrmSetOption.FormShow(Sender: TObject); 
var 
   AIniFile: TIniFile; 
   STrainPath,STestPath,Stemp1,Stemp2:string; 
begin 
  if not fileExists(ExtractFilePath(Application.ExeName) + 'SetOption.ini') then 
    exit; 
  AIniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'SetOption.ini'); 
  with AIniFile do begin 
    try 
      STrainPath := ReadString('Path', 'TrainPath', ''); 
      STestPath := ReadString('Path', 'TestPath', ''); 
      ComBSN.ItemIndex := ComBSN.Items.IndexOf(ReadString('S/N', 'S/N', '')); 
      ComBNum.ItemIndex := ComBNum.Items.IndexOf(ReadString('WordNum', 'WordNum', '')); 
      EdTrainNum.Text:= ReadString('PersonNum', 'TrainNum', ''); 
      EdTestNum.Text :=ReadString('PersonNum', 'TestNum', ''); 
    finally 
      Free; 
    end; 
  end; 
  GetMainPath(STrainPath, Stemp1,true); 
  GetMainPath(Stemp1, Stemp2,true); 
  EdTrain.Text:=Stemp2; 
  GetMainPath(STestPath, Stemp1,true); 
  GetMainPath(Stemp1, Stemp2,true); 
  EdTest.Text:=Stemp2; 
end; 
function OpenForm():integer; stdcall; 
var 
  //frmAbout: TfrmAbout; 
  Form1:TFrmSetOption; 
begin 
  Form1:=TFrmSetOption.Create(Application); 
  try 
    OpenForm:=Form1.ShowModal(); 
  finally 
    Form1.Free; 
  end; 
end; 
function replacStr(source,target:Pchar):pchar;stdcall; 
var position,StrLen:integer; 
   AIniFile: TIniFile; 
   STrainPath,S_temp,s:string; 
begin 
  if not fileExists(ExtractFilePath(Application.ExeName) + 'SetOption.ini') then begin 
    result:=''; 
    exit; 
  end;   
  AIniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'SetOption.ini'); 
  with AIniFile do begin 
    try 
      STrainPath := ReadString('Path', 'TrainPath', ''); 
    finally 
      Free; 
    end; 
  end; 
  {source在S中出现的位置} 
  s:= strainpath; 
  replacStr:=Pchar(s); 
  position:=pos(source,s); 
  if position<>0 then 
  begin 
      {source的长度} 
    StrLen:=length(source); 
      {删除source字符串} 
    delete(s,position,StrLen); 
      {插入target字符串到S中} 
    insert  (target,s,position); 
      {返回新串} 
    FrmSetOption.GetMainPath(s, S_temp,false); 
    replacStr:=Pchar(S_temp); 
  end; 
end; 
function TFrmSetOption.GetTxtFilename(ReadTxt, WriteTxt: string; WordNum, 
  Flag: integer): integer; 
var 
s:tsearchrec; 
i,k:integer; 
str:Tstringlist ; 
Stemp:string; 
begin 
  if Fileexists(WriteTxt) then begin 
   result:=0; 
   Exit; 
  end; 
  str:=Tstringlist.Create; 
  case Flag of 
  0,2:  //原始和test 
    begin 
      i:=findfirst(ReadTxt+'\*txt',faAnyFile,s) ; 
      while i=0 do 
      begin 
        if s.name[1]<>'.' then 
        begin 
          if (s.attr and fadirectory)=fadirectory then 
          else begin 
            if flag=0 then 
              str.Add(s.name) 
            else begin 
              k:=length(trim(s.name)); 
              stemp:=copy(trim(s.name),k-5,2) ; 
              str.Add(s.name+'   '+stemp) ; 
            end; 
          end; 
        end; 
        i:=findnext(s); 
      end; 
      findclose(s); 
    end ; 
  1: //train 
    begin 
      for k:=0 to WordNum-1 do begin 
        if k<10 then 
          i:=findfirst(ReadTxt+'\*0'+inttostr(k)+'.txt',faAnyFile,s) 
        else 
          i:=findfirst(ReadTxt+'\*'+inttostr(k)+'.txt',faAnyFile,s); 
        while i=0 do 
        begin 
          if s.name[1]<>'.' then 
          begin 
            if (s.attr and fadirectory)=fadirectory then 
            else 
              str.Add(s.name) ; 
          end; 
          i:=findnext(s); 
        end; 
        findclose(s); 
      end; 
    end; 
  end; 
  str.SaveToFile(WriteTxt); 
  Result:=str.Count ; 
end;   
end.