## ---- 
# mypath = "/Your/personal/path/created/in/lesson1"

## ---- 
# load packages
library(emuR)
library(dplyr)
library(ggplot2)

## ----
## # create demo data in directory
## 
create_emuRdemoData(dir = mypath)

## ---- 
# create path to demo database
path2ae = file.path(mypath, "emuR_demoData", "ae_emuDB")
# load database
ae = load_emuDB(path2ae, verbose = F)

## ------------------------------------------------------------------------
summary(ae)

## ---- 
serve(ae,autoOpenURL = "https://ips-lmu.github.io/EMU-webApp/?autoConnect=true")

## ----
# query A and V(front and back open vowels),
# i:and u: (front and back closed vowels), and
# E and o: (front and back mid vowels)
ae_vowels = query(emuDBhandle = ae,query = "[Phonetic== V|A|i:|u:|o:|E]")
#get the formants:
ae_formants = get_trackdata(ae, seglist = ae_vowels,ssffTrackName = "fm", resultType = "emuRtrackdata")
#get the formants at the vowels' temporal midpoints:
ae_formants_norm = normalize_length(ae_formants)
ae_midpoints = ae_formants_norm %>% filter(times_norm==0.5)
#or 
#ae_formants_norm[ae_formants_norm$times_norm==0.5,]
#plot the vowel space:
ggplot(ae_midpoints) +
  aes(x=T2,y=T1,label=labels,col=labels) +
  geom_text() +
  scale_y_reverse() + scale_x_reverse() + 
  labs(x = "F2 (Hz)", y = "F1 (Hz)") +
  theme(legend.position="none")



## ----
ae_midpoints$Word = requery_hier(ae,seglist = ae_vowels, level = "Text")$labels
#plot word labels instead of vowels' labels by label=Word
ggplot(ae_midpoints%>%filter(labels=="u:")) +
  aes(x=T2,y=T1,label=Word,col=labels) +
  geom_text() +
  scale_y_reverse() + scale_x_reverse() + 
  labs(x = "F2 (Hz)", y = "F1 (Hz)") +
  theme(legend.position="none") 

## ----
Cu = query(emuDBhandle = ae,query = "[Phonetic=~ .* -> Phonetic== u:]")
Cu_formants = get_trackdata(ae, seglist = Cu,ssffTrackName = "fm", resultType = "emuRtrackdata")
ggplot(Cu_formants) +
  aes(x=times_rel,y=T2,col=labels,group=sl_rowIdx) +
  geom_line() +
  labs(x = "Duration (ms)", y = "F2 (Hz)")

## ------------------------------------------------------------------------
V = query(emuDBhandle = ae,query = "[Phonetic==V]")

## ------------------------------------------------------------------------
V

## ------------------------------------------------------------------------
#Get labels:
V$labels

#Get start times:
V$start

#Get end times:
V$end

#durations of the [V]s 
V$end - V$start

#for the latter, there is also a special function in emuR:

dur(V)


## ------------------------------------------------------------------------
#Phonetic=EVENT, Phoneme=ITEM
list_levelDefinitions(ae)
V_phoneme=query(emuDBhandle = ae,query = "[Phoneme==V]")
V_phoneme
V

## ------------------------------------------------------------------------
list_linkDefinitions(ae)

## ------------------------------------------------------------------------
#Phonetic=EVENT, Phoneme=ITEM
list_levelDefinitions(ae)
V_phoneme2=query(emuDBhandle = ae,query = "[Phoneme==V]",calcTimes = FALSE)
V_phoneme2

## ------------------------------------------------------------------------
#find all "V"-labels in `ae`
V=query(emuDBhandle = ae,query = "[Phonetic==V]")

## ------------------------------------------------------------------------
(V_Text = requery_hier(emuDBhandle = ae,seglist = V,level = "Text"))

## ------------------------------------------------------------------------
requery_seq(emuDBhandle = ae,seglist = V,offset = 1)

## ---- eval=FALSE---------------------------------------------------------
## vignette("EQL")

## ------------------------------------------------------------------------
query(emuDBhandle = ae, query = "Phonetic == V")

## ------------------------------------------------------------------------
query(emuDBhandle = ae, query = "Phonetic = V")

## ---- eval = FALSE-------------------------------------------------------
## query(emuDBhandle = ae, query = "Phonetic != V")

