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   26046 use strict;
  2         4  
  2         45  
3 2     2   6 use warnings;
  2         3  
  2         35  
4 2     2   31 use 5.012000;
  2         4  
5 2     2   7 use utf8;
  2         2  
  2         765  
6 2     2   447 use version 0.77; our $VERSION = version->declare('v0.0.1');
  2         1412  
  2         9  
7 2     2   132 use Carp;
  2         2  
  2         113  
8 2     2   363 use Lingua::LO::Transform::Syllables;
  2         3  
  2         18  
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 31 my ($class, %args) = @_;
51 1 50       4 my $variant = delete $args{variant} or confess("`variant' arg missing");
52 1         3 my $hyphenate = delete $args{hyphenate};
53              
54 1         4 my $subclass = __PACKAGE__ . "::$variant";
55 1         5 (my $module = $subclass) =~ s!::!/!g;
56 1         461 require "$module.pm";
57              
58 1         8 my $self = $subclass->new(%args);
59 1         5 $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 5849 my ($self, $text) = @_;
78 18         18 my $result = '';
79 18 50       34 my $sep_char = $self->{hyphenate} ? '-' : ' ';
80              
81 18         59 my @frags = Lingua::LO::Transform::Syllables->new( text => $text )->get_fragments;
82 18         69 while(@frags) {
83 18         15 my @lao;
84 18   66     87 push @lao, shift @frags while @frags and $frags[0]->{is_lao};
85 18         23 $result .= join($sep_char, map { $self->romanize_syllable( $_->{text} ) } @lao);
  29         78  
86 18   33     76 $result .= (shift @frags)->{text} while @frags and not $frags[0]->{is_lao};
87             }
88 18         87 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