zattz
asked on
Count number of words in a text file
Hi,
I'm looking for some code that will open a text file and count the number of words in it.
Thanks
I'm looking for some code that will open a text file and count the number of words in it.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oops, in the case of the function it should of course be:
SL.LoadFromFile(FName);
instead of
SL.LoadFromFile('C:\Test.t xt');
SL.LoadFromFile(FName);
instead of
SL.LoadFromFile('C:\Test.t
To exclude non-word stand alone chars, the function could be expanded like this
(any char can be added to "NotValid")
function CountFileWords(FName:Strin g):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']...)
(any char can be added to "NotValid")
function CountFileWords(FName:Strin
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']...)
procedure TForm1.Button1Click(Sender
begin
ShowMessage(IntToStr(Count
end;
function CountFileWords(FName:Strin
var
SL:TStringList;
i,j:Integer;
s:String;
begin
Result:=0;
SL:=TStringList.create;
try
SL.LoadFromFile('C:\Test.t
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;