python - N gram NLP 到 Excel 文件

标签 python excel python-3.x nlp export-to-csv

我正在努力处理这段代码。我需要创建 1 克和 2 克模型,并将克与其频率进行映射;当我需要将 2 个模型写入两张不同的工作表中的一个 EXCEL 文件后.. 我来到这里显示 2 模型克和频率,但在如何附加结果和创建 Excel 文件方面苦苦挣扎。

import nltk
nltk.download('punkt')
f = open('data.json','r')
raw = f.read()

tokens = nltk.word_tokenize(raw)

#Create your bigrams
bgs = nltk.bigrams(tokens)

#compute frequency distribution for all the bigrams in the text
fdist = nltk.FreqDist(bgs)
for k,v in fdist.items():
print (k,v)

谢谢

最佳答案

此代码将导出 csv 文件中的频率分布。 :

import csv
import nltk
nltk.download('punkt')
f = open('data.json','r')
raw = f.read()
tokens = nltk.word_tokenize(raw)

#Create your bigrams
bgs = nltk.bigrams(tokens)

#compute frequency distribution for all the bigrams in the text
fdist = nltk.FreqDist(bgs)
with open("fdist.csv", "w") as fp:
    writer = csv.writer(fp, quoting=csv.QUOTE_ALL)
    writer.writerows(fdist.items())

关于python - N gram NLP 到 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53684098/

相关文章:

python - 加入 django 的附加条件

python - 将python打印输出保存到变量

excel - 在Excel中计算一系列值的幂总和?

excel - 在 excel vba 中使用 VLC player activex 作为免注册 COM

Excel if 语句显示行中是否有任何内容

python - 多处理 - 无法将列表写入 csv(TypeError : 'ApplyResult' object is not iterable)

在从命令行运行的 Windows 上安装 Python 3

python - 如何在终止子进程后自动从 Popen 获取返回码?

python - 是否有根据类型提示检查类型的 python linter?

python - 何时使用哪个模糊函数来比较 2 个字符串