Solved

Count number of words in a text file

Posted on 2006-11-21
5
490 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

19 Experts available now in Live!

Get 1:1 Help Now