Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2001-09

Word count function

32 messages · 14 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author

Does anyone know of an efficient method for counting the words in a line of data? I've written a few different functions, none of which were terribly efficient or accurate. I've check the Contributed Units page, but I don't see anything that looks like it might help me. I also checked the sources that come with FPC, but unless I'm missing something obvious (which is possible) I don't see anything their either.

Any help would be appreciated. TIA...

Jim

Reply by email

Marco van de Voort
to Unknown author
Show quoted text 9 lines
Does anyone know of an efficient method for counting the words in a line of data? I've written a few different functions, none of which were terribly efficient or accurate. I've check the Contributed Units page, but I don't see anything that looks like it might help me. I also checked the sources that come with FPC, but unless I'm missing something obvious (which is possible) I don't see anything their either. Any help would be appreciated. TIA...

Check out xtdfpc on the contributed pages, look into unit EPasStr to the "ITEM" functions (item, items, qitem etc).

I don't know how much of that is in compilable state, but you could check the algoritm of those functions, or copy and paste them to a new unit.

Reply by email

Lee, John

here is a noddy proc to count words, sort of based on the 4dos word function. Seems to work for me. HTH. J

this is 4dos word function description...
@WORD[["xxx",]n,string]: Returns the nth word in a string. The first word is numbered 0. If n is negative, words are returned from the end of the string. The first argument is a list of word separators you want to use; if you don't specify it, only spaces, tabs, and commas are considered to be word separators.

{ as in 4dos}
function word(delim:string; no:longint;l:string):string;
var n,ll,ul,ct:longint;
begin
if delim='' then delim:=' ,';
ct:=0;
ll:=0;
ul:=0;
n:=1;
repeat
if pos(l[n],delim)>0 then
begin
  ct:=ct+1;
  if ct=no+1
  then
    ul:=n
  else
  if ct=no then
   ll:=n;
