summaryrefslogtreecommitdiff
path: root/new_language_generation/perl_tools/generate_distribution.pl
blob: e86e04a7f299eb4be899de91e7023dd76c9a3cff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/perl

# USAGE: generate_distribution.pl word_list > character_list

use warnings;
use strict;

use Getopt::Long;
use Pod::Usage;

my $help = 0;

GetOptions(
           'help|?' => \$help) or pod2usage(2);

pod2usage(1) if $help;

=pod

=head1 NAME

generate_distribution.pl - Generate letter distribution data

=head1 SYNOPSIS

One argument: the output of generate_words.pl.

=cut

binmode STDOUT, ':utf8';

my %characters;

sub read_characters {
	for my $arg (@ARGV) {
		open (my $input, "<:encoding(utf8)", $arg);

		while (<$input>) {
			chomp;
			my ($word, $count) = split(/\s/, $_);

			for my $character (split(/;/, $word)) {
				$characters{$character} += $count;
			}
		}
	}
}

sub spit_characters {
	my @sorted_characters = sort { $characters{$b} <=> $characters{$a} } keys %characters;
	for my $character (@sorted_characters) {
		print "$character $characters{$character}\n";
	}
}

read_characters();
spit_characters();