Printing Simplified Corpus to Json File

235 views Asked by At

I'm trying to print out the Brown corpus with a simplified tagset to a file. This is the code I'm using, and its just ending up with a blank file.

import json
import nltk
from nltk.corpus import brown

brown_sents = nltk.corpus.brown.tagged_sents(tagset="universal")

for sent in brown_sents:
  with open('brown_corpus.txt', 'a') as outfile:
     json.dumps(sent, outfile)
1

There are 1 answers

0
lenz On BEST ANSWER

json.dumps() is meant for returning a str, not for writing to an open file. Use

json.dump(sent, outfile)

instead, and you should be fine.