Search code examples
pythonnltkwordnet

How can I extract all satellite adjectives from WordNet NLTK and save them to a text file?


I am trying to extract all satellite adjective synsets from WordNet and save them to a text file. Note that satellite adjectives are denoted as 's' in the synset name, e.g., "(fantastic.s.02)". The following is my code:

def extract_sat_adjectives():
    sat_adj_counter = 0
    sat_adjectives = []
    for i in wn.all_synsets():
        if i.pos() in ['s']:
            sat_adj_counter +=1
            sat_adjectives = sat_adjectives + [i.name()]
    fo = open("C:\\Users\\Nora\\Desktop\\satellite_adjectives.txt", "wb")
    for x in sat_adjectives:
        fo.write("%s\n" % x)
    fo.close()


extract_sat_adjectives()

The error I get is:

TypeError: 'str' does not support the buffer interface  

How can I save the adjectives to the text file? Thanks in advance.


Solution

  • The error is related to the combination of encoding errors and str()

    for x in sat_adjectives:
        fo.write("%s\n" % x)
    

    Change to:

    for x in sat_adjectives:
        fo.write(bytes("%s\n" % x, 'UTF-8'))