Skip to content

Commit

Permalink
add textgrid_to_df() function
Browse files Browse the repository at this point in the history
  • Loading branch information
agricolamz committed Jun 7, 2020
1 parent f07d035 commit 8043a55
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export(get_textgrid_names)
export(rename_soundfiles)
export(rename_videofiles)
export(set_textgrid_names)
export(textgrid_to_df)
export(tier_to_df)
importFrom(grDevices,dev.off)
importFrom(grDevices,png)
Expand Down
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
phonfieldwork 0.0.5 (07.06.2020)
- add "textgrid_to_df()" function for reading Praat files
- add "create_glossed_document()" function for converting .flextext files into a glossed document
- add "flextext_to_df()" function for reading FLEx files
- add "eaf_to_df()" function for reading ELAN files
Expand Down
37 changes: 37 additions & 0 deletions R/textgrid_to_df.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#' TextGrid to dataframe
#'
#' Convert Praat TextGrid to a dataframe.
#'
#' @author George Moroz <[email protected]>
#'
#' @param textgrid string with a filename or path to the TextGrid
#' @return a dataframe with columns: \code{id}, \code{start}, \code{end} (if it is an interval tier -- the same as the start value), \code{annotation}, and \code{tier}
#'
#' @examples
#' textgrid_to_df(example_textgrid)
#'
#' @export

