Skip to content

Instantly share code, notes, and snippets.

View fauxneticien's full-sized avatar

Nay San fauxneticien

View GitHub Profile
@fauxneticien
fauxneticien / conditional_freduce.R
Created March 10, 2018 13:55
R: Apply a list of functions conditionally based on some other set of boolean inputs
library(dplyr)
library(purrr)
library(magrittr)
user_input <- c(T, F, T)
pipe_funcs <- list(~ filter(., cyl == 4), ~ select(., cyl), nrow)
map2(.x = user_input, .y = pipe_funcs,
.f = ~ if(.x) as_mapper(.y) else NA) %>%
@fauxneticien
fauxneticien / pdf_wrangler.R
Created March 10, 2018 00:33
Wrangle PDF page headings into a data frame
options(stringsAsFactors = FALSE)
library(tidyverse)
library(pdftools)
library(purrr)
library(zoo)
library(readr)
headers_df <-
# read 406-page PDF of lexicon (i.e. no front matter, reversal list, etc.)
@fauxneticien
fauxneticien / _all-in-one_reprex.R
Last active March 30, 2022 16:56
String parsing using Nearley within R
library(tidyverse)
library(zoo)
library(V8)
lexicon <-
'\\lx rouge
\\ps adjective
\\de red
\\xv La chaise est rouge
\\xe The chair is red
@fauxneticien
fauxneticien / error.py
Created January 31, 2018 09:01
persephone error
In [3]: import run
---------------------------------------------------------------------------
PermissionError Traceback (most recent call last)
<ipython-input-3-c72c9d25002d> in <module>()
----> 1 import run
~/persephone/src/run.py in <module>()
11 import config
12 import rnn_ctc
---> 13 import datasets.na
@fauxneticien
fauxneticien / skinny.ne
Last active January 15, 2018 04:45
Skinny-warl
@lexer wrl_lexer
wrlLexicon -> mainEntry:+
mainEntry -> "\\me" %value englishGloss:+ "\\eme"
englishGloss -> "\\gl" %value:? "\\egl"
@{%
@fauxneticien
fauxneticien / add_ids.R
Last active December 19, 2017 03:56
Add id backslash codes following \me and \sse codes
library(tidyverse)
input_df <-
readLines("Wlp-all_entries.txt") %>%
data.frame(line = 1:length(.), data = .)
id_df <-
input_df %>%
filter(grepl("^\\\\(me|sse)", data)) %>%
mutate(data = paste0("\\_id ", line),
@fauxneticien
fauxneticien / run.R
Last active December 5, 2017 05:48
Pipeline test
library(phonpack)
library(base64enc)
library(googlesheets)
# Get from BitBucket pipeline environment
BATCH_NO <- Sys.getenv("BITBUCKET_REPO_SLUG")
AUTH_STR <- Sys.getenv("GOOGLE_AUTH")
# Convert env variable to txt file, then to RDS file, then supply
# to gs_auth() as RDS file
@fauxneticien
fauxneticien / gs_endecode.R
Last active December 5, 2017 04:05
Encode and decode authentication tokens for googlesheets R package
library(base64enc)
token2text <- function(token_obj, txt_file) {
rds_path <- tempfile()
saveRDS(object = token_obj,
file = rds_path)
# return base64 string
writeLines(base64encode(rds_path), txt_file)
}
@fauxneticien
fauxneticien / gather_names.R
Created November 30, 2017 23:36
Gather names from wide table to long table (and drop column name)
# Use install.packages() on dplyr and tidyr
library(dplyr)
library(tidyr)
# mock up data frame (or use read_xlsx function from readxl package to read in an excel sheet)
wrl_wide <-
data.frame(
Warlpiri = c("wumparlpa", "wartarurru"),
`scientificName 1` = c("ecucalyptus leucophloia", "acacia validinervia"),
`scientificName 2` = c("eucalyptus pruinosa", "acacia jennerae")
@fauxneticien
fauxneticien / getVowelMeans.R
Created November 28, 2017 06:44
Join TextGrid and Formant data output from Praat, then get mean F1 and F2
# How to export TextGrid and Formant data from Praat: http://recordit.co/ybhGxsf99a
# Make sure to setwd() to where annotations.csv and formants.csv are located
library(dplyr)
library(readr)
left_join(
read_csv("annotations.csv") %>% mutate(join_col = "temp"),
read_csv("formants.csv") %>% mutate(join_col = "temp")
) %>%