## ------------------------------------------------------------------------
Everything1 = query(emuDBhandle = ae, query = "Phonetic != xyz") 
Everything2 = query(emuDBhandle = ae, query = "Phonetic =~ .*")
any(Everything1 != Everything2) # should result in FALSE if both are equal everywhere

## ---- 
# What is the query to retrieve all ITEMs in the “Text” level that don’t begin with ‘a’?
query(emuDBhandle = ae, query = "Text !~ a.*")

## ------------------------------------------------------------------------
query(emuDBhandle = ae, query = "Phonetic == m|n")

## ------------------------------------------------------------------------
mnN = query(emuDBhandle = ae, query = "Phonetic == m | n | N")
summary(mnN)

## ---- 
mnN = query(emuDBhandle = ae, query = "[Phonetic == m|n|N]")
summary(mnN)

## ---- 

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

## ---- 
query(ae, "[#Phonetic == V -> Phonetic == m]") # finds V, if V is followed by m
query(ae, "[Phonetic == V -> #Phonetic == m]") #finds m, if m is preceded by V


## ------------------------------------------------------------------------
query(ae, "[[Phonetic == @ -> Phonetic == n ] -> Phonetic == s]")

## ------------------------------------------------------------------------
## What is the query to retrieve all sequences of ITEMs containing labels “offer” followed by two arbitrary labels followed by “resistance”?
query(ae, "[[[Text == offer -> Text =~ .*] -> Text =~ .* ] -> Text == resistance]")

## ------------------------------------------------------------------------
list_linkDefinitions(ae)

## What is the query to retrieve all ITEMs containing the label “p” in the “Phoneme” level that occur in strong syllables (i.e. dominated by / linked to ITEMs of the level “Syllable” that contain the label “S” (=STRONG, as opposed to "W"=WEAK))?
query(ae, "[Phoneme == p ^ Syllable == S]")

## ------------------------------------------------------------------------
query(ae, "[Syllable == S ^ #Phoneme == p]")

## ------------------------------------------------------------------------
## What is the query to retrieve all ITEMs on the “Phonetic” level that are part of a strong syllable (labeled “S”) and belong to the words “amongst” or “beautiful”?
query(ae, "[[Phonetic =~ .* ^ Syllable == S] ^ Text == amongst | beautiful]")

## ---- 
## # same as
## query(ae, "[[#Phonetic =~ .* ^ Syllable == S] ^ Text == amongst | beautiful]")

## ------------------------------------------------------------------------
## to get the "Text"-items instead, use
query(ae, "[[Phonetic =~ .* ^ Syllable == S] ^ #Text == amongst | beautiful]")

## ----eval=FALSE----------------------------------------------------------
## ## What is the query to retrieve all word-initial syllables?
## ## (NB: syllable labels are either "W" or "S")
## query(ae, "[Start(Word, Syllable) == TRUE]")

## ----echo=FALSE----------------------------------------------------------
## What is the query to retrieve all word-initial syllables?
## (NB: syllable labels are either "W" or "S")
head(query(ae, "[Start(Word, Syllable) == TRUE]"))

## ----
## ## What is the query to retrieve all word-medial syllables?
## query(ae, "[Medial(Word, Syllable) == TRUE]")
## ## What is the query to retrieve all word-final syllables?
## query(ae, "[End(Word, Syllable) == TRUE]")

## ----
## What is the query to retrieve all word-medial syllables?
head(query(ae, "[Medial(Word, Syllable) == TRUE]"))

## ----
## What is the query to retrieve all word-final syllables?
head(query(ae, "[End(Word, Syllable) == TRUE]"))

## ----
## query(ae, "[Medial(Word, Phoneme) == TRUE]")

## ----
head(query(ae, "[Medial(Word, Phoneme) == TRUE]"))

## ------------------------------------------------------------------------
## What is the query to retrieve all words that contain two syllables?
query(ae, "[Num(Text, Syllable) == 2]")
## What is the query to retrieve all syllables that contain more than four phonemes?
query(ae, "[Num(Syllable, Phoneme) > 4]")

## ------------------------------------------------------------------------
list_attributeDefinitions(ae,level="Word")

## ------------------------------------------------------------------------
query(ae, "[Text =~.* & Accent == S]")

## ------------------------------------------------------------------------
## What is the query to retrieve all non-word-final “S” syllables?
query(ae, "[[Syllable == S  &  End(Word, Syllable) == FALSE]^#Text=~.*]")

