unit WS_General_API_tester;

interface

uses
  // standard Delphi units
  Windows, Messages, SysUtils, Variants, Classes,
  Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ExtCtrls,

  // my own Delphi units
  WS_General_API_types,
  TEXTBUF_mini,
  WS_General_API_caller_main;

type
  TForm4 = class(TForm)
    StatusBar1: TStatusBar;
    PanelTop: TPanel;
    ButtonConcordance: TButton;
    ListBoxFiles: TListBox;
    Label1: TLabel;
    EditFoldername: TEdit;
    PageControl1: TPageControl;
    TabSheetConcord: TTabSheet;
    TabSheetWordList: TTabSheet;
    TabSheetKeyWords: TTabSheet;
    ListBoxWordlist: TListBox;
    CheckBoxWordlistAlphabetical: TCheckBox;
    ButtonWordlist: TButton;
    ButtonKeywords: TButton;
    ListBoxKeyWords: TListBox;
    Panel1: TPanel;
    ListBoxConcordance: TListBox;
    Splitter1: TSplitter;
    ListBoxCollocates: TListBox;
    EditConcFilename: TEdit;
    ButtonConcSave: TButton;
    ButtonSaveWordlist: TButton;
    EditWordlistfilename: TEdit;
    ButtonSaveKWS: TButton;
    EditKWfilename: TEdit;
    EditWlist: TEdit;
    EditRefCorpWList: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    procedure ButtonConcordanceClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ButtonWordlistClick(Sender: TObject);
    procedure ButtonKeywordsClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ButtonSaveKWSClick(Sender: TObject);
    procedure ButtonSaveWordlistClick(Sender: TObject);
    procedure ButtonConcSaveClick(Sender: TObject);
  private
    { Private declarations }
    procedure LetOtherProgramsWorkToo;
    procedure CMGotMessage(var Message: TMessage);
      message WM_USER + 1;
  public
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation

{$R *.dfm}

procedure TForm4.FormCreate(Sender: TObject);
begin
  EditFolderName.Text := 'D:\text\humphreys interviews';
  EditRefCorpWList.Text := '';
  EditWList.Text := '';
  EditWordlistFilename.Text := ExtractFilePath(Application.ExeName);
  EditKWFilename.Text := EditWordlistFilename.Text;
  EditConcFilename.Text := EditWordlistFilename.Text;
  TEXTBUF_mini.SetAllDefaults;
end;

procedure TForm4.FormDestroy(Sender: TObject);
begin
{  logging_utilities.SetLogFilename(ChangeFileExt(Application.ExeName,'.txt'));
  keeping_log := true;
  TEXTBUF.SetLogFileName(ChangeFileExt(Application.ExeName,'.txt'));
  TEXTBUF.SetLogging(true);
  logging_utilities.StartNewLogSection;   }
end;

procedure TForm4.LetOtherProgramsWorkToo;
begin
  Application.ProcessMessages;
end;

procedure TForm4.CMGotMessage(var Message: TMessage);
//var x, y : integer;
begin
//  x := sizeof(Textbuf_mini.GetProgressParams.main);
//  y := sizeof(Textbuf_mini.GetProgressParams.other_requests);
//  if (x < 0) or (y < 0) then halt;
  case Message.WParamLo of
    cm_Progress_number   // show a number
     :
      Statusbar1.SimpleText := IntToStr(
      Textbuf_mini.GetProgressParams.other_value);
    cm_Progress_thisfile // show currentfile percent
     :
      Statusbar1.SimpleText := IntToStr(
      Textbuf_mini.GetProgressParams.percents[0]);
    cm_Progress_overall   // show overall percent
     :
      Statusbar1.SimpleText := IntToStr(
      Textbuf_mini.GetProgressParams.percents[1]);
    cm_Progress_caption : // show a message
      Statusbar1.SimpleText :=
        Textbuf_mini.GetProgressParams.caption;
    cm_Progress_clear_bars,
    cm_Progress_clear_all : // clear messages
      Statusbar1.SimpleText := '';
  end;
  Application.ProcessMessages;
end;


procedure TForm4.ButtonConcordanceClick(Sender: TObject);
var f : TSearchRec;
  i, j : integer;
  c : TAPICollocateEntryRec;
  d : TAPIConcordEntryRec;
  w : widestring;
  list : TStringlist;
