Skip to content

Instantly share code, notes, and snippets.

@acrymble
Created July 6, 2011 08:33
Show Gist options
  • Select an option

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

Select an option

Save acrymble/1066833 to your computer and use it in GitHub Desktop.
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:
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