Skip to content

Instantly share code, notes, and snippets.

View alexander-dejeu's full-sized avatar

Alexander Dejeu alexander-dejeu

  • San Francisco
View GitHub Profile
@alexander-dejeu
alexander-dejeu / markov.py
Created January 28, 2017 18:26
Generate random sentences using a Markov Model
from histograms import Dictogram
import random
from collections import deque
import re
def generate_random_start(model):
# To just generate any starting word uncomment line:
# return random.choice(model.keys())
@alexander-dejeu
alexander-dejeu / markov.py
Created January 28, 2017 18:22
Nth Order Markov Model
from histograms import Dictogram
def make_higher_order_markov_model(order, data):
markov_model = dict()
for i in range(0, len(data)-order):
# Create the window
window = tuple(data[i: i+order])
# Add to the dictionary
@alexander-dejeu
alexander-dejeu / markov.py
Last active January 29, 2017 23:22
Markov Model - basic implementation
from histograms import Dictogram
def make_markov_model(data):
markov_model = dict()
for i in range(0, len(data)-1):
if data[i] in markov_model:
# We have to just append to the existing histogram
markov_model[data[i]].update([data[i+1]])
else:
import random
class Dictogram(dict):
def __init__(self, iterable=None):
"""Initialize this histogram as a new dict; update with given items"""
super(Dictogram, self).__init__()
self.types = 0 # the number of distinct item types in this histogram
self.tokens = 0 # the total count of all item tokens in this histogram
if iterable:
self.update(iterable)
@alexander-dejeu
alexander-dejeu / KVO_Test.swift
Created January 26, 2017 14:53
KVO_Test in Swift
//
// ViewController.swift
// KVO_Test
//
// Created by Alexander Dejeu on 1/25/17.
// Copyright © 2017 Do Good Technology. All rights reserved.
//
import UIKit
import WebKit