Delphi-PRAXiS
Seite 2 von 5     12 34     Letzte »    

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Buchstabenhäufigkeit (https://www.delphipraxis.net/191707-buchstabenhaeufigkeit.html)

Luckie 13. Feb 2017 11:01

AW: Buchstabenhäufigkeit
 
Meine IDE kann noch kein Unicode, deswegen habe ich explizit den Datentyp AnsiString genommen, damit es zu keinen Problemen mit neueren IDE's und Compilern kommt.

Sherlock 13. Feb 2017 11:02

AW: Buchstabenhäufigkeit
 
OT: Ist der TB nicht was feines?
:roll:

Sherlock
- der wieder stänkert, obwohl das Jahrespensum doch bereits erfüllt schien...mea culpa.

t.roller 13. Feb 2017 16:54

AW: Buchstabenhäufigkeit
 
Liste der Anhänge anzeigen (Anzahl: 1)
Ich habe auch eine Lösung:
Es arbeitet mit der Buchstabenhäufigkeit lt. Tabelle in
Buchstabenhäufigkeit in deutschsprachigen Texten

Jeder gefundene Buchstabe wird gelöscht und verkürzt den Text, was nachfolgende Suchen beschleunigt.
Delphi-Quellcode:
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids, Vcl.ExtDlgs;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Memo11: TMemo;
    Memo13: TMemo;
    Button4: TButton;
    OpenTextFileDialog1: TOpenTextFileDialog;
    Button1: TButton;
    procedure Button4Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);

  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  nextLetter : Integer;
  LOT : Integer; //Length(Memo11.text);

implementation

{$R *.dfm}

function CountChars(const subtext, Text: String): Integer;
var Text3 : String;
begin
inc(nextLetter);
Text3:= StringReplace(Text, subtext, '', [rfReplaceAll, rfIgnoreCase]);
Result := Length(Text) - Length(Text3);
Form1.Memo11.text:= Text3;
Form1.StringGrid1.Cells[0,nextLetter]:= subtext;
Form1.StringGrid1.Cells[1,nextLetter]:= INTTOSTR(Result);
Form1.StringGrid1.Cells[2,nextLetter]:= FLOATTOSTR(Result / (LOT/100));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Memo11.Clear; Memo13.Clear;
if OpenTextfileDialog1.Execute then
BEGIN
  Memo11.Lines.LoadFromFile(OpenTextfileDialog1.FileName);
  nextLetter:=0;
  Application.ProcessMessages;
  LOT:= Length(Memo11.text);
END;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
    StringGrid1.Cells[0,0]:= 'Buchstabe';
    StringGrid1.Cells[1,0]:= 'Häufigkeit';
    StringGrid1.Cells[2,0]:= 'Prozent';
    Memo13.Lines.Add('Memo11-Length: '+INTTOSTR(Length(Memo11.text)));
    Memo13.Lines.Add('e: '+INTTOSTR(CountChars('e',Memo11.Text)));
    Memo13.Lines.Add('n:'+INTTOSTR(CountChars('n',Memo11.Text)));
    Memo13.Lines.Add('i: '+INTTOSTR(CountChars('i',Memo11.Text)));
    Memo13.Lines.Add('s: '+INTTOSTR(CountChars('s',Memo11.Text)));
    Memo13.Lines.Add('r: '+INTTOSTR(CountChars('r',Memo11.Text)));
    Memo13.Lines.Add('a: '+INTTOSTR(CountChars('a',Memo11.Text)));
    Memo13.Lines.Add('t: '+INTTOSTR(CountChars('t',Memo11.Text)));
    Memo13.Lines.Add('d: '+INTTOSTR(CountChars('d',Memo11.Text)));
    Memo13.Lines.Add('h: '+INTTOSTR(CountChars('h',Memo11.Text)));
    Memo13.Lines.Add('u: '+INTTOSTR(CountChars('u',Memo11.Text)));
    Memo13.Lines.Add('l: '+INTTOSTR(CountChars('l',Memo11.Text)));
    Memo13.Lines.Add('c: '+INTTOSTR(CountChars('c',Memo11.Text)));
    Memo13.Lines.Add('g: '+INTTOSTR(CountChars('g',Memo11.Text)));
    Memo13.Lines.Add('m: '+INTTOSTR(CountChars('m',Memo11.Text)));
    Memo13.Lines.Add('o: '+INTTOSTR(CountChars('o',Memo11.Text)));
    Memo13.Lines.Add('b: '+INTTOSTR(CountChars('b',Memo11.Text)));
    Memo13.Lines.Add('w: '+INTTOSTR(CountChars('w',Memo11.Text)));
    Memo13.Lines.Add('f: '+INTTOSTR(CountChars('f',Memo11.Text)));
    Memo13.Lines.Add('k: '+INTTOSTR(CountChars('k',Memo11.Text)));
    Memo13.Lines.Add('z: '+INTTOSTR(CountChars('z',Memo11.Text)));
    Memo13.Lines.Add('p: '+INTTOSTR(CountChars('p',Memo11.Text)));
    Memo13.Lines.Add('v: '+INTTOSTR(CountChars('v',Memo11.Text)));
    Memo13.Lines.Add('ß: '+INTTOSTR(CountChars('ß',Memo11.Text)));
    Memo13.Lines.Add('j: '+INTTOSTR(CountChars('j',Memo11.Text)));
    Memo13.Lines.Add('y: '+INTTOSTR(CountChars('y',Memo11.Text)));
    Memo13.Lines.Add('x: '+INTTOSTR(CountChars('x',Memo11.Text)));
    Memo13.Lines.Add('q: '+INTTOSTR(CountChars('q',Memo11.Text)));
    Memo13.Lines.Add('ä: '+INTTOSTR(CountChars('ä',Memo11.Text)));
    Memo13.Lines.Add('ö: '+INTTOSTR(CountChars('ö',Memo11.Text)));
    Memo13.Lines.Add('ü: '+INTTOSTR(CountChars('ü',Memo11.Text)));
    Memo13.Lines.Add('0: '+INTTOSTR(CountChars('0',Memo11.Text)));
    Memo13.Lines.Add('1: '+INTTOSTR(CountChars('1',Memo11.Text)));
    Memo13.Lines.Add('2: '+INTTOSTR(CountChars('2',Memo11.Text)));
    Memo13.Lines.Add('3: '+INTTOSTR(CountChars('3',Memo11.Text)));
    Memo13.Lines.Add('4: '+INTTOSTR(CountChars('4',Memo11.Text)));
    Memo13.Lines.Add('5: '+INTTOSTR(CountChars('5',Memo11.Text)));
    Memo13.Lines.Add('6: '+INTTOSTR(CountChars('6',Memo11.Text)));
    Memo13.Lines.Add('7: '+INTTOSTR(CountChars('7',Memo11.Text)));
    Memo13.Lines.Add('8: '+INTTOSTR(CountChars('8',Memo11.Text)));
    Memo13.Lines.Add('9: '+INTTOSTR(CountChars('9',Memo11.Text)));
    Memo13.Lines.Add('Memo11-Length: '+INTTOSTR(Length(Memo11.text)));
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  nextLetter:=0;
  Memo11.Lines.LoadFromFile('F:\schtasks.txt');
  Application.ProcessMessages;
  LOT:= Length(Memo11.text);
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
var s: string;
begin
  s := StringGrid1.Cells[ACol, ARow];
  StringGrid1.Canvas.FillRect(Rect);
  DrawText(StringGrid1.Canvas.Handle, PChar(s), Length(s), Rect,
  DT_SINGLELINE or DT_Center or DT_VCENTER); // Text zentrieren
