Getting all the values of one key.

This is the place for queries that don't fit in any of the other categories.

Getting all the values of one key.

Postby Jesstess » Fri Apr 05, 2013 3:54 pm

I'm working on coding a program that checks prepositions in the english language. This is executed by the open(file, "r") method/function/whatever
If they are incorrect its supposed to get the correct preposition after the word that rules the preposition after the word itself.
But there are some whom have multiple prepositions, like work f.e. There is work FOR, work ON, work WITH etc etc.
I've put them in a dictionary with prepositions as the value and the verb infront, as a key.
Lets take the key work
It has 3 different values : FOR, On, WITH
So my question is how do i get them all?
Should i use a for loop, if so how should it look?
Jesstess
 
Posts: 13
Joined: Fri Apr 05, 2013 3:47 pm

Re: Getting all the values of one key.

Postby Jesstess » Fri Apr 05, 2013 4:43 pm

Oh, and this is how the dictionary looks like :
Code: Select all
{'surplus': 'of', 'similarity': 'in', 'sceptical': 'of', 'beware': 'of', 'paper': 'on', 'useful': 'to', 'skill': 'in', 'knowledge': 'of', 'dispose': 'of', 'improvement': 'in', 'disparity': 'in', 'version': 'of', 'hope': 'for', 'dependent': 'on', 'indicative': 'of', 'know': 'of', 'fall': 'in', 'difference': 'in', 'desire': 'for', 'equality': 'in', 'die': 'of', 'conceive': 'of', 'drop': 'in', 'shortage': 'of', 'mean': 'by', 'requirement': 'for', 'because': 'of', 'rate': 'of', 'result': 'of', 'increase': 'with', 'critical': 'of', 'available': 'for', 'full': 'of', 'exchange': 'for', 'free': 'of', 'reason': 'for', 'worthy': 'of', 'article': 'on', 'estimate': 'at', 'change': 'in', 'threat': 'to', 'consequence': 'of', 'approve': 'of', 'think': 'of', 'comment': 'on', 'regardless': 'of', 'apprehensive': 'of', 'decrease': 'in', 'oblivious': 'of', 'speak': 'of', 'decline': 'in', 'capable': 'of', 'start': 'to', 'call': 'for', 'interest': 'in', 'empty': 'of', 'wish': 'for', 'explanation': 'for', 'aware': 'of', 'hear': 'of', 'demand': 'for', 'copy': 'of', 'exception': 'to', 'look': 'at', 'consist': 'of', 'characteristic': 'of', 'work': 'with', 'aim': 'at', 'limit': 'to', 'learn': 'of', 'irrespective': 'of', 'problem': 'of', 'certain': 'of', 'evidence': 'for', 'obstacle': 'to', 'need': 'of', 'afraid': 'of', 'end': 'to', 'discussion': 'about', 'smell': 'of', 'development': 'of', 'independent': 'of', 'sure': 'of', 'variation': 'in', 'designed': 'for', 'necessity': 'for', 'multiply': 'by', 'transcript': 'of', 'neglectful': 'of', 'short': 'of', 'conscious': 'of', 'proud': 'of', 'incapable': 'of', 'time': 'of', 'typical': 'of'}
Jesstess
 
Posts: 13
Joined: Fri Apr 05, 2013 3:47 pm

Re: Getting all the values of one key.

Postby setrofim » Fri Apr 05, 2013 4:52 pm

Change the dictionary so that the values are lists, then append each preposition to the list corresponding to the word.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Getting all the values of one key.

Postby tnknepp » Fri Apr 05, 2013 4:52 pm

I don't think you are understanding dictionaries. Invoking the key returns all values associated with it.

Code: Select all
prep = {'work':['for','on','with']}


Now, if you request:
prep['work']

you get all your prepositions in a list:
['for','on','with']
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 114
Joined: Mon Mar 11, 2013 7:41 pm

Re: Getting all the values of one key.

Postby Jesstess » Fri Apr 05, 2013 5:15 pm

tnkknepp.
Yeah i was thinking so too, but then i noticed when i read from the file, it only stored one of the possible key-value pair. So the main problem here is that my dictionary doesnt contain all the key-value pair.
Here are my prepositions with accompanied with the verb.
afraid of
aim at
apprehensive of
approve of
article on
available for
aware of
because of
beware of
call for
capable of
certain of
change in
characteristic of
comment on
conceive of
conscious of
consequence of
consist of
copy of
critical of
decline in
decrease in
demand for
dependent on
designed for
desire for
development in
development of
die of
difference in
discussion on
discussion about
disparity in
dispose of
drop in
empty of
end of
end to
equality in
estimate at
evidence for
exception to
exchange for
explanation for
fall in
free of
full of
hear of
hope for
improvement in
incapable of
increase of
increase by
increase in
increase with
independent of
indicative of
interest in
irrespective of
know of
knowledge of
learn of
limit to
look at
mean by
multiply by
necessity for
need for
need of
neglectful of
oblivious of
obstacle to
paper on
problem of
proud of
rate of
reason for
regardless of
requirement for
result of
sceptical of
short of
shortage of
similarity in
skill in
smell of
speak of
start to
sure of
surplus of
think of
threat to
time of
transcript of
typical of
useful for
useful to
variation in
version of
wish for
work of
work on
work with
worthy of

Hmm there is no spoiler function here? ^^

Anyways these prepositions are stored in prepositioner.txt

Code: Select all
def prepositioner(self):
        prep_ord = {}
        filen = open("prepositioner.txt", "r")
        for line in filen:
            (key,val) = line.split()
            prep_ord[str(key)] = val
        print (prep_ord)


However, as you can see in the textfile some verbs have multiple prepositions, but my for loop only takes 1 of each and store them in my dictionary. Why is that?
Jesstess
 
Posts: 13
Joined: Fri Apr 05, 2013 3:47 pm

Re: Getting all the values of one key.

Postby setrofim » Fri Apr 05, 2013 5:18 pm

Code: Select all
    def prepositioner(self):
            prep_ord = {}
            with open("prepositioner.txt") as filen:
                for line in filen:
                    key, val = line.split()
                    if key not in prep_ord:
                        prep_ord[key] = []
                    prep_ord[key].append(val)
            print(prep_ord)
            return prep_ord
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Getting all the values of one key.

Postby tnknepp » Fri Apr 05, 2013 5:28 pm

Jesstess wrote:However, as you can see in the textfile some verbs have multiple prepositions, but my for loop only takes 1 of each and store them in my dictionary. Why is that?


It is only storing the LAST preposition in the list for a given word, right? That would make sense. You are constantly re-writing your key:val pair and not properly updating the dictionary.

The first solution that comes to mind is to put the values into a list from the get go, then add to them as you go. e.g.

Code: Select all
for line in filen:
    key, val = line.split()
    if key not in prep_ord:
        prep_ord.update( {key:[val]} )
    else:
        prep_ord[key].append(val)
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython
User avatar
tnknepp
 
Posts: 114
Joined: Mon Mar 11, 2013 7:41 pm

Re: Getting all the values of one key.

Postby Jesstess » Fri Apr 05, 2013 8:02 pm

Thanks alot man!
Really neat with the values in a list :D
Jesstess
 
Posts: 13
Joined: Fri Apr 05, 2013 3:47 pm

Re: Getting all the values of one key.

Postby stranac » Fri Apr 05, 2013 8:29 pm

The example tnknepp has shown can be further simplified by using a collections.defaultdict:
Code: Select all
from collections import defaultdict

prep_ord = defaultdict(list)
for line in filen:
    prep_ord[key].append(val)


Also, in his code,
Code: Select all
prep_ord.update( {key:[val]} )
should be
Code: Select all
prep_ord[key] = [val]

The result is the same, but the code is simpler, and creating a new dictionary just to add a key to an old one is unnecessary.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 909
Joined: Thu Feb 07, 2013 3:42 pm

Re: Getting all the values of one key.

Postby Jesstess » Fri Apr 05, 2013 8:29 pm

Almost done now.
I've got a list that looks like this
self.after_editing = ['During', 'the', 'last', 'ten', 'years,', 'the', 'decrease', '*', 'of', '*', '(', ['in'], ')', 'interest', '*', 'in', '*', '(', ['in'], ')', 'reptilian', 'mammals', 'has', 'been', 'barely', 'noticeable.', 'We', 'hope', '*', 'that', '*', '(', ['for'], ')', 'the', 'desire', '*', 'for', '*', '(', ['for'], ')', 'knowledge', '*', 'on', '*', '(', ['of'], ')', 'these', 'strange', 'creatures', 'will', 'continue', 'to', 'exceed', 'our', 'expectations.']

Then i add every element into an empty string with this code
Code: Select all
for i in self.after_editing:
            self.ny_strang += str(i)
            self.ny_strang += " "


The now no longer empty string prints out like this.

During the last ten years, the decrease * of * ( ['in'] ) interest in reptilian mammals has been barely noticeable. We hope * that * ( ['for'] ) the desire for knowledge * on * ( ['of'] ) these strange creatures will continue to exceed our expectations.

Is there anyway to remove those [ 'in'] so it looks like this instead
During the last ten years, the decrease * of * ( in ) interest in reptilian mammals has been barely noticeable. We hope * that * ( for ) the desire for knowledge * on * ( of ) these strange creatures will continue to exceed our expectations.
Jesstess
 
Posts: 13
Joined: Fri Apr 05, 2013 3:47 pm

Re: Getting all the values of one key.

Postby stranac » Fri Apr 05, 2013 8:38 pm

A better way to create a single string out of a list like that is to use str.join()(in this case ' '.join(your_list))

How did you end up with that list?
Why are some elements lists with a single element, while others are strings?

It's possible to solve your problem as it is now(either type checking or a similar alternative), but it would be better to make sure your list only contains strings in the first place.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 909
Joined: Thu Feb 07, 2013 3:42 pm

Re: Getting all the values of one key.

Postby Jesstess » Fri Apr 05, 2013 9:06 pm

stranac wrote:A better way to create a single string out of a list like that is to use str.join()(in this case ' '.join(your_list))

How did you end up with that list?
Why are some elements lists with a single element, while others are strings?

It's possible to solve your problem as it is now(either type checking or a similar alternative), but it would be better to make sure your list only contains strings in the first place.

Well my values of the keys are stored in a list of either 1,2 or 3 elements. I used this code
Code: Select all
prep_ord = {}
        filen = open("prepositioner.txt", "r")
        for line in filen:
            key, val = line.split()
            if key not in prep_ord:
                prep_ord.update( {key:[val]} )
            else:
                prep_ord[key].append(val)

Personally, i would like to store the values as a tuple , if possible.
Jesstess
 
Posts: 13
Joined: Fri Apr 05, 2013 3:47 pm

Re: Getting all the values of one key.

Postby stranac » Fri Apr 05, 2013 9:16 pm

Jesstess wrote:Well my values of the keys are stored in a list of either 1,2 or 3 elements... and then some code

I understand how you're creating the prep_ord dict, but how are you creating the self.after_editing list?

Jesstess wrote:Personally, i would like to store the values as a tuple , if possible.

It's possible, but using lists here makes more sense, as they are mutable(you can add elements to them).
And after creating the dict, using lists or tuples should make very little difference.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 909
Joined: Thu Feb 07, 2013 3:42 pm

Re: Getting all the values of one key.

Postby Jesstess » Fri Apr 05, 2013 9:20 pm

stranac wrote:
Jesstess wrote:Well my values of the keys are stored in a list of either 1,2 or 3 elements... and then some code

I understand how you're creating the prep_ord dict, but how are you creating the self.after_editing list?

Jesstess wrote:Personally, i would like to store the values as a tuple , if possible.

It's possible, but using lists here makes more sense, as they are mutable(you can add elements to them).
And after creating the dict, using lists or tuples should make very little difference.

Its a pretty messy code. But anyway, here it is.
self.listan_med_orden is a list of all the words in the text document i'm going to correct.
Code: Select all
def redigera_fil(self): # Här går vi igenom alla ord i lista och kollar om ordet finns med som ett av verben, om det finns med kollar vi att det har rätt preposition efter.
        lenght = 0
       
       
        for i in self.listan_med_orden:
       
           
            if self.listan_med_orden[lenght] in self.prep_keys:
                if self.listan_med_orden[lenght+1] in self.prep[self.listan_med_orden[lenght]]:
                    self.after_editing.append(self.listan_med_orden[lenght])
                    lenght += 1
                elif self.listan_med_orden[lenght+1] != self.prep[self.listan_med_orden[lenght]]:
                    self.after_editing.append(self.listan_med_orden[lenght])
                    self.after_editing.append("*")
                    self.after_editing.append(self.listan_med_orden[lenght+1])
                    self.after_editing.append("*")
                    self.after_editing.append("(")
                    self.after_editing.append(self.prep[self.listan_med_orden[lenght]])
                    self.after_editing.append(")")
                    del self.listan_med_orden[lenght+1]
                    lenght += 1
            else:
                self.after_editing.append(self.listan_med_orden[lenght])
               
                lenght += 1
       
        for i in self.after_editing: # Nu har vi lagrat den nya strängen och använder en for loop för att skriva ut den som en sträng.
            self.ny_strang += str(i)
            self.ny_strang += " "
Jesstess
 
Posts: 13
Joined: Fri Apr 05, 2013 3:47 pm

Re: Getting all the values of one key.

Postby stranac » Fri Apr 05, 2013 9:38 pm

Wow, that really is a mess.
If you can explain what that's supposed to be doing(and also what your current variables contain), maybe we can suggest a better way to write it.

I'm just gonna guess this is the line that puts the list to your other list:
Code: Select all
self.after_editing.append(self.prep[self.listan_med_orden[lenght]])


You should turn this list to a string here, before appending.
How you do that depends on what you want to get as a result.
If you want all the elements, separated by commas, you would do something like:
Code: Select all
self.after_editing.append(', '.join(self.prep[self.listan_med_orden[lenght]]))
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 909
Joined: Thu Feb 07, 2013 3:42 pm

Re: Getting all the values of one key.

Postby Jesstess » Fri Apr 05, 2013 9:47 pm

This for loop checks every word in the list where all the words of the text document is represented.
If the word is a verb (all verbs are represented in the list self.prep_keys)
If the following word (lenght+1) after the verb is a value of the key it won't change anything, just add it to the list
If the following isn't a value of the key the following word will be marked with * * and have the correct preposition (value(s)) in a paranthesis ()
Edit: Thanks that join stuff worked out great! :)
Jesstess
 
Posts: 13
Joined: Fri Apr 05, 2013 3:47 pm

Re: Getting all the values of one key.

Postby stranac » Fri Apr 05, 2013 10:38 pm

Sorry, still don't get it.
Can you show a simple example of how this process would go?

What I need to know in order to suggest an alternate solution:
  • examples of self.listan_med_orden, self.prep_keys and self.prep
  • wanted output for the examples
  • step-by-step explanation of how you got to that output manually
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 909
Joined: Thu Feb 07, 2013 3:42 pm

Re: Getting all the values of one key.

Postby Jesstess » Fri Apr 05, 2013 11:26 pm

stranac wrote:Sorry, still don't get it.
Can you show a simple example of how this process would go?

What I need to know in order to suggest an alternate solution:
  • examples of self.listan_med_orden, self.prep_keys and self.prep
  • wanted output for the examples
  • step-by-step explanation of how you got to that output manually

Why ? It's all solved now ^^

Here's my whole code:
Code: Select all
class korrektur(object):
    def __init__(self):
       
        self.ny_strang = ""
        self.listan_med_orden = []
        self.prep = {}
        self.prep_keys =[]
        self.after_editing = []
       
       
    def __str__(self):
        print(self.ny_strang)

    def fraga_efter_fil(self): # Tar alla orden i textfilen och lägger till dem i en lista med orden  som element.
        words = []
        fil = input("Vänligen ange filnamnet(directory) på den fil du vill kontrollera\n")
       
        f = open(fil, "r")
        q = f.read().split()
        for w in q:
            words.append(w)
        self.listan_med_orden = words
       

    def prepositioner(self): # Lägger alla verb (key) med dess prepositioner (values) i en dictionary
        prep_ord = {}
        filen = open("prepositioner.txt", "r")
        for line in filen:
            key, val = line.split()
            if key not in prep_ord:
                prep_ord.update( {key:[val]} )
            else:
                prep_ord[key].append(val)
        self.prep = prep_ord
       
       
        for keys in prep_ord.keys():
            self.prep_keys.append(keys)
       
    def redigera_fil(self): # Här går vi igenom alla ord i lista och kollar om ordet finns med som ett av verben, om det finns med kollar vi att det har rätt preposition efter.
        lenght = 0
       
       
        for i in self.listan_med_orden:
       
           
            if self.listan_med_orden[lenght] in self.prep_keys:
                if self.listan_med_orden[lenght+1] in self.prep[self.listan_med_orden[lenght]]:
                    self.after_editing.append(self.listan_med_orden[lenght])
                    lenght += 1
                elif self.listan_med_orden[lenght+1] != self.prep[self.listan_med_orden[lenght]]:
                    self.after_editing.append(self.listan_med_orden[lenght])
                    self.after_editing.append("*")
                    self.after_editing.append(self.listan_med_orden[lenght+1])
                    self.after_editing.append("*")
                    self.after_editing.append("(")
                    self.after_editing.append(', '.join(self.prep[self.listan_med_orden[lenght]]))
                    self.after_editing.append(")")
                    del self.listan_med_orden[lenght+1]
                    lenght += 1
            else:
                self.after_editing.append(self.listan_med_orden[lenght])
               
                lenght += 1
       
        for i in self.after_editing: # Nu har vi lagrat den nya strängen och använder en for loop för att skriva ut den som en sträng.
            self.ny_strang += str(i)
            self.ny_strang += " "
       
       
   
           


   
       
       
   

   
def main():
    korrekterad_fil = korrektur()
    korrekterad_fil.fraga_efter_fil()
    korrekterad_fil.prepositioner()
    korrekterad_fil.redigera_fil()
    korrekterad_fil.__str__()
main()


The "prepositioner.txt" is a file with the verbs and prepositions after each verb.
Jesstess
 
Posts: 13
Joined: Fri Apr 05, 2013 3:47 pm

Re: Getting all the values of one key.

Postby stranac » Sat Apr 06, 2013 9:25 am

Jesstess wrote:Why ? It's all solved now ^^

It might work, for now.
But what if you need to make a change to it at some point?
Will you be able to do it, with your code as messy as it is?

But I guess if you're satisfied with this, there's no need for me to suggest an improvement...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 909
Joined: Thu Feb 07, 2013 3:42 pm

Re: Getting all the values of one key.

Postby Jesstess » Sat Apr 06, 2013 3:56 pm

stranac wrote:
Jesstess wrote:Why ? It's all solved now ^^

It might work, for now.
But what if you need to make a change to it at some point?
Will you be able to do it, with your code as messy as it is?

But I guess if you're satisfied with this, there's no need for me to suggest an improvement...

Its a program for my friend who is completely lost at python, but since its a necessary course for his programme he must complete it.
Therefore he's only aiming at getting an E.
Jesstess
 
Posts: 13
Joined: Fri Apr 05, 2013 3:47 pm

Next

Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 1 guest