Yur*_*ios -1 delphi statistics ms-word count word-count
你能解释一下如何计算TMemo中的单词并在TLabet或TEdit中显示结果吗?可能吗?另外我想知道怎么算相似的单词(重复的单词)数量.谢谢.PS:我怎样才能在文字中找到单词密度?例如:单词"dog"在文本中出现三次.文本的字数是100.因此,"狗"这个词的密度是3%.(3/100*100%).
And*_*and 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;
Run Code Online (Sandbox Code Playgroud)
对于第二部分,
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;
Run Code Online (Sandbox Code Playgroud)