end;

end.

DeddyH 13. Feb 2017 17:48

AW: Buchstabenhäufigkeit
 
Vielleicht denke ich zu simpel, aber würde es für Ansi nicht auch einfach so gehen (die nicht darstellbaren Zeichen habe ich bewusst nicht herausgefiltert)?
Delphi-Quellcode:
type
  TCharCounter = class
  private
    FCounts: array [AnsiChar] of Cardinal;
    FTotal: int64;
  public
    procedure ReadString(const s: AnsiString);
    function GetCount(c: AnsiChar): integer;
    function GetPercentage(c: AnsiChar): single;
    constructor Create;
  end;

constructor TCharCounter.Create;
begin
  FillChar(FCounts, SizeOf(FCounts), 0);
end;

function TCharCounter.GetCount(c: AnsiChar): integer;
begin
  Result := FCounts[c];
end;

function TCharCounter.GetPercentage(c: AnsiChar): single;
begin
  if FTotal = 0 then
    Result := 0
  else
    Result := GetCount(c) / FTotal * 100;
end;

procedure TCharCounter.ReadString(const s: AnsiString);
var
  i: Cardinal;
begin
  FTotal := 0;
  for i := Low(s) to High(s) do
    begin
      FCounts[s[i]] := FCounts[s[i]] + 1;
      inc(FTotal);
    end;
end;

Jumpy 14. Feb 2017 09:27

AW: Buchstabenhäufigkeit
 
Zitat:

Zitat von DeddyH (Beitrag 1361462)
Delphi-Quellcode:
FCounts[s[i]] := FCounts[s[i]] + 1;

Das scheint mir aber nicht richtig zu sein, oder?

DeddyH 14. Feb 2017 09:39

AW: Buchstabenhäufigkeit
 
Wieso nicht? Mal in Langform:
Delphi-Quellcode:
procedure TCharCounter.ReadString(const s: AnsiString);
var
  i: Cardinal;
  c: AnsiChar;
begin
  FTotal := 0;
  for i := Low(s) to High(s) do
    begin
      c := s[i];
      FCounts[c] := FCounts[c] + 1;
      inc(FTotal);
    end;
end;

Luckie 14. Feb 2017 10:05

AW: Buchstabenhäufigkeit
 
Und wie weißt du jetzt, wie häufig jeder einzelne Buchstabe vorkommt?

DeddyH 14. Feb 2017 10:22

AW: Buchstabenhäufigkeit
 
Die Anzahl steht je Zeichen im Array.

[edit] Nachtrag: Das Nullen des Arrays beim erneuten Einlesen habe ich noch vergessen.
Delphi-Quellcode:
procedure TCharCounter.ReadString(const s: AnsiString);
var
  i: Cardinal;
begin
  FTotal := 0;
  FillChar(FCounts, SizeOf(FCounts), 0);
  for i := Low(s) to High(s) do
    begin
      FCounts[s[i]] := FCounts[s[i]] + 1;
      inc(FTotal);
    end;
end;

Jumpy 14. Feb 2017 10:35

AW: Buchstabenhäufigkeit
 
Zitat:

Zitat von DeddyH (Beitrag 1361502)
Wieso nicht? Mal in Langform:

Irgendwie sehe ich den Trick nicht, wieso der Char der Index im Array ist? Ich steh da wohl auf der Leitung :oops:

DeddyH 14. Feb 2017 10:46

AW: Buchstabenhäufigkeit
 
Wenn Du je Zeichen die Anzahl ermitteln willst, bietet es sich doch an, das Zeichen dann auch als Index zu verwenden, oder?


Alle Zeitangaben in WEZ +1. Es ist jetzt 12:07 Uhr.
Seite 2 von 5     12 34     Letzte »    

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz