Skip to content

Instantly share code, notes, and snippets.


Russell Richie drussellmrichie

View GitHub Profile
drussellmrichie /
Created Oct 21, 2021
Example of Shapley explanations for a zero-shot classifier
from transformers import pipeline
from numpy import argmax
import shap
from transformers import RobertaConfig, RobertaModel
from transformers import RobertaTokenizer
import torch
import numpy as np
import scipy as sp
# import datasets
import pandas as pd
words = [{1: {'text': 'elephant', 'position': (320, 180)}},
{2: {'text': 'wug', 'position': (220, 140)}},
{3: {'text': 'dog', 'position': (320, 180)}}]
import pyactr as actr
environment = actr.Environment(focus_position=(0,0))
lex_decision = actr.ACTRModel(
drussellmrichie /
Last active Feb 6, 2021
Computes the average dependency parse tree height for a sequence of sentences
import spacy
import numpy as np
nlp = spacy.load('en', disable=['ner'])
def tree_height(root):
Find the maximum depth (height) of the dependency parse of a spacy sentence by starting with its root
Code adapted from
drussellmrichie /
Last active Oct 5, 2016
Script to extract HW grades from a table, and send separate emails to students with their grades. Potentially useful for a TA of large classes. (Any elegance or cleverness is due to @hsharrison, not me!)
"""Usage: [--name=NAME] [--password=PASSWORD] [--dry-run] <sender-email> <grades-path>
--name=NAME The name of the grader.
Used in the signature and From field of the email.
If not given, <sender-email> will be used.
--password=PASSWORD The password to the sender's email account.
If not given, it will be prompted for.
--dry-run To test, send emails to yourself instead of to the students.
drussellmrichie /
Last active Oct 4, 2016
A quick and dirty implementation of the cohort model of word recognition (devised by Marslen-Wilson, I believe?)
def cohortModel(word, EnglishWords):
soFar = '' # when we start, we haven't heard anything yet, so we'll represent that as an empty string
candidates = set(EnglishWords) # before we've heard anything, all the words we know are possible
for letter in word: # then start listening to the word letter by letter
soFar += letter # add the newly heard letter to the portion of the word heard so far
for word in set(candidates): # now look through the candidate words
if not word.startswith(soFar): # if the word seen so far is NOT consistent with a word we know
candidates.remove(word) # remove the word from the candidates
print("These are the possible words when we've heard {} so far:\n{}".format(str(soFar),str(candidates)))
return candidates
drussellmrichie /
Last active Jan 25, 2016
A script to help pick office hours to fit student schedules
This is a script I wrote to help me select office hours for my spring 2016 TAship of
COGS 2201: Foundations in Cognitive Science at the University of Connecticut.
# import a few packages that we'll need to structure the doodle data, change the working directory,
# and compute the pairs of dates/times.
import pandas as pd
import os, itertools
drussellmrichie / ottoANN
Created Mar 25, 2015
My attempt at an ANN for predicting product category in the Otto Kaggle competition
View ottoANN
With 93/120/9 FFN with sigmoid hidden layer, and fixing the activations on
the output layer (make them sum to one, put on interval 0-1), and train for
5 epochs, only get score of 1.28
import pandas as pd
import os
from import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer
drussellmrichie / lookupTagger
Created Mar 13, 2015
Lookup tagger from Chp 5 of NLTK book
View lookupTagger
# Natural Language Toolkit: code_baseline_tagger
# functions from
from nltk.corpus import brown
import nltk
def performance(cfd, wordlist):
lt = dict((word, cfd[word].max()) for word in wordlist)
baseline_tagger = nltk.UnigramTagger(model=lt, backoff=nltk.DefaultTagger('NN'))
return baseline_tagger.evaluate(brown.tagged_sents(categories='news'))