Created
July 6, 2011 08:33
-
-
Save acrymble/1066833 to your computer and use it in GitHub Desktop.
Python ngrams to KWIC dictionary
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: | |
| kwicdict[k[keyindex]] = [k] | |
| else: | |
| kwicdict[k[keyindex]].append(k) | |
| return kwicdict |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment