Solved

Count number of words in a text file

Posted on 2006-11-21
5
494 Views
Last Modified: 2010-04-04
Hi,

I'm looking for some code that will open a text file and count the number of words in it.

Thanks
0
Comment
Question by:zattz
  • 4
5 Comments
 
LVL 19

Accepted Solution

by:
MerijnB earned 250 total points
ID: 17985883
see QStrings.pas: http://www.torry.net/vcl/vcltools/text/adqstrings.zip which is a high speed strings manipulation unit.
It has the functions

function Q_CountOfWords(const S, Delimiters: string): Integer; overload;
function Q_CountOfWords(const S: string; const Delimiters: TCharSet =  Q_StdDelimsSet): Integer; overload;

so you could do something like:

var Data: TStringlist;
 NrOfWords: integer;
begin
 Data := TStringList.Create();
 try
  Data.LoadFromFile('myfile.text');
  NrOfWords := Q_CountOfWords(Data.Text, ' ');
 finally
  Data.Free();
 end;
end;
0
 
LVL 28

Assisted Solution

by:TName
TName earned 250 total points
ID: 17986023
Hi, or simply try this:

procedure TForm1.Button1Click(Sender: TObject);
var
SL:TStringList;
i,j,c:Integer;
s:String;
begin
   c:=0;
   SL:=TStringList.create;
   SL.LoadFromFile('C:\Test.txt');
   for i:=0 to SL.Count-1 do begin
     s:=Trim(SL[i]);
     if Length(s)>0 then
       for j:=1 to Length(s) do
         if (((s[j]<>' ') and (j=1)) or ((s[j]<>' ') and (s[j-1]=' '))) then
           inc(c)
   end;
   SL.Free;
   ShowMessage(IntToStr(c));
end;
0
 
LVL 28

Expert Comment

by:TName
ID: 17986055
The same as a function:

procedure TForm1.Button1Click(Sender: TObject);  
begin
   ShowMessage(IntToStr(CountFileWords('C:\Test.txt')));
end;


function CountFileWords(FName:String):Integer;
var
SL:TStringList;
i,j:Integer;
s:String;
begin
   Result:=0;
   SL:=TStringList.create;
   try
     SL.LoadFromFile('C:\Test.txt');
     for i:=0 to SL.Count-1 do begin
       s:=Trim(SL[i]);
       if Length(s)>0 then
         for j:=1 to Length(s) do
           if (((s[j]<>' ') and (j=1)) or ((s[j]<>' ')and(s[j-1]=' '))) then
             inc(Result);
   end;
   finally
     SL.Free;
   end;
end;
0
 
LVL 28

Expert Comment

by:TName
ID: 17986063
Oops, in the case of the function it should of course be:

  SL.LoadFromFile(FName);

instead of

 SL.LoadFromFile('C:\Test.txt');
0
 
LVL 28

Expert Comment

by:TName
ID: 17986184
To exclude non-word stand alone chars, the function could be expanded like this
(any char can be added to "NotValid")

function CountFileWords(FName:String):Integer;
var
SL:TStringList;
i,j:Integer;
s:String;
var NotValid : set of Char;
begin
   Result:=0;
   NotValid:= ['.',',','-','_','|','<','>'];
   SL:=TStringList.create;
   try
     SL.LoadFromFile(FName);
     for i:=0 to SL.Count-1 do begin
       s:=Trim(SL[i]);
       if Length(s)>0 then
         for j:=1 to Length(s) do
           if (((s[j]<>' ') and (j=1)) or ((s[j]<>' ') and ((s[j-1]=' ') or (s[j-1] in NotValid)))
               and (not(s[j] in NotValid))) then
             inc(Result);
     end;
   finally
     SL.Free;
   end;
end;

An exact definition of what a word is can help decide which method is best (e.g use ['a'..'z','A'..'Z']...)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now