Thanks Rory for your answer
I already found the TStringlist and the ability to add a second parameter (an
object), but i mis on how I could use this as a counter? ("Add to add a new
word, and Objects
(typecast as an Integer") to increment the count of a word that is already in
the list.
)
A small sniplet of code could help maybe. It sounds like the easiest solution.
I would expect max about 20 different wordt, but every one could occur about 5
times.
regards
Andries
________________________________
From: Rory Daulton <[email protected]>
To: [email protected]
Sent: Friday, October 2, 2009 11:40:51 PM
Subject: Re: [delphi-en] counting occurances of unique words
Andries,
> I'm scrolling through a list of strings one by one (e.g. all words in
> a rich edit) and I want to count how much unique words I have and how
> often these words have been used.
Sorry I'm joining this thread late.
The best answer would depend on several things. About how many words do you
typically expect, and what would be the average count for each word? Would
the words be in random order, or are they sorted or in some other known
order? And did you list all the information you need?
If the number of words is fairly small, just about any algorithm would
suffice. You could use a sorted TStringList to store the words, IndexOf to
see if a word is already in the list, Add to add a new word, and Objects
(typecast as an Integer) to increment the count of a word that is already in
the list.
This algorithm would slow down as the number of words becomes large. For
many random-order words with a moderate count (say an average of around 5
per word), I would use a red-black tree to store the words and counts. This
is more complicated but would speed up the searching and adding of words.
The EZDSL package for Delphi includes code for the red-black tree. The code
does have a bug, which I corrected. Let me know if you need more info on
this choice or the corrected code.
Rory Daulton
rorydaulton@ email.com
[Non-text portions of this message have been removed]