sakeesh
sakeesh

Reputation: 1039

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

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.

Upvotes: 0

Views: 96

Answers (1)

sakeesh
sakeesh

Reputation: 1039

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

Upvotes: 0

Related Questions