Skip to content

Instantly share code, notes, and snippets.

@acrymble
Created July 6, 2011 09:50
Show Gist options
  • Select an option

  • Save acrymble/1066933 to your computer and use it in GitHub Desktop.

Select an option

Save acrymble/1066933 to your computer and use it in GitHub Desktop.
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)
fullwordlist += ('# ' * (n//2)).split()
ngrams = obo.getNGrams(fullwordlist, n)
worddict = obo.nGramsToKWICDict(ngrams)
# output KWIC and wrap with html
target = 'black'
outstr = '<pre>'
if worddict.has_key(target):
for k in worddict[target]:
outstr += obo.prettyPrintKWIC(k)
outstr += '<br />'
else:
outstr += 'Keyword not found in source'
outstr += '</pre>'
obo.wrapStringInHTML('html-to-kwic', url, outstr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment