Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #20659

The measure of a whale

From "WJ" <w_a_x_man@yahoo.com>
Newsgroups comp.lang.forth
Subject The measure of a whale
Date 2013-03-14 08:56 +0000
Organization A noiseless patient Spider
Message-ID <khs3bn$3i7$1@dont-email.me> (permalink)

Show all headers | View raw


Consider all of the words in Project Gutenburg's "Moby Dick",
where a word is simply a sequence of letters.
Determine the number of distinct words.
Display the most common word and the number of times it occurs.

Factor:

USING: hashtables regexp io.files locals io.encodings.ascii ;

: best-pair ( seq -- pair )
  unclip
  [ 2dup [ last ] bi@ < [ nip ] [ drop ] if ] reduce ;

:: doit ( -- )
  H{ } clone :> table
  "moby10b.txt" ascii file-contents
  R/ [a-z]+/i all-matching-subseqs
  [ 1 swap table at+ ] each
  table count>> .
  table >alist best-pair .
;


[ doit ] time
19319
{ "the" 13784 }
Running time: 0.247124425 seconds



Ruby:

table = {} ; table.default = 0
IO.read("moby10b.txt").scan( /[a-z]+/i ).each{|s| table[s] += 1} ;
p table.size
p table.max_by{|k,v| v}

19319
["the", 13784]

Back to comp.lang.forth | Previous | NextNext in thread | Find similar | Unroll thread


Thread

The measure of a whale "WJ" <w_a_x_man@yahoo.com> - 2013-03-14 08:56 +0000
  Re: The measure of a whale "WJ" <w_a_x_man@yahoo.com> - 2013-04-02 09:08 +0000

csiph-web