CAB/Analyzer/Morph/Helsinki.pm untested

filter: Statementstmt 0.0% 0 / 38 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 65 CC 6 SCAR 32.6
CC
6
Cov
0%
analyzeTypes
42.0
new
2.0
BEGIN
2.0
CRAP 26.0
1 ## -*- Mode: CPerl -*-
2 ##
3 ## File: DTA::CAB::Analyzer::Morph::Helsinki.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: morphological analysis via Gfsm automata, for use with Helsinki-style transducers
6 ## + transducers available in HFST format from https://sourceforge.net/projects/hfst/files/resources/morphological-transducers/
7  
8 ##==============================================================================
9 ## Package: Analyzer::Morph
10 ##==============================================================================
11 package DTA::CAB::Analyzer::Morph::Helsinki;
12 use DTA::CAB::Analyzer::Automaton::Gfsm;
13 use Carp;
14 use strict;
15 our @ISA = qw(DTA::CAB::Analyzer::Automaton::Gfsm);
16  
17 ## $obj = CLASS_OR_OBJ->new(%args)
18 ## + object structure: see DTA::CAB::Analyzer::Automaton::Gfsm
19 sub new {
20 my $that = shift;
21 my $aut = $that->SUPER::new(
22 ##-- defaults
23  
24 ##-- analysis selection
25 label => 'morph',
26 wantAnalysisLo => 0,
27 wantAnalysisLemma => 0, ##-- default=0
28 tolower => 1,
29  
30 ##-- verbosity
31 check_symbols => 0,
32  
33 ##-- Helsinki variant (e.g. 'de', 'en', ...)
34 helsinkiLang => '',
35  
36 ##-- user args
37 @_
38 );
39 return $aut;
40 }
41  
42 ##==============================================================================
43 ## Methods: Analysis: v1.x
44 ##==============================================================================
45  
46 ## $doc = $anl->analyzeTypes($doc,\%types,\%opts)
47 ## + perform type-wise analysis of all (text) types in %types (= %{$doc->{types}})
48 sub analyzeTypes {
49 my ($aut,$doc,$types,$opts) = @_;
50 return if (!$aut->SUPER::analyzeTypes($doc,$types,$opts));
51  
52 ##-- post-process: simulate TAGH-notation
53 my $label = $aut->{label};
54 my $null = [];
55 my ($w,$a,$hi,$tag,$lemma);
56 foreach $w (values %$types) {
57 foreach $a (@{$w->{$label}//$null}) {
58 $hi = $a->{hi};
59 if ($hi =~ /((?:\\?\[[^\<\>\[\]\/\\]+\\?\])+)$/) {
60 $tag = $1;
61 $lemma = substr($hi, 0, length($hi)-length($tag));
62 $tag = join('.', ($tag =~ /\[\+?([^\<\>\[\]\/\\]+)\\?\]/g));
63 }
64 elsif ($hi =~ /(\[\+[^<>\[\]\/\\]+\](?:\[[^\]]*\]|\\.)*)$/) {
65 ##-- de_free: "Haus[<NN>]Mann[<NN>]Kost[+NN][<Fem>][<Akk>][<Sg>]", "laufen[+V][<3>][<Sg>][<Pres>][<Ind>]"
66 $tag = $1;
67 $lemma = substr($hi, 0, length($hi)-length($tag));
68 $tag = join('.', ($tag =~ /\[\+?<?([^<>\[\]\/\\]+)\\?>?\]/g));
69 }
70 elsif ($hi =~ /((?:\\?\[\<?[^\<\>\[\]\/\\]+\>?\\?\]))$/) {
71 $tag = $1;
72 $lemma = substr($hi, 0, length($hi)-length($tag));
73 $tag =~ s/[\\\<\>\[\]\+]//g;
74 }
75 $lemma =~ s/(?:\[[^\+\]]*\]|\\)//g;
76 $lemma =~ s/\[([A-Z]+)\+\]/lc($1)."+"/eg;
77 $lemma =~ s/\[\+([A-Z]+)\]/"~".lc($1)/eg;
78 $a->{hi} = "$lemma\[_$tag]=$hi" if ($lemma || $tag);
79 }
80 }
81  
82 return $doc;
83 }
84  
85 1; ##-- be happy
86  
87 __END__