我有以下内容:
fdist = FreqDist(text)
我想将以下制表结果输出到 CSV(而不是 python 控制台)。
fdist.tabulate()
我该怎么做?
您可以将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"
您可以查看(复制)它们的源代码并根据需要更改打印语句以编写 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