Skip to content

Instantly share code, notes, and snippets.

View drussellmrichie's full-sized avatar

Russell Richie drussellmrichie

View GitHub Profile
@drussellmrichie
drussellmrichie / lookupTagger
Created March 13, 2015 18:17
Lookup tagger from Chp 5 of NLTK book
# Natural Language Toolkit: code_baseline_tagger
# functions from http://www.nltk.org/book/ch05.html
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'))
@drussellmrichie
drussellmrichie / ottoANN
Created March 25, 2015 16:59
My attempt at an ANN for predicting product category in the Otto Kaggle competition
"""
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 pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer
@drussellmrichie
drussellmrichie / officeHoursSelector.py
Last active January 25, 2016 18:49
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
drussellmrichie / cohortModel.py
Last active October 4, 2016 02:27
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
drussellmrichie / gradeEmailer.py
Last active October 5, 2016 02:03
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:
gradeEmailer.py [--name=NAME] [--password=PASSWORD] [--dry-run] <sender-email> <grades-path>
Options:
--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
drussellmrichie / average_parse_tree_height.py
Last active February 6, 2021 12:04
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 https://stackoverflow.com/questions/35920826/how-to-find-height-for-non-binary-tree
@drussellmrichie
drussellmrichie / lex_dec_model.py
Created April 18, 2021 19:23
lexical decision model
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(
environment=environment,
automatic_visual_search=False,
@drussellmrichie
drussellmrichie / zero-shot-explained.py
Created October 21, 2021 20:01
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