What I tried was to write the ouput of common_context to a file ans then read from it using splitlines
from nltk.book import *
import contextlib
fnallst=[]
for w in set(text2): ## we choose text2 as text2 has lesser distinct word
filename = "C:\\Users\\username\Log"
with open(filename,'w') as f:
with contextlib.redirect_stdout(f):
text2.common_contexts([w])
with open(filename) as f:
wct2 = f.read().splitlines()
What I tried was to write the ouput of common_context to a file ans then read from it using splitlines