Skip to content

Instantly share code, notes, and snippets.

@fauxneticien
Last active November 9, 2017 06:44
Show Gist options
  • Select an option

  • Save fauxneticien/a67916d80e9db346d539d0d04ee0b82b to your computer and use it in GitHub Desktop.

Select an option

Save fauxneticien/a67916d80e9db346d539d0d04ee0b82b to your computer and use it in GitHub Desktop.
Warlpiri reversalizer
library(shiny)
library(dplyr)
library(stringr)
library(zoo)
library(purrr)
library(tidyr)
options(shiny.maxRequestSize=30*1024^2)
reversalize <- function(wrl_txt_path) {
wrl_dict <- readLines(wrl_txt_path)
wrl_df <-
data.frame(
stringsAsFactors = FALSE,
line = 1:length(wrl_dict),
data = wrl_dict
) %>%
mutate(
bs_code = str_extract(data, "^\\\\[a-z]+") %>% str_replace("^\\\\", ""),
parent = ifelse(bs_code %in% c("me", "sse"), data, NA),
parent = na.locf(parent, na.rm = FALSE)) %>%
filter(bs_code == "gl") %>%
mutate(gloss = str_replace_all(data, "\\\\e?gl", "") %>% str_trim(),
parent = str_replace_all(parent, "\\\\me |\\\\sse", "") %>% str_trim()) %>%
select(gloss, parent)
gloss_df <-
map2_df(wrl_df$gloss, wrl_df$parent, function(gloss, parent) {
gloss_string <- str_split(gloss, ", ") %>% unlist()
gloss_group <- str_match_all(gloss_string, "\\^([^\\s]+)")[[1]][,2]
gloss_group <- ifelse(identical(gloss_group, character(0)), NA, gloss_group)
data.frame(
stringsAsFactors = FALSE,
gloss_string,
parent
)
})
gloss_df %>%
rowwise %>%
mutate(parents = str_extract_all(gloss_string, "\\^([^\\s]+)")[[1]] %>% paste0(collapse = ","),
parents = str_split(parents, ",")) %>%
unnest(.drop = FALSE) %>%
mutate(parents = str_replace(parents, "\\^", ""),
parents = ifelse(nchar(parents) == 0, gloss_string, parents)) %>%
group_by(parents, gloss_string) %>%
summarise(words = paste0(parent, collapse = ", ")) %>%
rename(Parent = parents, Gloss = gloss_string, Warlpiri = words)
}
# Define UI for application that accepts a .txt file upload of the Warlpiri lexicon
# and processes the data to make a reverse finder list
ui <- fluidPage(
# Application title
titlePanel("Warlpiri reversalizer"),
fluidRow(
column(12,fileInput("file1", "Upload .txt File",
multiple = FALSE,
accept = c(".txt")
)),
column(12, tableOutput("reversal_result"))
)
# # Sidebar with a slider input for number of bins
# sidebarLayout(
# sidebarPanel(
# fileInput("file1", "Upload .txt File",
# multiple = FALSE,
# accept = c(".txt")
# )
# ),
#
# # Show a plot of the generated distribution
# mainPanel(
# tableOutput("reversal_result")
# )
# )
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$reversal_result <- renderTable({
req(input$file1)
reversalize(input$file1$datapath)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment