0
我有以下几点:如何输出NLTK列表结果到CSV?
fdist = FreqDist(text)
我要输出的平板状的以下结果到CSV(而不是蟒蛇控制台)。
fdist.tabulate()
我该怎么做?
我有以下几点:如何输出NLTK列表结果到CSV?
fdist = FreqDist(text)
我要输出的平板状的以下结果到CSV(而不是蟒蛇控制台)。
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"
您可以查看(复制)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