Skip to content

Commit f203168

Browse files
committed
updated read/write corpus stats to handle chapters
1 parent 543aab3 commit f203168

1 file changed

Lines changed: 42 additions & 7 deletions

File tree

src/corpus/read_data.R

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,8 @@ update.all.tables <- function(
753753
# scene.stats: table describing all the scene constituting the series.
754754
# scene.chars: list of characters involved in each scene.
755755
# char.stats: table describing all the characters.
756+
# chapter.stats: table describing the series chapters (optionnal).
757+
# chapter.chars: list of characters involved in each chapter (optionnal).
756758
# volume.stats: table describing the series volumes.
757759
# volume.chars: list of characters involved in each volume.
758760
# arc.stats: table describing the series narrative arcs.
@@ -765,6 +767,7 @@ write.corpus.data <- function(
765767
page.stats, page.chars,
766768
scene.stats, scene.chars,
767769
char.stats,
770+
chapter.stats, chapter.chars,
768771
volume.stats, volume.chars,
769772
arc.stats, arc.chars)
770773
{ tlog(2,"Writing statistics and character lists")
@@ -821,6 +824,23 @@ write.corpus.data <- function(
821824
tlog(4,"Writing character stats \"",file,"\"")
822825
write.csv(x=char.stats, file=paste0(file,".csv"), fileEncoding="UTF-8", row.names=FALSE)
823826

827+
# possibly deal with chapters
828+
if(hasArg(chapter.stats))
829+
{ # chapter stats
830+
file <- get.path.stats.corpus(object="chapters", char.det=char.det, pref="_chapter_stats")
831+
tlog(4,"Writing chapter stats \"",file,"\"")
832+
write.csv(x=chapter.stats, file=paste0(file,".csv"), fileEncoding="UTF-8", row.names=FALSE)
833+
# chapter chars
834+
tab <- cbind(
835+
1:nrow(chapter.stats),
836+
sapply(chapter.chars, function(chars) paste(chars,collapse="\t"))
837+
)
838+
colnames(tab) <- c(COL_CHAP_ID, COL_CHARS)
839+
file <- get.path.stats.corpus(object="chapter", char.det=char.det, pref="_chapter_chars")
840+
tlog(4,"Writing chapter chars \"",file,"\"")
841+
write.table(tab, file=paste0(file,".txt"), fileEncoding="UTF-8", sep="\t", quote=FALSE, row.names=FALSE, col.names=TRUE)
842+
}
843+
824844
# volume stats
825845
file <- get.path.stats.corpus(object="volumes", char.det=char.det, pref="_volume_stats")
826846
tlog(4,"Writing volume stats \"",file,"\"")
@@ -940,6 +960,20 @@ read.corpus.data <- function(char.det)
940960
for(col in c(COL_NAME, COL_NAME_SHORT))
941961
char.stats[,col] <- fix.encoding(strings=char.stats[,col])
942962

963+
# possibly deal with chapters
964+
file <- get.path.stats.corpus(object="chapters", char.det=char.det, pref="_chapter_stats")
965+
if(file.exists(file))
966+
{ # chapter stats
967+
tlog(2,"Reading chapter stats file \"",file,"\"")
968+
chapter.stats <- read.csv(file=paste0(file,".csv"), header=TRUE, check.names=FALSE, stringsAsFactors=FALSE)
969+
for(col in c(COL_TITLE, COL_VOLUME, COL_ARC))
970+
chapter.stats[,col] <- fix.encoding(strings=chapter.stats[,col])
971+
# chapter chars
972+
file <- get.path.stats.corpus(object="chapters", char.det=char.det, pref="_chapter_chars")
973+
tlog(2,"Reading chapter chars file \"",file,"\"")
974+
chapter.chars <- read.char.list(file=paste0(file,".txt"))
975+
}
976+
943977
# volume stats
944978
file <- get.path.stats.corpus(object="volumes", char.det=char.det, pref="_volume_stats")
945979
tlog(2,"Reading volume stats file \"",file,"\"")
@@ -964,13 +998,14 @@ read.corpus.data <- function(char.det)
964998

965999
# build result list and return
9661000
result <- list(
967-
inter.df=inter.df, # interactions
968-
panel.stats=panel.stats, panel.chars=panel.chars, # panels
969-
page.stats=page.stats, page.chars=page.chars, # pages
970-
scene.stats=scene.stats, scene.chars=scene.chars, # scenes
971-
char.stats=char.stats, # characters
972-
volume.stats=volume.stats, volume.chars=volume.chars, # volumes
973-
arc.stats=arc.stats, arc.chars=arc.chars # arcs
1001+
inter.df=inter.df, # interactions
1002+
panel.stats=panel.stats, panel.chars=panel.chars, # panels
1003+
page.stats=page.stats, page.chars=page.chars, # pages
1004+
scene.stats=scene.stats, scene.chars=scene.chars, # scenes
1005+
char.stats=char.stats, # characters
1006+
chapter.stats=chapter.stats, chapter.chars=chapter.chars, # chapters
1007+
volume.stats=volume.stats, volume.chars=volume.chars, # volumes
1008+
arc.stats=arc.stats, arc.chars=arc.chars # arcs
9741009
)
9751010
return(result)
9761011
}

0 commit comments

Comments
 (0)