[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Count number of words in a text file

Posted on 2006-11-21
5
Medium Priority
?
514 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 1000 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 1000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Loops Section Overview
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

830 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