Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
agricolamz committed Jul 13, 2024
1 parent 07b4c94 commit 18a0465
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
21 changes: 6 additions & 15 deletions R/concatenate_textgrids.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Concatenate sounds
#' Concatenate TextGrids
#'
#' Creates a merged sound file from old sound files in a folder. If the annotation argument is not equal to \code{NULL}, it creates an annotation file (Praat .TextGrid, ELAN .eaf or EXMARaLDA .exb) with original sound names annotation.
#' Creates a merged TextGrids from TextGrids files in a folder.
#'
#' @author George Moroz <[email protected]>
#'
Expand Down Expand Up @@ -33,11 +33,7 @@ concatenate_textgrids <- function(path,

# get list of files -------------------------------------------------------
path <- normalizePath(path)
files <- paste0(
path,
"/",
list.files(path, "\\.TextGrid$")
)
files <- list.files(path, "\\.TextGrid$", full.names = TRUE)

# read all textgrids and converge them into one df ------------------------
start <- 0
Expand All @@ -59,14 +55,9 @@ concatenate_textgrids <- function(path,
tier_names <- unique(results$tier)
}

writeLines(
paste0(
'File type = "ooTextFile"\nObject class = "TextGrid"\n\nxmin = 0\nxmax = ',
max(results$time_end),
"\ntiers? <exists>\nsize = 0\nitem []:"
),
paste0(path, "/", result_file_name, ".TextGrid")
)
create_empty_textgrid(duration = max(results$time_end),
path = path,
result_file_name = result_file_name)

s <- split(results[, c("time_start", "time_end", "content")], results$tier)

Expand Down
19 changes: 14 additions & 5 deletions R/df_to_tier.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,20 @@ df_to_tier <- function(df, textgrid, tier_name = "", overwrite = TRUE) {
})
}

if(df$time_end[nrow(df)] != textgrid_duration){
df <- rbind(df,
data.frame(time_start = df$time_end[nrow(df)],
time_end = textgrid_duration,
content = ""))

if("time_end" %in% names(df)){
if(df$time_end[nrow(df)] != textgrid_duration){
df <- rbind(df,
data.frame(time_start = df$time_end[nrow(df)],
time_end = textgrid_duration,
content = ""))
}
} else {
if(df$time_start[nrow(df)] != textgrid_duration){
df <- rbind(df,
data.frame(time_start = df$time_start[nrow(df)],
content = ""))
}
}

tier_class <- ifelse("time_end" %in% names(df),
Expand Down
4 changes: 2 additions & 2 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"codeRepository": "https://github.com/ropensci/phonfieldwork",
"issueTracker": "https://github.com/ropensci/phonfieldwork/issues",
"license": "https://spdx.org/licenses/GPL-2.0",
"version": "0.0.15",
"version": "0.0.16",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down Expand Up @@ -235,7 +235,7 @@
},
"SystemRequirements": "pandoc (>= 1.14) - http://pandoc.org"
},
"fileSize": "2085.848KB",
"fileSize": "2086.182KB",
"citation": [
{
"datePublished": "2023",
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/test-df-to-tier.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
test_that("df_to_tier", {
time_start <- c(0.00000000, 0.01246583, 0.24781914, 0.39552363, 0.51157715)
time_end <- c(0.01246583, 0.24781914, 0.39552363, 0.51157715, 0.65267574)
time_end <- c(0.01246583, 0.24781914, 0.39552363, 0.51157715, 0.6526757369614512)
content <- c("", "T", "E", "S", "T")
my_df <- data.frame(id = 1:5, time_start, time_end, content)
tg <- df_to_tier(my_df,
system.file("extdata", "test.TextGrid", package = "phonfieldwork"),
overwrite = FALSE
)

readLines(system.file("extdata", "test.TextGrid", package = "phonfieldwork"))

expect_error(
df_to_tier(my_df[-2],
system.file("extdata", "test.TextGrid",
Expand Down

0 comments on commit 18a0465

Please sign in to comment.