Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View trinker's full-sized avatar

Tyler Rinker trinker

View GitHub Profile
@trinker
trinker / look_and_say_sequence.R
Last active September 15, 2022 15:35
look and say sequence
# https://mathworld.wolfram.com/LookandSaySequence.html
library(ggplot2)
library(stringi)
tic <- Sys.time()
len <- 68
s <- rep(NA, len)
s[1] <- 1
tm <- rep(NA, len)
prob <- unlist(lapply(1:1000000, function(i){
candles <- sort(runif(2, 0, 1))
cut <- runif(1, 0, 1)
(cut > candles[1]) & (cut < candles[2])
}))
mean(prob)
## https://youtu.be/094y1Z2wpJg
library(tidyverse)
collatz <- function(x){
v = c(x)
i = 1
while (v[i] != 1){
@trinker
trinker / fgsub.py
Last active March 29, 2021 19:02
textread fgsub equivalent in python
text = ['df dft sdf', 'sd fdggg sd dfhhh d', 'ddd']
def dbllttrwordrev(match):
match = match.group()
return '<<{}>>'.format(match[::-1])
{
'function': [re.sub("\\b\\w*([a-z])(\\1{2,})\\w*\\b", dbllttrwordrev, x, flags = re.IGNORECASE) for x in text],
'lambda': [re.sub("\\b\\w*([a-z])(\\1{2,})\\w*\\b", lambda x: '<<{}>>'.format(x.group()[::-1]) , x, flags = re.IGNORECASE) for x in text]
@trinker
trinker / quanteda_wordcloud.R
Created July 8, 2020 19:11
Wordcloud with quanteda
## Load dependencies
library(quanteda)
library(sentimentr)
library(tidyverse)
library(lexicon)
## Data set from sentimentr package
dat <- presidential_debates_2012
dat
corp <- corpus(dat, text_field = "dialogue")
@trinker
trinker / gist:a75144f8d90738a169dc4c99f4ad3717
Last active March 24, 2020 18:18
Norah's Phoneme Question
## Norah's Question: Are there any words that start with chr (phoenetically /k/ /r/) that don't have a a short i sound following it?
library(openssl)
library(textshape)
library(tidyverse)
cmudict <- readLines('https://raw.githubusercontent.com/michelleful/ToBoldlyStress/master/stressed_spelling.txt')
cmudict7b <- readLines('http://svn.code.sf.net/p/cmusphinx/code/trunk/cmudict/cmudict-0.7b') %>% tail(-121) %>% head(-4)
@trinker
trinker / rowwise_subsets.R
Created September 9, 2019 16:39
Rowwise subsets in dplyr
library(dplyr)
dat <- tibble(
x1=c(1,0,0,NA,0,1,1,NA,0,1),
x2=c(1,1,NA,1,1,0,NA,NA,0,1),
x3=c(0,1,0,1,1,0,NA,NA,0,1),
y4=c(1,0,NA,1,0,0,NA,0,0,1),
y5=c(1,1,NA,1,1,1,NA,1,0,1),
z = LETTERS[1:10]
)
library(tidyverse)
library(gridExtra)
plot1 <- ggplot(mtcars, aes(x = hp)) +
geom_histogram(bins = 10) +
labs(
y = 'Count of Awesomeness',
title = 'Count Histogram'
)
@trinker
trinker / roc.R
Created March 23, 2019 16:42
Calculating AUC: the area under a ROC Curve
## https://blog.revolutionanalytics.com/2016/11/calculating-auc.html
## Load Dependency
library(numform)
##=======================================================
## Make some fake data
##=======================================================
set.seed(10)
actual <- sample(0:1, 100, T, c(.8, .2))
@trinker
trinker / udpipeFormality.R
Created March 22, 2019 19:14
formality_with_udpipe
##==============================================================================
## Formality
##==============================================================================
## 1. tag parts of speech
## 2. convert to generic POS
## 3. COmpute formality off POS
udmodel <- udpipe::udpipe_download_model(language = "english")
udmodel <- udpipe::udpipe_load_model(file = udmodel$file_model)