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
| # 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: |
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
| 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 | |
| ... |
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-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) |
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
| # 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 |
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
| print ' '.join(kwic[(keyindex+1):]) | |
| -> there was one |
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
| print '#' + str(kwic[keyindex]).center(len(kwic[keyindex])+6) + '#' | |
| -> # black # |
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
| width = 10 | |
| print '#' + ' '.join(kwic[:keyindex]).rjust(width*keyindex) + '#' | |
| -> # amongst them a# |
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
| # 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):]) |
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) |
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
| whole = wrapper % (program, now, url, url, body) |