?
Solved

Count number of words in a text file

Posted on 2006-11-21
5
Medium Priority
?
507 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

764 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