Created
July 6, 2011 09:50
-
-
Save acrymble/1066933 to your computer and use it in GitHub Desktop.
Python HTML to KWIC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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