---
title: "Variation and palatalisation in the production of the plural prefix in Meru: a study of three dialects. 20th International Congress of Phonetic Sciences, August 7–11, 2023, Prague"
author: "Cunha, Conceição; Kanana, Fridah and Harrington, Jonathan (2023)"
output: 
  bookdown::html_document2:
    number_sections: TRUE
    toc: true
    theme: flatly
    highlight: pygments
editor_options: 
  markdown: 
    wrap: 72
---


<style>
div.col {background-color: #ffd5c0; border-radius: 5px; padding: 20px;}
body {font-size: 16pt;}
h1 {font-size: 24pt;}
h2 {font-size: 22pt;}
p.caption {font-size: 12pt; text-align: justify;}
code.sourceCode {font-size: 16pt;}
</style>

```{r, echo=FALSE}
# knitr::opts_chunk$set(echo = TRUE, fig.width = 10)
```

# Preliminaries

This are the commands for analysing the primary data associated
[with this publication.](https://epub.ub.uni-muenchen.de/104991/1/icphs_kenya.pdf)

Load libraries and data:

```{r, message=FALSE}
library(emuR)
library(tidyverse)
library(gridExtra)
library(lmerTest)
library(emmeans)

# Primary dataset: Change the path to the location of plurals_emuDB
pfad1 = "/Volumes/vdata/ERC3/Kenya/primary"

# Secondary dataset: create a path for storing secondary data
pfad2 = "/Volumes/vdata/ERC3/Kenya/secondary"

# read in morphological information
#m.df = read.table(file.path(pfad, "morpho_cc.txt"), header=T)

db = load_emuDB(file.path(pfad1, "plurals_emuDB"))
```


## Main aim(s) 
The main aim of this study is to document the phonetic variation in prefixes that mark plurals in class 8 nouns for three central Kenya dialects of the Meru-Tharaka group with a  particular focus on the unstudied variety of Tigania. Previous auditory-based studies of a small number of individuals suggest that Imenti in the north and Chuka in the south mark plurals with labial and non-labial prefixes respectively.  Given that Tigania borders Imenti to the north (and is therefore geographically distant from Chuka) and taking into account various kinds of evidence that Imenti has some characteristics of a standard accent in being the preferred variety that is taught in schools throughout Meru, the expectation was that Tigania should pattern more closely with Imenti than with Chuka.  

## Segment list

- word labels (`wordlabs`)
- vowel labels (`vlabs`)
- vowel start and end (`vstart`, `vend`)
- stem consonant (`stemcons`)
- dialect (`dial`)
- speaker (`spk`)
- speaker sex (`sex`)
- pair (`each singular-plural word pair`, includes all realisations of field, fields, )


```{r}
prefix.df = query(db, 
           "[prefix == b|c|ç|ɕ|ð|k|K|p|s|ʃ|tɕ|ts|tʃ|v|ʔ|β
           ^ #MAU =~ .*]")  %>% 
  mutate(
  wordlabs = requery_hier(db, ., "ORT-MAU")$labels,
  vlabs = requery_seq(db, ., 1)$labels,
  vstart = requery_seq(db, ., 1)$start,
  vend = requery_seq(db, ., 1)$end,
  stemcons = requery_seq(db, ., 2)$labels,
  dial= substring(bundle, 1, 2), 
  spk = substring(bundle, 1, 4),
  sex= substring(bundle, 5, 5),
  age= substring(session, 8, 9),
  c_partner = substring(session, 13, 13),
  pair = gsub('.{2}$', '', substring(bundle, 9)),
  age_group= ifelse(age >40, "old", "young"), 
  word_morpho = recode(wordlabs, 
                       "Biara" = "Bi.ara",
                       "BiEBa" = "Bi.EBa",
                       "Bieni" = "Bi.Eni",
                       "BiEni" = "Bi.Eni",
                       "Biimba" = "Bi.imba", 
                       "Biongo" = "Bi.ongo",
                       "Biura" = "Bi.ura",
                       "Biora" = "Bi.ura", 
                       "ciao" = "ciO",
                       "siao" = "siO", 
                       "ciara" = "ci.ara",
                       "ciEBa" = "ci.EBa",
                       "cieni" = "ci.Eni",
                       "ciEni" = "ci.Eni",
                       "ciimba" = "ci.imba",
                       "cimba" = "ci.imba",
                       "ciola" = "ci.ora",
                       "ciongo" = "ci.ongo",
                       "ciora" = "ci.ora",
                       "ciura" = "ci.ora",
                       "siara" = "si.ara",
                       "sjara" = "si.ara",
                       "siEBa" = "si.EBa",
                       "siEni" = "si.Eni",
                       "siimba" = "si.imba",
                       "simba" = "si.imba",
                       "siola" = "si.ora",
                       "siora" = "si.ora",
                       "siura" = "si.ora",
                       "siongo" = "si.ongo"),
category= recode(wordlabs,
                 "Biara" = "pl",
                 "BiEBa" = "pl",
                 "Bieni" = "pl",
                 "BiEni" = "pl",
                 "Biimba" = "pl",
                 "Biongo" = "pl", 
                 "Biura" = "pl", 
                 "Biora" = "pl",
                 "ciao" = "sg",
                 "ciO" = "sg",
                 "siao" = "pl",
                 "siO" ="pl", 
                 "ciara" = "sg",
                 "ciEBa" = "sg",
                 "cieni" = "sg",
                 "ciEni" = "sg",
                 "ciimba" = "sg",
                 "cimba" = "sg",
                 "ciola" = "sg", 
                 "ciora" = "sg",
                 "ciongo" = "sg",
                 "ciora" = "sg",
                 "ciura" = "sg",
                 "siara" = "pl",
                 "sjara" = "pl",
                 "siEBa" = "pl",
                 "siEni" = "pl",
                 "siimba" = "pl",
                 "simba" = "pl",
                 "siola" = "pl",
                 "siora" = "pl",
                 "siura" = "pl",
                 "siongo" = "pl"), 
stem= recode(word_morpho,
             "Bi.ara" = "ara",
             "Bi.EBa" = "EBa",
             "Bi.Eni" = "Eni",
             "Bi.imba" = "imba",
             "Bi.ongo" = "ongo", 
             "Bi.ura" = "ora", 
             "ci.ara" = "ara",
             "ci.EBa" = "EBa",
             "ci.Eni" = "Eni",
             "ci.imba" = "imba",
             "ci.ora" = "ora",
             "ci.ongo" = "ongo",
             "si.ara" = "ara",
             "si.EBa" = "EBa",
             "si.Eni" = "Eni",
             "si.imba" = "imba",
             "si.ora" = "ora",
             "si.ongo" = "ongo"), 
pair= recode(pair,
             "N02_ciola" = "N02_ciora",
             "N02_ciura" = "N02_ciora",
             "N05_cimba" ="N05_ciimba",
             "N07_cieni"="N07_ciEni",
             "N04_cieba" ="N04_ciEBa",
             "u07_kwanda" ="kwanda",
             "l21_kwanda" ="kwanda",
             "l32_kudara" ="kudara"), 
interviewer= recode(c_partner,
                    "C" = "native", 
                    "K"="Imenti"), 
labels = factor(labels, levels =
                           c("b","β","v", "ð",
                             "ts", "s", "tʃ" , "ʃ", 
                             "tɕ", "ɕ" , "ç" , "c","k" ,"ʔ")))
```


Exclude /siO, ciO/ because there are so few of them and exclude stem /Eni/ because this only occurs in 2 dialects. Also exclude  kudara  kwanda and one with no name ("")
Also make age numeric.

```{r}
prefix.df = prefix.df %>%
  filter(!(stem %in% 
             c("ciO", "Eni", "siO", "kudara", "kwanda", ""))) %>%
  mutate(age = as.numeric(age),
         stem = factor(stem))
```



This spectral data is required for fig. 2
```{r}


p.df = prefix.df %>%
  filter(labels %in% c("tʃ", "tɕ", "ʃ", "ɕ" )) %>%
  filter(dial!="Im") %>%
  mutate(labels = factor(labels),
         dial = factor(dial), 
         dorlabs = case_when(labels %in% c("tʃ", "ʃ") ~ "AP",
                             T ~ "DO")) %>%
  get_trackdata(db, ., "spectrum",  cut = .5) %>%
  convert_wideToLong(., calcFreqs = T) %>%
  group_by(sl_rowIdx) %>%
  # spectral smoothing with 10 coefficients
  mutate(dctsmooth = dct(track_value, m = 10, fit = T)) %>%
  ungroup() %>%
  filter(freq > 2000 & freq < 8000) %>%
 group_by(dorlabs, freq, dial) %>%
 summarise(track_value = mean(track_value), 
           dctsmooth = mean(dctsmooth)) %>%
 ungroup() 
```


Store in secondary dataset
```{r}
write.table(prefix.df, file.path(pfad2, 
                                 "prefix.df.txt"), quote=F)
write.table(p.df, file.path(pfad2, 
                                 "spectral.df.txt"), quote=F)

