works.pl


# This work is licensed under the Creative Commons
# Attribution-ShareAlike 3.0 Unported License. To view a copy of this
# license, visit http://creativecommons.org/licenses/by-sa/3.0/ or send
# a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042,
# USA. 
# Reference: http://www.lombardinetworks.net
# (C) Robert Tolksdorf, http://www.robert-tolksdorf.de
use URI::Escape;
use Data::Dumper;
require "/vhome/lombardinetworks.net/htdocs/bin/helper.pl";

&load_actorids;
&load_aliases;
&loadWorkIds;
&load_image_urls;
&load_year_range;
&load_types;
&load_node_overlap;

# the navigation snippet
sub section {
    return "<h4 id=\"$_[0]\">$_[1]</h4>\n".
	'(To <a href="#info">Information</a> - '.
	'<a href="#stats">Statistics</a> - '.
	'<a href="#actors">Actors</a> - '.
	'<a href="#visualization">Visualization</a>)';
}


# hTML stuff
$frame_before_title=&read_inc_file("frame-before-title");
$frame_before=&read_inc_file("frame-before");
$frame_after=&read_inc_file("frame-after");

# iterate through all works
foreach $work (keys %workid) {
    $id=$workid{$work};
    # emit the description page for the network
    open(O,">",&dir_of_network($id)."index.html");
    # emit html stuff
    print O $licence_comment.$frame_before_title.
	"<title>Mark Lombardi Networks - $work</title>".
	# the stuff for the visualization
	'<script src="/js/jquery.js"></script><script src="/js/arbor.js"></script><script src="/js/d3.js"></script><script src="/js/lombardinet.js"></script>'.
	'<style type="text/css">'.
	".node {stroke: black;stroke-width: 1px;}".
	".link {stroke: #999; stroke-opacity: .6;}".
	"</style>".
	# stuff for google charts
	'<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>'.
	# remaining stuff in header
	$frame_before.
	"<h3>$work</h3>";

    # emit links to representation ----------------------------------------
    print O &section("info","Information").
	"<p>(preliminary unstable) Work URI: ".	&uri_for_network($work)."</p>\n".
	"<p>Digital as data in ".
	&link_to_network_format($id,"xgmml").", \n".
	&link_to_network_format($id,"graphml").", \n".
	&link_to_network_format($id,"json").".\n";
    # show link to image on the web, if any
    if ($image_url{$id} ne "") {
	print O "Viewable as an ".&link_to_web($image_url{$id},"image on the Web. ");
    }
    # emit a google link
    $uri_work=$work;
    $uri_work=~s/ /\+/g;
    $uri_work=~s/"/%22/g;

    print O "More on this work with a ".&link_to_web("https://www.google.de/search?q=".
			 $uri_work.
			 # exclude our site
			 "+-lombardinetworks.net","Google search");
    print O ".</p>\n";
    
    if (exists $year_range{$id}) {
	print O "<p>The narration takes place in the years $year_range{$id} (this is computed).</p>\n";
    }

    # emit statistics ---------------------------------------
    print O &section("stats","Statistics");
    # load the xgmml
    $network=&read_network_as_line($id);
    # count <node and <edge
    $nodes = () = $network =~ /<node/gi;
    $edges = () = $network =~ /<edge/gi;
    
    printf O "<p>Contains $nodes nodes and $edges edges (edge/node ratio is %.2f).</p>\n",$edges/$nodes;

    print O "<p>The nodes and edges are of the following types (the charts are interactive):";

    # Count types and generate data table
    $nodedata="['Type','Number']";
    foreach $n (@nodetype) {
	$number = () = $network =~ /$n/gi;
	$n=~/http:\/\/www.lombardinetworks.net\/lombardi.owl#(.*)/;
	$nodedata.=",['$1',$number]";
    }

    print O &piechart("pienodes","Types of nodes",$nodedata);

    # Count types and generate data table
    $edgedata="['Type','Number']";
    foreach $e (@edgetype) {
	$number = () = $network =~ /$e/gi;
	$e=~/http:\/\/www.lombardinetworks.net\/lombardi.owl#(.*)/;
	$edgedata.=",['$1',$number]";
    }
    print O &piechart("pieedges","Types of edges",$edgedata);

    # emit similar works ---------------------------------------
 

    # get a list of indexes sorted by value...
    my @sorted_overlap = sort { $node_overlap[$id][$b] <=> $node_overlap[$id][$a] } 1000..$#node_overlap;
    $very_high_overlap="";
    $good_overlap="";
    $poor_overlap="";
    foreach $i (0..$#sorted_overlap) {
	if (&very_high_overlap($node_overlap[$id][$sorted_overlap[$i]]))  {
	    $very_high_overlap.= sprintf("<li>".&link_to_network($sorted_overlap[$i])." with %.4f overlap.",$node_overlap[$id][$sorted_overlap[$i]]);
	    if (&very_high_overlap($node_overlap[$sorted_overlap[$i]][$id])) {
		$very_high_overlap.= sprintf(" Since the inverse overlap is also high (%.3f), it might be a direct predecessor version.",
					     $node_overlap[$sorted_overlap[$i]][$id]);
	    }
	    if (&good_overlap($node_overlap[$sorted_overlap[$i]][$id])) {
		$very_high_overlap.= sprintf(" Since the inverse overlap is also not low (%.3f), it might have contributed to the larger work.",
					     $node_overlap[$sorted_overlap[$i]][$id]);
	    }
	    $very_high_overlap.="</li>\n"
	}
	if (&good_overlap($node_overlap[$id][$sorted_overlap[$i]]))  {
	    $good_overlap.= sprintf("<li>".&link_to_network($sorted_overlap[$i])." with %.4f overlap</li>\n",$node_overlap[$id][$sorted_overlap[$i]]);
	}
    } 
    if ($very_high_overlap ne "") {
	print O "<p>High similarity with the following works by percentage of overlapping nodes:</p>\n<ul>\n$very_high_overlap</ul>\n";
    }
    if ($good_overlap ne "") {
	print O "<p>Some similarity with the following works by percentage of overlapping nodes:</p>\n<ul>\n$good_overlap</ul>\n";
    }
    

    # emit the list of actors ---------------------------------------
    print O &section("actors","Actors");
    # get the names from the network
    @labels=readNodeLabels($id);
    print O "<p>The actors in this network are<ul>";
    # iterate through all labels
    foreach $label (sort @labels) {
	# emit linked item
	print O "<li>".&link_to_actor(&normalized_name($label))."</li>\n";
    }
    print O "</ul>\n";

    # emit visualization ----------------------------------------
    print O &section("visualization","Visualization");
    print O "<p>You can click on the nodes to get information on the actor.".
	"<div id=\"lombardinet\" url=\"/network/$id/$id.json\"></div>";

    # emit remaining html frame ----------------------------------------
    print O $frame_after;
    close O;
}