6 use lib '/Users/tla/Projects/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;
37 next if $rx->rank > 100;
38 next if $ry->rank > 100;
40 say STDERR "Checking relationship $rx -- $ry, of type " . $rel->type;
43 foreach my $val ( sort { $a<=>$b } keys %{$matrix->{"$rx"}->{"$ry"}} ) {
45 foreach my $mpair ( @{$matrix->{"$rx"}->{"$ry"}->{"$val"}} ) {
46 my $mrel = $c->get_relationship( $mpair );
47 my $mreltype = $mrel ? $mrel->type : '(no relation)';
48 my( $mx, $my ) = map { $c->reading( $_ ) } sort @$mpair;
49 push( @matched_rels, sprintf( "%f: %s (%s) -- %s (%s), type %s",
50 $val, $mx, $mx->text, $my, $my->text, $mreltype ) );
53 say sprintf( "Matches for %s (%s) -- %s (%s), type %s",
54 $rx, $rx->text, $ry, $ry->text, $rel->type );
55 foreach ( @matched_rels ) {
62 my @comm = $c->calculate_common_readings();
65 foreach my $cr ( sort { $a->rank <=> $b->rank } @comm ) {
66 until( $cr->rank == $ri ) {
67 $nextcomm{$ri++} = $cr->rank;
70 until ( $ri == $c->end->rank ) {
71 $nextcomm{$ri++} = $c->end->rank;
74 # Find all the relatable pairs
78 foreach my $rx ( $c->readings ) {
79 next if $rx->rank > 100;
80 next if $rx->is_meta();
81 # Have to compare each reading with each other, so do this only once
84 say STDERR "Looking at reading $rct ( $rx )";
85 foreach my $ry ( $c->readings ) {
86 next if $ry->rank > 100;
87 next if $ry->is_meta();
88 next if $analyzed{"$ry"};
90 # Get their textual and graph distances from each other.
92 textdiff => distance( $rx->text, $ry->text ),
93 rankdiff => abs( $rx->rank - $ry->rank )
95 $grid->{"$rx"}->{"$ry"} = $vector;
97 # Do they share one or more witnesses?
100 map { $rxwits{$_} = 1 } $rx->witnesses;
101 foreach my $rywit ( $ry->witnesses ) {
102 if( $rxwits{$rywit} ) {
104 $vector->{'reachable'} = 1;
108 $vector->{'share_witness'} = $sharewit;
109 # Enough with the analysis if they do share witnesses.
112 # Is one node reachable from the other, even if they don't share a
114 $vector->{'reachable'} = $nextcomm{$rx->rank} == $nextcomm{$ry->rank} ? 0 : 1;
118 # Construct the vector list
119 # Dimensions are textdiff, rankdiff, reachable, share_witness
121 my( $i, $values ) = ( 0, 0 );
125 foreach my $rx ( keys %$grid ) {
126 foreach my $ry ( keys %{$grid->{$rx}} ) {
127 my $vec = $grid->{$rx}->{$ry};
128 next if $vec->{'reachable'}; # Skip all but colocations
129 $vecindex->{++$i} = [ $rx, $ry ];
130 # Note the number of keys from the first vector we look at.
132 @keys = sort keys( %$vec );
134 # Construct the matrix.
136 foreach my $j ( 0 .. $#keys ) {
137 my $v = $vec->{$keys[$j]};
139 push( @fields, $j+1, $v );
143 push( @lines, join( " ", @fields ) );
147 my $matrix .= "$i " . scalar( @keys ) . " $values\n";
152 # Open a filehandle on the matrix string we get.
153 open( my $matrix_fh, '<', \$matrix ) or die "Could not open filehandle on string";
155 # Now send the remainder of the filehandle for calculation.
156 my $simmatrix = Text::SenseClusters::Simat::simat( $matrix_fh );
158 # Match the returned values with the actual node pair comparisons they refer to.
159 my $cosine_values = {};
160 foreach my $x ( keys %$simmatrix ) {
161 foreach my $y ( keys %{$simmatrix->{$x}} ) {
162 next unless $x < $y; # skip self-comparison & duplicates
163 my $val = $simmatrix->{$x}->{$y};
164 # If the value is below our threshold of interest, skip it.
165 next if $val < $threshold;
166 # Sort the lookup of a pair alphabetically by reading ID.
167 my ( $r1x, $r1y ) = sort @{$vecindex->{$x}};
168 my ( $r2x, $r2y ) = sort @{$vecindex->{$y}};
169 push( @{$cosine_values->{$r1x}->{$r1y}->{$val}}, [ $r2x, $r2y ] );
170 push( @{$cosine_values->{$r2x}->{$r2y}->{$val}}, [ $r1x, $r1y ] );
174 return $cosine_values;