Skip to content

Instantly share code, notes, and snippets.

print "I'm cool"
# Model parameters
model_method = "gbm"
model_grid = expand.grid( n.trees = 5000,
interaction.depth = 5,
shrinkage = 0.03,
n.minobsinnode = 20)
extra_params = NULL
# Cross-validation parameters
do_cv = TRUE
# Get most similar authors
author = "Aristotle"
most_similar = sort(similarity_matrix[author, ], decreasing=TRUE)
most_similar = most_similar[-1] # exclude top element
most_similar[1:10]
# Output:
# Plato Ralph Waldo Emerson Samuel Johnson Francis Bacon George Bernard Shaw
# 0.3762767 0.3170570 0.3099014 0.2928849 0.2922911
# This script computes the similarity matrix of authors based on their quotes.
# We will use the quanteda library to parse and tokenize the quotes.
# Similarities are then used from the Vector Space Model, where authors
# represent "documents".
library(quanteda)
library(dplyr)
# Load in the quotes data
quotes = read.csv('quotes.csv', stringsAsFactors = FALSE)
# -*- coding: utf-8 -*-
import csv
import logging
from scrapy import Spider, Request
from scrapy.selector import Selector
from brainyquote.items import BrainyQuoteItem, SearchResultItem
pantheon_file = '../data/pantheon.tsv'
missing_authors_file = '../data/missing_authors.txt'
open(missing_authors_file, 'w') # empty the file
# Get artist network
# Arguments:
# artists_ids - list of artist ids
# Returns:
# list containing artists and edge dataframes
get_artist_network = function(artist_ids){
# Get artists details
max_ids = min(MAX_NUM_ARTISTS, length(artist_ids))
URI = paste0('https://api.spotify.com/v1/artists?ids=', paste(artist_ids[1:max_ids], collapse=","))
# Javascript functions to set audio source, play and pause the audio
js_set_audio_src = "shinyjs.setAudioSrc = function(source){document.getElementById('audio_player').src=source};"
js_pause_audio = "shinyjs.pauseAudio = function(){document.getElementById('audio_player').pause()};"
js_play_audio = "shinyjs.playAudio = function(){document.getElementById('audio_player').play()};"
# Values stores the last point so we can turn off the audio
Values<-reactiveValues(old="Start")
# Preview the track when the user clicks on the graph point
# Pause the track if it's the same point
tracks_features_scatter <- gvisScatterChart(scatter,
options = list(
tooltip = "{isHtml:'True'}",
legend =
"none", lineWidth = 0, pointSize = 5,
title = "Track Features",
vAxis = paste0("{title:'", plot_labels[input$scatter_ycol], "'}"),
hAxis = paste0("{title:'", plot_labels[input$scatter_xcol], "'}"),
width = 1000, height = 500,
gvis.listener.jscode = "
library(jsonlite) # for parsing JSON
# Get a playlist list
# Arguments:
# user_id - string
# playlist_id - string
# Returns:
# list of playlist info
get_playlist = function(user_id, playlist_id){
library(httr) # for sending requests
# Get the Spotify token header for API calls
# Returns:
# Header for GET requests
get_token_header = function(){
clientID = 'your client id here'
secret = 'your secret id here'
response = POST(