| 1 | package Lingua::Treebank; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | ################################################################## | ||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 10 | require Exporter; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 10 | our @ISA = qw ( Exporter ) ; | |||||||||||||||||||||||||||
| 13 | 10 | our @EXPORT_OK = qw(); | |||||||||||||||||||||||||||
| 14 | 10 | our @EXPORT = qw(); | |||||||||||||||||||||||||||
| 15 | 10 | our $VERSION = '0.16'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 10 | our $MAX_WARN_TEXT = 100; | |||||||||||||||||||||||||||
| 18 | 10 | our $VERBOSE = 1; | |||||||||||||||||||||||||||
| 19 | ################################################################## | ||||||||||||||||||||||||||||
| 20 | 10 | ▶ | use Lingua::Treebank::Const; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 10 | our $CONST_CLASS = 'Lingua::Treebank::Const'; | |||||||||||||||||||||||||||
| 22 | ################################################################## | ||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub from_penn_file { | ||||||||||||||||||||||||||
|
Subroutine
from_penn_file: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 0 | my ($class, $file) = @_; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | open (my $fh, "<$file") or die "couldn't open $file: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 0 | my @results = $class->from_penn_fh($fh); | |||||||||||||||||||||||||||
| 28 | 0 | ▶ | close $fh or die "couldn't close $file: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | return @results; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | ################################################################## | ||||||||||||||||||||||||||||
| 33 | 2 | ▶ | sub from_penn_fh { | ||||||||||||||||||||||||||
|
Subroutine
from_penn_fh: called CC=9 SCAR=22.7
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 2 | my ($class, $fh) = @_; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 2 | my $rawTrees; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 2 | ▶ | if (not UNIVERSAL::isa($CONST_CLASS, 'Lingua::Treebank::Const')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | carp "CONST_CLASS value $CONST_CLASS", | |||||||||||||||||||||||||||
| 40 | " doesn't seem to be a subclass of Lingua::Treebank::Const\n"; | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | LINE: | ||||||||||||||||||||||||||||
| 44 | 2 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 28 | chomp; # remove newlines | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 28 | ▶ | if ( substr( $_, 0, 3 ) eq '*x*' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
substr($_, 0, 3) eq "*x*" or substr($_, 0, 10) eq "=========="
MC/DC50%1/2
substr($_, 0, 3) eq "*x*" or substr($_, 0, 10) eq "=========="
substr($_, 0, 3) eq "*x*"
substr($_, 0, 10) eq "=========="
Truth table66%2/3
A or B
A= substr($_, 0, 3) eq "*x*"B= substr($_, 0, 10) eq "=========="
| |||||||||||||||||||||||||||||
| 49 | or substr( $_, 0, 10 ) eq '=' x 10 ) | ||||||||||||||||||||||||||||
| 50 | { | ||||||||||||||||||||||||||||
| 51 | # skip header copyright comments, bar of ==== | ||||||||||||||||||||||||||||
| 52 | 24 | next LINE; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 4 | ▶ | next if /^\s*$/; # skip entirely blank lines | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # slurp in the rest of the merge file all at once | ||||||||||||||||||||||||||||
| 58 | 2 | local $/; | |||||||||||||||||||||||||||
| 59 | 2 | undef $/; | |||||||||||||||||||||||||||
| 60 | 2 | $rawTrees = $_ . (<$fh>); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | my (@utterances); | |||||||||||||||||||||||||||
| 65 | 2 | ▶ | while ($rawTrees) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 349 | $rawTrees =~ s/^\s+//; | |||||||||||||||||||||||||||
| 67 | 349 | my $token = Lingua::Treebank::Const->find_brackets($rawTrees); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 349 | ▶ | if (defined $token) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 349 | substr ($rawTrees, 0, length $token) = ''; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 349 | $rawTrees =~ s/^\s+//; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 349 | my $utt = $CONST_CLASS->new->from_penn_string($token); | |||||||||||||||||||||||||||
| 75 | 349 | ▶ | if (defined $utt) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 349 | push @utterances, $utt; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | else { | ||||||||||||||||||||||||||||
| 79 | 0 | carp "couldn't parse '", cite_warning($token), | |||||||||||||||||||||||||||
| 80 | "' remaining data '", cite_warning($rawTrees), | ||||||||||||||||||||||||||||
| 81 | "' in filehandle ignored"; | ||||||||||||||||||||||||||||
| 82 | 0 | last; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||||||||||
| 86 | # no token extractable | ||||||||||||||||||||||||||||
| 87 | 0 | carp "unrecognized data '", cite_warning($rawTrees), | |||||||||||||||||||||||||||
| 88 | "' remaining in filehandle ignored"; | ||||||||||||||||||||||||||||
| 89 | 0 | last; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 349 | $rawTrees =~ s/^\s*//; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 2 | return @utterances; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub from_cnf_file { | ||||||||||||||||||||||||||
|
Subroutine
from_cnf_file: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 0 | my ($class, $file) = @_; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | open (my $fh, "<$file") or die "couldn't open $file: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | my @root_nodes = $class->from_cnf_fh($fh); | |||||||||||||||||||||||||||
| 103 | 0 | ▶ | close $fh or die "couldn't close $file: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | return @root_nodes; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # BUGBUG Should share code with from_penn_fh | ||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub from_cnf_fh { | ||||||||||||||||||||||||||
|
Subroutine
from_cnf_fh: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 0 | my ($class, $fh) = @_; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | my @root_nodes; | |||||||||||||||||||||||||||
| 113 | LINE: | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | chomp; | |||||||||||||||||||||||||||
| 116 | 0 | s/#.*$//; # Remove comments | |||||||||||||||||||||||||||
| 117 | 0 | ▶ | next LINE if (/^\s*$/); # Skip empty lines. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | next LINE if (/^<s.*>$/); # Skip sentence annotation used by | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | # the Structured Language Model. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | NODE: | ||||||||||||||||||||||||||||
| 122 | 0 | ▶ | while (length $_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | my $text = Lingua::Treebank::Const->find_brackets($_); | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | if (length $text) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # Remove the matched constituent from the remaining | ||||||||||||||||||||||||||||
| 128 | # text. | ||||||||||||||||||||||||||||
| 129 | 0 | substr ($_, 0, length $text) = ''; | |||||||||||||||||||||||||||
| 130 | 0 | s/^\s+//; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # The bracketed text is a CNF treebank constituent. | ||||||||||||||||||||||||||||
| 133 | 0 | my Lingua::Treebank::Const $node = | |||||||||||||||||||||||||||
| 134 | Lingua::Treebank::Const->new->from_cnf_string($text); | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | if (not defined $node) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | warn "couldn't parse '$text', remaining data '$_; in line $.filehandle ignored"; | |||||||||||||||||||||||||||
| 138 | 0 | last NODE; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | push @root_nodes, $node; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | else { | ||||||||||||||||||||||||||||
| 144 | # No token extractable. | ||||||||||||||||||||||||||||
| 145 | 0 | warn "unrecognized data '$_', remaining in line $. ignored\n"; | |||||||||||||||||||||||||||
| 146 | 0 | last NODE; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | return @root_nodes; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | ################################################################## | ||||||||||||||||||||||||||||
| 158 | 0 | ▶ | sub cite_warning { | ||||||||||||||||||||||||||
|
Subroutine
cite_warning: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 159 | 0 | my $text = shift; | |||||||||||||||||||||||||||
| 160 | 0 | my $warning; | |||||||||||||||||||||||||||
| 161 | 0 | ▶ | if (length $text > $MAX_WARN_TEXT) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | $warning = | |||||||||||||||||||||||||||
| 163 | substr($text, 0, $MAX_WARN_TEXT / 2); | ||||||||||||||||||||||||||||
| 164 | 0 | $warning .= ' [ ... OMITTED ... ] '; | |||||||||||||||||||||||||||
| 165 | 0 | $warning .= | |||||||||||||||||||||||||||
| 166 | substr($text, -($MAX_WARN_TEXT / 2) ); | ||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | else { | ||||||||||||||||||||||||||||
| 169 | 0 | $warning = $text; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | 0 | return $warning; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | ################################################################## | ||||||||||||||||||||||||||||
| 174 | 10 | 1; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | __END__ | ||||||||||||||||||||||||||||