install.packages("devtools")
devtools::install_github("IPS-LMU/emuR")
#
library(emuR)

# tidyverse, containing - amongst others - 
# libraries dplyr, purrr, tibble, and ggplot2
install.packages("tidyverse")
library(tidyverse)



create_emuRdemoData(dir = mypath)
path2ae = file.path(mypath, "emuR_demoData", "ae_emuDB")
# load database
ae = load_emuDB(path2ae, verbose = F)
list_ssffTrackDefinitions(ae) #has dft and fm
summary(ae)
serve(ae) #should work now...

# a few examples of queries (cf. http://www.phonetik.uni-muenchen.de/%7Ejmh/lehre/sem/ws1819/emuR/LESSON3/Queries.html
# and https://ips-lmu.github.io/The-EMU-SDMS-Manual/chap-querysys.html#eql-the-emu-query-language-version-2)
#V in "amongst"
am=query(ae,"[Text==amongst]")

query(ae,"[Phonetic==V]")
# combine the two with "^"
query(ae,"[Phonetic==V ^ Text==amongst]")

#V followed by m, start time of V and end time of m
query(ae,"[Phonetic==V -> Phonetic=m]")
# only start and end time of V, if V is followed by m
query(ae,"[#Phonetic==V -> Phonetic==m]")

VmV=query(ae,"[[Phonetic==V -> Phonetic==m] -> Phonetic==V]")

(V2fromVmV = query(ae,"[[Phonetic==V -> Phonetic==m] -> #Phonetic==V]") )
(V1fromVmV = query(ae,"[[#Phonetic==V -> Phonetic==m] -> Phonetic==V]") )
(m_from_VmV = query(ae,"[[Phonetic==V -> #Phonetic==m] -> Phonetic==V]") )

allPhonSegments_but_V = query(ae,"[Phonetic!=V]")

sS_weakSyllables = query(ae,"[Phonetic==s|S ^ Syllable==W]")

# Hashtag
query(ae,"[Syllable==W ^ #Phonetic==s|S]")
# ==
query(ae,"[Phonetic==s|S ^ Syllable==W]")

(allFirstPhonSegmentsOfAllWords = query(ae, "[Start(Word, Phonetic) == TRUE]") )

#the opposite (i.e. all "Phonetic" segments except the ones at the start of any word)
query(ae, "[Start(Word, Phonetic) == FALSE]")
#first and last segment:
query(ae, "[Medial(Word, Phonetic) == FALSE]")

#all last "Phonetic" segments of all words
query(ae, "[End(Word, Phonetic) == TRUE]")

#all "V" segments that are first segment in a word
query(ae,"Phonetic==V & Start(Word,Phonetic) ==TRUE")

#all other "V"s
query(ae,"Phonetic==V & Start(Word,Phonetic) ==FALSE")




#V OR o: OR E in strong syllables
VoE.s = query(ae,"[Phonetic==V|o:|E ^ Syllable==S]")
#only the labels
VoE.s$labels
#count them
table(VoE.s$labels)
#extract their formants 1-4
VoE.fm = get_trackdata(ae,seglist=VoE.s,"fm",resultType = "tibble")
#show the complete tibble in nice format
View(VoE.fm)
# the complicated way of extracting the temporal midpoint would need the following lines
VoE.fm.norm = normalize_length(VoE.fm)

VoE.fm05compli = VoE.fm.norm %>% filter(times_norm==0.5)
# or, much much easier: add cut=0.5 to get_trackdata()
VoE.fm05 = get_trackdata(ae,seglist=VoE.s,"fm",resultType = "tibble",cut=0.5)

View(VoE.fm05compli) #minor differences in T1-T4, cf.
View(VoE.fm05)