if deb=1 then writeln('ct n ll ul no ln ',ct,' ',n,' ',ll,' ',ul,' ',no,'
',l[n]);
if deb=1 then writeln('l=',l);
end;
n:=n+1;
until ( (ct=no+1) or (n>length(l)) ) ;
word:=copy(l,ll+1,ul-ll-1);

end;{word}

Earlier message 20 lines
-----Original Message----- From: Sent: Thursday, September 06, 2001 01:08 To: Subject: [fpc-pascal]Word count function Does anyone know of an efficient method for counting the words in a line of data? I've written a few different functions, none of which were terribly efficient or accurate. I've check the Contributed Units page, but I don't see anything that looks like it might help me. I also checked the sources that come with FPC, but unless I'm missing something obvious (which is possible) I don't see anything their either. Any help would be appreciated. TIA... Jim

Reply by email

ron wilson
to Unknown author: Does anyone know of an efficient method for counting the words in a line of data? I've…

this is my first post to the list - i am a novice, so any help would be appreciated.

i have been using the TFileStream class to do some basic file *reading* but the class is undocumented and seems to be *very* deficient in features. does anybody have good documentation for it? i need an eof flag/method, error handling, etc.

also, i am not married to this class. if anybody has a better idea, let me know. ultimately what i need to do is read and write simple text and binary files.

regards,
ron wilson
icq 83250263

Reply by email

Michael Van Canneyt
to ron wilson: this is my first post to the list - i am a novice, so any help would be appreciated. i…
ron wilson wrote 7 lines
this is my first post to the list - i am a novice, so any help would be appreciated. i have been using the TFileStream class to do some basic file *reading* but the class is undocumented and seems to be *very* deficient in features. does anybody have good documentation for it? i need an eof flag/method, error handling, etc.

There is a preliminary but quite complete&accurate version of the documentation on

http://www.freepascal.org/docs-html/classes/index.html

Look at the classes section, TFileStream. It is completely documented

also, i am not married to this class. if anybody has a better idea, let me know. ultimately what i need to do is read and write simple text and binary files.

You can always use the FileCreate/FileOpen/FileRead/FileWrite functions of the SysUtils unit. These are about the most low-level functions you can get, and they are cross-platform.

Michael.

Reply by email

Lee, John

ooops sorry, didn't realize you wanted a word count function... really must read emails properly ...J

Earlier message 75 lines
-----Original Message----- From: Lee, John Sent: Thursday, September 06, 2001 01:36 To: '' Subject: RE: [fpc-pascal]Word count function here is a noddy proc to count words, sort of based on the 4dos word function. Seems to work for me. HTH. J this is 4dos word function description... @WORD[["xxx",]n,string]: Returns the nth word in a string. The first word is numbered 0. If n is negative, words are returned from the end of the string. The first argument is a list of word separators you want to use; if you don't specify it, only spaces, tabs, and commas are considered to be word separators. { as in 4dos} function word(delim:string; no:longint;l:string):string; var n,ll,ul,ct:longint; begin if delim='' then delim:=' ,'; ct:=0; ll:=0; ul:=0; n:=1; repeat if pos(l[n],delim)>0 then begin ct:=ct+1; if ct=no+1 then ul:=n else if ct=no then ll:=n; if deb=1 then writeln('ct n ll ul no ln ',ct,' ',n,' ',ll,' ',ul,' ',no,' ',l[n]); if deb=1 then writeln('l=',l); end; n:=n+1; until ( (ct=no+1) or (n>length(l)) ) ; word:=copy(l,ll+1,ul-ll-1); end;{word} -----Original Message----- From: Sent: Thursday, September 06, 2001 01:08 To: Subject: [fpc-pascal]Word count function Does anyone know of an efficient method for counting the words in a line of data? I've written a few different functions, none of which were terribly efficient or accurate. I've check the Contributed Units page, but I don't see anything that looks like it might help me. I also checked the sources that come with FPC, but unless I'm missing something obvious (which is possible) I don't see anything their either. Any help would be appreciated. TIA... Jim

Reply by email

Michael Van Canneyt
to Michael Van Canneyt: There is a preliminary but quite complete&accurate version of the documentation on…
Michael Van Canneyt wrote 18 lines
On Thu, 6 Sep 2001, ron wilson wrote: > this is my first post to the list - i am a novice, so any help would be > appreciated. > > i have been using the TFileStream class to do some basic file *reading* but > the class is undocumented and seems to be *very* deficient in features. > does anybody have good documentation for it? i need an eof flag/method, > error handling, etc. There is a preliminary but quite complete&accurate version of the documentation on http://www.freepascal.org/docs-html/classes/index.html Look at the classes section, TFileStream. It is completely documented

Correction to my own post; The information found there is quite old, in particular, TFileStream is without documentation in that version. I will update it tonight.

Michael.

Reply by email

ron wilson
to Michael Van Canneyt: There is a preliminary but quite complete&accurate version of the documentation on…

ok - i have been here before...
http://www.freepascal.org/docs-html/classes/index.html

TFileStream inherits from THandleStream which inherits from TStream. i am still having problems figuring out how to do error handling. for example, let me give some generic code for what i need:

open(stream)
while not(eof(stream))
line=readln(stream) <-- read to cr/lf
parse(line)
end
close(stream)

or -

open(stream)
while not(eof(stream))
packet=read(stream,size) <-- read a record of fixed length parse(packet)
end
close(stream)

i guess my problem is that i can find no examples of file handling that doesn't assume a known file size. i suppose i could compare the size and position properties of the stream and write my own eof function, but i just assumed that this kindof method would be standard.

does anybody have a fuller example that i could work from - something more developed? i don't know what 90% of the TStream methods and properties are for. why write code myself if the class already does what i need? should i just write another class that inherits from TFileStream?

regards,
ron wilson
icq 83250263

-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----

There is a preliminary but quite complete&accurate version of the documentation on

http://www.freepascal.org/docs-html/classes/index.html

Look at the classes section, TFileStream. It is completely documented

...

You can always use the FileCreate/FileOpen/FileRead/FileWrite functions of the SysUtils unit. These are about the most low-level functions you can get, and they are cross-platform.

Michael.

-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----8<-----

Reply by email

ron wilson
to Michael Van Canneyt: Correction to my own post; The information found there is quite old, in particular,…

thanks michael

Earlier message 41 lines
-----Original Message----- From: Behalf Of Michael Van Canneyt Sent: Thursday, September 06, 2001 09:53 AM To: Subject: Re: [fpc-pascal]TFileStream On Thu, 6 Sep 2001, Michael Van Canneyt wrote: > > > On Thu, 6 Sep 2001, ron wilson wrote: > > > this is my first post to the list - i am a novice, so any help would be > > appreciated. > > > > i have been using the TFileStream class to do some basic file *reading* but > > the class is undocumented and seems to be *very* deficient in features. > > does anybody have good documentation for it? i need an eof flag/method, > > error handling, etc. > > There is a preliminary but quite complete&accurate > version of the documentation on > > http://www.freepascal.org/docs-html/classes/index.html > > Look at the classes section, TFileStream. It is completely documented Correction to my own post; The information found there is quite old, in particular, TFileStream is without documentation in that version. I will update it tonight. Michael.

Reply by email

Vit Kavan
to ron wilson: this is my second post. does anybody know of a library for fpc that does matrix…

Hi all,

1. anyone uses PostrgeSQL database from FPC programs? do you use the original unit written by Michael Van Canneyt? It's written against Postgres 6.3.1, so I expect problems with 7.1. I also didn't succeed under Win32 (it compiles, but crashes afterwards (pg library). Any tips?

2. Few days ago, I asked about the possibility of AMD Hammer/x86-64 port, I'd like to start with it, but not alone. Any volunteers to join me? :-)))

Vitek Kavan

Reply by email

Vit Kavan
to ron wilson: this is my second post. does anybody know of a library for fpc that does matrix…

Hi all,

1. Anyone uses PostgreSQL database from FPC programs? Do you use the original unit written by Michael Van Canneyt? It's written against v6.3.1, so I expect problems with v7.1. I was also not able to make it working under Win32 (it compiles, but doesn't work with the cygwin library). Any tips?

2. I asked, a few days ago, about the possibility of AMD Hammer/x86-64 port. I'd like to start with it, but not alone. Any volunteers to join me? :-)))

Vitek Kavan

Reply by email

Jonas Maebe
to Vit Kavan
Vit Kavan wrote
Sorry for the duplicate message. My K-Mail makes me sometimes write the message twice :-/

The mailing list software does it also sometimes, so most likely it wasn't you mailer's fault?.

Jonas

Reply by email

Michael Van Canneyt
to Vit Kavan: 1. anyone uses PostrgeSQL database from FPC programs? do you use the original unit…
Vit Kavan wrote 6 lines
Hi all, 1. anyone uses PostrgeSQL database from FPC programs? do you use the original unit written by Michael Van Canneyt? It's written against Postgres 6.3.1, so I expect problems with 7.1. I also didn't succeed under Win32 (it compiles, but crashes afterwards (pg library). Any tips?

1) It wasn't meant to compile under win32, just under Linux.
2) I didn't know there was a 7.1 version of Postgres out.

