Skip to content

Instantly share code, notes, and snippets.

View acrymble's full-sized avatar

Adam Crymble acrymble

  • London
View GitHub Profile
@acrymble
acrymble / ngrams-to-kwic-dict.py
Created July 6, 2011 08:33
Python ngrams to KWIC dictionary
# Given a list of n-grams, return a dictionary of KWICs,
# indexed by keyword.
def nGramsToKWICDict(ngrams):
keyindex = len(ngrams[0]) // 2
kwicdict = {}
for k in ngrams:
if k[keyindex] not in kwicdict:
@acrymble
acrymble / pretty-printing.txt
Created July 6, 2011 08:33
Pretty Printing
amongst them a black there was one
first saw the black i turned to
had observed the black in the mob
say who that black was no seeing
i saw a black at first but
swear to any black yes there is
swear to a black than to a
...
@acrymble
acrymble / html-to-pretty-print.py
Created July 6, 2011 09:23
Python HTML to Pretty Print
# html-to-pretty-print.py
import obo
# create dictionary of n-grams
n = 7
url = 'http://www.oldbaileyonline.org/print.jsp?div=t17800628-33'
text = obo.webPageToText(url)
fullwordlist = obo.stripNonAlphaNum(text)
ngrams = obo.getNGrams(fullwordlist, n)
@acrymble
acrymble / slice.py
Created July 6, 2011 09:24
Python slice
# calculate the length of the n-gram
kwic = 'amongst them a black there was one'.split()
n = len(kwic)
print n
-> 7
# calculate the index position of the keyword
keyindex = n // 2
print keyindex
-> 3
@acrymble
acrymble / print-kwic.py
Created July 6, 2011 09:40
Python Print KWIC
print ' '.join(kwic[(keyindex+1):])
-> there was one
@acrymble
acrymble / print-kwic.py
Created July 6, 2011 09:41
Python Print KWIC
print '#' + str(kwic[keyindex]).center(len(kwic[keyindex])+6) + '#'
-> # black #
@acrymble
acrymble / print-kwic.py
Created July 6, 2011 09:43
Python Print KWIC
width = 10
print '#' + ' '.join(kwic[:keyindex]).rjust(width*keyindex) + '#'
-> # amongst them a#
@acrymble
acrymble / pretty-print-kwic.py
Created July 6, 2011 09:49
Python KWIC Pretty Printing
# Given a KWIC, return a string that is formatted for
# pretty printing.
def prettyPrintKWIC(kwic):
n = len(kwic)
keyindex = n // 2
width = 10
outstring = ' '.join(kwic[:keyindex]).rjust(width*keyindex)
outstring += str(kwic[keyindex]).center(len(kwic[keyindex])+6)
outstring += ' '.join(kwic[(keyindex+1):])
@acrymble
acrymble / html-to-kwic.py
Created July 6, 2011 09:50
Python HTML to KWIC
# html-to-kwic.py
import obo
# create dictionary of n-grams
n = 7
url = 'http://www.oldbaileyonline.org/print.jsp?div=t17800628-33'
text = obo.webPageToText(url)
fullwordlist = ('# ' * (n//2)).split()
fullwordlist += obo.stripNonAlphaNum(text)
@acrymble
acrymble / string-formatting-operator.py
Created September 22, 2011 11:24
String Formatting Operator
whole = wrapper % (program, now, url, url, body)