Question

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%).

Was it helpful?

Solution

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top