Silenus Silenus - 26 days ago 10
Python Question

How to access sets saved as files with Python?

I am doing some natural language processing with Python (2.7.9) and NLTK (3.2.1). The way I am currently doing things, every time I run my program I do part-of-speech tagging on a large corpus.

The resulting tagged corpus looks like a larger version of this:

[('a', 'DT'), ('better', 'JJR'), ('widower', 'JJR'), ('than', 'IN'),
('my', 'PRP$'), ('father', 'NN'), ('.', '.'), ('Aunt', 'NNP'),
('Sybil', 'NNP'), ('had', 'VBD'), ('pink-rimmed', 'JJ'), ('azure',
'JJ'), ('eyes', 'NNS'), ('and', 'CC'), ('a', 'DT'), ('waxen', 'JJ'),
('complexion', 'NN'), ('.', '.'), ('She', 'PRP'), ('wrote', 'VBD'),
('poetry', 'NN'), ('.', '.'), ('She', 'PRP'), ('was', 'VBD'),
('poetically', 'RB'), ('superstitious', 'JJ')]


Ideally, I would just save this set to a file and then read the file into a variable every time I run my program. Saving the set to a file is very easy:

POScorpus = pos_tag(words)

#I convert this to a string so I can write it to a file.

POScorpus_string = str(POScorpus)

#I then write it to a file.

f = open('C:\Desktop\POScorpus.txt', 'w')

f.write(POScorpus_string)

f.close()


The problem is that when I go to read the file into a variable, the
read()
function only reads the file as a string—not as a set.

My question is simple: How can I read the file as a set rather than as a string? I imagine this is relatively simple, but I could not find any information about how to do it.

(Apologies if this is off-topic or a dupe.)

Answer

You can cast a string into a set using eval(your_string).