我已经在 SO 上进行了大量的潜伏,并进行了大量的搜索和阅读,但我也必须承认在一般编程方面是一个相对的菜鸟。我正在努力学习,所以我一直在玩 Python 的 NLTK。在下面的脚本中,我可以让一切正常工作,除了它只写多屏输出的第一个屏幕,至少我是这么想的。
这是脚本:
#! /usr/bin/env python
import nltk
# First we have to open and read the file:
thefile = open('all_no_id.txt')
raw = thefile.read()
# Second we have to process it with nltk functions to do what we want
tokens = nltk.wordpunct_tokenize(raw)
text = nltk.Text(tokens)
# Now we can actually do stuff with it:
concord = text.concordance("cultural")
# Now to save this to a file
fileconcord = open('ccord-cultural.txt', 'w')
fileconcord.writelines(concord)
fileconcord.close()
这是输出文件的开头:
Building index...
Displaying 25 of 530 matches:
y . The Baobab Tree : Stories of Cultural Continuity The continuity evident
regardless of ethnicity , and the cultural legacy of Africa as well . This Af
为了将整个 530 个匹配项写入文件,我在这里缺少什么?