我有以下内容:
fdist = FreqDist(text)
我想将以下表格结果输出到CSV(而不是python控制台)。
fdist.tabulate()
我该怎么做?
答案 0 :(得分:2)
您可以将FreqDist
视为dict
,并使用csv
模块。例如:
from nltk import FreqDist
import csv
fdist = FreqDist("aaa b cccc dd e")
with open("fdist.csv", "wb") as fp:
writer = csv.writer(fp, quoting=csv.QUOTE_ALL)
writer.writerows(fdist.items())
产生
>>> !cat fdist.csv
" ","4"
"c","4"
"a","3"
"d","2"
"b","1"
"e","1"
答案 1 :(得分:0)
您可以查看(复制)their source并根据需要更改打印语句以编写CSV文件。来源复制如下:
def tabulate(self, *args, **kwargs):
"""
Tabulate the given samples from the frequency distribution (cumulative),
displaying the most frequent sample first. If an integer
parameter is supplied, stop after this many samples have been
plotted. If two integer parameters m, n are supplied, plot a
subset of the samples, beginning with m and stopping at n-1.
(Requires Matplotlib to be installed.)
@param samples: The samples to plot (default is all samples)
@type samples: C{list}
"""
if len(args) == 0:
args = [len(self)]
samples = list(islice(self, *args))
cumulative = _get_kwarg(kwargs, 'cumulative', False)
if cumulative:
freqs = list(self._cumulative_frequencies(samples))
else:
freqs = [self[sample] for sample in samples]
# percents = [f * 100 for f in freqs] only in ProbDist?
for i in range(len(samples)):
print "%4s" % str(samples[i]),
print
for i in range(len(samples)):
print "%4d" % freqs[i],
print