DiaColloDB/Document/TEI.pm untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 152 CC 20 SCAR 54.7
CC
20
Cov
0%
fromFile
342.0
nodval
12.0
new
2.0
CRAP 238.5
1 ## -*- Mode: CPerl -*-
2 ## File: DiaColloDB::Document::TEI.pm
3 ## Author: Bryan Jurish <moocow@cpan.org>
4 ## Description: collocation db, source document, TEI-likie format (w,p,l, #s,#p)
5  
6 package DiaColloDB::Document::TEI;
7 use DiaColloDB::Document;
8 use XML::LibXML; ##-- require v1.70 for load_xml() method
9 use strict;
10  
11 ##==============================================================================
12 ## Globals & Constants
13  
14 our @ISA = qw(DiaColloDB::Document);
15  
16 ##==============================================================================
17 ## Constructors etc.
18  
19 ## $doc = CLASS_OR_OBJECT->new(%args)
20 ## + %args, object structure:
21 ## (
22 ## ##-- parsing options
23 ## tei_ns_NS => $uri, ##-- namespace URI for user-defined XPaths
24 ## tei_date => $xpath, ##-- XPath for parsing document date, relative to document root
25 ## tei_meta_ATTR => $xpath, ##-- XPath for parsing meta-attribute ATTR, relative to document root
26 ## tei_word_ATTR => $xpath, ##-- XPath for parsing token-attribute ATTR, relative to //w element
27 ## tei_break_BRK => $xpath, ##-- XPath for parsing break nodes, relative to document root
28 ## tei_eos => $break, ##-- default break-level (default: 's')
29 ## ##
30 ## ##-- document data
31 ## date =>$date, ##-- year
32 ## tokens =>\@tokens, ##-- tokens, including undef for EOS
33 ## meta =>\%meta, ##-- document metadata (e.g. author, title, collection, ...)
34 ## ## + parsed from /D-Spin/MetaData/source[@type] for $type !~ /^meta:ATTR/
35 ## )
36 ## + default namespaces:
37 ## (
38 ## tei_ns_tei => "http://www.tei-c.org/ns/1.0",
39 ## )
40 ## + default XPaths:
41 ## (
42 ## tei_date => 'teiHeader/fileDesc/publicationStmt/date'
43 ## tei_meta_title => 'teiHeader/fileDesc/titleStmt/title',
44 ## tei_meta_author => 'teiHeader/fileDesc/titleStmt/author',
45 ## tei_meta_textClass => 'teiHeader/fileDesc/profileDesc/textClass/classCode',
46 ## tei_word_w => 'text()',
47 ## tei_word_l => '@lemma',
48 ## tei_word_p => '@type',
49 ## tei_break_s => '//text//s',
50 ## tei_break_p => '//text//p',
51 ## tei_break_div => '//text//div',
52 ## tei_break_page => '//text//pb',
53 ## )
54 sub new {
55 my $that = shift;
56 my $doc = $that->SUPER::new(
57 ##-- default xpaths
58 tei_ns_tei => "http://www.tei-c.org/ns/1.0",
59 tei_date => '*[local-name()="teiHeader"]/*[local-name()="fileDesc"]/*[local-name()="publicationStmt"]/*[local-name()="date"]',
60 tei_meta_title => '*[local-name()="teiHeader"]/*[local-name()="fileDesc"]/*[local-name()="titleStmt"]/*[local-name()="title"]',
61 tei_meta_author => '*[local-name()="teiHeader"]/*[local-name()="fileDesc"]/*[local-name()="titleStmt"]/*[local-name()="author"]',
62 tei_meta_textClass => '*[local-name()="teiHeader"]/*[local-name()="fileDesc"]/*[local-name()="profileDesc"]/*[local-name()="textClass"]/*[local-name()="classCode"]',
63 ##
64 tei_word_w => 'text()',
65 tei_word_p => '@type',
66 tei_word_l => '@lemma',
67 ##
68 tei_break_s => '//*[local-name()="text"]//*[local-name()="s"]',
69 tei_break_p => '//*[local-name()="text"]//*[local-name()="p"]',
70 tei_break_div => '//*[local-name()="text"]//*[local-name()="div"]',
71 tei_break_page => '//*[local-name()="text"]//*[local-name()="pb"][1]',
72 ##
73 tei_eos => 's',
74  
75 ##-- user arguments
76 @_,
77 );
78 return $doc;
79 }
80  
81 ##==============================================================================
82 ## API: I/O
83  
84  
85 ## $ext = $doc->extension()
86 ## + default extension, for Corpus::Compiled
87 sub extension {
88 return '.tei';
89 }
90  
91 ##--------------------------------------------------------------
92 ## API: I/O: parse
93  
94 ## $bool = $doc->fromFile($filename_or_fh, %opts)
95 ## + parse tokens from $filename_or_fh
96 ## + %opts : clobbers %$doc
97 sub fromFile {
98 my ($doc,$file,%opts) = @_;
99 $doc = $doc->new() if (!ref($doc));
100 @$doc{keys %opts} = values %opts;
101 $doc->{label} = ref($file) ? "$file" : $file;
102  
103 ##-- setup xpaths
104 my (%xp_meta,%xp_word,%xp_break);
105 my $xpc = XML::LibXML::XPathContext->new;
106 foreach (keys %$doc) {
107 if (/^tei_meta_(.*)$/) {
108 $xp_meta{$1} = $doc->{$_};
109 }
110 elsif (/^tei_word_(.*)$/) {
111 $xp_word{$1} = $doc->{$_};
112 }
113 elsif (/^tei_break_(.*)$/) {
114 $xp_break{$1} = $doc->{$_};
115 }
116 elsif (/^tei_ns_(.*)$/) {
117 $xpc->registerNs($1, $doc->{$_}) if ($doc->{$_});
118 }
119 }
120 delete @xp_meta{grep {!$xp_meta{$_}} keys %xp_meta};
121 delete @xp_word{grep {!$xp_word{$_}} keys %xp_word};
122 delete @xp_break{grep {!$xp_break{$_}} keys %xp_break};
123  
124 ##-- parse xml document
125 my $xdoc = XML::LibXML->load_xml(location => $file)
126 or $doc->logconfess("fromFile(): cannot load file '$file' as XML document: $!");
127  
128 my $tokens = $doc->{tokens};
129 @$tokens = qw();
130 my $meta = $doc->{meta};
131 %$meta = qw();
132  
133 ##-- parse: basic
134 my $xroot = $xdoc->documentElement;
135  
136 ##-- parse: metadata
137 my ($akey,$xp,$nods);
138 while (($akey,$xp) = each %xp_meta) {
139 next if (!defined($nods = $xpc->findnodes($xp, $xroot)));
140 $meta->{$akey} = join(':', map {nodval($_)} @$nods) if (@$nods);
141 }
142 ##-- parse: date (integer values only)
143 $doc->{date} = nodval($xpc->findnodes($doc->{tei_date}, $xroot));
144 ($doc->{date} //= $meta->{date} // $meta->{date_} // 0) =~ s/^[^0-9]*([0-9]+)[^0-9].*$//;
145  
146 ##-- parse: tokens
147 my %key2w = qw(); ## $key => \%w ; additional keys "n_"=>$w_pos
148 my ($wnod,$anod,$w);
149 my $nw = 0;
150 foreach $wnod ($xpc->findnodes('//*[local-name()="text"]//*[local-name()="w"]',$xroot)) {
151 $w = $key2w{$wnod->unique_key} = { n_=>$nw++ };
152 while (($akey,$xp) = each %xp_word) {
153 next if (!defined($nods = $xpc->findnodes($xp, $wnod)));
154 $w->{$akey} = join(':', map {nodval($_)} @$nods) if (@$nods);
155 }
156 }
157  
158 ##-- parse: breaks; setup $w->{break_before_} = {$brk=>undef,...}; %break_final = ($brk=>undef,...)
159 my ($bkey,$bnod);
160 my (%break_final);
161 while (($bkey,$xp) = each %xp_break) {
162 foreach $bnod ($xpc->findnodes($xp,$xroot)) {
163 if (defined($wnod = $xpc->findnodes('descendant::*[local-name()="w"][1]',$bnod)->[0])) {
164 $w->{break_before_}{$bkey} = undef if (defined($w=$key2w{$wnod->unique_key}));
165 }
166 if (defined($wnod = $xpc->findnodes('following::*[local-name()="w"][1]',$bnod)->[0])) {
167 $w->{break_before_}{$bkey} = undef if (defined($w=$key2w{$wnod->unique_key}));
168 } else {
169 $break_final{$bkey} = undef;
170 }
171 }
172 }
173  
174 ##-- construct: tokens
175 my $eos = $doc->{tei_eos};
176 push(@$tokens,'#file'); ##-- always include 'file' break
177 foreach $w (sort {$a->{n_} <=> $b->{n_}} values %key2w) {
178 if ($w->{break_before_}) {
179 push(@$tokens,
180 ($eos && exists($w->{break_before_}{$eos}) ? undef : qw()),
181 (map {"#$_"} sort keys %{$w->{break_before_}}),
182 );
183 }
184 delete @$w{qw(break_before_ n_)};
185 push(@$tokens,$w);
186 }
187 push(@$tokens,
188 (map {"#$_"} sort keys %break_final),
189 undef, ##-- always push final eos
190 );
191  
192 return $doc;
193 }
194  
195 ##==============================================================================
196 ## XPath utilities
197  
198 ## $val = nodval($nod)
199 sub nodval {
200 return undef if (!defined($_[0]));
201 return UNIVERSAL::isa($_[0],'XML::LibXML::Attribute') ? $_[0]->nodeValue : $_[0]->textContent;
202 }
203  
204 ##==============================================================================
205 ## Footer
206 1;
207  
208 __END__