nltk.book 中的 common_contexts 返回 NoneType ,因此如何将其输出存储到变量中
from nltk.book import *
wtc=text1.common_contexts(['you'])
print(wtc)
print(type(wtc))
上面的 wtc 变量将返回 NONE。
我尝试将 common_context 的输出写入文件,然后使用拆分线从中读取
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()