How to take output of nltk.book, common_contexts function to a variable

92 Views Asked by At

common_contexts in nltk.book returns NoneType and hence how is it possible to store its output to a variable

from nltk.book import *
wtc=text1.common_contexts(['you'])
print(wtc)
print(type(wtc))

wtc variable above will return NONE.

1

There are 1 best solutions below

0
sakeesh On

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()