| 1 | package Taxon::Parse::Normalize; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use utf8; | ||
| 6 | |||
| 7 | use parent qw( Taxon::Parse ); | ||
| 8 | |||
| 9 | #use HTML::Entities; | ||
| 10 | |||
| 11 | our $VERSION = '0.013'; | ||
| 12 | |||
| 13 | # decode_entities($a); | ||
| 14 | # encode_entities($a, "\200-\377"); | ||
| 15 | |||
| 16 | #use Unicode::Normalize; | ||
| 17 | # NFC($string); | ||
| 18 | |||
| 19 | sub init { | ||
| 20 | my $self = shift; | ||
| 21 | |||
| 22 | my $p = $self->{pattern_parts}; | ||
| 23 | |||
| 24 | $p->{comma_before_year} = qr/ | ||
| 25 | (?: | ||
| 26 | ,+ | ||
| 27 | |[^0-9\(\[\"])\s*\d{3} #???? | ||
| 28 | ) | ||
| 29 | /xms; | ||
| 30 | $p->{quotes} = qr/[\"'`´]+/xms; | ||
| 31 | $p->{UPPERCASE_WORD} = qr/ \b ( \p{Lu}{2,} ) \b /xms; | ||
| 32 | $p->{WHITESPACE} = qr/\s+/xms; | ||
| 33 | $p->{SQUARE_BRACKETS} = qr/ \[ (.*?) \] /xms; | ||
| 34 | $p->{BRACKETS_OPEN} = qr/( [{(\[] ) \s* /xms; | ||
| 35 | $p->{BRACKETS_CLOSE} = qr/ \s* ( [})\]] ) /xms; | ||
| 36 | $p->{BRACKETS_OPEN_STRONG} = qr/(\s?[{(\[]\s?)+/xms; | ||
| 37 | $p->{BRACKETS_CLOSE_STRONG} = qr/(\s?[})\\]]\s?)+/xms; | ||
| 38 | $p->{NORM_AND} = qr/ \s (and|et|und|&) \s /xms; | ||
| 39 | $p->{NORM_ET_AL} = qr/ & \s al\.?/xms; | ||
| 40 | $p->{NORM_AMPERSAND_WS} = qr/&/xms; | ||
| 41 | $p->{NORM_HYPHENS} = qr/\s*-\s*/xms; | ||
| 42 | $p->{NORM_COMMAS} = qr/\s*,+/xms; | ||
| 43 | |||
| 44 | $p->{NORM_HYBRIDS_FORM} = qr/\s[×xX]\s/xms; | ||
| 45 | $p->{NORM_HYBRIDS_GENUS} = ""; | ||
| 46 | $p->{NORM_HYBRIDS_EPITH} = ""; | ||
| 47 | |||
| 48 | my $patterns = $self->{patterns}; | ||
| 49 | my @patterns = qw< full abbreviated_name>; | ||
| 50 | map { $patterns->{$_} = $p->{$_} } @patterns; | ||
| 51 | |||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | sub normalise { | ||
| 56 | my $self = shift; | ||
| 57 | my $string = shift; | ||
| 58 | |||
| 59 | if ($string eq '') { | ||
| 60 | return $string; | ||
| 61 | } | ||
| 62 | |||
| 63 | $string =~ s/—//g; | ||
| 64 | $string =~ s/://g; | ||
| 65 | |||
| 66 | # use commas before years | ||
| 67 | # ICZN §22A.2 http://www.iczn.org/iczn/includes/page.jsp?article=22&nfv= | ||
| 68 | $string =~ s/$this->COMMA_BEFORE_YEAR/$1, $2'/; | ||
| 69 | |||
| 70 | |||
| 71 | # no whitespace around hyphens | ||
| 72 | $string =~ s/$this->NORM_HYPHENS/-/; | ||
| 73 | # use whitespace with & | ||
| 74 | $string =~ s/$p->{NORM_AMPERSAND_WS}/ & /; | ||
| 75 | |||
| 76 | # whitespace before and after brackets, keeping the bracket style | ||
| 77 | $string =~ s/$p->{NORM_BRACKETS_OPEN}/ $1/; | ||
| 78 | $string =~ s/$p->{NORM_BRACKETS_CLOSE}/$1 /; | ||
| 79 | # remove whitespace before commas and replace double commas with one | ||
| 80 | $string =~ s/$p->{NORM_COMMAS}/, /; | ||
| 81 | # normalize hybrid markers | ||
| 82 | $string =~ s/$p->{NORM_HYBRIDS_GENUS}/×$1/; | ||
| 83 | $string =~ s/$p->{NORM_HYBRIDS_EPITH}/$1 ×$2/; | ||
| 84 | $string =~ s/$p->{NORM_HYBRIDS_FORM}/ × /; | ||
| 85 | # capitalize all entire upper case words | ||
| 86 | $string =~ s/$p->{NORM_UPPERCASE_WORDS}/ucfirst(lc($1)/eg; | ||
| 87 | |||
| 88 | $string =~ s/\s\s+/ /g; | ||
| 89 | $string =~ s/^\s//; | ||
| 90 | $string =~ s/\s$//; | ||
| 91 | |||
| 92 | return $string; | ||
| 93 | } | ||
| 94 | |||
| 95 | 1; |