File Coverage

blib/lib/Lingua/LO/Transform/Romanize.pm
Criterion Covered Total %
statement 40 43 93.0
branch 2 6 33.3
condition 3 6 50.0
subroutine 9 10 90.0
pod 3 3 100.0
total 57 68 83.8


line stmt bran cond sub pod time code
1             package Lingua::LO::Transform::Romanize;
2 2     2   55848 use strict;
  2         3  
  2         58  
3 2     2   7 use warnings;
  2         2  
  2         40  
4 2     2   28 use 5.012000;
  2         4  
5 2     2   6 use utf8;
  2         2  
  2         9  
6 2     2   381 use version 0.77; our $VERSION = version->declare('v0.0.1');
  2         1276  
  2         9  
7 2     2   132 use Carp;
  2         3  
  2         108  
8 2     2   374 use Lingua::LO::Transform::Syllables;
  2         4  
  2         16  
9              
10             =encoding UTF-8
11              
12             =head1 NAME
13              
14             Lingua::LO::Transform::Romanize - Romanize Lao syllables
15              
16             =head1 FUNCTION
17              
18             This s a factory class for Lingua::LO::Transform::Romanize::*. Currently there
19             is only L but other variants are
20             planned.
21              
22             =head1 SYNOPSIS
23              
24             my $o = Lingua::LO::Transform::Romanize->new(
25             variant => 'PCGN',
26             hyphenate => 1,
27             );
28              
29             =cut
30              
31             =head1 METHODS
32              
33             =head2 new
34              
35             See L on how to use the constructor. Arguments supported are:
36              
37             =over 4
38              
39             =item C: standard according to which to romanize. "PCGN" is the only
40             one currently implemented.
41              
42             =item C: separate runs of Lao syllables with hyphens if true.
43             Otherwise, blanks are used.
44              
45             =back
46              
47             =cut
48              
49             sub new {
50 1     1 1 22 my ($class, %args) = @_;
51 1 50       4 my $variant = delete $args{variant} or confess("`variant' arg missing");
52 1         2 my $hyphenate = delete $args{hyphenate};
53              
54 1         2 my $subclass = __PACKAGE__ . "::$variant";
55 1         5 (my $module = $subclass) =~ s!::!/!g;
56 1         425 require "$module.pm";
57              
58 1         7 my $self = $subclass->new(%args);
59 1         4 $self->{hyphenate} = $hyphenate;
60 1         3 return $self;
61             }
62              
63             =head2 romanize
64              
65             romanize( $text )
66              
67             Return the romanization of C<$text> according to the standard passed to the
68             constructor. Text is split up by
69             L; Lao syllables are processed
70             and everything else is passed through unchanged save for possible conversion of
71             combining characters to a canonically equivalent form in
72             L.
73              
74             =cut
75              
76             sub romanize {
77 18     18 1 7216 my ($self, $text) = @_;
78 18         22 my $result = '';
79 18 50       37 my $sep_char = $self->{hyphenate} ? '-' : ' ';
80              
81 18         53 my @frags = Lingua::LO::Transform::Syllables->new( text => $text )->get_fragments;
82 18         58 while(@frags) {
83 18         16 my @lao;
84 18   66     98 push @lao, shift @frags while @frags and $frags[0]->{is_lao};
85 18         19 $result .= join($sep_char, map { $self->romanize_syllable( $_->{text} ) } @lao);
  29         68  
86 18   33     72 $result .= (shift @frags)->{text} while @frags and not $frags[0]->{is_lao};
87             }
88 18         78 return $result;
89             }
90              
91             =head2 romanize_syllable
92              
93             romanize_syllable( $syllable )
94              
95             Return the romanization of a single C<$syllable> according to the standard passed to the
96             constructor. This is a virtual method that must be implemented by subclasses.
97              
98             =cut
99              
100             sub romanize_syllable {
101 0     0 1   my $self = shift;
102 0 0         ref $self or die "romanize_syllable is not a class method";
103 0           die ref($self) . " must implement romanize_syllable()";
104             }
105              
106             1;
107