| 1 | ## -*- Mode: CPerl -*- | ||
| 2 | ## | ||
| 3 | ## File: DTA::CAB::Format::SynCoPe::CSV.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: Datum parser: SynCoPe CSV (for NE-recognizer) | ||
| 6 | |||
| 7 | package DTA::CAB::Format::SynCoPe::CSV; | ||
| 8 | use DTA::CAB::Format; | ||
| 9 | use DTA::CAB::Format::TT; | ||
| 10 | use DTA::CAB::Datum ':all'; | ||
| 11 | use IO::File; | ||
| 12 | use Carp; | ||
| 13 | use strict; | ||
| 14 | |||
| 15 | ##============================================================================== | ||
| 16 | ## Globals | ||
| 17 | ##============================================================================== | ||
| 18 | |||
| 19 | our @ISA = qw(DTA::CAB::Format::TT); | ||
| 20 | |||
| 21 | BEGIN { | ||
| 22 | DTA::CAB::Format->registerFormat(name=>__PACKAGE__, short=>'syncope-csv', filenameRegex=>qr/\.(?i:syn(?:cope)?[-\.](?:csv|tsv|tab)|)$/); | ||
| 23 | DTA::CAB::Format->registerFormat(name=>__PACKAGE__, short=>'syncope-tab'); | ||
| 24 | DTA::CAB::Format->registerFormat(name=>__PACKAGE__, short=>'syn-csv'); | ||
| 25 | DTA::CAB::Format->registerFormat(name=>__PACKAGE__, short=>'syn-tab'); | ||
| 26 | } | ||
| 27 | |||
| 28 | ##============================================================================== | ||
| 29 | ## Constructors etc. | ||
| 30 | ##============================================================================== | ||
| 31 | |||
| 32 | ## $fmt = CLASS_OR_OBJ->new(%args) | ||
| 33 | ## + object structure: assumed HASH | ||
| 34 | ## ( | ||
| 35 | ## ##---- Input | ||
| 36 | ## doc => $doc, ##-- buffered input document | ||
| 37 | ## | ||
| 38 | ## ##---- Output | ||
| 39 | ## syntags => $bool, ##-- output POS-tags as 1st field? (default: true; for SynCoPe NER >= 2013-12-09) | ||
| 40 | ## #level => $formatLevel, ##-- output formatting level: n/a | ||
| 41 | ## #outbuf => $stringBuffer, ##-- buffered output | ||
| 42 | ## outbase => $outputBasename, ##-- for use by syncope (default: $inputDoc->{base}) | ||
| 43 | ## | ||
| 44 | ## ##---- Common | ||
| 45 | ## utf8 => $bool, ##-- default: 1 | ||
| 46 | ## ) | ||
| 47 | sub new { | ||
| 48 | my $that = shift; | ||
| 49 | return $that->SUPER::new(syntags=>1, @_); | ||
| 50 | } | ||
| 51 | |||
| 52 | ##============================================================================== | ||
| 53 | ## Methods: Persistence | ||
| 54 | ##============================================================================== | ||
| 55 | |||
| 56 | ##============================================================================== | ||
| 57 | ## Methods: Input | ||
| 58 | ##============================================================================== | ||
| 59 | |||
| 60 | ##-------------------------------------------------------------- | ||
| 61 | ## Methods: Input: Local | ||
| 62 | |||
| 63 | ## $fmt = $fmt->parseCsvString(\$string) | ||
| 64 | BEGIN { *parseTTString = \&parseCsvString; } | ||
| 65 | sub parseCsvString { | ||
| 66 | my ($fmt,$src) = @_; | ||
| 67 | no warnings 'uninitialized'; | ||
| 68 | $$src =~ s{^(\S+).*\n}{%% base=$1\n}; | ||
| 69 | $$src =~ s{^normal$}{}mg; | ||
| 70 | if ($fmt->{syntags}) { | ||
| 71 | $$src =~ s{^([^\t]+)\t([^\t]*)\t([^\t]*)(?:\t([^\t]*))?(?:\t([^\t]*))?$}{$1\t[syncope_tag] $2\t[syncope_type] $3\t[syncope_loc] $4}mg; | ||
| 72 | } else { | ||
| 73 | $$src =~ s{^([^\t]+)\t([^\t]*)(?:\t([^\t]*))?(?:\t([^\t]*))?$}{$1\t[syncope_type] $2\t[syncope_loc] $3}mg; | ||
| 74 | } | ||
| 75 | return DTA::CAB::Format::TT::parseTTString($fmt,$src); | ||
| 76 | } | ||
| 77 | |||
| 78 | ##============================================================================== | ||
| 79 | ## Methods: Output | ||
| 80 | ##============================================================================== | ||
| 81 | |||
| 82 | ##-------------------------------------------------------------- | ||
| 83 | ## Methods: Output: Generic | ||
| 84 | |||
| 85 | ## $ext = $fmt->defaultExtension() | ||
| 86 | ## + returns default filename extension for this format | ||
| 87 | sub defaultExtension { return '.syncope-csv'; } | ||
| 88 | |||
| 89 | ##-------------------------------------------------------------- | ||
| 90 | ## Methods: Output: put Document | ||
| 91 | |||
| 92 | ## $fmt = $fmt->putDocument($doc) | ||
| 93 | ## $fmt = $fmt->putDocument($doc,\$buf) | ||
| 94 | ## + concatenates formatted sentences, adding document 'xmlbase' comment if available | ||
| 95 | ## + \$bufr is ignored here. | ||
| 96 | sub putDocument { | ||
| 97 | my ($fmt,$doc) = @_; | ||
| 98 | my $fh = $fmt->{fh}; | ||
| 99 | my $docname = $fmt->{outbase}||$doc->{base}||ref($doc)||$doc; | ||
| 100 | my $syntags = $fmt->{syntags}; | ||
| 101 | $fh->print("$docname\n"); | ||
| 102 | my ($si,$s,$wi,$sid,$wid,$w,$txt,$tag,$typ); | ||
| 103 | foreach $si (0..$#{$doc->{body}}) { | ||
| 104 | $s = $doc->{body}[$si]; | ||
| 105 | $sid = $s->{id}||''; | ||
| 106 | $fh->print("normal\n"); | ||
| 107 | foreach $wi (0..$#{$s->{tokens}}) { | ||
| 108 | $w = $s->{tokens}[$wi]; | ||
| 109 | $wid = ($w->{id}||''); | ||
| 110 | $txt = $w->{moot} ? $w->{moot}{word} : ($w->{xlit} ? $w->{xlit}{latin1Text} : $w->{text}); | ||
| 111 | |||
| 112 | if (defined($typ=$w->{syncope_type}) && $typ ne '') { ; } ##-- re-use stored type | ||
| 113 | elsif ($txt =~ /^[[:upper:]]+$/) { $typ = 'UPPERCASE '.(length($txt)==1 ? 'LETTER' : 'WORD'); } | ||
| 114 | elsif ($txt =~ /^[[:lower:]]+$/) { $typ = 'LOWERCASE WORD'; } | ||
| 115 | elsif ($txt =~ /^[[:punct:]][[:lower:]]+$/) { $typ = 'LOWERCASE WORD'; } ##-- 2013-02-22: "'s" should be LOWERCASE_WORD since python islower("'")=>1 | ||
| 116 | elsif ($txt =~ /^[[:upper:]]/) { $typ = 'CAPITALIZED WORD'; } | ||
| 117 | elsif ($txt =~ /^[[:digit:]]+\.?$/) { $typ = 'DIGIT'; } | ||
| 118 | elsif ($txt eq '-') { $typ = 'HYPHEN-MINUS'; } | ||
| 119 | elsif ($txt eq '.') { $typ = 'FULL STOP'; } | ||
| 120 | elsif ($txt eq ',') { $typ = 'COMMA'; } | ||
| 121 | elsif ($txt eq ':') { $typ = 'COLON'; } | ||
| 122 | elsif ($txt =~ /^[\"\']$/) { $typ = 'QUOTATION MARK'; } | ||
| 123 | elsif ($txt eq '!') { $typ = 'EXCLAMATION MARK'; } | ||
| 124 | elsif ($txt eq '&') { $typ = 'AMPERSAND'; } | ||
| 125 | elsif ($txt eq '?') { $typ = 'QUESTION MARK'; } | ||
| 126 | elsif ($txt eq '/') { $typ = 'SOLIDUS'; } | ||
| 127 | else { $typ = 'SYMBOL'; } | ||
| 128 | |||
| 129 | $tag = $w->{syncope_tag} // ($w->{moot} ? $w->{moot}{tag} : 'NOTAG') if ($syntags); | ||
| 130 | |||
| 131 | $fh->print(join("\t", $txt, ($syntags ? $tag : qw()), $typ, join(' ',$si,$wi,$sid,$wid)), "\n"); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | return $fmt; | ||
| 135 | } | ||
| 136 | |||
| 137 | |||
| 138 | 1; ##-- be happy | ||
| 139 | |||
| 140 | __END__ |