掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
テキストに現れる単語の頻度を高速でカウントするには? (ID:9903)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
正規表現を使わなくするだけでもかなり違いますね。 こんな感じでどうでしょう。 procedure TForm1.Button2Click(Sender: TObject); var sr, hash:TStringList; i, hindo:integer; S, w: String; c: integer; p, np: PCHAR; begin sr:=TStringList.Create; hash:=TStringList.Create; hash.CaseSensitive := True; sr.LoadFromFile('C:\Program Files\Borland\Delphi7\Source\Rtl\Win\UxTheme.pas');//Windows.pas'); // Windows.pasだと時間がかかりすぎました^^; S := sr.Text; p := PCHAR(S); while p^ <> #0 do begin //空白をスキップ while p^ in [#9, #10, #13, ' '] do begin Inc(p); end; np := p; c := 0; //単語を取得 while not (np^ in [#0, #9, #10, #13, ' ']) do begin Inc(np); Inc(c); end; SetLength(w, c); CopyMemory(PCHAR(w), p, c); SetLength(w, c); p := np; //単語をカウント i := hash.IndexOf(w); if i >= 0 then begin hash.Objects[i] := TObject(integer(hash.Objects[i]) + 1); end else begin hash.AddObject(w, TObject(1)); end; end; EditorEx1.Lines.Clear; for i := 0 to hash.Count - 1 do begin EditorEx1.Lines.Add(hash[i] + '=' + IntToStr(Integer(hash.Objects[i]))); end; hash.Free; sr.Free; end; 気になったのですが、TStringListのCaseSensitiveを設定してやらないと、大文字小文字の区別をしないと思うのですが、それでよいのでしょうか。 二分探索木は前に作ったのですがファイル管理用でして、ハッシュとして使うには変更点が多いので^^; もしやられるのであれば、自力でがんばってください。
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2020 Takeshi Okamoto All Rights Reserved.