I'll update the unit, and try to get it to work under Windows as well.

Michael.

Reply by email

Full_Name
to Michael Van Canneyt: There is a preliminary but quite complete&accurate version of the documentation on…

Quoting Michael Van Canneyt :

If TFileStream is the same as Delphi's class, the following should work:

WRITE:

var
  fs: TFileStream;
  teststr: string;
  i: integer;
begin
  teststr := 'THIS IS A TEST STRING';
  fs := TFileStream.Create('c:\out\text.txt', fmCreate);
  try
    for i := 1 to length(teststr) do
      fs.Write(teststr[i], sizeof(char));
  finally
    fs.free;
  end;
end;

READ:

var
  fs: TFileStream;
  teststr: string;
  a: char;
  i: integer;
begin
  teststr := '';
  fs := TFileStream.Create('c:\out\text.txt', fmOpenRead);
  try
    fs.Position := 0;
    for i := 1 to fs.size do begin
      fs.Read(a, sizeof(char));
      teststr := teststr + a;
    end;
  finally
    fs.free;
  end;
  writeln(teststr);
end;

These examples are a bit cheesey (not how I would personally do things), but they are taken from an example I knocked up for a delegate on a course I taught a while back. He wanted a simple example of how to read and write to files.

Using this basic template, you could change 'char' for any type (word, integer, record etc).. Strings are tricky, as Delphi does some clever stuff behind the scenes, but FPC might allow them to be used directly.

