Skip to content

Instantly share code, notes, and snippets.

View fnielsen's full-sized avatar

Finn Årup Nielsen fnielsen

View GitHub Profile
@fnielsen
fnielsen / mandelbrot.py
Created September 5, 2011 12:39
Mandelbrot
xs = [ float(i)/64.0 for i in range(-150, 41) ]
ys = [ float(i)/16.0 for i in range(-25,26) ]
for y in ys:
s = ''
for x in xs:
z = 0j; i = 0
while i < 10:
z = z**2 + x+y*1j
if abs(z) > 2:
break # Get out of inner loop
@fnielsen
fnielsen / gist:1226214
Created September 19, 2011 09:32
Email classification example with Python, NLTK, ...
documents = [ dict(
email=open("conference/%d.txt" % n).read().strip(),
category='conference') for n in range(1,372) ]
documents.extend([ dict(
email=open("job/%d.txt" % n).read().strip(),
category='job') for n in range(1,275)])
documents.extend([ dict(
email=open("spam/%d.txt" % n).read().strip(),
category='spam') for n in range(1,799) ])
@fnielsen
fnielsen / gist:1373887
Created November 17, 2011 17:50
Co-author mining for papers in the Brede Wiki
import matplotlib.pyplot as plt
import networkx as nx
from pysqlite2 import dbapi2
connection = dbapi2.Connection('bredewiki-templates.sqlite3')
sql = "SELECT DISTINCT pid FROM brede WHERE (template='paper' OR template='conference_paper');"
cursor = connection.cursor()
cursor.execute(sql)
pids = [ row[0] for row in cursor.fetchall() ]
@fnielsen
fnielsen / gist:1384457
Created November 22, 2011 00:19
Co-author mining with shortest path in the Brede Wiki
# wget http://neuro.imm.dtu.dk/services/bredewiki/download/bredewiki-templates.sqlite3
import matplotlib.pyplot as plt
import networkx as nx
from pysqlite2 import dbapi2
connection = dbapi2.Connection('bredewiki-templates.sqlite3')
sql = "SELECT DISTINCT tid FROM brede WHERE (template='paper' OR template='conference_paper');"
cursor = connection.cursor()
cursor.execute(sql)
tids = [ row[0] for row in cursor.fetchall() ]
@fnielsen
fnielsen / gist:1392607
Created November 25, 2011 01:29
Computation of co-author distance in the Brede Wiki co-author graph
# wget http://neuro.imm.dtu.dk/services/bredewiki/download/bredewiki-templates.sqlite3
import matplotlib.pyplot as plt
import networkx as nx
from pysqlite2 import dbapi2
connection = dbapi2.Connection('bredewiki-templates.sqlite3')
sql = "SELECT DISTINCT tid FROM brede WHERE (template='paper' OR template='conference_paper');"
cursor = connection.cursor()
cursor.execute(sql)
tids = [ row[0] for row in cursor.fetchall() ]
@fnielsen
fnielsen / gist:1410094
Created November 30, 2011 18:12
Movie review sentiment classifier and the AFINN word list
# Code inspired and developed from:
# http://streamhacker.com/2010/05/10/text-classification-sentiment-analysis-naive-bayes-classifier/
from __future__ import division
import nltk.classify, nltk.corpus, nltk.classify.util
from pylab import *
filebase = '/home/fn'
@fnielsen
fnielsen / Nielsen2012Numpy_quotes.py
Created June 12, 2012 17:33
Wikipedia/stock quote visualization with Python and matplotlib
import urllib, urllib2
import simplejson as json
import dateutil.parser
import datetime
import matplotlib.dates
import matplotlib.finance
from matplotlib import pyplot as plt
import nltk.corpus
import numpy as np
import re
@fnielsen
fnielsen / Nielsen2012Python_case.py
Last active April 29, 2020 23:49
Text mining example in Python
# $Id: Nielsen2012Python_case.py,v 1.2 2012/09/02 16:55:25 fn Exp $
# Define a url as a Python string (note we are only getting 100 documents)
url = "http://wikilit.referata.com/" + \
"wiki/Special:Ask/" + \
"-5B-5BCategory:Publications-5D-5D/" + \
"-3FHas-20author%3DAuthor(s)/-3FYear/" + \
"-3FPublished-20in/-3FAbstract/-3FHas-20topic%3DTopic(s)/" + \
"-3FHas-20domain%3DDomain(s)/" + \
"format%3D-20csv/limit%3D-20100/offset%3D0"
# python2.6
'Finn Årup Nielsen'
u'Finn Årup Nielsen'
unicode('Finn Årup Nielsen', 'utf-8')
# So what is wrong with that?}
#!/usr/bin/python
import math, sys # Importing modules.
def formatresult(res): # Define function. Remember colon!
"""This is the documentation for a function."""
return "The result is %f" % res # Percentage for formating
if len(sys.argv) < 3: # Conditionals should be indended
print("Too few input argument")