textgrid_to_df <- function(textgrid, tier = 1){
if(grepl("TextGrid", textgrid[2])){
tg <- textgrid
} else{
tg <- readLines(textgrid)
}
n_tiers <- as.double(regmatches(tg[7], regexpr("\\d", tg[7])))
lapply(1:n_tiers, function(x){
df <- phonfieldwork::tier_to_df(tg, x)
df$tier <- x
if(!("end" %in% names(df))){
df <- data.frame(id = df$id,
start = df$start,
end = df$start,
annotation = df$annotation,
tier = df$tier)
}
return(df)
}) ->
l
result <- Reduce(rbind, l)
return(result[order(result$start),])
}
6 changes: 6 additions & 0 deletions docs/index.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,12 @@ You can find the created example [here](https://agricolamz.github.io/phonfieldwo

The `phonfieldwork` package provides also several methods for reading several file types into R (just change the `system.file()` function to path to the file):

* file `.TextGrid` from Praat
```{r}
textgrid_to_df(example_textgrid)
```


* file `.eaf` from ELAN
```{r}
eaf_to_df(system.file("extdata", "test.eaf", package = "phonfieldwork"))
Expand Down
9 changes: 9 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1993,6 +1993,15 @@ <h2><span class="header-section-number">4.8</span> Create a viewer</h2>
<h2><span class="header-section-number">4.9</span> Read files to R</h2>
<p>The <code>phonfieldwork</code> package provides also several methods for reading several file types into R (just change the <code>system.file()</code> function to path to the file):</p>
<ul>
<li>file <code>.TextGrid</code> from Praat</li>
</ul>
<pre class="r"><code>textgrid_to_df(example_textgrid)</code></pre>
<div data-pagedtable="false">
<script data-pagedtable-source type="application/json">
{"columns":[{"label":[""],"name":["_rn_"],"type":[""],"align":["left"]},{"label":["id"],"name":[1],"type":["int"],"align":["right"]},{"label":["start"],"name":[2],"type":["dbl"],"align":["right"]},{"label":["end"],"name":[3],"type":["dbl"],"align":["right"]},{"label":["annotation"],"name":[4],"type":["chr"],"align":["left"]},{"label":["tier"],"name":[5],"type":["int"],"align":["right"]}],"data":[{"1":"1","2":"0.00000000","3":"0.01246583","4":"","5":"1","_rn_":"1"},{"1":"1","2":"0.00000000","3":"0.01246583","4":"","5":"2","_rn_":"6"},{"1":"2","2":"0.01246583","3":"0.24781914","4":"t","5":"1","_rn_":"2"},{"1":"2","2":"0.01246583","3":"0.24781914","4":"","5":"2","_rn_":"7"},{"1":"1","2":"0.01246583","3":"0.01246583","4":"t","5":"3","_rn_":"11"},{"1":"3","2":"0.24781914","3":"0.39552363","4":"e","5":"1","_rn_":"3"},{"1":"3","2":"0.24781914","3":"0.39552363","4":"","5":"2","_rn_":"8"},{"1":"2","2":"0.24781914","3":"0.24781914","4":"e","5":"3","_rn_":"12"},{"1":"4","2":"0.39552363","3":"0.51157715","4":"s","5":"1","_rn_":"4"},{"1":"4","2":"0.39552363","3":"0.51157715","4":"","5":"2","_rn_":"9"},{"1":"3","2":"0.39552363","3":"0.39552363","4":"s","5":"3","_rn_":"13"},{"1":"5","2":"0.51157715","3":"0.65267574","4":"t","5":"1","_rn_":"5"},{"1":"5","2":"0.51157715","3":"0.65267574","4":"","5":"2","_rn_":"10"},{"1":"4","2":"0.51157715","3":"0.51157715","4":"t","5":"3","_rn_":"14"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
</script>
</div>
<ul>
<li>file <code>.eaf</code> from ELAN</li>
</ul>
<pre class="r"><code>eaf_to_df(system.file(&quot;extdata&quot;, &quot;test.eaf&quot;, package = &quot;phonfieldwork&quot;))</code></pre>
Expand Down
4 changes: 2 additions & 2 deletions docs/s1/stimuli_viewer.html
Original file line number Diff line number Diff line change
Expand Up @@ -3252,8 +3252,8 @@ <h1 class="title toc-ignore">Sound Viewer</h1>
<h1></h1>
<div id="data" class="section level2">
<h2>data</h2>
<div id="htmlwidget-856cf1ed3cd37610ff04" style="width:100%;height:auto;" class="datatables html-widget"></div>
<script type="application/json" data-for="htmlwidget-856cf1ed3cd37610ff04">{"x":{"filter":"top","filterHTML":"<tr>\n <td data-type=\"character\" style=\"vertical-align: top;\">\n <div class=\"form-group has-feedback\" style=\"margin-bottom: auto;\">\n <input type=\"search\" placeholder=\"All\" class=\"form-control\" style=\"width: 100%;\"/>\n <span class=\"glyphicon glyphicon-remove-circle form-control-feedback\"><\/span>\n <\/div>\n <\/td>\n <td data-type=\"character\" style=\"vertical-align: top;\">\n <div class=\"form-group has-feedback\" style=\"margin-bottom: auto;\">\n <input type=\"search\" placeholder=\"All\" class=\"form-control\" style=\"width: 100%;\"/>\n <span class=\"glyphicon glyphicon-remove-circle form-control-feedback\"><\/span>\n <\/div>\n <\/td>\n<\/tr>","data":[["tip <a onmouseover=\"resize(this, '200%')\" onmouseout=\"resize(this, '100%')\" onclick = 'pic_appear(\"s1_pics/1_s1_ı.png\", \"tip\")'> &#x1f441;<a> <a onmouseover=\"resize(this, '200%')\" onmouseout=\"resize(this, '100%')\" onclick = 'sound_play(\"s1_sounds/1_s1_ı.wav\")'> &#x1f442;<a>","tap <a onmouseover=\"resize(this, '200%')\" onmouseout=\"resize(this, '100%')\" onclick = 'pic_appear(\"s1_pics/2_s1_æ.png\", \"tap\")'> &#x1f441;<a> <a onmouseover=\"resize(this, '200%')\" onmouseout=\"resize(this, '100%')\" onclick = 'sound_play(\"s1_sounds/2_s1_æ.wav\")'> &#x1f442;<a>","top <a onmouseover=\"resize(this, '200%')\" onmouseout=\"resize(this, '100%')\" onclick = 'pic_appear(\"s1_pics/3_s1_ɒ.png\", \"top\")'> &#x1f441;<a> <a onmouseover=\"resize(this, '200%')\" onmouseout=\"resize(this, '100%')\" onclick = 'sound_play(\"s1_sounds/3_s1_ɒ.wav\")'> &#x1f442;<a>"],["ı","æ","ɒ"]],"container":"<table class=\"display\">\n <thead>\n <tr>\n <th>labels<\/th>\n <th>X2<\/th>\n <\/tr>\n <\/thead>\n<\/table>","options":{"pageLength":50,"dom":"tip","order":[],"autoWidth":false,"orderClasses":false,"orderCellsTop":true}},"evals":[],"jsHooks":[]}</script>
<div id="htmlwidget-bef8bd1f9fcd817c372f" style="width:100%;height:auto;" class="datatables html-widget"></div>
<script type="application/json" data-for="htmlwidget-bef8bd1f9fcd817c372f">{"x":{"filter":"top","filterHTML":"<tr>\n <td data-type=\"character\" style=\"vertical-align: top;\">\n <div class=\"form-group has-feedback\" style=\"margin-bottom: auto;\">\n <input type=\"search\" placeholder=\"All\" class=\"form-control\" style=\"width: 100%;\"/>\n <span class=\"glyphicon glyphicon-remove-circle form-control-feedback\"><\/span>\n <\/div>\n <\/td>\n <td data-type=\"character\" style=\"vertical-align: top;\">\n <div class=\"form-group has-feedback\" style=\"margin-bottom: auto;\">\n <input type=\"search\" placeholder=\"All\" class=\"form-control\" style=\"width: 100%;\"/>\n <span class=\"glyphicon glyphicon-remove-circle form-control-feedback\"><\/span>\n <\/div>\n <\/td>\n<\/tr>","data":[["tip <a onmouseover=\"resize(this, '200%')\" onmouseout=\"resize(this, '100%')\" onclick = 'pic_appear(\"s1_pics/1_s1_ı.png\", \"tip\")'> &#x1f441;<a> <a onmouseover=\"resize(this, '200%')\" onmouseout=\"resize(this, '100%')\" onclick = 'sound_play(\"s1_sounds/1_s1_ı.wav\")'> &#x1f442;<a>","tap <a onmouseover=\"resize(this, '200%')\" onmouseout=\"resize(this, '100%')\" onclick = 'pic_appear(\"s1_pics/2_s1_æ.png\", \"tap\")'> &#x1f441;<a> <a onmouseover=\"resize(this, '200%')\" onmouseout=\"resize(this, '100%')\" onclick = 'sound_play(\"s1_sounds/2_s1_æ.wav\")'> &#x1f442;<a>","top <a onmouseover=\"resize(this, '200%')\" onmouseout=\"resize(this, '100%')\" onclick = 'pic_appear(\"s1_pics/3_s1_ɒ.png\", \"top\")'> &#x1f441;<a> <a onmouseover=\"resize(this, '200%')\" onmouseout=\"resize(this, '100%')\" onclick = 'sound_play(\"s1_sounds/3_s1_ɒ.wav\")'> &#x1f442;<a>"],["ı","æ","ɒ"]],"container":"<table class=\"display\">\n <thead>\n <tr>\n <th>labels<\/th>\n <th>X2<\/th>\n <\/tr>\n <\/thead>\n<\/table>","options":{"pageLength":50,"dom":"tip","order":[],"autoWidth":false,"orderClasses":false,"orderCellsTop":true}},"evals":[],"jsHooks":[]}</script>
<div id="my_block" class="my_block" onclick="pic_disappear()">
<span class="close">×</span> <img class="my_img" id="my_img">
<div id="caption" class="caption">
Expand Down
24 changes: 24 additions & 0 deletions man/textgrid_to_df.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8043a55

Please sign in to comment.