open(stream)
while not(eof(stream))
packet=read(stream,size) <-- read a record of fixed length parse(packet)
end
close(stream)

could be translated as (untested):

var
  fs: TFileStream;
  aPacket: TPacket; //defined as whatever you like else where
  i: integer;
begin
  teststr := '';
  fs := TFileStream.Create('c:\filetoread.ext', fmOpenRead); //opens stream
  try
    fs.Position := 0; //make double sure at start.. delphi doesn't
                      //open at position '0'... not always, anyway...
    {may want a check to make sure there's enough file left to read..
     otherwise this may fail/never exit... }
    while (fs.position < fs.size) {NB ^ see above..} do begin
      fs.Read(aPacket, sizeof(TPacket));
      parse(aPacket); //defined somewhere else...
      //however, this should handle the 'endless loop' scenario..
      if (fs.size - fs.position < sizeof(TPacket) then break;
      fs.position := fs.position + sizeof(TPacket); //inc the position
    end;
  finally
    fs.free; //closes stream
  end;
end;

Something like that.. I once wrote a unit that reads/writes ZX Spectrum SNA files and lets you see the dump of the memory using a similar technique.

The above is untested, it might not work.. should give you the jist though.

"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." Kristian Wilson, Nintendo, Inc, 1989 -----BEGIN GEEK CODE BLOCK----- Version: 3.12 GCS d? s+++:+ a- C++ UL+ P L++ E---- W- N+ o+ K- w O- M V PS PE-- Y PGP- t- 5-- X- R- tv+ b+ DI++ D+ G e++ h--- r+++ y+++ ------END GEEK CODE BLOCK------

Reply by email

Vit Kavan
Show quoted text 6 lines
1) It wasn't meant to compile under win32, just under Linux. 2) I didn't know there was a 7.1 version of Postgres out. I'll update the unit, and try to get it to work under Windows as well. Michael.

Great, Thank you.

Vitek

______________________________________________________________________ Hledani lidi a jejich e-mailu: http://www.lide.cz

Reply by email

Peter Vreman
Show quoted text 7 lines
> does FPC not support default parameters like? > > function GetServerFilename(const boxname: String; Extension: String = > ''): String Yes, but only in version 1.1 (and only in Delphi mode I think, but possibly also in objfpc mode)

It is supported in both delphi and objfpc mode.

Reply by email

Peter Vreman
Show quoted text 7 lines
> does FPC not support default parameters like? > > function GetServerFilename(const boxname: String; Extension: String = > ''): String Yes, but only in version 1.1 (and only in Delphi mode I think, but possibly also in objfpc mode)

It is supported in both delphi and objfpc mode.

Reply by email

Andy Chandler
to Unknown author: As long as you don't use any of the new features (Interfaces, Dynamical arrays,default…

Is the Dynamic array addition 'safe' to use yet, or is it wise to wait a little longer?

I also have another question:- Will it be possible to change the length of the array on the fly with SetLength, say making an existing Dynamic array larger, and still retain the data currently in the array?

Andy

Earlier message 30 lines
----- Original Message ----- From: To: Sent: Saturday, September 08, 2001 3:01 PM Subject: Re: [fpc-pascal]default parameters? > > > On Sat, 8 Sep 2001, Markus Kaemmerer wrote: > > > Jonas Maebe wrote:, > > > > >Yes, but only in version 1.1 (and only in Delphi mode I think, but > > >possibly also in objfpc mode) > > > > thx. Is version 1.1 ready to compile release versions of programs > > (OpenXP) or should we use 1.0.5? When will there be an first release > > of 1.1? > > As long as you don't use any of the new features (Interfaces, Dynamical > arrays,default params), you should be fine. I think even default params > are stable enough these days. > > Michael. > > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Gabor DEAK JAHN
to Unknown author: Does anyone know of an efficient method for counting the words in a line of data? I've…

