[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Count number of words in a text file

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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 my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

656 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