6 use lib '/home/tla/cpanmods/Text-SenseClusters-1.03/lib';
7 use Text::SenseClusters::Simat;
9 use Text::WagnerFischer qw/distance/;
11 binmode STDOUT, ':utf8';
12 binmode STDERR, ':utf8';
15 my( $traditionfile, $threshold ) = @ARGV;
16 $threshold = 0.99 unless $threshold;
22 $t = Text::Tradition->new(
23 file => $traditionfile,
25 say STDERR "Parsed tradition file";
26 my $c = $t->collation;
28 # Get the cosine similarity values
29 my ( $matrix ) = make_matrix( $t );
31 # For each relationship in the graph, see how it compares to other node pairs
34 foreach my $pair ( $c->relationships ) {
35 my $rel = $c->get_relationship( $pair );
36 my( $rx, $ry ) = map { $c->reading( $_ ) } sort @$pair;
38 say STDERR "Checking relationship $rx -- $ry, of type " . $rel->type;
41 foreach my $val ( sort { $a<=>$b } keys %{$matrix->{"$rx"}->{"$ry"}} ) {
43 foreach my $mpair ( @{$matrix->{"$rx"}->{"$ry"}->{"$val"}} ) {
44 my $mrel = $c->get_relationship( $mpair );
45 my $mreltype = $mrel ? $mrel->type : '(no relation)';
46 my( $mx, $my ) = map { $c->reading( $_ ) } sort @$mpair;
47 push( @matched_rels, sprintf( "%f: %s (%s) -- %s (%s), type %s",
48 $val, $mx, $mx->text, $my, $my->text, $mreltype ) );
51 say sprintf( "Matches for %s (%s) -- %s (%s), type %s",
52 $rx, $rx->text, $ry, $ry->text, $rel->type );
53 foreach ( @matched_rels ) {
60 my @comm = $c->calculate_common_readings();
63 foreach my $cr ( sort { $a->rank <=> $b->rank } @comm ) {
64 until( $cr->rank == $ri ) {
65 $nextcomm{$ri++} = $cr->rank;
68 until ( $ri == $c->end->rank ) {
69 $nextcomm{$ri++} = $c->end->rank;
72 # Find all the relatable pairs
76 foreach my $rx ( $c->readings ) {
77 next if $rx->is_meta();
78 # Have to compare each reading with each other, so do this only once
81 say STDERR "Looking at reading $rct ( $rx )";
82 foreach my $ry ( $c->readings ) {
83 next if $ry->is_meta();
84 next if $analyzed{"$ry"};
86 # Get their textual and graph distances from each other.
88 textdiff => distance( $rx->text, $ry->text ),
89 rankdiff => abs( $rx->rank - $ry->rank )
91 $grid->{"$rx"}->{"$ry"} = $vector;
93 # Do they share one or more witnesses?
96 map { $rxwits{$_} = 1 } $rx->witnesses;
97 foreach my $rywit ( $ry->witnesses ) {
98 if( $rxwits{$rywit} ) {
100 $vector->{'reachable'} = 1;
104 $vector->{'share_witness'} = $sharewit;
105 # Enough with the analysis if they do share witnesses.
108 # Is one node reachable from the other, even if they don't share a
110 $vector->{'reachable'} = $nextcomm{$rx->rank} == $nextcomm{$ry->rank} ? 0 : 1;
114 # Construct the vector list
115 # Dimensions are textdiff, rankdiff, reachable, share_witness
117 my( $i, $values ) = ( 0, 0 );
121 foreach my $rx ( keys %$grid ) {
122 foreach my $ry ( keys %{$grid->{$rx}} ) {
123 my $vec = $grid->{$rx}->{$ry};
124 next if $vec->{'reachable'}; # Skip all but colocations
125 $vecindex->{++$i} = [ $rx, $ry ];
126 # Note the number of keys from the first vector we look at.
128 @keys = sort keys( %$vec );
130 # Construct the matrix.
132 foreach my $j ( 0 .. $#keys ) {
133 my $v = $vec->{$keys[$j]};
135 push( @fields, $j+1, $v );
139 push( @lines, join( " ", @fields ) );
143 my $matrix .= "$i " . scalar( @keys ) . " $values\n";
148 # Open a filehandle on the matrix string we get.
149 open( my $matrix_fh, '<', \$matrix ) or die "Could not open filehandle on string";
151 # Now send the remainder of the filehandle for calculation.
152 my $simmatrix = Text::SenseClusters::Simat::simat( $matrix_fh );
154 # Match the returned values with the actual node pair comparisons they refer to.
155 my $cosine_values = {};
156 foreach my $x ( keys %$simmatrix ) {
157 foreach my $y ( keys %{$simmatrix->{$x}} ) {
158 next unless $x < $y; # skip self-comparison & duplicates
159 my $val = $simmatrix->{$x}->{$y};
160 # If the value is below our threshold of interest, skip it.
161 next if $val < $threshold;
162 # Sort the lookup of a pair alphabetically by reading ID.
163 my ( $r1x, $r1y ) = sort @{$vecindex->{$x}};
164 my ( $r2x, $r2y ) = sort @{$vecindex->{$y}};
165 push( @{$cosine_values->{$r1x}->{$r1y}->{$val}}, [ $r2x, $r2y ] );
166 push( @{$cosine_values->{$r2x}->{$r2y}->{$val}}, [ $r1x, $r1y ] );
170 return $cosine_values;