我正在努力处理这段代码。我需要创建 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/