1 package Text::Tradition::Parser::JSON;
5 use JSON qw/ from_json /;
9 Text::Tradition::Parser::JSON
15 my $tradition = Text::Tradition->new(
18 'string' => $json_encoded_utf8,
23 Parser module for Text::Tradition to read a JSON alignment table format such
24 as that produced by CollateX.
28 =head2 B<parse>( $tradition, $option_hash )
30 Takes an initialized tradition and a set of options; creates the
31 appropriate nodes and edges on the graph, as well as the appropriate
32 witness objects. The $option_hash must contain either a 'file' or a
33 'string' argument with the JSON structure to be parsed.
35 The structure of the JSON is thus:
37 { alignment => [ { witness => "SIGIL",
38 tokens => [ { t => "TEXT" }, ... ] },
40 tokens => [ { t => "TEXT" }, ... ] },
45 Longer lacunae in the text, to be disregarded in cladistic analysis, may be
46 represented with the meta-reading '#LACUNA#'. Multiple lacuna tags in sequence
47 are collapsed into a single multi-reading lacuna.
49 If a witness name ends in the collation's ac_label, it will be treated as
50 an extra layer of the 'main' witness whose sigil it shares.
55 binmode STDOUT, ":utf8";
56 binmode STDERR, ":utf8";
57 eval { no warnings; binmode $DB::OUT, ":utf8"; };
59 use_ok( 'Text::Tradition::Parser::JSON' );
61 open( JSFILE, 't/data/cx16.json' );
62 binmode JSFILE, ':utf8';
66 my $t = Text::Tradition->new(
69 'string' => join( '', @lines ),
72 is( ref( $t ), 'Text::Tradition', "Parsed a JSON alignment" );
74 is( scalar $t->collation->readings, 26, "Collation has all readings" );
75 is( scalar $t->collation->paths, 32, "Collation has all paths" );
76 is( scalar $t->witnesses, 3, "Collation has all witnesses" );
80 map { $seen_wits{$_} = 0 } qw/ A B C /;
81 # Check that we have the right witnesses
82 foreach my $wit ( $t->witnesses ) {
83 $seen_wits{$wit->sigil} = 1;
85 is( scalar keys %seen_wits, 3, "No extra witnesses were made" );
86 foreach my $k ( keys %seen_wits ) {
87 ok( $seen_wits{$k}, "Witness $k still exists" );
90 # Check that the witnesses have the right texts
91 foreach my $wit ( $t->witnesses ) {
92 my $origtext = join( ' ', @{$wit->text} );
93 my $graphtext = $t->collation->path_text( $wit->sigil );
94 is( $graphtext, $origtext, "Collation matches original for witness " . $wit->sigil );
102 my( $tradition, $opts ) = @_;
103 my $c = $tradition->collation;
105 my $table = from_json( $opts->{'string'} );
107 # Create the witnesses
108 my @witnesses; # Keep the ordered list of our witnesses
109 my %ac_wits; # Track these for later removal
110 foreach my $sigil ( map { $_->{'witness'} } @{$table->{'alignment'}} ) {
111 my $wit = $tradition->add_witness(
112 'sigil' => $sigil, 'sourcetype' => 'collation' );
113 $wit->path( [ $c->start ] );
114 push( @witnesses, $wit );
115 my $aclabel = $c->ac_label;
116 if( $sigil =~ /^(.*)\Q$aclabel\E$/ ) {
117 $ac_wits{$sigil} = $1;
121 # Save the original witness text for consistency checking. We do this
122 # in a separate loop to make sure we have all base witnesses defined,
123 # and to make sure that our munging and comparing later doesn't affect
125 foreach my $intext ( @{$table->{'alignment'}} ) {
126 my $rs = $intext->{'witness'};
127 my $is_layer = exists $ac_wits{$rs};
128 my $wit = $tradition->witness( $is_layer ? $ac_wits{$rs} : $rs );
129 my @tokens = grep { $_ && $_->{'t'} !~ /^\#.*\#$/ } @{$intext->{'tokens'}};
130 my @words = map { _restore_punct( $_ ) } @tokens;
131 $is_layer ? $wit->layertext( \@words ) : $wit->text( \@words );
134 # Create the readings in each row
135 my $length = exists $table->{'length'}
137 : scalar @{$table->{'alignment'}->[0]->{'tokens'}};
139 foreach my $idx ( 0 .. $length - 1 ) {
140 my @tokens = map { $_->{'tokens'}->[$idx] } @{$table->{'alignment'}};
141 my @readings = make_nodes( $c, $idx, @tokens );
142 foreach my $w ( 0 .. $#readings ) {
143 # push the appropriate node onto the appropriate witness path
144 my $rdg = $readings[$w];
146 my $wit = $witnesses[$w];
147 push( @{$wit->path}, $rdg );
148 } # else skip it for empty readings.
152 # Collapse our lacunae into a single node and
153 # push the end node onto all paths.
154 $c->end->rank( $length );
155 foreach my $wit ( @witnesses ) {
157 my $last_rdg = shift @$p;
158 my $new_p = [ $last_rdg ];
159 foreach my $rdg ( @$p ) {
160 # Omit the reading if we are in a lacuna already.
161 next if $rdg->is_lacuna && $last_rdg->is_lacuna;
162 # Save the reading otherwise.
163 push( @$new_p, $rdg );
166 push( @$new_p, $c->end );
167 $wit->path( $new_p );
170 # Fold any a.c. witnesses into their main witness objects, and
171 # delete the independent a.c. versions.
172 foreach my $a ( keys %ac_wits ) {
173 my $ac_wit = $tradition->witness( $a );
174 my $main_wit = $tradition->witness( $ac_wits{$a} );
175 next unless $main_wit;
176 $main_wit->uncorrected_path( $ac_wit->path );
177 $tradition->del_witness( $ac_wit );
181 $c->make_witness_paths;
182 # Delete our unused lacuna nodes.
183 foreach my $rdg ( grep { $_->is_lacuna } $c->readings ) {
184 $c->del_reading( $rdg ) unless $c->reading_witnesses( $rdg );
187 # Note that our ranks and common readings are set.
188 $c->_graphcalc_done(1);
191 =head2 make_nodes( $collation, $index, @tokenlist )
193 Create readings from the unique tokens in @tokenlist, and set their rank to
194 $index. Returns an array of readings of the same size as the original @tokenlist.
199 my( $c, $idx, @tokens ) = @_;
203 foreach my $j ( 0 .. $#tokens ) {
205 my $word = _restore_punct( $tokens[$j] );
207 if( exists( $unique{$word} ) ) {
208 $rdg = $unique{$word};
210 my %args = ( 'id' => 'r' . join( '.', $idx, $j+1 ),
214 if( $word eq '#LACUNA#' ) {
215 $args{'is_lacuna'} = 1
219 $rdg = Text::Tradition::Collation::Reading->new( %args );
220 $unique{$word} = $rdg;
222 push( @readings, $rdg );
225 push( @readings, undef );
228 if( $commonctr == 1 ) {
229 # Whichever reading isn't a lacuna is a common node.
230 foreach my $rdg ( values %unique ) {
231 next if $rdg->is_lacuna;
232 $rdg->is_common( 1 );
235 map { $c->add_reading( $_ ) } values( %unique );
239 # Utility function for parsing JSON from nCritic
242 my $word = $token->{'t'};
243 return $word unless exists $token->{'punctuation'};
244 foreach my $p ( sort { $a->{pos} <=> $b->{pos} } @{$token->{'punctuation'}} ) {
245 substr( $word, $p->{pos}, 0, $p->{char} );
254 This package is free software and is provided "as is" without express
255 or implied warranty. You can redistribute it and/or modify it under
256 the same terms as Perl itself.
260 Tara L Andrews E<lt>aurum@cpan.orgE<gt>