字数统计,如在Delphi上的MS Word中

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)

  • @Yurios:如果您不知道如何在TLabel或TEdit中显示函数的结果,那么您是否需要首先阅读一些基本的Delphi教程?http://delphi.about.com是一个很好的起点(虽然这一分钟网站似乎正在等待tqn.com上的某些事情,但它通常非常敏感.) (4认同)
  • @Yurios分配给`TLabel`的`Caption`属性.对于编辑控件,请使用"Text"属性.对于`TMemo`,使用`Lines`属性.如果你不知道如何做到这一点,你需要在考虑计算事件之前先了解它.在你跑步之前走路. (2认同)