begin
//  current_tool_var := concord;
  PageControl1.ActivePage := TabsheetConcord;
  if not FreeUpConc then halt;
  ReadSettingsAndSetupWindowMessaging(Handle,LetOtherProgramsWorkToo);
  SetCurrentTool(1);
  for i := 0 to 3 do begin
    if SetupSearchWords(i) then begin
      ClearSearchwords(i);   // to allow for multiple clicks of this button
      if i = 0 then
        AddASearchWord ('the',i);
    end;
  end;
  ListboxFiles.Clear;
  ListBoxConcordance.Clear;
  ListboxCollocates.Clear;
  list := TStringlist.Create;
  if (EditFolderName.Text > '') and
     (DirectoryExists(EditFolderName.Text)) then begin
    if FindFirst(EditFolderName.Text+'\*.txt',faAnyFile,f)= 0 then begin
      repeat
        if (f.Attr and faDirectory <> faDirectory) then begin
          List.Add(EditFolderName.Text+'\'+f.Name);
        end;
      until FindNext(f) <> 0;
      FindClose(f);
    end;
  end;
  for i := 0 to List.count-1 do begin
    w := List.Strings[i];
    ListboxFiles.Items.Add(IntToStr(i+1)+' '+w);
    if not
      AddAFileToProcess(pwidechar(w)) then halt;
  end;
  List.Free;
  if ComputeConcordance then begin
    for i := 0 to GetConcordanceCount-1 do begin
      d := ConcordanceLine(i);
      w := d.conc_line;
      ListBoxConcordance.Items.Add(IntToStr(i+1)+' '+
        Copy(w,d.midpoint,maxint));
    end;
    for i := 0 to GetCollocateCount-1 do begin
      c := GetCollocateDetails(i);
      w := c.w;
      w := w + ' '+ IntToStr(c.total) + ' (';
      for j := 0 to c.freqs_used-1 do begin
        w := w + IntToStr(c.freqs[j]);
        if j < c.freqs_used-1 then w := w + ',';
      end;
      w := w + ')';
      ListboxCollocates.Items.Add(IntToStr(i+1)+' '+ w);
    end;
  end;
  FreeFilesToProcess;
end;

procedure TForm4.ButtonWordlistClick(Sender: TObject);
var f : TSearchRec;
  i : integer;
  d : TAPIWordlistEntryRec;
  w : widestring;
  list : TStringlist;
begin
//  current_tool_var := wordlist;
  PageControl1.ActivePage := TabsheetWordlist;
  if not FreeUpWordlist then halt;
  ReadSettingsAndSetupWindowMessaging(Handle,LetOtherProgramsWorkToo);
  SetCurrentTool(0);
  ListboxFiles.Clear;
  ListBoxWordlist.Clear;
  list := TStringlist.Create;
  if (EditFolderName.Text > '') and
     (DirectoryExists(EditFolderName.Text)) then begin
    if FindFirst(EditFolderName.Text+'\*.txt',faAnyFile,f)= 0 then begin
      repeat
        if (f.Attr and faDirectory <> faDirectory) then begin
          List.Add(EditFolderName.Text+'\'+f.Name);
        end;
      until FindNext(f) <> 0;
      FindClose(f);
    end;
  end;
  for i := 0 to List.count-1 do begin
    w := List.Strings[i];
    ListboxFiles.Items.Add(IntToStr(i+1)+' '+w);
    if not
      AddAFileToProcess(pwidechar(w)) then halt;
  end;
  List.Free;
  if ComputeWordlist then begin
    for i := 0 to GetWordlistCount-1 do begin
      if CheckBoxWordlistAlphabetical.Checked then
       d := GetWordListDetails(i, alphabetical_order)
      else
        d := GetWordListDetails(i, frequency_order);
      w := d.word;
      w := w + '; freq='+ IntToStr(d.frequency);
      w := w + '; texts='+IntToStr(d.n_files);
      ListBoxWordlist.Items.Add(IntToStr(i+1)+' '+ w);
    end;
  end;
  FreeFilesToProcess;
end;

function WideStringFromString(const s : string) : widestring;
begin
  Result := s;
end;

procedure TForm4.ButtonKeywordsClick(Sender: TObject);
var i : integer;
  w : widestring;
  d : TAPIKeyWordsEntryRec;
begin
  PageControl1.ActivePage := TabsheetKeyWords;
  ListBoxKeywords.Clear;
  if (not FileExists(EditRefCorpWList.Text)) or (not FileExists(EditWList.Text)) then exit;
  if not FreeUpKeyWords then halt;
  ReadSettingsAndSetupWindowMessaging(Handle,LetOtherProgramsWorkToo);
  SetCurrentTool(2);
  if ComputeKeyWords(pwidechar(WideStringFromString(EditWList.Text)),
                     pwidechar(WideStringFromString(EditRefCorpWList.Text))) then begin
    for i := 0 to GetKeyWordlistCount-1 do begin
      d := GetKeyWordDetails(i);
      w := d.word;
      w := w + '; freq='+ IntToStr(d.frequencies[0]);
      w := w + '; ref-corp freq='+ IntToStr(d.frequencies[1]);
      w := w + '; p='+FloatToStr(d.p_value);
      ListBoxKeyWords.Items.Add(IntToStr(i+1)+' '+ w);
    end;
  end;
end;

procedure TForm4.ButtonSaveKWSClick(Sender: TObject);
begin
  if (GetKeyWordlistCount > 0) and (EditKWFilename.Text > '') then
  if SaveKeyWordlist(pwidechar(widestring(EditKWFilename.Text)))
  then Statusbar1.SimpleText := GetSavedKeyWordlistFilename + ' saved';
end;

procedure TForm4.ButtonSaveWordlistClick(Sender: TObject);
begin
  if (GetWordlistCount > 0) and (EditWordlistFilename.Text > '') then
  if SaveWordlist(pwidechar(widestring(EditWordlistFilename.Text)))
  then begin
    Statusbar1.SimpleText := GetSavedWordlistFilename + ' saved';
    EditRefCorpWList.Text := TEXTBUF_mini.GetReferenceCorpus;
    EditWList.Text := GetSavedWordlistFilename;
  end;
end;

procedure TForm4.ButtonConcSaveClick(Sender: TObject);
begin
  if (GetConcordanceCount > 0) and (EditConcFilename.Text > '') then
  if SaveConcordance(pwidechar(widestring(EditConcFilename.Text)))
  then Statusbar1.SimpleText := GetSavedConcordanceFilename + ' saved';
end;

end.
