File: Koremutake.pm

package info (click to toggle)
libstring-koremutake-perl 0.30-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 124 kB
  • sloc: perl: 82; makefile: 2
file content (201 lines) | stat: -rw-r--r-- 6,221 bytes parent folder | download | duplicates (4)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package String::Koremutake;
use strict;
use warnings;
use Error;
our $VERSION = '0.30';

my @phonemes = map { lc } qw{BA BE BI BO BU BY DA DE DI DO DU DY FA FE FI
FO FU FY GA GE GI GO GU GY HA HE HI HO HU HY JA JE JI JO JU JY KA KE
KI KO KU KY LA LE LI LO LU LY MA ME MI MO MU MY NA NE NI NO NU NY PA
PE PI PO PU PY RA RE RI RO RU RY SA SE SI SO SU SY TA TE TI TO TU TY
VA VE VI VO VU VY BRA BRE BRI BRO BRU BRY DRA DRE DRI DRO DRU DRY FRA
FRE FRI FRO FRU FRY GRA GRE GRI GRO GRU GRY PRA PRE PRI PRO PRU PRY
STA STE STI STO STU STY TRA TRE};

my %phoneme_to_number;
my %number_to_phoneme;

my $number = 0;
foreach my $phoneme (@phonemes) {
  $phoneme_to_number{$phoneme} = $number;
  $number_to_phoneme{$number}  = $phoneme;
  $number++;
}

sub new {
  my $class = shift;
  my $self = {};
  bless $self, $class;
  return $self;
}

sub _numbers_to_koremutake {
  my($self, $numbers) = @_;
  my $string;
  foreach my $n (@$numbers) {
    throw Error::Simple("0 <= $n <= 127") unless (0 <= $n) && ($n <= 127);
    $string .= $number_to_phoneme{$n};
  }
  return $string;
}

sub _koremutake_to_numbers {
  my($self, $string) = @_;
  my @numbers;
  my $phoneme;
  my @chars = split //, $string;
  while (@chars) {
    $phoneme .= shift @chars;
    next unless $phoneme =~ /[aeiouy]/;
    my $number = $phoneme_to_number{$phoneme};
    throw Error::Simple("Phoneme $phoneme not valid") unless defined $number;
    push @numbers, $number;
    $phoneme = "";
  }
  return \@numbers;
}

sub integer_to_koremutake {
  my($self, $integer) = @_;

  throw Error::Simple("No integer given") unless defined $integer;
  throw Error::Simple('Negative numbers not acceptable') if $integer < 0;

  my @numbers;
  
  @numbers = (0) if $integer == 0;

  while ($integer != 0) {
    push @numbers, $integer % 128;
    $integer = int($integer/128);
  }
  return $self->_numbers_to_koremutake([reverse @numbers]);
}

sub koremutake_to_integer {
  my($self, $string) = @_;
  throw Error::Simple("No koremutake string given") unless defined $string;

  my $numbers = $self->_koremutake_to_numbers($string);
  my $integer = 0;
  while (@$numbers) {
    my $n = shift @$numbers;
    $integer = ($integer * 128) + $n;
  }
  return $integer;
}

1;

__END__

=head1 NAME

String::Koremutake - Convert to/from Koremutake Memorable Random Strings

=head1 SYNOPSIS

  use String::Koremutake;
  my $k = String::Koremutake->new;

  my $s = $k->integer_to_koremutake(65535);        # botretre
  my $i = $k->koremutake_to_integer('koremutake'); # 10610353957

=head1 DESCRIPTION

The String::Koremutake module converts to and from Koremutake
Memorable Random Strings.

The term "Memorable Random String" was thought up by Sean B. Palmer as
a name for those strings like dopynl, glargen, glonknic, spoopwiddle,
and kebble etc. that don't have any conventional sense, but can be
used as random identifiers, especially in URIs to keep them
persistent. See http://infomesh.net/2001/07/MeRS/

Koremutake is a MeRS algorithm which is used by Shorl
(http://shorl.com/koremutake.php). As they explain: "It is, in plain
language, a way to express any large number as a sequence of
syllables. The general idea is that word-sounding pieces of
information are a lot easier to remember than a sequence of digits."

=head1 INTERFACE

=head2 new()

The new() method is the constructor:

=head2 integer_to_koremutake($i)

The integer_to_koremutake method converts a positive integer to a
Koremutake string:

  my $s = $k->integer_to_koremutake(65535);        # botretre

=head2 koremutake_to_integer($s)

The koremutake_to_integer method converts a Koremutake string to the
integer it represents:

  my $i = $k->koremutake_to_integer('koremutake'); # 10610353957

=head1 CAVEATS

You need to "use bigint;" if you want String::Koremutake to work with
integers larger than what fits into a normal Perl integer before it
gets converted to a floating point number on your platform.

=head2 Example

Without "use bigint;" big integers get converted to fixed precision
floating point numbers:

  $ perl -MString::Koremutake -le '
    my $a = 65536**4;
    my $k = String::Koremutake->new;
    foreach my $b ($a, $a+1, $a+2, $a+3) {
      print "$b: ".$k->integer_to_koremutake($b);
    }'
  1.84467440737096e+19: bibababababababababa
  1.84467440737096e+19: bibababababababababa
  1.84467440737096e+19: bibababababababababa
  1.84467440737096e+19: bibababababababababa

If you use that large integers, you should add "use bigint;" to your
program which solves that issue:

  $ perl -Mbigint -MString::Koremutake -le '
    my $a = 65536**4;
    my $k = String::Koremutake->new;
    foreach my $b ($a, $a+1, $a+2, $a+3) {
      print "$b: ".$k->integer_to_koremutake($b);
    }'
  18446744073709551616: bibababababababababa
  18446744073709551617: bibababababababababe
  18446744073709551618: bibababababababababi
  18446744073709551619: bibababababababababo

It will likely save you from other issues with big integers, too.

Note that "foreach my $b ($a .. $a+3)" doesn't work either as the ".."
operator can't be overloaded. See CAVEATS in "perldoc bigint" for
details.

=head1 BUGS AND LIMITATIONS                                                     
                                                                                
No bugs have been reported.                                                     
                                                                                
Please report any bugs or feature requests to                                   
C<bug-String-Koremutake@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org>.  

=head1 AUTHOR

Leon Brocard C<acme@astray.com>

=head1 LICENCE AND COPYRIGHT                                                    
                                                                                
Copyright (c) 2005, Leon Brocard C<acme@astray.com>. All rights reserved.
                                                                                
This module is free software; you can redistribute it and/or                    
modify it under the same terms as Perl itself.