Skip to content

Instantly share code, notes, and snippets.

View fnielsen's full-sized avatar

Finn Årup Nielsen fnielsen

View GitHub Profile
@fnielsen
fnielsen / gist:3810848
Created October 1, 2012 10:39
Tkinter mouse example
from Tkinter import *
root = Tk()
canvas = Canvas(root, width=400, height=200)
canvas.pack()
canvas.create_oval(10, 10, 110, 60, fill="grey")
canvas.create_text(60, 35, text="Oval")
canvas.create_rectangle(10, 100, 110, 150, outline="blue")
canvas.create_text(60, 125, text="Rectangle")
canvas.create_line(60, 60, 60, 100, width=3)
@fnielsen
fnielsen / imagenet_label_to_wordnet_synset.txt
Created August 23, 2017 12:44
ImageNet label to wordnet synset identifier
{0: {'id': '01440764-n',
'label': 'tench, Tinca tinca',
'uri': 'http://wordnet-rdf.princeton.edu/wn30/01440764-n'},
1: {'id': '01443537-n',
'label': 'goldfish, Carassius auratus',
'uri': 'http://wordnet-rdf.princeton.edu/wn30/01443537-n'},
2: {'id': '01484850-n',
'label': 'great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias',
'uri': 'http://wordnet-rdf.princeton.edu/wn30/01484850-n'},
3: {'id': '01491361-n',
@fnielsen
fnielsen / brownzipf.py
Last active November 6, 2022 12:32
Zipf plot from word counts in the Brown corpus
from __future__ import division
from itertools import *
from pylab import *
from nltk.corpus import brown
from string import lower
from collections import Counter
# The data: token counts from the Brown corpus
tokens_with_count = Counter(imap(lower, brown.words()))
@fnielsen
fnielsen / Federated queries with Virtuoso.py
Created May 10, 2017 12:11
Federated queries with Virtuoso
"""Federated queries with Virtuoso."""
import sys
from SPARQLWrapper import SPARQLWrapper, JSON
QUERY_WIKIDATA = """
PREFIX wd: <http://www.wikidata.org/entity/>
@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) ])
#!//usr/bin/env python
"""
Usage:
url_to_sentence_collector_sentences <url>
Description:
https://commonvoice.mozilla.org/sentence-collector/#/how-to
"""
from docopt import docopt
@fnielsen
fnielsen / afinn.py
Last active May 6, 2021 11:41
Simplest sentiment analysis in Python with AFINN
#!/usr/bin/python
#
# (originally entered at https://gist.github.com/1035399)
#
# License: GPLv3
#
# To download the AFINN word list do:
# wget http://www2.imm.dtu.dk/pubdb/views/edoc_download.php/6010/zip/imm6010.zip
# unzip imm6010.zip
#
@fnielsen
fnielsen / gist:3904327
Created October 17, 2012 08:11
Python shelve concurrency
import shelve
import multiprocessing
import os
filename = "tmp.shelve"
N = 4
end = 10000
def insert((offset, jump, end, filename)):
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@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"