TMemoで単語を数え、結果をTLabetまたはTEditで表示する方法を説明してください。出来ますか?また、類似語(重複語)の数をどのように数えるか知りたいです。ありがとうございました。PS:テキスト内の単語密度をどのように見つけることができますか?例:「犬」という単語がテキストに3回表示されます。テキストの単語番号は100です。したがって、「犬」という単語の密度は3%です。(3/100 * 100%)。
1166 次
1 に答える
11
最初の部分 ( uses Character
) については、
function CountWordsInMemo(AMemo: TMemo): integer;
var
i: Integer;
IsWhite, IsWhiteOld: boolean;
txt: string;
begin
txt := AMemo.Text;
IsWhiteOld := true;
result := 0;
for i := 1 to length(txt) do
begin
IsWhite := IsWhiteSpace(txt[i]);
if IsWhiteOld and not IsWhite then
inc(result);
IsWhiteOld := IsWhite;
end;
end;
第二部では、
function OccurrencesOfWordInMemo(AMemo: TMemo; const AWord: string): integer;
var
LastPos: integer;
len: integer;
txt: string;
begin
txt := AMemo.Text;
result := 0;
LastPos := 0;
len := Length(AWord);
repeat
LastPos := PosEx(AWord, txt, LastPos + 1);
if (LastPos > 0) and
((LastPos = 1) or not IsLetter(txt[LastPos-1])) and
((LastPos + len - 1 = length(txt)) or not IsLetter(txt[LastPos+len])) then
inc(result);
until LastPos = 0;
end;
function DensityOfWordInMemo(AMemo: TMemo; const AWord: string): real;
begin
result := OccurrencesOfWordInMemo(AMemo, AWord) / CountWordsInMemo(AMemo);
end;
于 2011-12-03T22:46:08.673 に答える