Solved

How To Count Words&Phrase Occurence In Memo

Posted on 2008-11-07
932 Views
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
(and so on.....)

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

Thanks!

Regards,

Delphio

0
Question by:delphio

LVL 28

Expert Comment

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."

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

Author Comment

Hi ciuly,

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

LOL....

There actually no rules. So it will count what you've mentioned :

(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

LVL 28

Expert Comment

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;
``````
0

Author Comment

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

Thanks
0

Author Comment

Plus, how to make it automatically count for all one words, two words and three words phrases/words occurence?

Thanks
0

LVL 28

Expert Comment

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

Author Comment

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
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

LVL 28

Expert Comment

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

Author Comment

Sorry, what I meant was

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

Thanks
0

LVL 28

Accepted Solution

>> 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

skipwhite;

end

else

//      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;
``````
0

Author Comment

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

Author Closing Comment

Thanks ciuly!
0

LVL 28

Expert Comment

0

Featured Post

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…