Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow loading patterns and exceptions from files or an array reference #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions hyphenmin.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"See: http://mirror.ctan.org/language/hyph-utf8/source/generic/hyph-utf8/languages.rb",,,
grc-x-ibycus,ibycus,2,2
el-polyton,greek,1,1
el-monoton,monogreek,1,1
grc,ancientgreek,1,1
cop,coptic,1,1
de-1901,german,2,2
de-1996,ngerman,2,2
de-ch-1901,swissgerman,2,2
ru,russian,2,2
uk,ukrainian,2,2
af,afrikaans,1,2
ca,catalan,2,2
cs,czech,2,3
sk,slovak,2,3
cy,welsh,2,3
da,danish,2,2
eo,esperanto,2,2
es,spanish,2,2
eu,basque,2,2
fr,french,2,2
gl,galician,2,2
et,estonian,2,3
fi,finnish,2,2
hr,croatian,2,2
hu,hungarian,2,2
hy,armenian,1,2
ia,interlingua,2,2
id,indonesian,2,2
is,icelandic,2,2
ga,irish,2,3
it,italian,2,2
rm,romansh,2,2
fur,friulan,2,2
pms,piedmontese,2,2
kmr,kurmanji,2,2
la,latin,2,2
la-x-classic,classiclatin,2,2
la-x-liturgic,liturgicallatin,2,2
lt,lithuanian,2,2
lv,latvian,2,2
nl,dutch,2,2
oc,occitan,2,2
pl,polish,2,2
pt,portuguese,2,3
zh-latn-pinyin,pinyin,1,1
ro,romanian,2,2
sl,slovenian,2,2
hsb,uppersorbian,2,2
sv,swedish,2,2
tk,turkmen,2,2
tr,turkish,2,2
en-gb,ukenglish,2,3
en-us,usenglishmax,2,3
sh-latn,serbian,2,2
sh-cyrl,serbianc,2,2
mn-cyrl,mongolian,2,2
mn-cyrl-x-lmc,mongolianlmc,2,2
bg,bulgarian,2,2
sa,sanskrit,1,3
no,norwegian,2,2
nb,bokmal,2,2
nn,nynorsk,2,2
as,assamese,1,1
bn,bengali,1,1
gu,gujarati,1,1
hi,hindi,1,1
kn,kannada,1,1
ml,malayalam,1,1
mr,marathi,1,1
or,oriya,1,1
pa,panjabi,1,1
ta,tamil,1,1
te,telugu,1,1
th,thai,2,3
mul-ethi,ethiopic,1,1
ka,georgian,1,2
cu,churchslavonic,1,2
61 changes: 59 additions & 2 deletions lib/Text/Hyphen.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use strict;

use 5.006;

use Carp qw(croak);

=head1 NAME

Text::Hyphen - determine positions for hyphens inside words
Expand Down Expand Up @@ -82,6 +84,34 @@ English.
Minimal suffix to leave wothout any hyphens. Defaults to 2 for
English.

=item load_patterns, load_exceptions

The name of a file containing hyphenation patterns or exceptions
respectively, or an array reference with patterns or exceptions
respectively, or C<undef> to explicitly not load any exceptions
or to explicitly use the defaults. If these options were missing
in the call to the constructor the builtin defaults (Knuth's data
for US English) are loaded.

A file should contain nothing but whitespace-separated patterns
or exceptions. Suitable files can be found at
L<http://mirror.ctan.org/language/hyph-utf8/tex/generic/hyph-utf8/patterns/txt/>
Those files contain UTF-8 hyphenation data for many languages,
converted to UTF-8 by the people who maintain hyphenation files
for TeX. The patterns are in the files ending in F<.pat.txt> and
the exceptions are in the files ending in F<.hyp.txt>. This
distribution contains a file F<hyphenmin.csv> which contains
mappings from the language codes in the file names to the long
TeX names of the languages, and recommended C<min_prefix> and
C<min_suffix> values derived from data found at CTAN. Note that
this module has I<not> been tested with all those data.

=item binmode

A suitable second argument to L<binmode|binmode/"FILEHANDLE, LAYER">,
used when reading files specified with C<load_patterns>
or C<load_exceptions>. Defaults to C<:encoding(UTF-8)>.

=back

