line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Wiktionary::Parser::TemplateParser; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
17
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
104
|
|
4
|
3
|
|
|
3
|
|
18
|
use warnings; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
84
|
|
5
|
3
|
|
|
3
|
|
23
|
use Data::Dumper; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
2079
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
sub new { |
8
|
81
|
|
|
81
|
0
|
125
|
my $class = shift; |
9
|
81
|
|
|
|
|
130
|
my %args = @_; |
10
|
81
|
|
|
|
|
206
|
my $self = bless \%args, $class; |
11
|
81
|
|
|
|
|
310
|
return $self; |
12
|
|
|
|
|
|
|
} |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# given a line of text, extract the content of each template |
15
|
|
|
|
|
|
|
sub extract_templates { |
16
|
52
|
|
|
52
|
0
|
61
|
my $self = shift; |
17
|
52
|
|
|
|
|
118
|
my %args = @_; |
18
|
52
|
|
|
|
|
70
|
my $line = $args{line}; |
19
|
|
|
|
|
|
|
|
20
|
52
|
|
|
|
|
125
|
my @templates = $line =~ m/\{\{([^\}]+)\}\}/g; |
21
|
|
|
|
|
|
|
|
22
|
52
|
|
|
|
|
223
|
return @templates; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub extract_tokens { |
26
|
52
|
|
|
52
|
0
|
61
|
my $self = shift; |
27
|
52
|
|
|
|
|
103
|
my %args = @_; |
28
|
52
|
|
|
|
|
64
|
my $line = $args{line}; |
29
|
|
|
|
|
|
|
|
30
|
52
|
|
|
|
|
256
|
my @tokens = $line =~ m/\[\[([^\]]+)\]\]/g; |
31
|
|
|
|
|
|
|
|
32
|
52
|
|
|
|
|
274
|
return @tokens; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub parse_template { |
37
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
38
|
0
|
|
|
|
|
|
my %args = @_; |
39
|
0
|
|
|
|
|
|
my $template = $args{template}; |
40
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
my @parts = split(/\|/,$template); |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
my $template_type = shift @parts; |
44
|
0
|
|
|
|
|
|
$template_type =~ s/\s+/_/g; |
45
|
0
|
|
|
|
|
|
my $template_parsing_method = "template_$template_type"; |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
my $meta = {}; |
48
|
0
|
0
|
|
|
|
|
if ($self->can($template_parsing_method)) { |
|
|
0
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
$meta = $self->$template_parsing_method(@parts); |
50
|
|
|
|
|
|
|
} elsif (@parts) { |
51
|
0
|
|
|
|
|
|
$meta = { content => \@parts }; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
$meta->{template_type} = $template_type; |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
return $meta; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub template_ws_beginlist { |
60
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
61
|
0
|
|
|
|
|
|
return {meta => 'begin'}; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub template_ws_endlist { |
65
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
66
|
0
|
|
|
|
|
|
return {meta => 'end'}; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub template_ws_sense { |
71
|
0
|
|
|
0
|
0
|
|
my ($self, @parts) = @_; |
72
|
0
|
|
|
|
|
|
my %meta; |
73
|
0
|
|
|
|
|
|
for my $part (@parts) { |
74
|
0
|
|
|
|
|
|
push @{$meta{sense}}, $part; |
|
0
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
} |
76
|
0
|
|
|
|
|
|
return \%meta; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub template_ws { |
80
|
0
|
|
|
0
|
0
|
|
my ($self, @parts) = @_; |
81
|
0
|
|
|
|
|
|
my %meta; |
82
|
0
|
|
|
|
|
|
$meta{word} = $parts[0]; |
83
|
0
|
0
|
|
|
|
|
$meta{gloss} = $parts[-1] if scalar @parts > 1; |
84
|
0
|
|
|
|
|
|
return \%meta; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub template_l { |
88
|
0
|
|
|
0
|
0
|
|
my ($self, @parts) = @_; |
89
|
0
|
|
|
|
|
|
my %meta; |
90
|
0
|
|
|
|
|
|
$meta{lang} = $parts[0]; |
91
|
0
|
|
|
|
|
|
$meta{word} = $parts[1]; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |