10 Replies - 519 Views - Last Post: 19 April 2013 - 04:37 AM Rate Topic: -----

#1 Jane Janey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-April 13

Python least characters function

Posted 18 April 2013 - 03:30 PM

I have to write a function which takes one argument text containing a block of text in the form of a str, and returns an alphabetically sorted list of word(s) with the lowest “vowel proportion” as a list. The vowel proportion is defined as the proportion of letters in the word which are vowels (e.g. the vowel proportion of "life" is 2 = 0.5).

For example:

>>> vowels_least("the rhythm of life")
["rhythm"]
>>> vowels_least("The quality of mercy is not strain\'d ... Percy.")
["mercy","percy"]


My solution is:

def vowels_least(sentence):
    punc_char= ",.?!:'\/"
    for letter in sentence:
        if letter in punc_char:
          sentence = sentence.replace(letter, ' ')
    sentence = sentence.lower().split()
    vowel = 'aeiou'
    least_characters = {}
    count = 0
    for i in sentence:
        if vowel in i:
            count += 1
            total = len(i)
            result_div = float(count)/total
            least_characters[i] = result_div
        else:
            least_characters[i] = 0
    return min(least_characters, key = value)


But the code is error as it says value is not defined. And also when I print the least_characters to check the dictionary, it turns out to be like this:

vowels_least("the rhythm of life")
{'of': 0, 'the': 0, 'life': 0, 'rhythm': 0}


How do I fix this?

Is This A Good Question/Topic? 0
  • +

Replies To: Python least characters function

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3807
  • View blog
  • Posts: 13,500
  • Joined: 12-December 12

Re: Python least characters function

Posted 18 April 2013 - 04:23 PM

>>> vowels_least("the rhythm of life")
["rhythm"]
>>> vowels_least("The quality of mercy is not strain\'d ... Percy.")
["mercy","percy"]

This example does not correspond with your description of the assignment(?).

However, I assume you intended:

# if vowel in i:
if i in vowel:

and the key is supposed to be a (lambda) function so, yes, value is undefined.
Was This Post Helpful? 0
  • +
  • -

#3 Jane Janey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-April 13

Re: Python least characters function

Posted 18 April 2013 - 08:28 PM

View Postandrewsw, on 18 April 2013 - 04:23 PM, said:

>>> vowels_least("the rhythm of life")
["rhythm"]
>>> vowels_least("The quality of mercy is not strain\'d ... Percy.")
["mercy","percy"]

This example does not correspond with your description of the assignment(?).



It does. As for the first example, it returns
["rythm"]
as there is no vowel there thus making the proportion 0.
For example 2, it returns
["mercy","percy"]
because both mercy and percy has the same vowel
proportion which is 1/5 = 0.2 and they are the least proportion of vowel in the string:
"The quality of mercy is not strain\'d ... Percy."


Did you get my question now?
Was This Post Helpful? 0
  • +
  • -

#4 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 117
  • View blog
  • Posts: 215
  • Joined: 11-January 13

Re: Python least characters function

Posted 18 April 2013 - 08:35 PM

As he said you need to use:
if i in vowel:
Also you aren't using min correctly.

You want something like:
min_ratio = min(least_characters.values())
word_list = [aword for aword in least_characters if least_characters[aword]==min_ratio]
return sorted(word_list)

-Mek
Was This Post Helpful? 0
  • +
  • -

#5 Jane Janey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-April 13

Re: Python least characters function

Posted 18 April 2013 - 09:39 PM

View PostMekire, on 18 April 2013 - 08:35 PM, said:

As he said you need to use:
if i in vowel:
Also you aren't using min correctly.

You want something like:
min_ratio = min(least_characters.values())
word_list = [aword for aword in least_characters if least_characters[aword]==min_ratio]
return sorted(word_list)

-Mek


I have change my code. Here it is:

def vowels_least(sentence):
    punc_char= ",.?!:'\/"
    for letter in sentence:
        if letter in punc_char:
          sentence = sentence.replace(letter, ' ')
    sentence = sentence.lower().split()
    vowel = ['a','e','i','o','u']
    least_characters = {}
    count = 0
    for i in sentence:
        total = len(i)
        for n in vowel:
            if n in i:
                count += 1
                result_div = count/float(total)
                least_characters[i] = result_div
            else:
                least_characters[i] = 0

    print least_characters


I use print least_characters as I want to see the item and the value in the dictionary but the output
is not what I expected. It returned to this

vowels_least("the rhythm of life")
{'of': 0, 'the': 0, 'life': 0, 'rhythm': 0}


It should be like this:

vowels_least("the rhythm of life")
{'the': 0.3333, 'rythm': 0, 'of': 0.5, 'life': 0.5}


I have no idea on how to fix it.
Was This Post Helpful? 0
  • +
  • -

#6 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 117
  • View blog
  • Posts: 215
  • Joined: 11-January 13

Re: Python least characters function

Posted 18 April 2013 - 09:59 PM

So when you split the sentence you get a list of words. Then you need to iterate through each word and count vowels. Here is a little bit. Try a few more things and get back to me:
vowels = 'aeiou'
words = sentence.split()
for word in words:
    #look at each word in the sentence
    for letter in word:
        #look at each letter in the word
        if letter in vowels:
            #count vowels

-Mek
Was This Post Helpful? 0
  • +
  • -

#7 Jane Janey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-April 13

Re: Python least characters function

Posted 18 April 2013 - 10:24 PM

View PostMekire, on 18 April 2013 - 09:59 PM, said:

So when you split the sentence you get a list of words. Then you need to iterate through each word and count vowels. Here is a little bit. Try a few more things and get back to me:
vowels = 'aeiou'
words = sentence.split()
for word in words:
    #look at each word in the sentence
    for letter in word:
        #look at each letter in the word
        if letter in vowels:
            #count vowels

-Mek


This is my code after I changed it.

def vowels_least(sentence):
    punc_char= ",.?!:'\/"
    for letter in sentence:
        if letter in punc_char:
          sentence = sentence.replace(letter, ' ')
    vowels = 'aeiou'
    words = sentence.lower().split()
    least_characters = {}
    count = 0
    for word in words:
        for letter in word:
		count = 0
		if letter in vowels:
			total = len(word)
			count += 1
			result_div = count/float(total)
			least_characters[word] = result_div
		else:
			least_characters[word] = 0

    print least_characters


But still, the output is not what I expected. It printed:

vowels_least("the rhythm of life")
{'of': 0, 'the': 0.3333333333333333, 'life': 0.25, 'rhythm': 0}


instead of

vowels_least("the rhythm of life")
{'of': 0.5, 'the': 0.3333333333333333, 'life': 0.5, 'rhythm': 0}


Where did I get it wrong?
Was This Post Helpful? 0
  • +
  • -

#8 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 117
  • View blog
  • Posts: 215
  • Joined: 11-January 13

Re: Python least characters function

Posted 18 April 2013 - 10:31 PM

Minor things.

Try this:
def vowels_least(sentence):
    punc_char= ",.?!:'\/"
    for letter in sentence:
        if letter in punc_char:
          sentence = sentence.replace(letter, ' ')
    vowels = 'aeiou'
    words = sentence.lower().split()
    least_characters = {}
    for word in words:
        count = 0
        for letter in word:
    		if letter in vowels:
    			total = len(word)
    			count += 1
		least_characters[word] = count/float(len(word))
    print least_characters

vowels_least("The quality of mercy is not strain\'d ... Percy.")


Output:
{'mercy': 0.2, 'd': 0.0, 'of': 0.5, 'is': 0.5, 'percy': 0.2, 'strain': 0.3333333333333333, 'not': 0.3333333333333333, 'the': 0.3333333333333333, 'quality': 0.42857142857142855}


The way you are treating punctuation is a bit odd too. Here "d" is a word for example.
-Mek
Was This Post Helpful? 0
  • +
  • -

#9 Jane Janey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-April 13

Re: Python least characters function

Posted 18 April 2013 - 11:17 PM

View PostMekire, on 18 April 2013 - 10:31 PM, said:

Minor things.

