Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1057
  • Last Modified:

How To Count Words&Phrase Occurence In Memo

Say I have 100 words of text in a memo1.

How to calculate the number of words and phrase occurence in the memo and display it to a stringgrid1?

Here is the text example
=======================================
Hi, my name is Delphio I am a Delphi Programmer.
I am addicted to Delphi and programming.
I love programming and solving problem.

Programming is my way of life.
Remember me, my name is Delphio a Delphi Programmer
=======================================

The output in the stringgrid will be like this below
Note : I am open for any other idea how to display it better other than in string grid.

=======================================
Three words count  :
my name is Delphio | 2
name is Delphio | 2

Two words count :
Delphi Programmer | 2

One word count :
Programming | 3
name | 2
delphio | 2
delphi | 2
addicted | 2
(and so on.....)

=======================================

Thanks!

Regards,

Delphio


0
delphio
Asked:
delphio
  • 7
  • 6
1 Solution
 
2266180Commented:
Three words count  :
my name is Delphio | 2

there are 4 words there :P

moreover, what's the logic behind counting those as 3 words?

taking for example "I am addicted to Delphi and programming."
yuo can have: "I am addicted", "am addicted to", addicted to Delphi" and so on.

so, what are the rules for counting words? because on your example I don't see any.
0
 
delphioAuthor Commented:
Hi ciuly,

Thanks for replying!

Whack.... what I mean is :
name is Delphio | 2

LOL....

There actually no rules. So it will count what you've mentioned :
 "I am addicted", "am addicted to", addicted to Delphi" and so on...

(Sorry for not going into detail)
I though I was going to add a rules. But it will just make things harder.

What do you think?
0
 
2266180Commented:
how about the following function? is this what you're looking for?


uses strutils;
 
function countwords(words, text:string):integer;
var i:integer;
begin
  i:=1;
  result:=0;
  repeat
    i:=posex(words, text, i);
    if i>0 then
      inc(result);
  until i=0;
end;

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
delphioAuthor Commented:
Hi ciuly, I have tested your code but I could not make it work.

Do you have any more information how to execute/call your code?

Thanks
0
 
delphioAuthor Commented:
Plus, how to make it automatically count for all one words, two words and three words phrases/words occurence?

Thanks
0
 
2266180Commented:
if you place your testing text into a memo, you call it like:

showmessage(inttostr(countwords('my name is Delphio', memo1.text)));

it's a very simple function. I don't see why is it so hard to call it in your context. I have no idea what your context is.

plus that you are mixing things up like hell. first you say 3 words. then you give an example of 3 words which actually contains 4 words.
no rules mean anarchy. you cannot expect anybody to give you something nice if you don't give them some rules to base it on. you cannot say 3 words and give bad examples and expect us to deliver correct code.
how in the world are we supopsed to test the code and see that it does what you want? it's basic logic: false statement results in false statement no matter what. so bad examples will obviously result in bad code. so I cannot base my code on your examples because it won't do what you want.

so, either provide CORRECT examples and some rules, or try to figure out yourself how to call a function. calling a function is basic programming. it's the first thing you do. why is it so hard for you to use that function?

anyway, you want good results, then give us good information.
0
 
delphioAuthor Commented:
Hi ciuly,

Sorry if my question is not up to the standard here.
This is my second question, so I am  not aware how to make a good question yet.

I did try to use this procedure to call up your functions :

procedure TForm1.Button1Click(Sender: TObject);
begin
  edit1.text := inttostr(countwords(SearchFor, memo1.text));
end;

When I entered my example text above into the memo and click button1
I ended up in an endless loop.

People do make mistakes, and I have CORRECT my example above.
Appologize me if the example is not clear enough, so here again my example for the text above :

Text Example :
=======================================
Hi, my name is Delphio I am a Delphi Programmer.
I am addicted to Delphi and programming.
I love programming and solving problem.

Programming is my way of life.
Remember me, my name is Delphio a Delphi Programmer
=======================================
These result will be shown in 2 column stringgrid

Three Words :
Words/Phrases                 | Count
Hi, my name                       | 1
my name is                        | 2
name is Delphio                 | 1
is Delphio I                         | 1
Delphio I am                       | 1
I am a                                 | 1
am a Delphi                        | 2
a Delphi Programmer         | 2
.........
.........until the end of the text

Two Words :
Words/Phrases   | Count
Hi my                   |    1           -> comma, full stop, etc doesn't count.
my name             |    2
name is               |     2
is Delphio            |    2
Delphio I              |    1
I am                     |    2
am a                    |    2
a Delphi               |    2
.........
.........until the end of the text


One Words :
Words/Phrases   | Count
Hi                               | 1  
my                             |  3
name                         |  2
is                               |  3
Delphio                      |  2
I                                 |  3
am                             |  2
a                                |  2
Delphi                        |  2
Programmer              |  2
addicted                    |  1
to                               |  1
and                            |  2
programming             |  2    
.........
.........until the end of the text

Rules :
1. If the text is already in the list it's not counted.
2. The count procedure is case sensitive

I hope this information is good enough.

Thanks
0
 
2266180Commented:
I don't understand rule 1. can you give one example (on your text) which is counted and one which is not, according to rule 1?
0
 
delphioAuthor Commented:
Sorry, what I meant was

1. If the text is already in the list it will increase the COUNTER

Thanks
0
 
2266180Commented:
>> and I have CORRECT my example above.

it's still not correct. I wasted some time to make you teh code and obviously, it's not correct, becuase you gave incorrect information, again. nothing personal :) this is the truth.

look. you say, 3 words, right?
Hi, my name                       | 1
and then you say 2 words
Hi my                   |    1           -> comma, full stop, etc doesn't count.

you see? in 3 words, it counts. in 2 words, it doesn't.

attached is my test code which obviously yelds incorrect results based on your example.

so, do comma, point, etc count, or not?
uses strutils;
 
function count(words, text:string):integer;
var i:integer;
begin
  i:=0;
  result:=0;
  repeat
    i:=posex(words, text, i+1);
    if i>0 then
      inc(result);
  until i=0;
end;
 
procedure countwords(number:byte; text:string; list:TStrings);
var i,j:integer;
    l:TStringList;
    s:string;
  procedure skipwhite;
  begin
    while (i<length(text)) and (text[i]=#32) do
      inc(i);
  end;
begin
  list.clear;
  for i:=1 to length(text) do
    if not (text[i] in ['a'..'z', 'A'..'Z', '0'..'9', #32]) then
      text[i]:=#32;
  l:=TStringList.Create;
  try
    i:=1;
    skipwhite;
    repeat
      j:=i;
      i:=posex(#32, text, i+1);
      if i>0 then
      begin
        l.add(copy(text, j, i-j));
        skipwhite;
      end
      else
        l.add(copy(text, j, length(text)));
//      inc(i);
    until i=0;
    for i:=1 to l.count-number+1 do
    begin
      s:='';
      for j:=1 to number do
        s:=s+' '+l[i-1+j-1];
      delete(s, 1, 1);
      if list.Values[s]='' then
        list.Values[s]:=inttostr(count(s, text));
    end;
  finally
    freeandnil(l);
  end;
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  countwords(3, memo1.text, memo2.lines);
end;

Open in new window

0
 
delphioAuthor Commented:
Hi ciuly,

Thanks for the code!
It work perfectly as I wanted despite the errors in the examples,coma doesnt count :)


I am going to work with this code.

Thanks again!
0
 
delphioAuthor Commented:
Thanks ciuly!
0
 
2266180Commented:
ok. glad it helped.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now