Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Number of Words in a String

  • To: mathgroup at smc.vnet.net
  • Subject: [mg102550] Re: [mg102538] Re: Number of Words in a String
  • From: "David Annetts" <davidannetts at aapt.net.au>
  • Date: Thu, 13 Aug 2009 03:21:53 -0400 (EDT)
  • References: <h5r8b5$l4q$1@smc.vnet.net> <200908120837.EAA14797@smc.vnet.net>

Hi Albert,
 
> I don't know what you did when turning it into a function, but this:
> 
> numwords[s_String] := Length[ReadList[StringToStream[s], Word]]
> 
> numwords["The cat in the hat."]
> 
> seems to work alright....

It does for a few calls.  But many calls to this (or similar) functions
leaves many open streams which slows your machine. To see this, try the
following sequence (which numwords defined as above)

opn = Streams[]
numwords@"the cat in the hat" & /@ Range[15];
opn = Streams[]

The streams can be closed using Close[#]& /@ Select[opn, SameQ[Head@#,
InputStream] &];

There are at least three remedies to this.

One is to remember to periodically close all opened streams.

Another is to modify Albert's function to something like

numwordsb[s_String] := Block[{opn, lng}, lng = Length[ReadList[opn =
StringToStream[s], Word]]; Close[opn]; lng]

But the most effective is probably to avoid StringToStream as much as you
can.  For me, this is by using Import[, "Table"] rather than my <6.0
hand-rolled code.

YMMV.

Regards,

Dave.



  • Prev by Date: Re: Sticky scaling? (for printing)
  • Next by Date: Re: Sticky scaling? (for printing)
  • Previous by thread: Re: Number of Words in a String
  • Next by thread: Re: Number of Words in a String