[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Hash Function
From: |
Sergei Steshenko |
Subject: |
Re: Hash Function |
Date: |
Wed, 5 Dec 2012 08:04:19 -0800 (PST) |
----- Original Message -----
> From: Jordi Gutiérrez Hermoso <address@hidden>
> To: Jonathan Karsch <address@hidden>
> Cc: address@hidden
> Sent: Wednesday, December 5, 2012 5:35 PM
> Subject: Re: Hash Function
>
> On 5 December 2012 10:07, Jonathan Karsch <address@hidden> wrote:
>> I am trying to figure out how many distinct words are in a text
>> document, and how many instances there are of each.
>
> Octave does not have hashes nor sufficiently flexible associative
> arrays. I recommend using a language other than Octave for this task.
> For example, here is how you can do it in Python:
>
> #!/usr/bin/env python
>
> import sys
> from collections import defaultdict
>
> f = open(sys.argv[1])
>
> wordcount = defaultdict(int)
>
> for line in f.readlines():
> words = line.split()
> for word in words:
> wordcount[word] += 1
>
> f.close()
>
> for word, count in wordcount.iteritems():
> print "%s: %d" % (word, count)
>
>
> Here is how to do it in Perl:
>
> #!/usr/bin/env perl -w
>
> use strict;
>
> my %wordcount;
> while(<>){
> my @words = split;
> foreach my $word (@words){
> $wordcount{$word}++;
> }
> }
>
>
> while (my ($word, $count) = each %wordcount){
> print "$word: $count\n";
> }
>
>
> Both languages are installed already in your McIntosh PC. Put either
> of those programs into a file named count_words, give it executable
> permissions, and do "./count_words somefile".
>
> HTH,
> - Jordi G. H.
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://mailman.cae.wisc.edu/listinfo/help-octave
Jordi,
why are you posting distracting info ?
It _is_ possible to do this in Octave (with all my love for Perl, and you know
it.
Here is a _simple_ demo:
"
octave:1> system("cat -n /home/sergei/junk/hash_demo.m");
1 words_to_count = {"John", "Paul", "Mary", "Julia", "John", "Paul",
"John"};
2
3 hash = struct;
4
5 for word_number = 1:numel(words_to_count)
6 if(isfield(hash, words_to_count{word_number}))
7 hash = setfield(hash, words_to_count{word_number}, getfield(hash,
words_to_count{word_number}) + 1);
8 else
9 hash = setfield(hash, words_to_count{word_number}, 1);
10 endif
11 endfor
12
13
14 hash
octave:2> source("/home/sergei/junk/hash_demo.m");
hash =
scalar structure containing the fields:
John = 3
Paul = 2
Mary = 1
Julia = 1
octave:3>
".
Regards,
Sergei.
>
- Hash Function, Jonathan Karsch, 2012/12/05
- Re: Hash Function, Jordi Gutiérrez Hermoso, 2012/12/05
- Re: Hash Function,
Sergei Steshenko <=
- Re: Hash Function, Jordi Gutiérrez Hermoso, 2012/12/05
- Re: Hash Function, Sergei Steshenko, 2012/12/05
- Re: Hash Function, Juan Pablo Carbajal, 2012/12/05
- Re: Hash Function, Dimitri Maziuk, 2012/12/05
- Re: Hash Function, Sergei Steshenko, 2012/12/05
- Re: Hash Function, Dimitri Maziuk, 2012/12/05
- Re: Hash Function, Sergei Steshenko, 2012/12/05
- Re: Hash Function, Jordi Gutiérrez Hermoso, 2012/12/05
- Re: Hash Function, Dimitri Maziuk, 2012/12/05
Re: Hash Function, Przemek Klosowski, 2012/12/05