Created
April 5, 2024 00:27
-
-
Save agricolamz/5b3b241c2aeb84fb278fe3fc6a33f0fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(phonfieldwork) | |
files <- list.files(pattern = "TextGrid") | |
walk(files, function(file){ | |
textgrid <- textgrid_to_df(file) | |
textgrid |> | |
mutate(content = str_extract(content, "^.*?-"), | |
content = str_remove(content, "-"), | |
content = str_replace_all(content, "SS", "ss"), | |
content = str_replace_all(content, "S", "s"), | |
content = str_replace_all(content, "Z", "s"), | |
content = str_replace_all(content, "d", "t"), | |
content = str_replace_all(content, "g", "k"), | |
content = str_replace_all(content, "b", "p"), | |
content = str_replace_all(content, "A", "ä")) |> | |
df_to_tier(file) | |
}) | |
walk(files, function(file){ | |
textgrid <- textgrid_to_df(file) | |
textgrid |> | |
filter(tier == 2) |> | |
select(time_start, time_end, content) |> | |
mutate(content = str_squish(content), | |
content2 = str_split(content, "")) |> | |
unnest_longer(content2) |> | |
group_by(time_start, time_end, content) |> | |
summarise(content2 = str_c(content2, collapse = "-")) |> | |
mutate(n_annotations = str_count(content2, "-")+1, | |
content2 = str_split(content2, "-")) |> | |
unnest_longer(content2) |> | |
group_by(time_start, time_end, content) |> | |
mutate(id = 1:n()-1, | |
id_rev = n():1-1, | |
diff = (time_end - time_start)/n_annotations, | |
time_start = time_start+id*diff, | |
time_end = time_end-id_rev*diff) |> | |
ungroup() |> | |
select(time_start, time_end, content2) |> | |
rename(content = content2) -> | |
result | |
result |> | |
arrange(time_start) |> | |
df_to_tier(file, tier_name = "auSegments") | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment