I have a Python list containing hundreds of thousands of words. The words appear in the order they are in the text.
I’m looking to create a dictionary of each word associated with a string containing that word with 2 (say) words that appear before and after it.
For example the list: “This” “is” “an” “example” “sentence”
Should become the dictionary:
"This" = "This is an"
"is" = "This is an example"
"an" = "This is an example sentence"
"example" = "is an example sentence"
"sentence" = "an example sentence"
Something like:
WordsInContext = Dict()
ContextSize = 2
wIndex = 0
for w in Words:
WordsInContext.update(w = ' '.join(Words[wIndex-ContextSize:wIndex+ContextSize]))
wIndex = wIndex + 1
This may contain a few syntax errors, but even if those were corrected, I’m sure it would be a hideously inefficient way of doing this.
Can someone suggest a more optimized method please?
My suggestion: