1919#
2020# inter.df: dataframe containing the pairwise interactions.
2121# char.stats: table describing all the characters occurring in the BD series.
22+ # scene.stats: table describing all the scenes constituting the BD series.
23+ # scene.chars: list of characters occurring in each scene.
2224# volume.stats: table describing all the volumes constituting the BD series.
2325# vol: volume of interest (optional, and ignored if arc is specififed or if ret.seq is TRUE).
2426# arc: narrative arc of interest (optional, and ignored if ret.seq is TRUE).
3032# - Duration: total duration (in number of panels).
3133# If ret.set==TRUE, then the function returns a list of graphs.
3234# ##############################################################################
33- extract.static.graph.scenes <- function (inter.df , char.stats , volume.stats , vol = NA , arc = NA , ret.seq = FALSE , pub.order = TRUE )
35+ extract.static.graph.scenes <- function (inter.df , char.stats , scene.stats , scene.chars , volume.stats , vol = NA , arc = NA , ret.seq = FALSE , pub.order = TRUE )
3436{ tlog(2 ," Extracting the scene-based static graph" )
3537 res <- list ()
3638 vname <- NA
@@ -48,28 +50,50 @@ extract.static.graph.scenes <- function(inter.df, char.stats, volume.stats, vol=
4850 # possibly filter interactions
4951 if (! is.na(arc ))
5052 { is <- which(inter.df [,COL_ARC_ID ]== arc )
53+ scenes.ord <- which(scene.stats [,COL_ARC_ID ]== arc )
5154 }
5255 else if (! is.na(vol ))
5356 { vname <- paste0(vol ," _" ,volume.stats [vol ,COL_VOLUME ])
5457 is <- which(inter.df [,COL_VOLUME_ID ]== vol )
58+ scenes.ord <- which(scene.stats [,COL_VOLUME_ID ]== vol )
5559 }
5660 else
57- { # order interactions by publication order
58- if (pub.order )
59- is <- 1 : nrow(inter.df )
60- # or by story order
61- else
62- { vol.ranks <- volume.stats [inter.df [,COL_VOLUME_ID ],COL_RANK ]
63- scene.ranks <- inter.df [,COL_SCENE_ID ]
64- is <- order(vol.ranks , scene.ranks )
61+ { is <- 1 : nrow(inter.df )
62+ scenes.ord <- 1 : nrow(scene.stats )
63+ }
64+
65+ # possibly sort scenes and interactions by story order
66+ if (! pub.order )
67+ { is <- is [order(inter.df [is ,COL_RANK ])]
68+ scenes.ord <- scenes.ord [order(scene.stats [scenes.ord ,COL_RANK ])]
69+ }
70+
71+ # possibly init the list with empty graphs or isolates
72+ if (ret.seq )
73+ { # tlog(2,"Initializing the graph list")
74+ s <- 1
75+ while (scenes.ord [s ]!= inter.df [is [1 ],COL_SCENE_ID ])
76+ { tlog(4 ," Processing s=" ,s ," (scenes.ord[s]=" ,scenes.ord [s ]," and inter.df[is[1],COL_SCENE_ID]=" ,inter.df [is [1 ],COL_SCENE_ID ]," ) -- (length(scene.chars[[s]]=" ,length(scene.chars [[s ]])," )" )
77+ if (length(scene.chars [[scenes.ord [s ]]])== 0 )
78+ g <- make_empty_graph(n = 0 , directed = FALSE )
79+ else if (length(scene.chars [[scenes.ord [s ]]])== 1 )
80+ { idx <- which(char.stats [,COL_NAME ]== scene.chars [[scenes.ord [s ]]])
81+ g <- graph_from_data_frame(d = static.df , directed = FALSE , vertices = char.stats [idx ,])
82+ }
83+ res [[s ]] <- g
84+ s <- s + 1
6585 }
6686 }
6787
6888 # build the edgelist by considering each line (i.e. interaction) in the dataframe
6989 prev.scene <- NA
90+ prev.scene.idx <- NA
91+ tlog(2 ," Building the edge list" )
7092 for (i in is )
7193 { # get the current scene id
7294 cur.scene <- inter.df [i ,COL_SCENE_ID ]
95+ cur.scene.idx <- which(scenes.ord == cur.scene )
96+ # tlog(4,"Processing scene ",cur.scene," (",cur.scene.idx,"/",length(scenes.ord),")")
7397
7498 # get the characters
7599 from.char <- inter.df [i ,COL_CHAR_FROM ]
@@ -98,9 +122,11 @@ extract.static.graph.scenes <- function(inter.df, char.stats, volume.stats, vol=
98122 if (ret.seq )
99123 { # possibly copy previous graph
100124 if (! is.na(prev.scene ) && cur.scene != prev.scene )
101- { for (s in (prev.scene + 1 ): (cur.scene - 1 ))
102- { g <- res [[s - 1 ]]
103- g <- set_graph_attr(graph = g , name = " Scene" , value = s )
125+ { # possibly several times, to represent interaction-less scenes
126+ for (s in (prev.scene.idx + 1 ): (cur.scene.idx - 1 ))
127+ { # tlog(6,"s=",s," scenes.ord[s-1]=",scenes.ord[s-1]," length(res)=",length(res))
128+ g <- res [[s - 1 ]]
129+ g <- set_graph_attr(graph = g , name = " SceneId" , value = scene.stats [scenes.ord [s ],COL_SCENE_ID ])
104130 res [[s ]] <- g
105131 }
106132 }
@@ -109,10 +135,11 @@ extract.static.graph.scenes <- function(inter.df, char.stats, volume.stats, vol=
109135 idx <- which(char.stats [,COL_NAME ] %in% c(cbind(static.df [,COL_CHAR_FROM ],static.df [,COL_CHAR_TO ])))
110136 g <- graph_from_data_frame(d = static.df , directed = FALSE , vertices = char.stats [idx ,])
111137 g $ Scene <- cur.scene
112- res [[cur.scene ]] <- g
138+ res [[cur.scene.idx ]] <- g
113139 }
114140
115141 prev.scene <- cur.scene
142+ prev.scene.idx <- cur.scene.idx
116143 }
117144
118145 # set up result variable
@@ -236,12 +263,15 @@ extract.static.graphs.base <- function(data)
236263 inter.df <- data $ inter.df
237264 page.stats <- data $ page.stats
238265 char.stats <- data $ char.stats
266+ scene.stats <- data $ scene.stats
267+ scene.chars <- data $ scene.chars
239268 volume.stats <- data $ volume.stats
240269
241270 # extract the full scene-based static graph
242271 g <- extract.static.graph.scenes(
243272 inter.df = inter.df ,
244273 char.stats = char.stats ,
274+ scene.stats = scene.stats , scene.chars = scene.chars ,
245275 volume.stats = volume.stats
246276 )
247277 # record to file
@@ -270,6 +300,7 @@ extract.static.graphs.base <- function(data)
270300 g <- extract.static.graph.scenes(
271301 inter.df = inter.df ,
272302 char.stats = char.stats ,
303+ scene.stats = scene.stats , scene.chars = scene.chars ,
273304 volume.stats = volume.stats ,
274305 arc = a
275306 )
@@ -297,6 +328,7 @@ extract.static.graphs.base <- function(data)
297328 g <- extract.static.graph.scenes(
298329 inter.df = data $ inter.df ,
299330 char.stats = char.stats ,
331+ scene.stats = scene.stats , scene.chars = scene.chars ,
300332 volume.stats = volume.stats ,
301333 vol = v
302334 )
0 commit comments