=cut
Expand All @@ -107,9 +137,19 @@ sub _add_pattern {
sub _load_patterns {
my $self = shift;

$self->_add_pattern($_) foreach @{$self->_PATTERNS};
my ( $patterns, $exceptions ) = qw(patterns exceptions);
for my $data ( $patterns, $exceptions ) {
my $key = "load_$data";
my $builtin = uc "_$data";
$data
= exists( $self->{$key} ) ? $self->_load_data( $self->{$key} )
: $self->{load_patterns} ? []
: $self->$builtin;
}

$self->_add_pattern($_) foreach @{$patterns};

foreach my $ex (@{$self->_EXCEPTIONS}) {
foreach my $ex (@{$exceptions}) {
(my $word = $ex) =~ tr/-//d;
# wo-rd-le => { wordle => [0, 0, 1, 0, 1, 0, 0] }
# ||a||a||
Expand All @@ -118,6 +158,23 @@ sub _load_patterns {
}
}

sub _load_data {
my($self, $arg) = @_;
return $arg if 'ARRAY' eq ref $arg;
return [] unless defined $arg;
croak "No such file or not a file: $arg"
unless !-d $arg and -f $arg;
local *FH;
open FH, '<', $arg or croak "Couldn't open file ($!): $arg";
my $binmode = exists($self->{binmode}) ? $self->{binmode} : ':encoding(UTF-8)';
binmode FH, $binmode if $binmode;
my @items = map { grep { length $_ } split /\s+/, $_ } <FH>;
close FH or croak "Couldn't close file ($!): $arg";
# use DDP;
# pp @items;
return \@items;
}

sub _PATTERNS {
return [qw(
.ach4 .ad4der .af1t .al3t .am5at .an5c .ang4 .ani5m .ant4 .an3te .anti5s .ar5s
Expand Down
78 changes: 78 additions & 0 deletions t/04-files.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/perl
use strict;
use warnings;

# These must be loaded before Test::More!
use utf8;
use open qw[ :std :utf8 ];

use Test::More;

BEGIN { use_ok('Text::Hyphen') };

my @data = (
# Portuguese was chosen because it is the smallest data set with both
# patterns and exceptions at *the* source for these files:
# http://mirror.ctan.org/language/hyph-utf8/tex/generic/hyph-utf8/patterns/txt/
[ 'Portuguese',
[ load_patterns => 't/data/hyph-pt.pat.txt',
load_exceptions => 't/data/hyph-pt.hyp.txt'],
{
'Todos' => 'To-dos',
'consciência' => 'cons-ci-ên-cia',
'devem' => 'de-vem',
'dignidade' => 'dig-ni-da-de',
'direitos' => 'di-rei-tos',
'dotados' => 'do-ta-dos',
'espírito' => 'es-pí-ri-to',
'fraternidade' => 'fra-ter-ni-da-de',
'hardware' => 'hard-ware',
'humanos' => 'hu-ma-nos',
'iguais' => 'iguais',
'livres' => 'li-vres',
'nascem' => 'nas-cem',
'outros' => 'ou-tros',
'razão' => 'ra-zão',
'relação' => 're-la-ção',
'seres' => 'se-res',
'software' => 'soft-ware',
},
],
# Default (English) patterns are expected to perform crappy
# on Portuguese words! This test is here to make sure the
# Portuguese patterns *were* loaded before and are *not* loaded now.
[ 'default',
[],
{ 'Todos' => 'To-dos',
'consciência' => 'con-sciên-cia',
'devem' => 'de-vem',
'dignidade' => 'dig-nidade',
'direitos' => 'di-re-itos',
'dotados' => 'dota-dos',
'espírito' => 'es-píri-to',
'fraternidade' => 'frater-nidade',
'hardware' => 'hard-ware',
'humanos' => 'hu-manos',
'iguais' => 'iguais',
'livres' => 'livres',
'nascem' => 'nascem',
'outros' => 'out-ros',
'razão' => 'razão',
'relação' => 're-lação',
'seres' => 'seres',
'software' => 'soft-ware',
},
],
);

for my $lang ( @data ) {
my($name, $opts, $words, $print) = @$lang;

my $hyp = new_ok 'Text::Hyphen', $opts, "$name hyphenator";

for my $word ( sort keys %$words ) {
is $hyp->hyphenate($word), $words->{$word}, "$name: $word";
}
}

done_testing;
2 changes: 2 additions & 0 deletions t/data/hyph-pt.hyp.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hard-ware
soft-ware
56 changes: 56 additions & 0 deletions t/data/hyph-pt.lic.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
% This file has been converted for the hyph-utf8 project from pthyph.tex
% (Version 1.2, 1996-07-21), whose authors have been identified as Pedro J. de
% Rezende <rezende at dcc.unicamp.br> and J.Joao Dias Almeida <jj at
% di.uminho.pt>. The licence terms are unchanged.
%
% See http://www.hyphenation.org for details on the project.
% ---------------------------------------------------------------------
% BSD 3-Clause License (https://opensource.org/licenses/BSD-3-Clause):
%
% Copyright (c) 1987, Pedro J. de Rezende ([email protected]) and J.Joao Dias Almeida ([email protected])
%
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in the
% documentation and/or other materials provided with the distribution.
% * Neither the name of the University of Campinas, of the University of
% Minho nor the names of its contributors may be used to endorse or
% promote products derived from this software without specific prior
% written permission.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
% ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
% DISCLAIMED. IN NO EVENT SHALL PEDRO J. DE REZENDE OR J.JOAO DIAS ALMEIDA BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
% GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
% HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
% OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The Portuguese TeX hyphenation table.
% (C) 2015 by Pedro J. de Rezende ([email protected])
% and J.Joao Dias Almeida ([email protected])
% Version: 1.3 Release date: 12/08/2015
%
% (C) 1996 by Pedro J. de Rezende ([email protected])
% and J.Joao Dias Almeida ([email protected])
% Version: 1.2 Release date: 07/21/1996
%
% (C) 1994 by Pedro J. de Rezende ([email protected])
% Version: 1.1 Release date: 04/12/1994
%
% (C) 1987 by Pedro J. de Rezende
% Version: 1.0 Release date: 02/13/1987
%
% -----------------------------------------------------------------
% Remember! If you *must* change it, then call the resulting file
% something else and attach your name to your *documented* changes.
% =================================================================
%
Loading