Skip to content

Instantly share code, notes, and snippets.

View almogsi's full-sized avatar

Almog Simchon almogsi

View GitHub Profile
@almogsi
almogsi / ANEW.r
Last active October 3, 2017 11:52
ANEW is the Affective Norms for English Words (Bradley & Lang, 2009). The function takes a string as an argument and returns the mean value of valence, arousal and dominance.
#ANEW function
ANEW_txt <- read.table("Downloaded_ANEW_txt.txt", header = T, sep = "\t") #you should download ANEW first
ANEW_txt$Word <- as.character(ANEW_txt$Word)
library(stringr)
library(tm)
ANEW <- function(a){
a <- as.character(a)
@almogsi
almogsi / NoVAD.R
Last active October 3, 2017 11:49
NoVAD is the Norms of Valence, Arousal and Dominance (Warriner, Kuperman & Brysbaert , 2013). The function takes a string as an argument and returns the mean value of valence, arousal and dominance.
novad <- read.csv("Download_NoVAD_csv.csv") #Change for you local csv file
novad$Word <- as.character(novad$Word)
library(stringr)
library(tm)
NoVAD <- function(a){
a <- as.character(a)
sms_corpus <- Corpus(VectorSource(a))
#translate all letters to lower case
clean_corpus <- tm_map(sms_corpus, tolower)
@almogsi
almogsi / passive.R
Last active August 19, 2019 21:55
This code snippet takes a vector of strings and calculates the percentage of passive voice in the input text. It uses Stanford NLP tool and coreNLP for R.
library(rJava)
library(coreNLP)
initCoreNLP()
#in this case, 'test' is a data frame with a col named 'text'
for (i in 1:dim(test)[1]) {
cat(paste0(i / dim(test)[1] * 100, '% completed'))
ann <- annotateString(paste(test$text[i]), format = c("obj"), outputFile = NA, includeXSL = FALSE)
gd <- getDependency(ann)
@almogsi
almogsi / cat_state.R
Created June 25, 2019 09:50
function for classifying States' political affiliation
cat_state <- function(a, abbreviation=F) {
Swing = c("Wisconsin", "Virginia", "Michigan", "Pennsylvania",
"New Hampshire", "Colorado", "Nevada", "North Carolina",
"Florida", "Ohio", "Arizona", "Iowa", "Georgia")
Swing_abb = state.abb[match(Swing,state.name)]
Democrat = c("New Mexico", "Minnesota", "Maine", "Oregon",
"New Jersey", "Delaware", "Connecticut", "Illinois",
"Washington", "Rhode Island", "New York", "California",
"Massachusetts", "Hawaii", "Maryland", "Vermont")
Democrat_abb = state.abb[match(Democrat,state.name)]
@almogsi
almogsi / lime_with_caret.R
Created March 21, 2020 15:04
Lime with Caret
library(tidyverse)
library(caret)
library(lime)
#fit
lasso_caret<- train(data = mtcars, mpg~., method = "glmnet",
tuneGrid = expand.grid(alpha = 1,
lambda = 0))
#LIME
explainer <- lime(mtcars, lasso_caret) #here the train data
library(tidyverse)
library(quanteda)
library(spacyr)
get_passive <- function(text, ratio = F){
d <- spacy_parse(text, dependency = T)
aux_pass <- d %>%
mutate(doc_id = as.numeric(str_remove_all(doc_id, "text"))) %>%
group_by(doc_id) %>%