Skip to content

Instantly share code, notes, and snippets.

View amintos's full-sized avatar

Toni Mattis amintos

View GitHub Profile
@amintos
amintos / btm.py
Created May 2, 2017 13:42
Bi-term Topic Model implementation in pure Python
"""
Bi-Term Topic Model (BTM) for very short texts.
Literature Reference:
Xiaohui Yan, Jiafeng Guo, Yanyan Lan, and Xueqi Cheng:
"A biterm topic model for short texts"
In Proceedings of WWW '13, Rio de Janeiro, Brazil, pp. 1445-1456.
ACM, DOI: https://doi.org/10.1145/2488388.2488514
This module requires pre-processing of textual data,
@amintos
amintos / reactive_test.py
Created September 4, 2015 12:28
Example for modular event handling using "activities" as Python generators, which are stepped by external events. Activities reacting to events are a single method and not scattered across callbacks or multiple handlers.
class Element:
"""I represent an unspecific UI component"""
def __init__(self, id, env):
self.id = id
self.env = env
self.children = []
def trigger(self, event):
@amintos
amintos / lda.py
Created April 29, 2015 01:00
LDA with Gibbs Sampling in Python
import random
def dirichlet(alpha):
"""Sample dirichlet-distributed vector from Dir(alpha)"""
s = [random.gammavariate(a, 1) for a in alpha]
norm = sum(s)
return [d / norm for d in s]
@amintos
amintos / .gitignore
Last active August 29, 2015 14:09
Sorted .gitignore for output files of LaTeX projects (pdflatex, bibtex, beamer, listings, biber, hyperref, TexMaker, ...)
*.acn
*.acr
*.alg
*.aux
*.bak
*.bbl
*.bcf
*.blg
*.brf
*.dvi
@amintos
amintos / tags.py
Created December 11, 2013 15:36
Hacky super-compact HTML generator for python
#
# Simple Inline X/HTML Generator
# by Toni Mattis
#
'''
Example usage:
from tags import *
print div(cls = 'myclass')[ p['Hello'], hr, p[strong['World']] ]
@amintos
amintos / vm.py
Last active December 30, 2015 00:39
Trans-chroot helper for translating and running the R/Squeak (SPy) VM
#! /usr/bin/python
# --- RPYTHON/CHROOT TOOLCHAIN HELPER ---
#
# This script restarts itself inside the 32-bit chroot environment
# and runs either the translation or a benchmark.
# (!) Double-check that everything uses absolute paths (!)
#
import os
#!/usr/bin/env python
#-*- coding: utf-8 -*-
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
@amintos
amintos / tf512.py
Created September 6, 2013 01:54
Simple string encryption and decryption in pure Python using Threefish-512. No guarantee this implementation is 100% correct!
#
# Simple Pure-Python Threefish-512 Encryption.
# (No guarantee that this implementation is 100% correct!)
#
# Use encrypt(text, key) and decrypt(text, key) for string encryption.
#
# The cipher operates in CBC mode with a random tweak value.
#
from StringIO import StringIO
@amintos
amintos / compressor.java
Created July 6, 2013 20:33
Simple LZ-Like compression in Java.
/** Fast and simple text compression.
*
* This is a modified variant of LZ77:
* The algorithm inserts "marker bytes" whose bits state which
* of the following 8 symbols are actual symbols (0) or backward
* references (1). Backward references are actually two bytes, the
* first 6 bits encoding the length and the next 10 bits encoding
* the negative offset where the repetition occurred.
* The idea is related to LZO and LZJB compression.
*
@amintos
amintos / base64_fixpoint.py
Created February 12, 2013 10:51
Method generating a string of given length which is its own prefix in base64 encoding.
def base64_fixpoint(prefix_len):
s = 'rofl'
t = 'lol'
while s != t:
s = t
t = s.encode("base64")[:prefix_len]
return t