At 9/6/01 08:07 AM, you wrote:

Jim,

Does anyone know of an efficient method for counting the words in a line of data? I've written a few different functions, none of which were terribly

It also depends on what you exactly want, how you define words, whether you need to treat quoted parts as a single word or not but basically, you simply iterate over the string and count how many word separators you meet. To cope with runs of more than one consequtive separator, you could simply use a three state machine roughly like this:

var state : (insideword, firstseparator, moreseparator)

state := firstseparator
count := 0
for each character in the string do
   case state of
     firstseparator:
       increment (count)
       if character is separator then state := moreseparator
     moreseparator:
       if character is not separator then state := insideword
     insideword:
       if character is separator then state := firstseparator
if state = word then increment (count)

Bye,
Gábor

------------------------------------------------------------------- Gabor DEAK JAHN -- Budapest, Hungary.
E-mail:

Reply by email

Unknown author
to Markus Kaemmerer
Markus Kaemmerer wrote 8 lines
Jonas Maebe wrote:, >Yes, but only in version 1.1 (and only in Delphi mode I think, but >possibly also in objfpc mode) thx. Is version 1.1 ready to compile release versions of programs (OpenXP) or should we use 1.0.5? When will there be an first release of 1.1?

As long as you don't use any of the new features (Interfaces, Dynamical arrays,default params), you should be fine. I think even default params are stable enough these days.

Michael.

Reply by email

Jonas Maebe
to Andy Chandler: Is the Dynamic array addition 'safe' to use yet, or is it wise to wait a little longer? I…
Andy Chandler wrote
Is the Dynamic array addition 'safe' to use yet, or is it wise to wait a little longer?

Afaik it should be ready, though recently someone reported that multiple dimensions don't work yet for some reason.

I also have another question:- Will it be possible to change the length of the array on the fly with SetLength, say making an existing Dynamic array larger, and still retain the data currently in the array?

That's already the case.

Jonas

Reply by email

Lee, John

1.1 (snapshot)is still being developed/modified quite substantially & occasionally breaks - the release will be called 1.2 & AFAIK no date has been fixed yet. 1.0.5 is fixes for 1.0.4 (but it is still a snapshot) not a release. However it is much more stable than 1.1 because no development is being done only fixes for the 1.0.4 release - my colleagues will tell you about any specific issues for openxp... hth J

Earlier message 19 lines
-----Original Message----- From: Markus Kaemmerer Sent: Friday, September 07, 2001 11:32 To: Subject: Re: [fpc-pascal]default parameters? Jonas Maebe wrote:, >Yes, but only in version 1.1 (and only in Delphi mode I think, but >possibly also in objfpc mode) thx. Is version 1.1 ready to compile release versions of programs (OpenXP) or should we use 1.0.5? When will there be an first release of 1.1? -- Happy Arts Software

Reply by email

Gabor DEAK JAHN

At 9/6/01 08:07 AM, you wrote:

Jim,

Does anyone know of an efficient method for counting the words in a line of data? I've written a few different functions, none of which were terribly

It just happened that I needed this function myself, and I found two errors in my pseudo-code. Here it is, corrected:

var state : (insideword, firstseparator, moreseparator)

state := insideword
count := 0
for each character in the string do
   case state of
     firstseparator:
       increment (count)
       if character is separator then state := moreseparator
                 else state = insideword
     moreseparator:
       if character is not separator then state := insideword
     insideword:
       if character is separator then state := firstseparator
if state = insideword then increment (count)

Bye,
Gábor

------------------------------------------------------------------- Gabor DEAK JAHN -- Budapest, Hungary.
E-mail:

Reply by email