-
Notifications
You must be signed in to change notification settings - Fork 1
/
simplepairs.pl
executable file
·114 lines (103 loc) · 3.05 KB
/
simplepairs.pl
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/perl
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# https://github.com/flimzy/minimal-pairs/
use strict;
use warnings;
use utf8;
use open qw(:std :utf8);
use Unicode::GCString;
use List::Util qw( max );
use List::MoreUtils qw( any );
my $ignore = "-ːˈ ˌ";
my %chars;
my %ipa;
while (my $line = <>) {
$line =~ m#^\s*(.*?)\s:\s(.*)$# or die "Unable to read input: $line\n";
my ( $ipa, $words ) = ($1,$2);
$ipa =~ s/[$ignore]//g;
my $gc = Unicode::GCString->new($ipa);
$ipa{$ipa} = [ $gc, [split /, /,$words] ];
$chars{ "$_" }++ for $gc->as_array;
}
my $longest_word = max ( map { $_->[0]->columns } values %ipa );
my %pairs;
for (my $i=0; $i < $longest_word; $i++) {
for my $word (keys %ipa) {
next if length($word) <= $i;
my @word = $ipa{$word}[0]->as_array;
delete $word[$i];
my $key = join('', grep { defined } @word);
$pairs{$key} ||= [];
push @{$pairs{$key}}, $word;
}
}
sub heading {
print<<EOF;
<tr>
<th>Pronunciation Match</th>
<th>Pronunciation</th>
<th>Word(s)</th>
</tr>
EOF
}
my $count;
for (keys %pairs) {
next if scalar @{$pairs{$_}}==1;
$count++;
}
print<<EOF;
<html>
<head>
<meta charset="UTF-8">
</head>
<style>
table {
border: 1px solid black;
text-align: left;
}
td {
border: 1px solid black;
}
.ipa {
font-family: 'Charis SIL', 'DejaVu Sans', 'Segoe UI', 'Lucida Grande', 'Doulos SIL', 'TITUS Cyberbit Basic', Code2000, 'Lucida Sans Unicode', sans-serif
}
</style>
<h1>$count French Minimal Pairs</h1>
<p>This file was generated automatically by a script which is <a href='https://github.com/flimzy/minimal-pairs/'>hosted on GitHub</a>.
<table>
EOF
my $j=0;
heading();
for my $pair ( sort { scalar @{$pairs{$b}} <=> scalar @{$pairs{$a}} || $a cmp $b } keys %pairs ) {
last if scalar @{$pairs{$pair}} == 1;
if ( $j >= 30 ) {
heading();
$j=0;
}
my @matches;
for my $ipa ( sort @{$pairs{$pair}} ) {
$j++;
my $gc = $ipa{$ipa}->[0];
push @matches, sprintf "<td class='ipa'>/%s/</td><td>%s</td>\n",
$gc, join(', ',@{$ipa{$ipa}[1]});
}
my $matches = scalar(@{ $pairs{$pair} });
printf "<tr><td rowspan='%i'><span class='ipa'>~/%s/</span><br />%i phonetically similar words</td>\n",
$matches, $pair, $matches;
print join("</tr>\n<tr>",@matches)."</tr>\n";
}
print "</table>\n";
printf "<p>Encountered %i distinct graphemes: %s</p>\n",
scalar(keys %chars), join(' ', sort keys %chars);