text.concordance(self, word, width=79, lines=25)
似乎还有其他参数manual http://nltk.googlecode.com/svn/trunk/doc/api/nltk.text-pysrc.html#Text.concordance.
我看不出有什么办法可以提取一致性索引的大小,但是索引打印代码 http://nltk.googlecode.com/svn/trunk/doc/api/nltk.text-pysrc.html#ConcordanceIndex.print_concordance似乎有这部分:lines = min(lines, len(offsets))
,因此你可以简单地通过sys.maxint
作为最后一个论点:
concord = text.concordance("cultural", 75, sys.maxint)
Added:
现在看看你的原始代码,我看不出它以前可以工作的方式。text.concordance
不返回任何内容,但将所有内容输出到stdout
using print
。因此,简单的选择是将标准输出重定向到您的文件,如下所示:
import sys
....
# Open the file
fileconcord = open('ccord-cultural.txt', 'w')
# Save old stdout stream
tmpout = sys.stdout
# Redirect all "print" calls to that file
sys.stdout = fileconcord
# Init the method
text.concordance("cultural", 200, sys.maxint)
# Close file
fileconcord.close()
# Reset stdout in case you need something else to print
sys.stdout = tmpout
另一种选择是直接使用相应的类并省略文本包装器。只需复制位here http://nltk.googlecode.com/svn/trunk/doc/api/nltk.text-pysrc.html#Text.concordance并将它们与来自的位组合起来here http://nltk.googlecode.com/svn/trunk/doc/api/nltk.text-pysrc.html#ConcordanceIndex.print_concordance你就完成了。