2121# volume.stats: allows ordering volumes by publication date or story-wise.
2222# filtered: whether characters should be filtered or not.
2323# pub.order: whether to consider volumes in publication vs. story order.
24+ # narr.unit: narrative unit to perform the aggregation (scene, volume, arc).
2425#
2526# returns: a sequence of graphs corresponding to a dynamic graph.
2627# ##############################################################################
27- cum.graph.extraction <- function (inter.df , char.stats , scene.chars , scene.stats , volume.stats , filtered = FALSE , pub.order = TRUE )
28+ cum.graph.extraction <- function (inter.df , char.stats , scene.chars , scene.stats , volume.stats , filtered = FALSE , pub.order = TRUE , narr.unit = NA )
2829{
2930 # extract the graph
30- g <- extract.static.graph.scenes(
31+ tlog(2 ," Extracting the scene sequence graph" )
32+ gg <- extract.static.graph.scenes(
3133 inter.df = inter.df ,
3234 char.stats = char.stats ,
3335 volume.stats = volume.stats ,
@@ -40,9 +42,91 @@ cum.graph.extraction <- function(inter.df, char.stats, scene.chars, scene.stats,
4042 { tlog(2 ," Filtering the characters" )
4143 filt.names <- char.stats [char.stats [,COL_FILTER ]== " Discard" ,COL_NAME ]
4244 if (length(filt.names )== 0 ) stop(" Empty list of filtered characters" )
43- g <- future_lapply(g , function (g ) delete_vertices(g , v = intersect(filt.names ,V(g )$ name )))
45+ gg <- future_lapply(gg , function (g ) delete_vertices(g , v = intersect(filt.names ,V(g )$ name )))
4446 }
4547
48+ # aggregate to get a cumulative network
49+ tlog(2 ," Aggregating by " ,narr.unit )
50+ res <- list ()
51+ prev.unit <- NA
52+ for (s in 1 : length(gg ))
53+ { tlog(4 ," Processing scene " ,s ," /" ,length(gg ))
54+
55+ # retrieve current scene graph
56+ sc.g <- gg [[s ]]
57+
58+ # retrieve current narrative unit
59+ sc.id <- gg [[s ]]$ SceneId
60+ sc.idx <- which(scene.stats [,COL_SCENE_ID ]== sc.id )
61+ if (narr.unit == " scene" )
62+ cur.unit <- sc.id
63+ else if (narr.unit == " volume" )
64+ cur.unit <- scene.stats [sc.idx ,COL_VOLUME_ID ]
65+ else if (narr.unit == " arc" )
66+ cur.unit <- scene.stats [sc.idx ,COL_ARC_ID ]
67+ tlog(4 ," Current " ,narr.unit ," : " ,cur.unit ," (previous " ,narr.unit ," : " ,prev.unit ," )" )
68+
69+ # very first graph of the sequence
70+ if (s == 1 )
71+ { sc.g $ NarrUnit <- paste0(narr.unit ," _" ,cur.unit )
72+ res [[1 ]] <- sc.g
73+ prev.unit <- cur.unit
74+ }
75+ # rest of the sequence
76+ else
77+ { cur.g <- prev.g <- res [[length(res )]]
78+
79+ # add current edges to previous graph
80+ if (gsize(sc.g )> 0 )
81+ { el <- as_edgelist(graph = sc.g , names = TRUE )
82+ for (e in 1 : nrow(el ))
83+ { # tlog(6,"e=",e," nrow(el)=",nrow(el))
84+ # edge already exists: increment weights
85+ if (are_adjacent(graph = cur.g , v1 = el [e ,1 ], v2 = el [e ,2 ]))
86+ { idx <- get.edge.ids(graph = cur.g , vp = el [e ,])
87+ # tlog(6,"idx=",idx)
88+ E(cur.g )[idx ]$ Occurrences <- E(cur.g )[idx ]$ Occurrences + E(sc.g )$ Occurrences [e ]
89+ E(cur.g )[idx ]$ Duration <- E(cur.g )[idx ]$ Duration + E(sc.g )$ Duration [e ]
90+ }
91+ # otherwise: create new edge
92+ else
93+ cur.g <- add_edges(graph = cur.g , edges = el [e ,],
94+ attr = list (Occurrences = E(sc.g )$ Occurrences [e ], Duration = E(sc.g )$ Duration [e ]))
95+ }
96+ }
97+
98+ # if same narr unit as previous: store as previous graph
99+ if (prev.unit == cur.unit )
100+ res [[length(res )]] <- cur.g
101+
102+ # otherwise: store as new graph
103+ else
104+ { # remove isolates in previous graph
105+ prev.isolates <- which(degree(prev.g ,mode = " all" )== 0 )
106+ prev.g <- delete_vertices(graph = prev.g , v = prev.isolates )
107+ res [[length(res )]] <- prev.g
108+
109+ # add new graph in the sequence
110+ cur.g $ NarrUnit <- paste0(narr.unit ," _" ,cur.unit )
111+ res [[length(res )+ 1 ]] <- cur.g
112+ # res <- c(res, list(prev.g))
113+ prev.unit <- cur.unit
114+ }
115+ }
116+ }
117+ # remove isolates in last graph
118+ last.g <- res [[length(res )]]
119+ last.isolates <- which(degree(last.g ,mode = " all" )== 0 )
120+ last.g <- delete_vertices(graph = last.g , v = last.isolates )
121+ res [[length(res )]] <- last.g
122+
123+ # test
124+ v.nbr <- sapply(res , gorder )
125+ units <- 1 : length(res )
126+ x.labels <- sapply(res , function (g ) g $ NarrUnit )
127+ plot(x = units , y = v.nbr , xaxt = " n" , xlab = paste0(narr.unit ," s" ), ylab = " Vertices" , col = " RED" )
128+ axis(side = 1 , at = units , labels = x.labels , las = 2 )
129+
46130 return (res )
47131}
48132
0 commit comments