| 1 | package List::Analyse::Sequence::Analyser::OL::RomanNumerals; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use List::Util qw( first ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use Roman; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 2 | our $VERSION = 0.01; | |||||||||
| 10 | |||||||||||
| 11 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 12 | 0 | return bless {}, shift; | |||||||||
| 13 | } | ||||||||||
| 14 | |||||||||||
| 15 | 0 | ▶ | sub analyse { | ||||||||
|
Subroutine
analyse: not called CC=14 SCAR=50.7
Pod
documented
| |||||||||||
| 16 | 0 | my $self = shift; | |||||||||
| 17 | 0 | my $datum = shift; | |||||||||
| 18 | |||||||||||
| 19 | 2 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 0 | my %pairs; # For finding multiple possible numerals. | |||||||||
| 21 | |||||||||||
| 22 | 0 | ▶ | while ( $datum =~ /\G(.*?)([divxmlc]+)/ig ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 0 | my ($prefix, $numeral) = ($1, $2); | |||||||||
| 24 | |||||||||||
| 25 | # The prefix should be everything from start of string, which | ||||||||||
| 26 | # means we have to keep concatenating the previous one | ||||||||||
| 27 | 0 | ▶ | if( %pairs ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | # The last prefix we found is the longest one, by definition. | ||||||||||
| 29 | 0 | my $prev_prefix = (sort { length $a <=> length $b } keys %pairs)[-1]; | |||||||||
| 30 | |||||||||||
| 31 | # The last one was not really a number and so remove it. | ||||||||||
| 32 | 0 | ▶ | delete $pairs{$prev_prefix} unless $pairs{$prev_prefix}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | |||||||||||
| 34 | 2 | ▶ | no warnings 'uninitialized'; # sorry. | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 0 | $prefix = $prev_prefix . $pairs{$prev_prefix} . $prefix; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 0 | ▶ | if( isroman( $numeral ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 0 | $pairs{$prefix} = $numeral; | |||||||||
| 40 | } | ||||||||||
| 41 | else { | ||||||||||
| 42 | # If it was not an actual numeral, use the whole lot, and we will delete it next time. | ||||||||||
| 43 | 0 | $pairs{$prefix . $numeral} = ""; | |||||||||
| 44 | } | ||||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 0 | ▶ | unless( exists $self->{prefix} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | # No point doing the rest of this sub if we've not done it before. | ||||||||||
| 49 | 0 | ▶ | if( %pairs ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 0 | $self->{prefix} = \%pairs; | |||||||||
| 51 | 0 | return 1; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 0 | return; | |||||||||
| 55 | } | ||||||||||
| 56 | # Now we have found all potential prefix-numeral combinations we can compare | ||||||||||
| 57 | # them against the previous set. | ||||||||||
| 58 | 0 | ▶ | return unless keys %{ $self->{prefix} }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | |||||||||||
| 60 | 0 | ▶ | if (exists $self->{prefix}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 0 | for (keys %{ $self->{potential_pairs} }) { | |||||||||
| 62 | 0 | ▶ | delete $self->{prefix}->{$_} unless exists $pairs{$_}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 0 | for my $prefix (keys %{ $self->{potential_pairs} }) { | |||||||||
| 66 | 0 | my $new_numeral = $pairs{$prefix}; | |||||||||
| 67 | 0 | my $previous_version = $self->{potential_pairs}->{$prefix}; | |||||||||
| 68 | |||||||||||
| 69 | 0 | ▶ | if ( arabic( $new_numeral ) != arabic( $previous_version ) + 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 0 | delete $self->{prefix}->{$prefix}; | |||||||||
| 71 | 0 | next; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 0 | $self->{prefix}->{$prefix} = $new_numeral; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | |||||||||||
| 78 | 0 | ▶ | return unless keys %{ $self->{prefix} }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | } | ||||||||||
| 80 | else { | ||||||||||
| 81 | 0 | $self->{prefix} = \%pairs; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 0 | return 1; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 0 | ▶ | sub prefix { | ||||||||
|
Subroutine
prefix: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 88 | 0 | return shift->{prefix}; | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 0 | ▶ | sub done { | ||||||||
|
Subroutine
done: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 92 | 0 | my $self = shift; | |||||||||
| 93 | 0 | my $shortest_prefix = (sort { length $a <=> length $b } keys %{ $self->{prefix} })[-1]; | |||||||||
| 94 | |||||||||||
| 95 | 0 | $self->{last_numer} = $self->{prefixes}->{$shortest_prefix}; | |||||||||
| 96 | 0 | $self->{prefix} = $shortest_prefix; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 2 | 1; | |||||||||
| 100 | |||||||||||
| 101 | __END__ | ||||||||||