UNECRecommendation20: Mapping von Einheitsnamen auf Codes
authorMoritz Bunkus <m.bunkus@linet-services.de>
Fri, 28 Feb 2020 11:00:01 +0000 (12:00 +0100)
committerMoritz Bunkus <m.bunkus@linet-services.de>
Fri, 28 Feb 2020 15:07:11 +0000 (16:07 +0100)
SL/Helper/UNECERecommendation20.pm [new file with mode: 0644]

diff --git a/SL/Helper/UNECERecommendation20.pm b/SL/Helper/UNECERecommendation20.pm
new file mode 100644 (file)
index 0000000..3b77428
--- /dev/null
@@ -0,0 +1,56 @@
+package SL::Helper::UNECERecommendation20;
+
+use strict;
+use warnings;
+use utf8;
+
+use Exporter qw(import);
+our @EXPORT_OK = qw(map_name_to_alpha_2_code);
+
+use List::Util qw(first);
+
+my @mappings = (
+  # areas
+  [ 'MTK', qr{^(?:m²|qm|quadrat *meter|quadrat *metre)$}i ],
+
+  # distances
+  [ 'CMT', qr{^(?:cm|centi *meter|centi *metre)$}i ],
+  [ 'MTR', qr{^(?:m|meter|metre)$}i ],
+  [ 'KMT', qr{^(?:km|kilo *meter|kilo *metre)$}i ],
+
+  # durations
+  [ 'SEC', qr{^(?:s|sec|second|sek|sekunde)$}i ],
+  [ 'MIN', qr{^min(?:ute)?$}i ],
+  [ 'MON', qr{^mon(?:th|at)?$}i ],
+  [ 'HUR', qr{^(?:h(?:our)?|std(?:unde)?)$}i ],
+  [ 'DAY', qr{^(?:day|tag)$}i ],
+
+  # mass
+  [ 'MGM', qr{^(?:mg|milli *gramm?)$}i ],
+  [ 'GRM', qr{^(?:g|gramm?)$}i ],
+  [ 'KGM', qr{^(?:kg|kilo *gramm?)$}i ],
+  [ 'KTN', qr{^(?:t|tonne|kilo *tonne)$}i ],
+
+  # volumes
+  [ 'MLT', qr{^(?:ml|milli *liter|milli *litre)$}i ],
+  [ 'LTR', qr{^(?:l|liter|litre)$}i ],
+
+  # miscellaneous
+  [ 'C62', qr{^(?:stck|stück|pieces?|pc|psch|pauschal)$}i ],
+);
+
+sub map_name_to_code {
+  my ($unit) = @_;
+
+  return undef if ($unit // '') eq '';
+
+  my $code = first { $unit =~ $_->[1] } @mappings;
+  return $code->[0] if $code;
+
+  no warnings 'once';
+  $::lxdebug->message(LXDebug::WARN(), "UNECERecommendation20::map_name_code: no mapping found for '$unit'");
+
+  return undef;
+}
+
+1;