
# load required libraries
#########################

library('dplyr')
library('ggplot2')
source('coeff.R') # the unalikeability coefficient of Kader and Perry
library('ggthemes')

# read in the data for analysis
###############################

sentences <- read.csv('sentences.csv')
fi0 <- read.csv('FI0.RESULTS.txt')
fi <- read.csv('FI.RESULTS.txt')
fix <- read.csv('FIX.RESULTS.txt')
indo <- read.csv('INDO.RESULTS.txt')
hu <- read.csv('HU.RESULTS.txt')

ucmax <- 0.866 # based on 7 pronouns in output set

# add the variation measures
# ===========================

sentences$uc_fi0 = NA
sentences$uc_fi = NA
sentences$uc_fix = NA
sentences$uc_indo = NA
sentences$uc_hu = NA

# add the UC 
# ##########

for (ix in as.vector(sentences$sentID)) {
  gp_fi0 <- filter(fi0, sentID == ix)
  gp_fi <- filter(fi, sentID == ix)
  gp_fix <- filter(fix, sentID == ix)
  gp_indo <- filter(indo, sentID == ix)
  gp_hu <- filter(hu, sentID == ix)
  
  sentences$uc_fi0[sentences$sentID == ix] <- ucoeff(gp_fi0$pronoun)
  sentences$uc_fi[sentences$sentID == ix] <- ucoeff(gp_fi$pronoun)
  sentences$uc_fix[sentences$sentID == ix] <- ucoeff(gp_fix$pronoun)
  sentences$uc_indo[sentences$sentID == ix] <- ucoeff(gp_indo$pronoun)
  sentences$uc_hu[sentences$sentID == ix] <- ucoeff(gp_hu$pronoun)
}

# ... and the UCA
#################

sentences <- mutate(sentences, uca_fi0= uc_fi0/ucmax)
sentences <- mutate(sentences, uca_fi= uc_fi/ucmax)
sentences <- mutate(sentences, uca_fix= uc_fix/ucmax)
sentences <- mutate(sentences, uca_indo= uc_indo/ucmax)
sentences <- mutate(sentences, uca_hu= uc_hu/ucmax)

# data for Table 1
##################

fi0_count <- fi0 %>%
  group_by(pronoun) %>% 
  summarise(n=n())

fi_count <- fi %>%
  group_by(pronoun) %>% 
  summarise(n=n())

fix_count <- fix %>%
  group_by(pronoun) %>% 
  summarise(n=n())

indo_count <- indo %>%
  group_by(pronoun) %>% 
  summarise(n=n())

hu_count <- hu %>%
  group_by(pronoun) %>% 
  summarise(n=n())

# data for Table 2
##################

summary(sentences$uca_fi0)
summary(sentences$uca_fi)
summary(sentences$uca_indo)
summary(sentences$uca_hu)

# data for Table 3
##################

verbs <- sentences %>%
  group_by(verb) %>%
  summarise(n = n(), 
            uca_fi.mean = mean(uca_fi),  
            uca_indo.mean = mean(uca_indo),
            uca.hu.mean = mean(uca_hu))

verbs2 <- filter(verbs, n >= 2)

# data for Table 4 (ANOVA test)
###############################

sentences2 <- filter(sentences, sentences$verb %in% verbs2$verb)
aov (uca_fi ~ verb, data = sentences2) %>%  summary()
aov (uca_indo ~ verb, data = sentences2) %>%  summary()
aov (uca_hu ~ verb, data = sentences2) %>%  summary()

# Figure 1
##########

fi %>%
  ggplot() +
  aes(x=sentID, fill=pronoun) +
  geom_bar() +
  theme_stata(base_family = 'serif', base_size = 18) +
  ggtitle('Pronoun Variation, Finnish Back-Trans.')  +
  theme(plot.background = element_rect(fill = "white", colour = "white")) +
  theme(plot.title = element_text(vjust = -3)) +
  xlab('Sentence 1 .. 56') + ylab('Count') +
  theme(axis.text=element_text(size=18), axis.title=element_text(size=20))

indo %>%
  ggplot() +
  aes(x=sentID, fill=pronoun) +
  geom_bar() +
  theme_stata(base_family = 'serif', base_size = 18) +
  ggtitle('Pronoun Variation, Indonesian Back-Trans.')  +
  theme(plot.background = element_rect(fill = "white", colour = "white")) +
  theme(plot.title = element_text(vjust = -3)) +
  xlab('Sentence 1 .. 56') + ylab('Count') +
  theme(axis.text=element_text(size=18), axis.title=element_text(size=20))

hu %>%
  ggplot() +
  aes(x=sentID, fill=pronoun) +
  geom_bar() +
  theme_stata(base_family = 'serif', base_size = 18) +
  ggtitle('Pronoun Variation, Hungarian Back-Trans.')  +
  theme(plot.background = element_rect(fill = "white", colour = "white")) +
  theme(plot.title = element_text(vjust = -3)) +
  xlab('Sentence 1 .. 56') + ylab('Count') +
  theme(axis.text=element_text(size=18), axis.title=element_text(size=20))

# Figure 2
##########

sentences %>%
  ggplot() +
  aes(x = uca_fi, y = uca_hu) +
  geom_point() +
  theme_stata(base_family = 'serif', base_size = 18) +
  ggtitle("UCA per sentence, Hungarian vs Finnish")   +
  theme(plot.background = element_rect(fill = "white", colour = "white")) +
  theme(panel.background = element_rect(fill = "gray95", colour = "gray95")) +
  theme(plot.title = element_text(vjust = -4)) +
  xlab('Adjusted unalikeability, Finnish') +
  ylab('Adjusted unalikeability, Hungarian') +
  theme(axis.text=element_text(size=18), axis.title=element_text(size=20))

sentences %>%
  ggplot() +
  aes(x = uca_indo, y = uca_hu) +
  geom_point() +
  theme_stata(base_family = 'serif', base_size = 18) +
  ggtitle("UCA per sentence, Hungarian vs Indonesian")   +
  theme(plot.background = element_rect(fill = "white", colour = "white")) +
  theme(panel.background = element_rect(fill = "gray95", colour = "gray95")) +
  theme(plot.title = element_text(vjust = -4)) +
  xlab('Adjusted unalikeability, Indonesian') +
  ylab('Adjusted unalikeability, Hungarian') +
  theme(axis.text=element_text(size=18), axis.title=element_text(size=20))

# correlation
#############

cor(sentences$uc_fi, sentences$uca_hu)  
cor(sentences$uc_indo, sentences$uca_hu)  





