| 1 | ## -*- Mode: CPerl -*- | ||
| 2 | ## File: DiaColloDB::Document::TCF.pm | ||
| 3 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 4 | ## Description: collocation db, source document, TCF format (tokens, sentences, postags, lemmas) | ||
| 5 | |||
| 6 | package DiaColloDB::Document::TCF; | ||
| 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 | ## #(none) | ||
| 24 | ## ## | ||
| 25 | ## ##-- document data | ||
| 26 | ## date =>$date, ##-- year | ||
| 27 | ## tokens =>\@tokens, ##-- tokens, including undef for EOS | ||
| 28 | ## meta =>\%meta, ##-- document metadata (e.g. author, title, collection, ...) | ||
| 29 | ## ## + parsed from /D-Spin/MetaData/source[@type] for $type !~ /^meta:ATTR/ | ||
| 30 | ## ) | ||
| 31 | ## + namespaces: | ||
| 32 | ## dspin : http://www.dspin.de/data | ||
| 33 | ## meta : http://www.dspin.de/data/metadata | ||
| 34 | ## + each token in @tokens is a HASH-ref {w=>$word,p=>$pos,l=>$lemma,...} | ||
| 35 | sub new { | ||
| 36 | my $that = shift; | ||
| 37 | my $doc = $that->SUPER::new( | ||
| 38 | @_, ##-- user arguments | ||
| 39 | ); | ||
| 40 | return $doc; | ||
| 41 | } | ||
| 42 | |||
| 43 | ##============================================================================== | ||
| 44 | ## API: I/O | ||
| 45 | |||
| 46 | |||
| 47 | ## $ext = $doc->extension() | ||
| 48 | ## + default extension, for Corpus::Compiled | ||
| 49 | sub extension { | ||
| 50 | return '.tcf'; | ||
| 51 | } | ||
| 52 | |||
| 53 | ##-------------------------------------------------------------- | ||
| 54 | ## API: I/O: parse | ||
| 55 | |||
| 56 | ## $bool = $doc->fromFile($filename_or_fh, %opts) | ||
| 57 | ## + parse tokens from $filename_or_fh | ||
| 58 | ## + %opts : clobbers %$doc | ||
| 59 | sub fromFile { | ||
| 60 | my ($doc,$file,%opts) = @_; | ||
| 61 | $doc = $doc->new() if (!ref($doc)); | ||
| 62 | @$doc{keys %opts} = values %opts; | ||
| 63 | $doc->{label} = ref($file) ? "$file" : $file; | ||
| 64 | |||
| 65 | my $xdoc = XML::LibXML->load_xml(location => $file) | ||
| 66 | or $doc->logconfess("fromFile(): cannot load file '$file' as XML document: $!"); | ||
| 67 | |||
| 68 | my $tokens = $doc->{tokens}; | ||
| 69 | @$tokens = qw(); | ||
| 70 | my $meta = $doc->{meta}; | ||
| 71 | %$meta = qw(); | ||
| 72 | my $eos = undef; | ||
| 73 | |||
| 74 | ##-- parse: basic | ||
| 75 | my $xroot = $xdoc->documentElement; | ||
| 76 | |||
| 77 | ##-- parse: metadata | ||
| 78 | if (defined(my $xmeta = [$xroot->getChildrenByLocalName("MetaData")]->[0])) { | ||
| 79 | ##-- parse: metadata: : /*/MetaData/source[@type="meta:KEY"] | ||
| 80 | my ($type); | ||
| 81 | foreach (@{$xmeta->getChildrenByLocalName("source")}) { | ||
| 82 | $type = $_->getAttribute('type'); | ||
| 83 | $meta->{$1} = $_->textContent if ($type && $type =~ /^meta:(.*)$/); | ||
| 84 | } | ||
| 85 | |||
| 86 | ##-- parse: metadata: CMDI header | ||
| 87 | my ($xhdr); | ||
| 88 | if (defined($xhdr = $xmeta->findnodes('./*[local-name()="source"]/*[local-name()="CMD"]/*[local-name()="Header"]')->[0])) { | ||
| 89 | foreach (grep {UNIVERSAL::isa($_,'XML::LibXML::Element')} @{$xhdr->childNodes}) { | ||
| 90 | $type = $_->nodeName; | ||
| 91 | $meta->{"$type"} = $_->textContent; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | ##-- parse: metadata: embedded TEI header (DTA-style) | ||
| 96 | my ($nod); | ||
| 97 | if (defined($xhdr = $xmeta->findnodes('.//*[local-name()="teiHeader"]')->[0])) { | ||
| 98 | ##-- tei header: date: teiHeader/fileDesc/sourceDesc/biblFull/publicationStmt/date | ||
| 99 | if (defined($nod=$xmeta->findnodes('.//*[local-name()="sourceDesc"]//*[local-name()="date"]')->[0])) { | ||
| 100 | $meta->{date} = $nod->textContent; | ||
| 101 | } | ||
| 102 | |||
| 103 | ##-- tei header: author | ||
| 104 | if (defined($nod=$xmeta->findnodes('.//*[local-name()="author"]')->[0])) { | ||
| 105 | $meta->{author} = $nod->textContent; | ||
| 106 | } | ||
| 107 | |||
| 108 | ##-- tei header: title | ||
| 109 | if (defined($nod=$xmeta->findnodes('.//*[local-name()="title" and level="a" and @type="main"]')->[0]) | ||
| 110 | || defined($nod=$xmeta->findnodes('.//*[local-name()="title" and @type="main"]')->[0]) | ||
| 111 | || defined($nod=$xmeta->findnodes('.//*[local-name()="title"]')->[0])) { | ||
| 112 | $meta->{title} = $nod->textContent; | ||
| 113 | } | ||
| 114 | |||
| 115 | ##-- tei header: bibl | ||
| 116 | if (defined($nod=$xmeta->findnodes('.//*[local-name()="sourceDesc"]/*[local-name()="bibl"]')->[0])) { | ||
| 117 | $meta->{bibl} = $nod->textContent; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | ##-- metadata: trim | ||
| 123 | foreach (values %$meta) { | ||
| 124 | s/^\s+//; | ||
| 125 | s/\s+$//; | ||
| 126 | s/\s+/ /g; | ||
| 127 | } | ||
| 128 | |||
| 129 | |||
| 130 | ##-- parse: date | ||
| 131 | ($doc->{date} = $meta->{date} // $meta->{date_} // 0) =~ s/^[^0-9]*([0-9]+)[^0-9].*$//; | ||
| 132 | |||
| 133 | ##-- parse: corpus | ||
| 134 | my $xcorpus = [$xroot->getChildrenByLocalName('TextCorpus')]->[0] | ||
| 135 | or $doc->logconfess("fromFile(): no <TextCorpus> node found in XML document for file '$file'"); | ||
| 136 | |||
| 137 | ##-- parse: tokens -> w | ||
| 138 | my ($w,@w,%id2w); | ||
| 139 | my $xtokens = [$xcorpus->getChildrenByLocalName('tokens')]->[0] | ||
| 140 | or $doc->logconfess("fromFile(): no <tokens> node found in XML document for file '$file'"); | ||
| 141 | foreach ($xtokens->getChildrenByLocalName('token')) { | ||
| 142 | push(@w, $w={w=>$_->textContent, id=>$_->getAttribute('ID')}); | ||
| 143 | $id2w{$w->{id}} = $w if (defined($w->{id})); | ||
| 144 | } | ||
| 145 | |||
| 146 | ##-- parse: sentences | ||
| 147 | @$tokens = ("#file"); ##-- always include '#file' break | ||
| 148 | if (defined(my $xsents = [$xcorpus->getChildrenByLocalName('sentences')]->[0])) { | ||
| 149 | foreach ($xsents->getChildrenByLocalName('sentence')) { | ||
| 150 | push(@$tokens, "#s", @id2w{split(' ',$_->getAttribute('tokenIDs'))}, undef); | ||
| 151 | } | ||
| 152 | } else { | ||
| 153 | @$tokens = @w; | ||
| 154 | push(@$tokens,undef); | ||
| 155 | } | ||
| 156 | |||
| 157 | ##-- parse: POStags -> p | ||
| 158 | my ($id); | ||
| 159 | if (defined(my $xpostags = [$xcorpus->getChildrenByLocalName('POStags')]->[0])) { | ||
| 160 | foreach ($xpostags->getChildrenByLocalName('tag')) { | ||
| 161 | ($id = $_->getAttribute('tokenIDs')) =~ s/\s.*$//; | ||
| 162 | $id2w{$id}{p} = $_->textContent; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | ##-- parse: lemmas -> l | ||
| 167 | if (defined(my $xlemmas = [$xcorpus->getChildrenByLocalName('lemmas')]->[0])) { | ||
| 168 | foreach ($xlemmas->getChildrenByLocalName('lemma')) { | ||
| 169 | ($id = $_->getAttribute('tokenIDs')) =~ s/\s.*$//; | ||
| 170 | $id2w{$id}{l} = $_->textContent; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | return $doc; | ||
| 175 | } | ||
| 176 | |||
| 177 | ##============================================================================== | ||
| 178 | ## Footer | ||
| 179 | 1; | ||
| 180 | |||
| 181 | __END__ |