像 Delphi 上的 MS Word 一样的字数统计

发布于 2024-12-19 12:41:23 字数 161 浏览 1 评论 0原文

您能解释一下如何在 TMemo 中计算单词数并在 TLabet 或 TEdit 中显示结果吗? 是否可以?我还想知道如何计算相似单词(重复单词)的数量。谢谢。 PS:如何找到文本中的单词密度?例如:“狗”这个词在文中出现了3次。文本的字数为100。因此“狗”一词的密度为3%。 (3/100 * 100%)。

Could you explain how to count words in TMemo and show the results in TLabet or TEdit?
Is it possible? Also I would like to know how count similar words (duplicate words) quantity. Thank you.
PS: how can i found words density in the text? For example: word "dog" appears three times in the text. Word number of the text is 100. Therefore density of the word "dog" is 3%. (3/100 * 100%).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

总攻大人 2024-12-26 12:41:23

对于第一部分(使用字符),

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;

For the first part (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;

For the second part,

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;
墨小沫ゞ 2024-12-26 12:41:23

maXbox4 有一个更简单、更快、更好的功能:

{ Returns a count of the number of occurences of SubText in Text }
function CountOccurences( const SubText: string;
                             const Text: string): Integer;
begin
  Result:= Pos(SubText, Text); 
  if Result > 0 then
    Result:= (Length(Text)-Length(StringReplace(Text,SubText,'',
                                     [rfReplaceAll]))) div  Length(subtext);
end;  { CountOccurences }

println('keyword freq. '+itoa(CountOccurences('exit',memo1.text)));

Theres a simpler, faster and better one from maXbox4:

{ Returns a count of the number of occurences of SubText in Text }
function CountOccurences( const SubText: string;
                             const Text: string): Integer;
begin
  Result:= Pos(SubText, Text); 
  if Result > 0 then
    Result:= (Length(Text)-Length(StringReplace(Text,SubText,'',
                                     [rfReplaceAll]))) div  Length(subtext);
end;  { CountOccurences }

println('keyword freq. '+itoa(CountOccurences('exit',memo1.text)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文