Try this:
def vowels_least(sentence):
    punc_char= ",.?!:'\/"
    for letter in sentence:
        if letter in punc_char:
          sentence = sentence.replace(letter, ' ')
    vowels = 'aeiou'
    words = sentence.lower().split()
    least_characters = {}
    for word in words:
        count = 0
        for letter in word:
    		if letter in vowels:
    			total = len(word)
    			count += 1
		least_characters[word] = count/float(len(word))
    print least_characters

vowels_least("The quality of mercy is not strain\'d ... Percy.")


Output:
{'mercy': 0.2, 'd': 0.0, 'of': 0.5, 'is': 0.5, 'percy': 0.2, 'strain': 0.3333333333333333, 'not': 0.3333333333333333, 'the': 0.3333333333333333, 'quality': 0.42857142857142855}


The way you are treating punctuation is a bit odd too. Here "d" is a word for example.
-Mek


I've changed my punctuation treatment.

this is my code:

from string import punctuation

def vowels_least(sentence):
    words = [w.rstrip(punctuation) for w in sentence.lower().split()]
    vowels = 'aeiou'
    least_characters = {}
    for word in words:
        count = 0
        for letter in word:
            if letter in vowels:
                total = len(word)
                count += 1
        least_characters[word] = count/float(len(word))

    print least_characters


but for this example:

vowels_least("The quality of mercy is not strain\'d ... Percy.")


it returns to ZeroDivisionerror because I believe the len of"..." will be 0 .
How to fix it?

And how do I return the output to the least value of vowel proportion? Do I have to use min()?
Was This Post Helpful? 0
  • +
  • -

#10 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 117
  • View blog
  • Posts: 215
  • Joined: 11-January 13

Re: Python least characters function

Posted 19 April 2013 - 12:43 AM

Well, you've fought with this quite a bit so I will just show you my implementation (not guaranteed to be the best or even good):
Spoiler

I wrote the len_without_punc function because I still want words with medial punctuation to display correctly, but I don't want the punctuation characters taken into account when calculating the vowel ratio.

Have fun,
-Mek
Was This Post Helpful? 1
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5930
  • View blog
  • Posts: 12,853
  • Joined: 16-October 07

Re: Python least characters function

Posted 19 April 2013 - 04:37 AM

The trick with programming is to break the problem down into simple chunks. You have one very large chunk, really.

Without giving code, I'll offer a break down:
# returns 1 if true, 0 if false
def isVowel(c):
	pass # your code here

# returns 1 if true, 0 if false
def isLetter(c):
	pass # your code here
	
def vowelCount(s):
	pass # your code here

def letterCount(s):
	pass # your code here

def wordWeight(w):
	pass # your code here

def wordsInSentence(sentence):
	return [ (wordWeight(w), w) for w in sentence.split() ]

print wordsInSentence("The quality of mercy is not strain\'d ... Percy.")



Results:
[(0.3333333333333333, 'The'), (0.42857142857142855, 'quality'), (0.5, 'of'), (0.2, 'mercy'), (0.5, 'is'), (0.3333333333333333, 'not'), (0.2857142857142857, "strain'd"), (0, '...'), (0.2, 'Percy.')]



It's a good example of a messy sentence, which is why I kept it. It's also grammatically wrong. If correct it would work properly:
[(0.3333333333333333, 'The'), (0.42857142857142855, 'quality'), (0.5, 'of'), (0.2, 'mercy'), (0.5, 'is'), (0.3333333333333333, 'not'), (0.2857142857142857, "strain'd..."), (0.2, 'Percy.')]



But input data is rarely correct. Without resorting to a library, we can just flag the nonsence:
[(0.3333333333333333, 'The'), (0.42857142857142855, 'quality'), (0.5, 'of'), (0.2, 'mercy'), (0.5, 'is'), (0.3333333333333333, 'not'), (0.2857142857142857, "strain'd"), (None, '...'), (0.2, 'Percy.')]



To get the least from this, keep following the principal of breakdown into smaller problems.
def getLeastWeight(words):
	pass # your code here

def leastWordInSentence(sentence):
	pass # your code here



Since you already have an answer, Here's an example of the above described implementation:
Spoiler


Hope this helps.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1