-
Notifications
You must be signed in to change notification settings - Fork 34
/
smartWordWrap_simple.pl
executable file
·159 lines (121 loc) · 3.79 KB
/
smartWordWrap_simple.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
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
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 15th October 2013
# https://trizenx.blogspot.com
# Smart word wrap algorithm
# See: https://en.wikipedia.org/wiki/Word_wrap#Minimum_raggedness
use 5.016;
use strict;
use warnings;
package Smart::Word::Wrap {
sub new {
my (undef, %args) = @_;
my %opt = (
width => 6,
text => '',
);
foreach my $key (keys %args) {
if (exists $opt{$key}) {
$opt{$key} = delete $args{$key};
}
else {
local $" = ', ';
die "ERROR: invalid key-option '$key' (expected one of {@{[keys %opt]}})";
}
}
bless \%opt, __PACKAGE__;
}
# This is the ugliest function! It, recursively,
# prepares the words for the combine() function.
sub prepare_words {
my ($self, @array) = @_;
my @root;
my $len = 0;
for (my $i = 0 ; $i <= $#array ; $i++) {
$len += (my $wordLen = length($array[$i]));
if ($len > $self->{width}) {
if ($wordLen > $self->{width}) {
$len -= $wordLen;
splice(@array, $i, 1, unpack "(A$self->{width})*", $array[$i]);
$i--, next;
}
last;
}
push @root, {"@array[0 .. $i]" => __SUB__->($self, @array[$i + 1 .. $#{array}])};
last if ++$len >= $self->{width};
}
@root ? \@root : undef;
}
# This function combines the
# the parents with the children.
sub combine {
my ($root, $hash) = @_;
my @row;
while (my ($key, $value) = each %{$hash}) {
push @{$root}, $key;
if (ref $value eq 'ARRAY') {
foreach my $item (@{$value}) {
push @row, __SUB__->($root, $item);
}
}
else {
@row = [@{$root}];
}
pop @{$root};
}
@row;
}
# This function finds the best
# combination available and returns it.
sub find_best {
my ($self, @arrays) = @_;
my %best = (
score => 'inf',
value => [],
);
foreach my $array_ref (@arrays) {
my $score = 0;
foreach my $string (@{$array_ref}) {
$score += ($self->{width} - length($string))**2;
}
if ($score < $best{score}) {
$best{score} = $score;
$best{value} = $array_ref;
}
}
@{$best{value}};
}
# This is the main function of the algorithm
# which calls all the other functions and
# returns the best possible wrapped string.
sub smart_wrap {
my ($self, %opt) = @_;
if (%opt) {
$self = $self->new(%{$self}, %opt);
}
my @words =
ref($self->{text}) eq 'ARRAY'
? @{$self->{text}}
: split(' ', $self->{text});
join "\n", $self->find_best(map { combine([], $_) } @{$self->prepare_words(@words)});
}
}
#
## Usage example
#
my $text = 'As shown in the above phases (or steps), the algorithm does many useless transformations';
my $obj = Smart::Word::Wrap->new(width => 20);
say "=>>> SMART WRAP:";
say $obj->smart_wrap(text => $text);
say "\n=>>> GREEDY WRAP (Text::Wrap):";
require Text::Wrap;
$Text::Wrap::columns = $obj->{width};
$Text::Wrap::columns += 1;
say Text::Wrap::wrap('', '', $text);
say "\n", '-' x 80, "\n";
say "=>>> SMART WRAP:";
$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
say $obj->smart_wrap(text => $text);
say "\n=>>> GREEDY WRAP (Text::Wrap):";
say Text::Wrap::wrap('', '', $text);