-4

As part of a beginners' university Python project, I am currently creating a database of words, be it Nouns, Verbs, Determiners, Adjectives.

Now the problem I am having is that the words being read into the program via the lexicon.readfromfile method are being put into the dictionary via an instance of a class ( be it noun, verb or adjective ). This created the problem that I have absolutely no idea how to call these objects from the dictionary since they do not have variables as keys, but rather memory locations (see the following):

{<__main__.Verb object at 0x02F4F110>, <__main__.Noun object at 0x02F4F130>, <__main__.Adjective object at 0x02F4F1D0>, <__main__.Noun object at 0x02F4F170>}

Does anyone have any idea how I can call these keys in such a way that I can make them usable in my code?

Here is the part I'm stuck on:

Add a method getPast() to the Verb class, which returns the past tense of the Verb. Your getPast() method can simple work by retrieving the value of ‘past’ from the attributes.

Here is a the majority of the code, leaving out the Noun and Adjective classes:

class Lexicon(object):
    'A container clas for word objects'

    def __init__(self):
        self.words = {}

    def addword(self, word):
        self.words[word.stringrep] = word

    def removeword(self, word):
        if word in self.words:
            del(word)
            print('Word has been deleted from the Lexicon' )
        else:
            print('That word is not in the Lexicon')


    def getword(self,wordstring):
        if wordstring in self.words:
            return self.words[wordstring]
        else:
            return None


    def containsword(self,string):
        if string in self.words:
            return True
        else:
            return False


    def getallwords(self):
        allwordslist = []
        for w in self.words:
            allwordslist.append(self.words[w])
        return set(allwordslist)

    def readfromfile(self, x):
        filehandle = open(x, 'r')
        while True:
            line = filehandle.readline()
            if line == '':
                break
            line = line.strip()
            info = line.split(',')
            if info[1] == 'CN' or info[1] == 'PN':
                noun=Noun(info[0],info[1])
                noun.setattribute('regular',bool(info[2]))
                self.addword(noun)
            elif info[1] == 'A':
                adjective=Adjective(info[0],info[1])
                adjective.setattribute('comparative', bool(info[2]))
                self.addword(adjective)
            elif info[1] == 'V':
                verb=Verb(info[0],info[1])
                verb.setattribute('transitive', bool(info[2]))
                verb.setattribute('past', info[3])
                self.addword(verb)




    def writetofile(self, x):
        filehandle = open(x, 'w')

        for t in self.words.values():
            filehandle.write(t.getFormattedString() + '\n')
        filehandle.close()
#---------------------------------------------------------------------------#           

class Word(object):
    'A word of any category'

    def __init__(self,stringrep,category):
        self.wordattribute = {}
        self.stringrep = stringrep
        self.category = category

    def setattribute(self, attributename, attributevalue):
        self.wordattribute[attributename] = attributevalue

    def getvalue(self,name):
        if name in self.wordattribute:
            return self.wordattribute[name]
        else:
            return none


    def __str__(self):
        return self.stringrep + ':' + self.category

    def __lt__(self,otherword):
        return self.stringrep < otherword.stringrep

class Verb(Word):
    '"Represents a Verb."'

    def __init__(self, stringrep, category):
        super().__init__(stringrep,category)

    def istransitive(self):
        return self.transitive

    def getFormattedString(self):
        n = '{stringrep},{category}' 
        n = n.format(stringrep=self.stringrep, category=self.category)
        for i in range(1,2):
            for v,b in self.wordattribute.items():
                n = n+','+str(b)
        return n
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Dean Henry
  • 13
  • 3

1 Answers1

0

You have a set there, not a dictionary. A set will let you check to see whether a given instance is in the set quickly and easily, but, as you have found, you can't easily get a specific value back out unless you already know what it is. That's OK because that's not what the set is for.

With a dictionary, you associate a key with a value when you add it to the dictionary. Then you use the key to get the value back out. So make a dictionary rather than a set, and use meaningful keys so you can easily get the value back.

Or, since I see you are already making a list before converting it to a set, just return that; you can easily access the items in the list by index. In other words, don't create the problem in the first place, and you won't have it.

kindall
  • 178,883
  • 35
  • 278
  • 309